CI Framework source reading---------output.php_php Tutorial

Source: Internet
Author: User
Tags flock md5 hash codeigniter
[PHP] _ZLIB_OC//If the output compression function 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//get mimetype if (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 string that is currently exported * @access public * @return String */function Get_output () {Retu RN $this->final_output; }//--------------------------------------------------------------------/** * Set Output * * Sets the output string * settings The output String * @access public * @param string * @return void */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 * @param string * @return void */function Append_output ($output) {if ($this->final_output = =) {$this->final_output = $output;} else {$this->final_output. = $output;} return $this; }//--------------------------------------------------------------------/** * Set Header * Using this method, Allows you to set the header of the HTTP protocol that will be sent to the browser, which is equivalent to PHP's standard function: 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 ... * * @access public * @param string * @param bool * @return void */function Set_header ($header, $replace = TRUE) {//If zlib.output_compression is enabled It'll compress the output,//But it won't modify the Content-length header to compensate compensation for//theReduction reduced restore, causing 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 ', + = = 0) {return;} $this->headers[] = array ($header, $replace); return $this; }//--------------------------------------------------------------------/** * Set Content Type Header * Set Content-type * @access Public * @param string extension of the file we ' re outputting * @return void */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 manually set the server Status header (header) * @access public * @param int the status code * @param string * @return void */function Set_status_header ($code = $, $text = ") {Set_status_header ($code, $text); ret Urn $this; }//--------------------------------------------------------------------/** * Enable/disable Profiler * allows you to turn on or disable the parser * @access public * @param bool * @return void */function Enable_profiler ($val = TRUE) {$this->enable_profiler = (is_boo L ($val))? $val: TRUE; return $this; }//--------------------------------------------------------------------/** * Set Profiler Sections * Set $this->_ Profiler_sections * Allows override of Default/config settings for Profiler section display * allows you to control (on/off) its specific part when the profiler is enabled * * @access Public * @param array * @return void */function set_profiler_sections ($sections) {foreach ($sections as $secti On = $enable) {$this->_profiler_sections[$section] = ($enable!== FALSE)? True:false; } return $this; }//--------------------------------------------------------------------/** * Set Cache * Sets Caching and cache time * @access public * @param integer where $time is the number of minutes you want the update to be cached * @return void */function cache ($time) {$this->cache_expiration = (! is_numer IC ($time))? 0: $time; return $this; }//--------------------------------------------------------------------/** * Display OUTPUT * Show outputs * All "view" data is Automatically put into this variable by the Controller class: * * $this->final_output * * This function sends the FINA lized output data to the browser along * with any server headers and profile data. It also stops the * benchmark timer so the page rendering speed and memory usage can be shown. * * @access Public * @param String * @return Mixed */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 Before 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 outputs if ($output = = ") {$output =& $this->final_output;}//---- ----------------------------------------------------------------//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_cache method, write a 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 the 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 server headers to send? Do you have server hair delivery? 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'll//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 (' Debug ', "Final output sent to browse R "); Log_message (' Debug ', "Total execution Time:". $elapsed); return TRUE; }//--------------------------------------------------------------------//Do we need to generate profile data? If So, load the profile class and run it. If 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 (! Emptyempty ($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 there. 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 {Ech o $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 * @access Publ IC * @param string * @return void */function _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: ". $cache _path); Return }//Get $uri $uri = $CI->config->item (' Base_url '). $CI->config->item (' Index_page '). $CI->uri->uri_string (); Generate the cache file. $cache _path. = MD5 ($uri); if (! $fp = @fopen ($cache _path, fopen_write_create_destructive)) {log_message (' error ', "Unable to WRITE cache file:". $c Ache_path); Return }//calculates the cache file expiration time. $expire = time () + ($this->cache_expiration * 60); if (Flock ($FP, lock_ex)) {fwrite ($fp, $expire. ' TS---> '. $output); Flock ($fp, lock_un);} else {log_message (' error ', "U Nable 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 in codeigniter.php output, then * The operation of this request will end directly, with the cached output as the response. * * @access public * @param object Config class * @param object URI class * @return void */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 you have this cache file but cannot read, get cached content fails, and also return false. if (! $fp = @fopen ($filepath, Fopen_read)) {return FALSE;}//opens to the cache file and takes $fp as a 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 the 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 this structure is not met, it can be treated as a non-CI cache file, or the file is corrupted,//Get cached content failed and return false. Strip out the embedded timestamp if (! Preg_match ("/(\d+ts--->)/", $cache, $match)) {return FALSE; }//has the file expired? If so we ' ll delete it. Determine if the cache expires and if it expires we will remove it if (time () >= trim (str_replace (' TS---> ', ', $match [' 1 '])) {if (Is_really_writable ($cache _path) {@unlink ($filepath); Log_message (' Debug ', "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 */

http://www.bkjia.com/PHPjc/477678.html www.bkjia.com true http://www.bkjia.com/PHPjc/477678.html techarticle [PHP]-PHP if (! defined (BasePath)) exit (No Direct script access allowed);/** * CodeIgniter * * An open source appli Cation development framework for PHP 5.1.6 or newer * * @packag ...

  • 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.