CI framework source code reading --------- Output. php & lt ;? Phpif (! Defined (BASEPATH) exit (Nodirectscriptaccessallowed); *** CodeIgniter *** Anopensourceapplicationdevelopmentframeworkfo CI framework source code reading --------- Output. php
_ Zlib_oc // if the output compression function is enabled 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 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 mimes. an array defined in php $ this-> mime_types = $ mimes; log_message ('debug ', "Output Class Initialized");} // --------------------------------/*** Get Output * use this method to obtain the data to be Output, and save it * Returns the current output string * to return the current output string * @ accesspublic * @ returnstring */function get_output () {return $ this-> final_output ;} // --------------------------------/*** Set Output ** Sets the output string * to Set the Output string * @ accesspublic * @ paramstring * @ returnvoid */function set_output ($ Output) {$ this-> final_output = $ output; return $ this;} // --------------------------------/*** Append Output * After the final output string, append data * Appends data onto the output string ** @ accesspublic * @ paramstring * @ returnvoid */function append_output ($ output) {if ($ this-> final_output = '') {$ this-> final_output = $ output;} else {$ this-> final_output. = $ output;} return $ this;} // ----------------------------------/*** S This method allows you to set the HTTP Header that will be sent to the browser, which is equivalent to the php Standard function: header (). * Lets you set a server header which will be outputted with the final display. * allows you to set a server header for the final display output. * Note: If a file is cached, headers will not be sent. we need to figure calculate out * how to permit header data to be saved with the cache data... ** @ accesspublic * @ paramstring * @ param bool * @ returnvoid */function set_header ($ header, $ replace = TRUE) {// If zlib. output_compression is enabled it will compress the output, // but it will not modify the content-length header to compensate compensation for // Function reduces restoration, 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', 14) = 0) {return ;} $ this-> headers [] = array ($ header, $ replace); return $ this ;} // --------------------------------/*** Set Content Type Header * Set Content-Type * @ accesspublic * @ paramstringextension of the file we're outputt Ing * @ 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 * allow you to manually Set the Server Status header * @ accesspublic * @ parami Ntthe status code * @ paramstring * @ returnvoid */function set_status_header ($ code = 200, $ text = '') {set_status_header ($ code, $ text); return $ this ;} // --------------------------------/*** Enable/disable Profiler * allows you to Enable or disable analyzer * @ accesspublic * @ parambool * @ returnvoid */function enable_profiler ($ val = TRUE) {$ this-> enable_profiler = (is_bool ($ val ))? $ Val: TRUE; return $ this ;} // ------------------------------/*** Set Profiler Sections * to Set $ this-> _ profiler_sections * Allows override of default/config settings for Profiler section display * Allows you to enable the evaluator, control (on/off) specific part ** @ 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 * Set cache and Cache time * @ accesspublic * @ paraminteger $ time indicates the number of minutes you want to cache updates * @ returnvoid */Cache function ($ time) {$ this-> cache_expiration = (! Is_numeric ($ time ))? 0: $ time; return $ this;} // --------------------------------/*** Display Output * All "view" data is automatically put into this variable by the controller class: ** $ this-> final_output ** This function sends the finalized 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 sho Wn. ** @ accesspublic * @ param string * @ returnmixed */function _ display ($ output = '') {// Note: we use globals because we can't use $ CI = & get_instance () // since this function is 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 you can get the super controller, we should first Take it. If (class_exists ('ci _ controller') {$ CI = & get_instance ();} // -------------------------------- // Set the output data if ($ output = '') {$ output = & $ this-> final_output ;} // -------------------------------- // Do we need to write a cache file? Only if the controller does not have its // own _ output () method and we are not dealing with a cache file, which we // can determine by the existence of the $ CI object above // if the cache time is greater than 0, $ CI super object exists and the _ output method exists under the super object // call the _ write_cache method to 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 the data // calculate the 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} and {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? Compression and transmission. 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') ;}}// -------------------------------- // Are there any server headers to send? // Is there a server header for sending? 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 are dealing with a cache file so we'll // simply echo out the data and exit. // if there is no $ CI, it indicates that the current output is a cache. we simply send data and exit if (! Isset ($ CI) {echo $ output; log_message ('debug', "Final output sent to browser"); 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 enabled, we will call it. // Some reports will be generated at the end of the page to assist us in debugging. 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 data // if YesTag, we will delete it, insert our performance analysis code, and then add it back to 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 the _ output method, we call it directly. // if no data is directly sent 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 * generate a Cache File * @ accesspublic * @ param string * @ returnvoid */function _ write_cache ($ output) {// call the super object $ CI = & get_instance (); // find the cache path $ path = $ CI-> config-> item ('cache _ path '); $ cache_path = ($ path = '')? APPPATH. 'cache/': $ path; // if the cache path is not a folder or cannot be written, 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 = $ CI-> config-> item ('base _ url '). $ CI-> config-> item ('index _ page '). $ CI-> uri-> uri_string (); // generates a 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 * 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);} // --------------------------------/*** Update/serve a cached file * In CodeIgniter. php calls this method. this method is used to cache the output. * If you use this method in CodeIgniter. if php calls this method with output, * the request runs directly and the cache output is used as the response. ** @ Accesspublic * @ param objectconfig class * @ param objecturi class * @ returnvoid */function _ display_cache (& $ CFG, & $ URI) {// Obtain the path to save the cache $ 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 // an accurate route will correspond to a cache file, and The cache file is the md5 ciphertext of The corresponding route string. $ Uri = $ CFG-> item ('base _ url '). $ CFG-> item ('index _ page '). $ URI-> uri_string; $ filepath = $ cache_path.md5 ($ uri); // if the cached file does not exist, the system returns FALSE. If (! @ File_exists ($ filepath) {return FALSE;} // if the cached file is available but cannot be read, obtaining the cached content fails and FALSE is returned. If (! $ Fp = @ fopen ($ filepath, FOPEN_READ) {return FALSE;} // open the cached file and use $ fp as the handle. Next, obtain the 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 text in the following TS ---> is only because the content in the CI cache file must start with a number + TS --->. This number indicates the creation time. // If this structure is not met, it can be viewed as a non-CI cached file or the file is damaged. // if the file fails to get the cached content, FALSE is returned. // Strip out the embedded timestampif (! Preg_match ("/(\ d + TS --->)/", $ cache, $ match) {return FALSE;} // Has the file expired? If so we'll delete it. // Determine whether the cache expires. if it expires, we will delete 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 */