Output.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Output {
00030
00031 var $final_output;
00032 var $cache_expiration = 0;
00033 var $headers = array();
00034 var $enable_profiler = FALSE;
00035
00036
00037 function CI_Output()
00038 {
00039 log_message('debug', "Output Class Initialized");
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 function get_output()
00053 {
00054 return $this->final_output;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 function set_output($output)
00069 {
00070 $this->final_output = $output;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 function append_output($output)
00085 {
00086 if ($this->final_output == '')
00087 {
00088 $this->final_output = $output;
00089 }
00090 else
00091 {
00092 $this->final_output .= $output;
00093 }
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 function set_header($header)
00111 {
00112 $this->headers[] = $header;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 function enable_profiler($val = TRUE)
00125 {
00126 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 function cache($time)
00139 {
00140 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function _display($output = '')
00160 {
00161
00162
00163
00164 global $BM, $CFG;
00165
00166
00167
00168
00169 if ($output == '')
00170 {
00171 $output =& $this->final_output;
00172 }
00173
00174
00175
00176
00177 if ($this->cache_expiration > 0)
00178 {
00179 $this->_write_cache($output);
00180 }
00181
00182
00183
00184
00185
00186
00187 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
00188 $output = str_replace('{elapsed_time}', $elapsed, $output);
00189
00190 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
00191 $output = str_replace('{memory_usage}', $memory, $output);
00192
00193
00194
00195
00196 if ($CFG->item('compress_output') === TRUE)
00197 {
00198 if (extension_loaded('zlib'))
00199 {
00200 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
00201 {
00202 ob_start('ob_gzhandler');
00203 }
00204 }
00205 }
00206
00207
00208
00209
00210 if (count($this->headers) > 0)
00211 {
00212 foreach ($this->headers as $header)
00213 {
00214 @header($header);
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223 if ( ! function_exists('get_instance'))
00224 {
00225 echo $output;
00226 log_message('debug', "Final output sent to browser");
00227 log_message('debug', "Total execution time: ".$elapsed);
00228 return TRUE;
00229 }
00230
00231
00232
00233
00234 $CI =& get_instance();
00235
00236
00237
00238 if ($this->enable_profiler == TRUE)
00239 {
00240 $CI->load->library('profiler');
00241
00242
00243
00244 if (preg_match("|</body>.*?</html>|is", $output))
00245 {
00246 $output = preg_replace("|</body>.*?</html>|is", '', $output);
00247 $output .= $CI->profiler->run();
00248 $output .= '</body></html>';
00249 }
00250 else
00251 {
00252 $output .= $CI->profiler->run();
00253 }
00254 }
00255
00256
00257
00258
00259
00260 if (method_exists($CI, '_output'))
00261 {
00262 $CI->_output($output);
00263 }
00264 else
00265 {
00266 echo $output;
00267 }
00268
00269 log_message('debug', "Final output sent to browser");
00270 log_message('debug', "Total execution time: ".$elapsed);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 function _write_cache($output)
00282 {
00283 $CI =& get_instance();
00284 $path = $CI->config->item('cache_path');
00285
00286 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
00287
00288 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00289 {
00290 return;
00291 }
00292
00293 $uri = $CI->config->item('base_url').
00294 $CI->config->item('index_page').
00295 $CI->uri->uri_string();
00296
00297 $cache_path .= md5($uri);
00298
00299 if ( ! $fp = @fopen($cache_path, 'wb'))
00300 {
00301 log_message('error', "Unable to write cache file: ".$cache_path);
00302 return;
00303 }
00304
00305 $expire = time() + ($this->cache_expiration * 60);
00306
00307 flock($fp, LOCK_EX);
00308 fwrite($fp, $expire.'TS--->'.$output);
00309 flock($fp, LOCK_UN);
00310 fclose($fp);
00311 @chmod($cache_path, DIR_WRITE_MODE);
00312
00313 log_message('debug', "Cache file written: ".$cache_path);
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 function _display_cache(&$CFG, &$URI)
00325 {
00326 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
00327
00328 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00329 {
00330 return FALSE;
00331 }
00332
00333
00334 $uri = $CFG->item('base_url').
00335 $CFG->item('index_page').
00336 $URI->uri_string;
00337
00338 $filepath = $cache_path.md5($uri);
00339
00340 if ( ! @file_exists($filepath))
00341 {
00342 return FALSE;
00343 }
00344
00345 if ( ! $fp = @fopen($filepath, 'rb'))
00346 {
00347 return FALSE;
00348 }
00349
00350 flock($fp, LOCK_SH);
00351
00352 $cache = '';
00353 if (filesize($filepath) > 0)
00354 {
00355 $cache = fread($fp, filesize($filepath));
00356 }
00357
00358 flock($fp, LOCK_UN);
00359 fclose($fp);
00360
00361
00362 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
00363 {
00364 return FALSE;
00365 }
00366
00367
00368 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
00369 {
00370 @unlink($filepath);
00371 log_message('debug', "Cache file has expired. File deleted");
00372 return FALSE;
00373 }
00374
00375
00376 $this->_display(str_replace($match['0'], '', $cache));
00377 log_message('debug', "Cache file is current. Sending it to browser.");
00378 return TRUE;
00379 }
00380
00381
00382 }
00383
00384
00385
00386