admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Output Class |
| 20 | * |
| 21 | * Responsible for sending final output to browser |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category Output |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/output.html |
| 28 | */ |
| 29 | class CI_Output { |
| 30 | |
| 31 | var $final_output; |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 32 | var $cache_expiration = 0; |
| 33 | var $headers = array(); |
| 34 | var $enable_profiler = FALSE; |
| 35 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 36 | |
| 37 | function CI_Output() |
| 38 | { |
| 39 | log_message('debug', "Output Class Initialized"); |
| 40 | } |
| 41 | |
| 42 | // -------------------------------------------------------------------- |
| 43 | |
| 44 | /** |
| 45 | * Get Output |
| 46 | * |
| 47 | * Returns the current output string |
| 48 | * |
| 49 | * @access public |
| 50 | * @return string |
| 51 | */ |
| 52 | function get_output() |
| 53 | { |
| 54 | return $this->final_output; |
| 55 | } |
| 56 | |
| 57 | // -------------------------------------------------------------------- |
| 58 | |
| 59 | /** |
| 60 | * Set Output |
| 61 | * |
| 62 | * Sets the output string |
| 63 | * |
| 64 | * @access public |
| 65 | * @param string |
| 66 | * @return void |
| 67 | */ |
| 68 | function set_output($output) |
| 69 | { |
| 70 | $this->final_output = $output; |
| 71 | } |
admin | 460f267 | 2006-09-21 02:41:37 +0000 | [diff] [blame] | 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Set Header |
| 77 | * |
| 78 | * Lets you set a server header which will be outputted with the final display. |
| 79 | * |
| 80 | * Note: If a file is cached, headers will not be sent. We need to figure out |
| 81 | * how to permit header data to be saved with the cache data... |
| 82 | * |
| 83 | * @access public |
| 84 | * @param string |
| 85 | * @return void |
| 86 | */ |
| 87 | function set_header($header) |
| 88 | { |
| 89 | $this->headers[] = $header; |
| 90 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 95 | * Enable/disable Profiler |
| 96 | * |
| 97 | * @access public |
| 98 | * @param bool |
| 99 | * @return void |
| 100 | */ |
| 101 | function enable_profiler($val = TRUE) |
| 102 | { |
| 103 | $this->enable_profiler = (is_bool($val)) ? $val : TRUE; |
| 104 | } |
| 105 | |
| 106 | // -------------------------------------------------------------------- |
| 107 | |
| 108 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 109 | * Set Cache |
| 110 | * |
| 111 | * @access public |
| 112 | * @param integer |
| 113 | * @return void |
| 114 | */ |
| 115 | function cache($time) |
| 116 | { |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 117 | $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
| 123 | * Display Output |
| 124 | * |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 125 | * All "view" data is automatically put into this variable by the controller class: |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 126 | * |
| 127 | * $this->final_output |
| 128 | * |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 129 | * This function sends the finalized output data to the browser along |
| 130 | * with any server headers and profile data. It also stops the |
| 131 | * benchmark timer so the page rendering speed and memory usage can be shown. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 132 | * |
| 133 | * @access public |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 134 | * @return mixed |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 135 | */ |
| 136 | function _display($output = '') |
| 137 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 138 | // Note: We use globals because we can't use $CI =& get_instance() |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 139 | // since this function is sometimes called by the caching mechanism, |
| 140 | // which happens before the CI super object is available. |
admin | b5a651c | 2006-09-23 17:39:41 +0000 | [diff] [blame] | 141 | global $BM, $CFG; |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | // Set the output data |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 146 | if ($output == '') |
| 147 | { |
| 148 | $output =& $this->final_output; |
| 149 | } |
| 150 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 151 | // -------------------------------------------------------------------- |
| 152 | |
admin | 460f267 | 2006-09-21 02:41:37 +0000 | [diff] [blame] | 153 | // Do we need to write a cache file? |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 154 | if ($this->cache_expiration > 0) |
| 155 | { |
| 156 | $this->_write_cache($output); |
| 157 | } |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 158 | |
| 159 | // -------------------------------------------------------------------- |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 160 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 161 | // Parse out the elapsed time and memory usage, |
| 162 | // then swap the pseudo-variables with the data |
| 163 | |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 164 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 165 | $output = str_replace('{elapsed_time}', $elapsed, $output); |
| 166 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 167 | $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; |
| 168 | $output = str_replace('{memory_usage}', $memory, $output); |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
admin | 9aaa75e | 2006-09-21 04:45:20 +0000 | [diff] [blame] | 172 | // Is compression requested? |
admin | b5a651c | 2006-09-23 17:39:41 +0000 | [diff] [blame] | 173 | if ($CFG->item('compress_output') === TRUE) |
admin | 9aaa75e | 2006-09-21 04:45:20 +0000 | [diff] [blame] | 174 | { |
| 175 | if (extension_loaded('zlib')) |
| 176 | { |
| 177 | if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
| 178 | { |
| 179 | ob_start('ob_gzhandler'); |
| 180 | } |
| 181 | } |
| 182 | } |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 183 | |
| 184 | // -------------------------------------------------------------------- |
admin | 9aaa75e | 2006-09-21 04:45:20 +0000 | [diff] [blame] | 185 | |
admin | 460f267 | 2006-09-21 02:41:37 +0000 | [diff] [blame] | 186 | // Are there any server headers to send? |
| 187 | if (count($this->headers) > 0) |
| 188 | { |
| 189 | foreach ($this->headers as $header) |
| 190 | { |
| 191 | @header($header); |
| 192 | } |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 193 | } |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 194 | |
| 195 | // -------------------------------------------------------------------- |
admin | 460f267 | 2006-09-21 02:41:37 +0000 | [diff] [blame] | 196 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 197 | // Does the get_instance() function exist? |
| 198 | // If not we know we are dealing with a cache file so we'll |
| 199 | // simply echo out the data and exit. |
| 200 | if ( ! function_exists('get_instance')) |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 201 | { |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 202 | echo $output; |
| 203 | log_message('debug', "Final output sent to browser"); |
| 204 | log_message('debug', "Total execution time: ".$elapsed); |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | // Grab the super object. We'll need it in a moment... |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 210 | $CI =& get_instance(); |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 211 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 212 | // Do we need to generate profile data? |
| 213 | // If so, load the Profile class and run it. |
| 214 | if ($this->enable_profiler == TRUE) |
| 215 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 216 | $CI->load->library('profiler'); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 217 | |
| 218 | // If the output data contains closing </body> and </html> tags |
| 219 | // we will remove them and add them back after we insert the profile data |
| 220 | if (preg_match("|</body>.*?</html>|is", $output)) |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 221 | { |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 222 | $output = preg_replace("|</body>.*?</html>|is", '', $output); |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 223 | $output .= $CI->profiler->run(); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 224 | $output .= '</body></html>'; |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 228 | $output .= $CI->profiler->run(); |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 229 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 230 | } |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | // Does the controller contain a function named _output()? |
| 235 | // If so send the output there. Otherwise, echo it. |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 236 | if (method_exists($CI, '_output')) |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 237 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 238 | $CI->_output($output); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 239 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 240 | else |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 241 | { |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 242 | echo $output; // Send it to the browser! |
| 243 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 244 | |
| 245 | log_message('debug', "Final output sent to browser"); |
| 246 | log_message('debug', "Total execution time: ".$elapsed); |
| 247 | } |
| 248 | |
| 249 | // -------------------------------------------------------------------- |
| 250 | |
| 251 | /** |
| 252 | * Write a Cache File |
| 253 | * |
| 254 | * @access public |
| 255 | * @return void |
| 256 | */ |
| 257 | function _write_cache($output) |
| 258 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 259 | $CI =& get_instance(); |
| 260 | $path = $CI->config->item('cache_path'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 261 | |
| 262 | $cache_path = ($path == '') ? BASEPATH.'cache/' : $path; |
| 263 | |
| 264 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 265 | { |
| 266 | return; |
| 267 | } |
| 268 | |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 269 | $uri = $CI->config->item('base_url'). |
| 270 | $CI->config->item('index_page'). |
| 271 | $CI->uri->uri_string(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 272 | |
| 273 | $cache_path .= md5($uri); |
| 274 | |
| 275 | if ( ! $fp = @fopen($cache_path, 'wb')) |
| 276 | { |
| 277 | log_message('error', "Unable to write ache file: ".$cache_path); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | $expire = time() + ($this->cache_expiration * 60); |
| 282 | |
| 283 | flock($fp, LOCK_EX); |
| 284 | fwrite($fp, $expire.'TS--->'.$output); |
| 285 | flock($fp, LOCK_UN); |
| 286 | fclose($fp); |
| 287 | @chmod($cache_path, 0777); |
| 288 | |
| 289 | log_message('debug', "Cache file written: ".$cache_path); |
| 290 | } |
| 291 | |
| 292 | // -------------------------------------------------------------------- |
| 293 | |
| 294 | /** |
| 295 | * Update/serve a cached file |
| 296 | * |
| 297 | * @access public |
| 298 | * @return void |
| 299 | */ |
admin | 15c2bcc | 2006-09-18 08:17:56 +0000 | [diff] [blame] | 300 | function _display_cache(&$CFG, &$RTR) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 301 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 302 | $CFG =& load_class('Config'); |
admin | 1b0ab46 | 2006-10-10 23:30:21 +0000 | [diff] [blame^] | 303 | $RTR =& load_class('Router'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 304 | |
| 305 | $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); |
| 306 | |
| 307 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 308 | { |
| 309 | return FALSE; |
| 310 | } |
| 311 | |
| 312 | // Build the file path. The file name is an MD5 hash of the full URI |
admin | 15c2bcc | 2006-09-18 08:17:56 +0000 | [diff] [blame] | 313 | $uri = $CFG->item('base_url'). |
| 314 | $CFG->item('index_page'). |
| 315 | $RTR->uri_string; |
admin | 1856f58 | 2006-09-15 20:48:15 +0000 | [diff] [blame] | 316 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 317 | $filepath = $cache_path.md5($uri); |
| 318 | |
| 319 | if ( ! @file_exists($filepath)) |
| 320 | { |
| 321 | return FALSE; |
| 322 | } |
| 323 | |
| 324 | if ( ! $fp = @fopen($filepath, 'rb')) |
| 325 | { |
| 326 | return FALSE; |
| 327 | } |
| 328 | |
| 329 | flock($fp, LOCK_SH); |
| 330 | |
| 331 | $cache = ''; |
| 332 | if (filesize($filepath) > 0) |
| 333 | { |
| 334 | $cache = fread($fp, filesize($filepath)); |
| 335 | } |
| 336 | |
| 337 | flock($fp, LOCK_UN); |
| 338 | fclose($fp); |
| 339 | |
| 340 | // Strip out the embedded timestamp |
| 341 | if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) |
| 342 | { |
| 343 | return FALSE; |
| 344 | } |
| 345 | |
| 346 | // Has the file expired? If so we'll delete it. |
| 347 | if (time() >= trim(str_replace('TS--->', '', $match['1']))) |
| 348 | { |
| 349 | @unlink($filepath); |
| 350 | log_message('debug', "Cache file has expired. File deleted"); |
| 351 | return FALSE; |
| 352 | } |
| 353 | |
| 354 | // Display the cache |
| 355 | $this->_display(str_replace($match['0'], '', $cache)); |
| 356 | log_message('debug', "Cache file is current. Sending it to browser."); |
| 357 | return TRUE; |
| 358 | } |
| 359 | |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 360 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 361 | } |
| 362 | // END Output Class |
| 363 | ?> |