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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Output Class |
| 20 | * |
| 21 | * Responsible for sending final output to browser |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 22 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 45 | * Get Output |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 60 | * Set Output |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 76 | * Set Header |
admin | 460f267 | 2006-09-21 02:41:37 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 95 | * Enable/disable Profiler |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 109 | * Set Cache |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 138 | // Note: We use globals because we can't use $CI =& get_instance() |
| 139 | // since this function is sometimes called by the caching mechanism, |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 161 | // Parse out the elapsed time and memory usage, |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 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"); |
admin | 3f643e6 | 2006-10-27 06:25:31 +0000 | [diff] [blame] | 204 | log_message('debug', "Total execution time: ".$elapsed); |
| 205 | return TRUE; |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // -------------------------------------------------------------------- |
| 209 | |
| 210 | // Grab the super object. We'll need it in a moment... |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 211 | $CI =& get_instance(); |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 212 | |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 213 | // Do we need to generate profile data? |
| 214 | // If so, load the Profile class and run it. |
| 215 | if ($this->enable_profiler == TRUE) |
| 216 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 217 | $CI->load->library('profiler'); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 218 | |
| 219 | // If the output data contains closing </body> and </html> tags |
| 220 | // we will remove them and add them back after we insert the profile data |
| 221 | if (preg_match("|</body>.*?</html>|is", $output)) |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 222 | { |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 223 | $output = preg_replace("|</body>.*?</html>|is", '', $output); |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 224 | $output .= $CI->profiler->run(); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 225 | $output .= '</body></html>'; |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 229 | $output .= $CI->profiler->run(); |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 230 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 231 | } |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
| 235 | // Does the controller contain a function named _output()? |
| 236 | // If so send the output there. Otherwise, echo it. |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 237 | if (method_exists($CI, '_output')) |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 238 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 239 | $CI->_output($output); |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 240 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 241 | else |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 242 | { |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 243 | echo $output; // Send it to the browser! |
| 244 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 245 | |
| 246 | log_message('debug', "Final output sent to browser"); |
| 247 | log_message('debug', "Total execution time: ".$elapsed); |
| 248 | } |
| 249 | |
| 250 | // -------------------------------------------------------------------- |
| 251 | |
| 252 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 253 | * Write a Cache File |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 254 | * |
| 255 | * @access public |
| 256 | * @return void |
| 257 | */ |
| 258 | function _write_cache($output) |
| 259 | { |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 260 | $CI =& get_instance(); |
| 261 | $path = $CI->config->item('cache_path'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 262 | |
| 263 | $cache_path = ($path == '') ? BASEPATH.'cache/' : $path; |
| 264 | |
| 265 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 266 | { |
| 267 | return; |
| 268 | } |
| 269 | |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 270 | $uri = $CI->config->item('base_url'). |
| 271 | $CI->config->item('index_page'). |
| 272 | $CI->uri->uri_string(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 273 | |
| 274 | $cache_path .= md5($uri); |
| 275 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 276 | if ( ! $fp = @fopen($cache_path, 'wb')) |
| 277 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 278 | log_message('error', "Unable to write ache file: ".$cache_path); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 279 | return; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | $expire = time() + ($this->cache_expiration * 60); |
| 283 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 284 | flock($fp, LOCK_EX); |
| 285 | fwrite($fp, $expire.'TS--->'.$output); |
| 286 | flock($fp, LOCK_UN); |
| 287 | fclose($fp); |
| 288 | @chmod($cache_path, 0777); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 289 | |
| 290 | log_message('debug', "Cache file written: ".$cache_path); |
| 291 | } |
| 292 | |
| 293 | // -------------------------------------------------------------------- |
| 294 | |
| 295 | /** |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 296 | * Update/serve a cached file |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 297 | * |
| 298 | * @access public |
| 299 | * @return void |
| 300 | */ |
admin | 15c2bcc | 2006-09-18 08:17:56 +0000 | [diff] [blame] | 301 | function _display_cache(&$CFG, &$RTR) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 302 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 303 | $CFG =& load_class('Config'); |
admin | 1b0ab46 | 2006-10-10 23:30:21 +0000 | [diff] [blame] | 304 | $RTR =& load_class('Router'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 305 | |
| 306 | $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); |
| 307 | |
| 308 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 309 | { |
| 310 | return FALSE; |
| 311 | } |
| 312 | |
| 313 | // 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] | 314 | $uri = $CFG->item('base_url'). |
| 315 | $CFG->item('index_page'). |
| 316 | $RTR->uri_string; |
admin | 1856f58 | 2006-09-15 20:48:15 +0000 | [diff] [blame] | 317 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 318 | $filepath = $cache_path.md5($uri); |
| 319 | |
| 320 | if ( ! @file_exists($filepath)) |
| 321 | { |
| 322 | return FALSE; |
| 323 | } |
| 324 | |
| 325 | if ( ! $fp = @fopen($filepath, 'rb')) |
| 326 | { |
| 327 | return FALSE; |
| 328 | } |
| 329 | |
| 330 | flock($fp, LOCK_SH); |
| 331 | |
| 332 | $cache = ''; |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 333 | if (filesize($filepath) > 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 334 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 335 | $cache = fread($fp, filesize($filepath)); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | flock($fp, LOCK_UN); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 339 | fclose($fp); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 340 | |
| 341 | // Strip out the embedded timestamp |
| 342 | if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) |
| 343 | { |
| 344 | return FALSE; |
| 345 | } |
| 346 | |
| 347 | // Has the file expired? If so we'll delete it. |
| 348 | if (time() >= trim(str_replace('TS--->', '', $match['1']))) |
| 349 | { |
| 350 | @unlink($filepath); |
| 351 | log_message('debug', "Cache file has expired. File deleted"); |
| 352 | return FALSE; |
| 353 | } |
| 354 | |
| 355 | // Display the cache |
| 356 | $this->_display(str_replace($match['0'], '', $cache)); |
| 357 | log_message('debug', "Cache file is current. Sending it to browser."); |
| 358 | return TRUE; |
| 359 | } |
| 360 | |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 361 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 362 | } |
| 363 | // END Output Class |
| 364 | ?> |