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; |
| 32 | var $cache_expiration = 0; |
| 33 | |
| 34 | function CI_Output() |
| 35 | { |
| 36 | log_message('debug', "Output Class Initialized"); |
| 37 | } |
| 38 | |
| 39 | // -------------------------------------------------------------------- |
| 40 | |
| 41 | /** |
| 42 | * Get Output |
| 43 | * |
| 44 | * Returns the current output string |
| 45 | * |
| 46 | * @access public |
| 47 | * @return string |
| 48 | */ |
| 49 | function get_output() |
| 50 | { |
| 51 | return $this->final_output; |
| 52 | } |
| 53 | |
| 54 | // -------------------------------------------------------------------- |
| 55 | |
| 56 | /** |
| 57 | * Set Output |
| 58 | * |
| 59 | * Sets the output string |
| 60 | * |
| 61 | * @access public |
| 62 | * @param string |
| 63 | * @return void |
| 64 | */ |
| 65 | function set_output($output) |
| 66 | { |
| 67 | $this->final_output = $output; |
| 68 | } |
| 69 | |
| 70 | // -------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
| 73 | * Set Cache |
| 74 | * |
| 75 | * @access public |
| 76 | * @param integer |
| 77 | * @return void |
| 78 | */ |
| 79 | function cache($time) |
| 80 | { |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 81 | $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Display Output |
| 88 | * |
| 89 | * All "view" data is automatically put into this variable |
| 90 | * by the controller class: |
| 91 | * |
| 92 | * $this->final_output |
| 93 | * |
| 94 | * This function simply echos the variable out. It also does the following: |
| 95 | * |
| 96 | * Stops the benchmark timer so the page rendering speed can be shown. |
| 97 | * |
| 98 | * Determines if the "memory_get_usage' function is available so that |
| 99 | * the memory usage can be shown. |
| 100 | * |
| 101 | * @access public |
| 102 | * @return void |
| 103 | */ |
| 104 | function _display($output = '') |
| 105 | { |
| 106 | $BM =& _load_class('CI_Benchmark'); |
| 107 | |
| 108 | if ($output == '') |
| 109 | { |
| 110 | $output =& $this->final_output; |
| 111 | } |
| 112 | |
| 113 | if ($this->cache_expiration > 0) |
| 114 | { |
| 115 | $this->_write_cache($output); |
| 116 | } |
| 117 | |
| 118 | $elapsed = $BM->elapsed_time('code_igniter_start', 'code_igniter_end'); |
| 119 | $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; |
| 120 | |
| 121 | $output = str_replace('{memory_usage}', $memory, $output); |
| 122 | $output = str_replace('{elapsed_time}', $elapsed, $output); |
| 123 | |
| 124 | echo $output; |
| 125 | |
| 126 | log_message('debug', "Final output sent to browser"); |
| 127 | log_message('debug', "Total execution time: ".$elapsed); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Write a Cache File |
| 134 | * |
| 135 | * @access public |
| 136 | * @return void |
| 137 | */ |
| 138 | function _write_cache($output) |
| 139 | { |
| 140 | $obj =& get_instance(); |
| 141 | $path = $obj->config->item('cache_path'); |
| 142 | |
| 143 | $cache_path = ($path == '') ? BASEPATH.'cache/' : $path; |
| 144 | |
| 145 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 146 | { |
| 147 | return; |
| 148 | } |
| 149 | |
admin | 1856f58 | 2006-09-15 20:48:15 +0000 | [diff] [blame] | 150 | $uri = $obj->config->item('base_url'). |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 151 | $obj->config->item('index_page'). |
| 152 | $obj->uri->uri_string(); |
| 153 | |
| 154 | $cache_path .= md5($uri); |
| 155 | |
| 156 | if ( ! $fp = @fopen($cache_path, 'wb')) |
| 157 | { |
| 158 | log_message('error', "Unable to write ache file: ".$cache_path); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | $expire = time() + ($this->cache_expiration * 60); |
| 163 | |
| 164 | flock($fp, LOCK_EX); |
| 165 | fwrite($fp, $expire.'TS--->'.$output); |
| 166 | flock($fp, LOCK_UN); |
| 167 | fclose($fp); |
| 168 | @chmod($cache_path, 0777); |
| 169 | |
| 170 | log_message('debug', "Cache file written: ".$cache_path); |
| 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Update/serve a cached file |
| 177 | * |
| 178 | * @access public |
| 179 | * @return void |
| 180 | */ |
admin | 15c2bcc | 2006-09-18 08:17:56 +0000 | [diff] [blame^] | 181 | function _display_cache(&$CFG, &$RTR) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 182 | { |
| 183 | $CFG =& _load_class('CI_Config'); |
| 184 | $RTR =& _load_class('CI_Router'); |
| 185 | |
| 186 | $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); |
| 187 | |
| 188 | if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) |
| 189 | { |
| 190 | return FALSE; |
| 191 | } |
| 192 | |
| 193 | // 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^] | 194 | $uri = $CFG->item('base_url'). |
| 195 | $CFG->item('index_page'). |
| 196 | $RTR->uri_string; |
admin | 1856f58 | 2006-09-15 20:48:15 +0000 | [diff] [blame] | 197 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 198 | $filepath = $cache_path.md5($uri); |
| 199 | |
| 200 | if ( ! @file_exists($filepath)) |
| 201 | { |
| 202 | return FALSE; |
| 203 | } |
| 204 | |
| 205 | if ( ! $fp = @fopen($filepath, 'rb')) |
| 206 | { |
| 207 | return FALSE; |
| 208 | } |
| 209 | |
| 210 | flock($fp, LOCK_SH); |
| 211 | |
| 212 | $cache = ''; |
| 213 | if (filesize($filepath) > 0) |
| 214 | { |
| 215 | $cache = fread($fp, filesize($filepath)); |
| 216 | } |
| 217 | |
| 218 | flock($fp, LOCK_UN); |
| 219 | fclose($fp); |
| 220 | |
| 221 | // Strip out the embedded timestamp |
| 222 | if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) |
| 223 | { |
| 224 | return FALSE; |
| 225 | } |
| 226 | |
| 227 | // Has the file expired? If so we'll delete it. |
| 228 | if (time() >= trim(str_replace('TS--->', '', $match['1']))) |
| 229 | { |
| 230 | @unlink($filepath); |
| 231 | log_message('debug', "Cache file has expired. File deleted"); |
| 232 | return FALSE; |
| 233 | } |
| 234 | |
| 235 | // Display the cache |
| 236 | $this->_display(str_replace($match['0'], '', $cache)); |
| 237 | log_message('debug', "Cache file is current. Sending it to browser."); |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | // END Output Class |
| 243 | ?> |