CI Frame source Pages-output.php

Source: Internet
Author: User
Tags flock md5 hash codeigniter
CI Framework source Reading---------output.php
 _ZLIB_OC//If output compression is turned on in the configuration item, the value of $this->_zlib_oc is On$this->_zlib_oc = @ini_get (' zlib.output_compression ');//Get MIME types for later//gets mimetypeif (defined (' Environment ') and file_exists (APPPATH. ' config/'). Environment. ' /mimes.php ') {include APPPATH. ' config/'. Environment. ' /mimes.php ';} Else{include APPPATH. ' config/mimes.php ';} $mimes is an array defined in mimes.php $this->mime_types = $mimes; Log_message (' Debug ', ' Output Class Initialized ');} --------------------------------/** * Get output * Using this method, you can get the data that will be output and save it * Returns the current output string * returns The current output String * @accesspublic * @returnstring */function get_output () {return $this->final_output;}  --------------------------------/** * Set Outputs * * Sets the OUTPUT String * Sets the strings of the input * @accesspublic * @paramstring * @returnvoid */function Set_output ($output) {$this->final_output = $output; return $this;} --------------------------------/** * Append Output * Append the data after the final input string * Appends the onto the output string * * @access Public * @paramstring * @returnvoid */function append_output ($output) {if ($this->final_output = = ") {$this->final_ output = $output;} else{$this->final_output. = $output;} return $this;} --------------------------------/** * Set Header * Use this method to allow you to set the header of the HTTP protocol that will be sent to the browser, acting as a standard function of PHP: Header (). * Lets You set a server header which is outputted with the final display. * Allows you to set a server header for the final display output.  * Note:if A file is cached, headers won't is sent.  We need to figure calculation out * How to permit header data to being saved with the cache data ... * * @accesspublic * @paramstring * @param bool * @returnvoid */function Set_header ($header, $replace = TRUE) {//If zlib.output_compression is enabled it wil L COMPRESS the output,//but it won't modify the Content-length header to compensate compensate for//the reduction reduce restore, Causi Ng the browser to hang waiting for more data.//We'll just skip content-length in those cases.if ($this->_zlib_oc & & strncasecmp ($header, ' content-length ', 14) = = 0) {return;} $this->headers[] = Array ($header, $replace); return $this;} --------------------------------/** * Set Content Type Header * Settings Content-type * @accesspublic * @paramstringextension The of the file we ' re outputting * @returnvoid */function set_content_type ($mime _type) {if (Strpos ($mime _type, '/') = = = = FALSE) { $extension = LTrim ($mime _type, '. '); /Is this extension supported?if (isset ($this->mime_types[$extension]) {$mime _type =& $this->mime_types[$ Extension];if (Is_array ($mime _type)) {$mime _type = current ($mime _type);}}} $header = ' Content-type: '. $mime _type; $this->headers[] = Array ($header, TRUE); return $this;} --------------------------------/** * Set HTTP Status Header * moved to Common procedural functions in 1.7.2 * allows you to set the service manually  Service Status Header (header) * @accesspublic * @paramintthe Status code * @paramstring * @returnvoid */function set_status_header ($code = $text = ") {Set_status_header ($code, $text); return $this;} --------------------------------/** * enable/disable ProFiler * allows you to turn on or disable the parser * @accesspublic * @parambool * @returnvoid */function enable_profiler ($val = TRUE) {$this->ENABLE_PR Ofiler = (Is_bool ($val))? $val: True;return $this;} --------------------------------/** * Set Profiler Sections * Set $this->_profiler_sections * allows override of Defau  Lt/config Settings for Profiler section display * allows you to control (on/off) its specific part when the profiler is enabled * * @accesspublic * @paramarray * @returnvoid */function set_profiler_sections ($sections) {foreach ($sections as $section = = $enable) {$this->_profiler_ sections[$section] = ($enable!== FALSE)? True:false;} return $this;}  --------------------------------/** * Set Cache * Sets Caching and cache time * @accesspublic * @paraminteger where $time is the number of minutes you wish to cache updates * @returnvoid */function Cache ($time) {$this->cache_expiration = (! Is_numeric ($time))? 0: $time; return $this;} --------------------------------/** * Display OUTPUT * Show outputs * All "view" data are automatically put into this variable b Y the Controller class: * * $this->fiNal_output * * This function sends the finalized output data to the browser along * with any server headers and profile da  Ta. It also stops the * benchmark timer so the page rendering speed and memory usage can be shown. * * @accesspublic * @param string * @returnmixed */function _display ($output = ") {//Note:we use globals because We can ' t use $CI =& get_instance ()//Since this function was sometimes called by the caching mechanism,//which happens befor E The CI Super object is available.//Note: We use global because we cannot use $ci =& get_instance () global $BM, $CFG;//Grab The Super Object if we can.////Of course if we can get the super controller, we'll get it first. if (class_exists (' Ci_controller ')) {$CI =& get_instance ();} --------------------------------//Set the output data//set the export data if ($output = = ") {$output =& $this->FINAL_OUTPU t;}  --------------------------------//Do we need to write a cache file? Only if the controller does not has its//own _output () method and we is not dealing with a cache file, whicH we//can determine by the existence of the $CI object above//if the cache time >0, $CI the Super object exists and the Super object exists _output method//Call _WRITE_CAC The He method, write a cache file if ($this->cache_expiration > 0 && isset ($CI) &&! method_exists ($CI, ' _output ')) {$ This->_write_cache ($output);} --------------------------------//Parse out the elapsed time and memory usage,//then swap the pseudo-variables with T He data//calculate code execution time and memory usage time $elapsed = $BM->elapsed_time (' Total_execution_time_start ', ' total_execution_time_end ') ;//If $this->parse_exec_vars is true, replace {elapsed_time},{memory_usage}//in the output with the calculated time. if ($this->parse_exec_vars = = = TRUE) {$memory = (! function_exists (' memory_get_usage '))? ' 0 ': Round (Memory_get_usage ()/1024/1024, 2). ' MB '; $output = Str_replace (' {elapsed_time} ', $elapsed, $output); $output = Str_replace (' {memory_usage} ', $memory, $ Output);} --------------------------------//Is compression requested? processing of compressed transmissions. if ($CFG->item (' compress_output ') = = = TRUE && $this->_zlib_oc= = FALSE) {if (extension_loaded (' zlib ')) {if (Isset ($_server[' http_accept_encoding ')) and Strpos ($_server[' http_ Accept_encoding '], ' gzip ')!== (FALSE) {ob_start (' Ob_gzhandler ');}}} --------------------------------//Is there any servers headers to send?//have server hair sent? if (count ($this->headers) > 0) {foreach ($this->headers as $header) {@header ($header [0], $header [1]);}} --------------------------------//Does The $CI object exist?//If Not we know we is dealing with a cache file so we ' L l//simply echo out the data and exit.//if there is no $ci to prove that the current is a cached output, we simply send the data and exit if (! Isset ($CI)) {echo $output; Log_message (' De Bug ', "Final output sent to browser"), Log_message (' Debug ', "Total execution Time:". $elapsed); return TRUE;} --------------------------------//Do we need to generate profiles data?//if so, load the profile class and run it.//as When the performance analysis is turned on we call,//will generate some reports to the end of the page to assist us in debugging. if ($this->enable_profiler = = TRUE) {$CI->load->library (' Profiler '); if (! empty ($this->_profiler_sectioNS) {$CI->profiler->set_sections ($this->_profiler_sections);}  If The output data contains closingandtags//we'll remove them and add them back after we insert the profile data//if presentTag, we will delete, insert our profiling code and then add back if (Preg_match ("|.*?|is ", $output)) {$output = Preg_replace (" |.*?|is ",", $output); $output. = $CI->profiler->run (); $output. = '';} else{$output. = $CI->profiler->run ();}} --------------------------------//Does the controller contain a function named _output ()//If so send the output ther E. Otherwise, Echo it.//if the controller has _output this method we call directly//If no data is sent directly to the browser if (method_exists ($CI, ' _output ')) {$CI->_output ( $output);}  Else{echo $output; Send it to the browser!} Log_message (' Debug ', "Final output sent to browser"), Log_message (' Debug ', "Total execution Time:". $elapsed);} --------------------------------/** * Write A cache file * Generates a cached files * @accesspublic * @param string * @returnvoid */fun Ction _write_cache ($output) {//Call Super Object $ci =& get_instance ();//Find cache path $path = $CI->config->item (' Cache_path ') ; $cache _path = ($path = = ")? APPPATH. ' cache/': $path;//If the cache path is not a folder or is not writable, an error is returned. if (! Is_dir ($cache _path) OR! is_really_writable ($cache _path)) {log_message (' error ', "Unable to write cache file:" $cach E_path); return;} Get $uri$uri = $CI->config->item (' Base_url '). $CI->config->item (' index_pAge '). $CI->uri->uri_string ();//Generate Cache file. $cache _path. = MD5 ($uri), if (! $fp = @fopen ($cache _path, fopen_write_create_destructive)) {log_message (' error ', "unable To write Cache file: ". $cache _path); return;} Calculates the cache file expiration time. $expire = time () + ($this->cache_expiration *), if (Flock ($FP, lock_ex)) {fwrite ($fp, $expire. ' TS---> '. $output); Flock ($FP, lock_un);} Else{log_message (' Error ', "Unable to secure a file-lock for file at:". $cache _path); return;} Fclose ($FP); @chmod ($cache _path, File_write_mode); Log_message (' Debug ', ' Cache FILE written: '. $cache _path);} --------------------------------/** * Update/serve A cached file * There is a call to this method in codeigniter.php, this method is responsible for the output of the cache, * If this method is called with output in codeigniter.php, the run of this request will end directly with the cached output as the response.  * * @accesspublic * @param objectconfig class * @param objecturi class * @returnvoid */function _display_cache (& $CFG, & $URI) {//Get the path to save cache $cache_path = ($CFG->item (' cache_path ') = = ")?  APPPATH. ' cache/': $CFG->item (' Cache_path ');//Build the file path.The file name is a MD5 hash of the full uri//one exact route will correspond to a cache file, and the cache file is the MD5 cipher for the corresponding routing string. $uri = $CFG->item (' Base_url '). $CFG->item (' Index_page '). $URI->uri_string; $filepath = $cache _path.md5 ($uri) If you do not have this cache file, you can return FALSE if you fail to get cached content. if (! @file_exists ($filepath)) {return FALSE;} If there is this cache file, but cannot read, the cache content fails and the same returns false. if (! $fp = @fopen ($filepath, Fopen_read)) {return FALSE;} Open to the cache file and use $FP as the handle. The next step is to obtain a shared lock (read). Flock ($FP, lock_sh); $cache = "; if (FileSize ($filepath) > 0) {$cache = Fread ($fp, FileSize ($filepath));} Unlock flock ($fp, lock_un);//close File connection. Fclose ($FP);//The following TS--->, just because the content of the CI cache file//is stipulated in the digital +ts---> start. This number represents the creation time. If it does not conform to this structure, it can be considered a non-CI cache file, or the file is corrupted,//Get cache content failed, return false. Strip out the embedded timestampif (! Preg_match ("/(\d+ts--->)/", $cache, $match)) {return FALSE;} Has the file expired? If so we'll delete it.//to determine if the cache expires, if it expires we will remove it if (time () >= trim (str_replace (' TS---> ', ', $match [' 1 ']))) {if (is_real Ly_writable ($cache _path)) {@unlink ($filepath); Log_message (' Debug ', "The Cache file has expired. File deleted "); return FALSE;}} Display the Cache$this->_display (Str_replace ($match [' 0 '], ' ', $cache)); Log_message (' Debug ', "Cache file is Current. Sending it to browser. "); return TRUE;}} End Output class/* end of file output.php *//* location:./system/core/output.php */

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.