Core File (Output class) Output. php

Source: Internet
Author: User
Tags flock md5 hash
The Output. php function of the CI Output class sends the final web page to the browser, which may be the least used. You loaded a view file using the loader. the content of this view file is automatically passed to the output class object. Then, after the method is executed

CI Output Class Output. phpThe function is to send the final web page to the browser. the content here may be the least used. You loaded a view file using the loader. the content of this view file is automatically passed to the output class object, after the method is executed, the output class object is automatically called to output the execution result. it is worth noting that the content of profiler and file cache is provided here.

/*** Output class: send the final Web page to the requested browser */class CI_Output {/*** final Output result string */protected $ final_output; /*** cache time */protected $ cache_expiration = 0;/*** header information */protected $ headers = array (); /*** mime type */protected $ mime_types = array ();/*** whether to enable the evaluator */protected $ enable_profiler = FALSE; /*** whether to enable gzip Research Institute */protected $ _ zlib_oc = FALSE;/*** enable evaluation module */protected $ _ profiler_sections = array (); /*** whether to replace the variable {elapsed_time} And {memory_usage} memory consumption */protected $ parse_exec_vars = TRUE;/*** Constructor **/function _ construct () {$ this-> _ zlib_oc = @ ini_get ('zlib. output_compression '); // introduce the mime type configuration file if (defined ('environment') AND file_exists (APPPATH. 'config /'. ENVIRONMENT. '/mimes. php ') {include APPPATH. 'config /'. ENVIRONMENT. '/mimes. php ';} else {include APPPATH. 'config/mimes. php ';} $ this-> mime_types = $ mimes; log_message ('debu G', "Output Class Initialized");} // outputs/*** returns the final Output result */function get_output () {return $ this-> final_output ;} /*** set the final returned result */function set_output ($ output) {$ this-> final_output = $ output; return $ this ;} /*** append the data to the final output result. this method is used in Loader. php is called. For details, see the Loader: view () method in Loader. php and the Loader: _ ci_load () method. */Function append_output ($ output) {if ($ this-> final_output = '') {$ this-> final_output = $ output;} else {$ this-> final_output. = $ output;} return $ this;} // --------------------------------------------------------------------/*** sets the final output header of the browser */function set_header ($ header, $ replace = TRUE) {// If gzip compression is enabled, the content-length header is not modified, causing the browser to wait for more data, skip this case directly if ($ this-> _ zlib_oc & strncasecmp ($ header, 'co Ntent-length', 14) = 0) {return;} $ this-> headers [] = array ($ header, $ replace); return $ this ;} // Configure/*** set Content Type */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 */function set_status_header ($ code = 200, $ text = '') {set_status_header ($ code, $ text); return $ this ;} //----------------------------- -----------------------------------/*** Whether to enable the evaluator */function enable_profiler ($ val = TRUE) {$ this-> enable_profiler = (is_bool ($ val ))? $ Val: TRUE; return $ this;} // ----------------------------------------------------------------------/*** custom open Evaluation Module */function set_profiler_sections ($ sections) {foreach ($ sections as $ section => $ enable) {$ this-> _ profiler_sections [$ section] = ($ enable! = FALSE )? TRUE: FALSE;} return $ this;} // --------------------------------------------------------------------/*** set the cache duration and enable the file cache function */cache function ($ time) {$ this-> cache_expiration = (! Is_numeric ($ time ))? 0: $ time; return $ this;} // ----------------------------------------------------------------------/*** output the final result to the browser */function _ display ($ output = '') {// The reason for using global is that _ display_cache is in Codeigniter. php is used. Output: _ display_cache () calls _ displayglobal $ BM, $ CFG; // Grab the super object if we can. if (class_exists ('ci _ controller') {$ CI = & get_instance ();}//--------------------------------------------------------- ----------- // Set the output dataif ($ output = '') {$ output = & $ this-> final_output;} // If cache is enabled, generate the cache file if ($ this-> cache_expiration> 0 & isset ($ CI )&&! Method_exists ($ CI, '_ output') {$ this-> _ write_cache ($ output );} // executor // the overall running time and memory consumption of the system are replaced here with $ elapsed = $ BM-> elapsed_time ('total _ execution_time_start ', 'total _ execution_time_end '); 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);} // -------------------------------------------------------------------- // whether to enable gzip compression if ($ CFG-> item ('compress _ output ') === TRUE & $ this-> _ zlib_oc = FALSE) {if (extension_loaded ('zlib ')) {if (isset ($ _ SERVER ['http _ ACCEPT_ENCODING ']) D strpos ($ _ SERVER ['http _ ACCEPT_ENCODING '], 'gzip ')! = FALSE) {ob_start ('OB _ gzhandler ') ;}}// -------------------------------------------------------------------- // Are there any server headers to send? If (count ($ this-> headers)> 0) {foreach ($ this-> headers as $ header) {@ header ($ header [0], $ header [1]) ;}/// -------------------------------------------------------------------- // if no super controller is available, it can be proved that the current output is processing a cached if (! Isset ($ CI) {echo $ output; log_message ('debug', "Final output sent to browser"); log_message ('debug', "Total execution time :". $ elapsed); return TRUE;} // if the evaluator is enabled, insert the evaluator information in the output result if ($ this-> enable_profiler = TRUE) {$ CI-> load-> library ('filer'); if (! Empty ($ this-> _ profiler_sections) {$ CI-> profiler-> set_sections ($ this-> _ profiler_sections);} // If the output data contains closingAndTags // we will remove them and add them back after we insert the profile dataif (preg_match ("|.*?| Is ", $ output) {$ output = preg_replace (" |.*?| Is ",'', $ output); $ output. = $ CI-> profiler-> run (); $ output. ='';} Else {$ output. = $ CI-> profiler-> run () ;}// ------------------------------------------------------------------ // CodeIgniter allows you to add a name named _ output () to your controller () to receive the final data. // Note: If your controller contains a _ output () method, it will always be called instead of directly outputting the final data. This method is similar to the Destructor in OO. Whether you call any method, this method will always be executed. 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);} // logs/*** write cache file * @ returnvoid */function _ write_cache ($ output) {$ CI = & get_instance (); $ path = $ CI-> config-> item ('cache _ path'); $ cache_path = ($ path = '')? APPPATH. 'cache/': $ path; // check whether the directory can be written if (! Is_dir ($ cache_path) OR! Is_really_writable ($ cache_path) {log_message ('error', "Unable to write cache file :". $ cache_path); return ;}$ uri = $ CI-> config-> item ('base _ url '). $ CI-> config-> item ('index _ page '). $ CI-> uri-> uri_string (); $ cache_path. = md5 ($ uri); // md5 ($ uri) is the object name if (! $ Fp = @ fopen ($ cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE) {log_message ('error', "Unable to write cache file :". $ cache_path); return ;}$ expire = time () + ($ this-> cache_expiration * 60); 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 :". $ cache_path); return;} fclose ($ fp); @ chmod ($ cache_path, FILE_WRITE _ MODE); log_message ('debug', "Cache file written :". $ cache_path );} // Configure/*** Update/serve a cached file ** @ accesspublic * @ param objectconfig class * @ param objecturi class * @ returnvoid */function _ display_cache (& $ CFG, & $ URI) {$ cache_path = ($ CFG-> item ('cache _ path') = '')? APPPATH. 'cache/': $ CFG-> item ('cache _ path'); // Build the file path. the file name is an MD5 hash of the full URI $ uri = $ CFG-> item ('base _ url '). $ CFG-> item ('index _ page '). $ URI-> uri_string; $ filepath = $ cache_path.md5 ($ uri); if (! @ File_exists ($ filepath) {return FALSE;} if (! $ Fp = @ fopen ($ filepath, FOPEN_READ) {return FALSE;} flock ($ fp, LOCK_SH); $ cache = ''; if (filesize ($ filepath)> 0) {$ cache = fread ($ fp, filesize ($ filepath);} flock ($ fp, LOCK_UN); fclose ($ fp ); // Very clever processing of the file header 13834243242TS ---> if (! Preg_match ("/(\ d + TS --->)/", $ cache, $ match) {return FALSE;} // check whether the cache file has expired, if (time ()> = trim (str_replace ('ts ---> ', '', $ match ['1']) is deleted after expiration. {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 ;}}

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.