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, $replace = TRUE)
00111 {
00112 $this->headers[] = array($header, $replace);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 function set_status_header($code = '200', $text = '')
00126 {
00127 $stati = array(
00128 '200' => 'OK',
00129 '201' => 'Created',
00130 '202' => 'Accepted',
00131 '203' => 'Non-Authoritative Information',
00132 '204' => 'No Content',
00133 '205' => 'Reset Content',
00134 '206' => 'Partial Content',
00135
00136 '300' => 'Multiple Choices',
00137 '301' => 'Moved Permanently',
00138 '302' => 'Found',
00139 '304' => 'Not Modified',
00140 '305' => 'Use Proxy',
00141 '307' => 'Temporary Redirect',
00142
00143 '400' => 'Bad Request',
00144 '401' => 'Unauthorized',
00145 '403' => 'Forbidden',
00146 '404' => 'Not Found',
00147 '405' => 'Method Not Allowed',
00148 '406' => 'Not Acceptable',
00149 '407' => 'Proxy Authentication Required',
00150 '408' => 'Request Timeout',
00151 '409' => 'Conflict',
00152 '410' => 'Gone',
00153 '411' => 'Length Required',
00154 '412' => 'Precondition Failed',
00155 '413' => 'Request Entity Too Large',
00156 '414' => 'Request-URI Too Long',
00157 '415' => 'Unsupported Media Type',
00158 '416' => 'Requested Range Not Satisfiable',
00159 '417' => 'Expectation Failed',
00160
00161 '500' => 'Internal Server Error',
00162 '501' => 'Not Implemented',
00163 '502' => 'Bad Gateway',
00164 '503' => 'Service Unavailable',
00165 '504' => 'Gateway Timeout',
00166 '505' => 'HTTP Version Not Supported'
00167 );
00168
00169 if ($code == '' OR ! is_numeric($code))
00170 {
00171 show_error('Status codes must be numeric');
00172 }
00173
00174 if (isset($stati[$code]) AND $text == '')
00175 {
00176 $text = $stati[$code];
00177 }
00178
00179 if ($text == '')
00180 {
00181 show_error('No status text available. Please check your status code number or supply your own message text.');
00182 }
00183
00184 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
00185
00186 if (substr(php_sapi_name(), 0, 3) == 'cgi')
00187 {
00188 header("Status: {$code} {$text}", TRUE);
00189 }
00190 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
00191 {
00192 header($server_protocol." {$code} {$text}", TRUE, $code);
00193 }
00194 else
00195 {
00196 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 function enable_profiler($val = TRUE)
00210 {
00211 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 function cache($time)
00224 {
00225 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 function _display($output = '')
00245 {
00246
00247
00248
00249 global $BM, $CFG;
00250
00251
00252
00253
00254 if ($output == '')
00255 {
00256 $output =& $this->final_output;
00257 }
00258
00259
00260
00261
00262 if ($this->cache_expiration > 0)
00263 {
00264 $this->_write_cache($output);
00265 }
00266
00267
00268
00269
00270
00271
00272 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
00273 $output = str_replace('{elapsed_time}', $elapsed, $output);
00274
00275 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
00276 $output = str_replace('{memory_usage}', $memory, $output);
00277
00278
00279
00280
00281 if ($CFG->item('compress_output') === TRUE)
00282 {
00283 if (extension_loaded('zlib'))
00284 {
00285 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
00286 {
00287 ob_start('ob_gzhandler');
00288 }
00289 }
00290 }
00291
00292
00293
00294
00295 if (count($this->headers) > 0)
00296 {
00297 foreach ($this->headers as $header)
00298 {
00299 @header($header[0], $header[1]);
00300 }
00301 }
00302
00303
00304
00305
00306
00307
00308 if ( ! function_exists('get_instance'))
00309 {
00310 echo $output;
00311 log_message('debug', "Final output sent to browser");
00312 log_message('debug', "Total execution time: ".$elapsed);
00313 return TRUE;
00314 }
00315
00316
00317
00318
00319 $CI =& get_instance();
00320
00321
00322
00323 if ($this->enable_profiler == TRUE)
00324 {
00325 $CI->load->library('profiler');
00326
00327
00328
00329 if (preg_match("|</body>.*?</html>|is", $output))
00330 {
00331 $output = preg_replace("|</body>.*?</html>|is", '', $output);
00332 $output .= $CI->profiler->run();
00333 $output .= '</body></html>';
00334 }
00335 else
00336 {
00337 $output .= $CI->profiler->run();
00338 }
00339 }
00340
00341
00342
00343
00344
00345 if (method_exists($CI, '_output'))
00346 {
00347 $CI->_output($output);
00348 }
00349 else
00350 {
00351 echo $output;
00352 }
00353
00354 log_message('debug', "Final output sent to browser");
00355 log_message('debug', "Total execution time: ".$elapsed);
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 function _write_cache($output)
00367 {
00368 $CI =& get_instance();
00369 $path = $CI->config->item('cache_path');
00370
00371 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
00372
00373 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00374 {
00375 return;
00376 }
00377
00378 $uri = $CI->config->item('base_url').
00379 $CI->config->item('index_page').
00380 $CI->uri->uri_string();
00381
00382 $cache_path .= md5($uri);
00383
00384 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
00385 {
00386 log_message('error', "Unable to write cache file: ".$cache_path);
00387 return;
00388 }
00389
00390 $expire = time() + ($this->cache_expiration * 60);
00391
00392 if (flock($fp, LOCK_EX))
00393 {
00394 fwrite($fp, $expire.'TS--->'.$output);
00395 flock($fp, LOCK_UN);
00396 }
00397 else
00398 {
00399 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
00400 return;
00401 }
00402 fclose($fp);
00403 @chmod($cache_path, DIR_WRITE_MODE);
00404
00405 log_message('debug', "Cache file written: ".$cache_path);
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 function _display_cache(&$CFG, &$URI)
00417 {
00418 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
00419
00420 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00421 {
00422 return FALSE;
00423 }
00424
00425
00426 $uri = $CFG->item('base_url').
00427 $CFG->item('index_page').
00428 $URI->uri_string;
00429
00430 $filepath = $cache_path.md5($uri);
00431
00432 if ( ! @file_exists($filepath))
00433 {
00434 return FALSE;
00435 }
00436
00437 if ( ! $fp = @fopen($filepath, FOPEN_READ))
00438 {
00439 return FALSE;
00440 }
00441
00442 flock($fp, LOCK_SH);
00443
00444 $cache = '';
00445 if (filesize($filepath) > 0)
00446 {
00447 $cache = fread($fp, filesize($filepath));
00448 }
00449
00450 flock($fp, LOCK_UN);
00451 fclose($fp);
00452
00453
00454 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
00455 {
00456 return FALSE;
00457 }
00458
00459
00460 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
00461 {
00462 @unlink($filepath);
00463 log_message('debug', "Cache file has expired. File deleted");
00464 return FALSE;
00465 }
00466
00467
00468 $this->_display(str_replace($match['0'], '', $cache));
00469 log_message('debug', "Cache file is current. Sending it to browser.");
00470 return TRUE;
00471 }
00472
00473
00474 }
00475
00476
00477
00478