blob: e5362740824645f4c8342bece47300934ef6c16b [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Output Class
20 *
21 * Responsible for sending final output to browser
admine334c472006-10-21 19:44:22 +000022 *
adminb0dd10f2006-08-25 17:25:49 +000023 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Output
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/libraries/output.html
28 */
29class CI_Output {
30
31 var $final_output;
admin08f60202006-10-03 05:28:00 +000032 var $cache_expiration = 0;
33 var $headers = array();
34 var $enable_profiler = FALSE;
35
adminb0dd10f2006-08-25 17:25:49 +000036
37 function CI_Output()
38 {
39 log_message('debug', "Output Class Initialized");
40 }
41
42 // --------------------------------------------------------------------
43
44 /**
admine334c472006-10-21 19:44:22 +000045 * Get Output
adminb0dd10f2006-08-25 17:25:49 +000046 *
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 /**
admine334c472006-10-21 19:44:22 +000060 * Set Output
adminb0dd10f2006-08-25 17:25:49 +000061 *
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 }
admin460f2672006-09-21 02:41:37 +000072
73 // --------------------------------------------------------------------
74
75 /**
admine334c472006-10-21 19:44:22 +000076 * Set Header
admin460f2672006-09-21 02:41:37 +000077 *
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 }
adminb0dd10f2006-08-25 17:25:49 +000091
92 // --------------------------------------------------------------------
93
94 /**
admine334c472006-10-21 19:44:22 +000095 * Enable/disable Profiler
admine26611f2006-10-02 21:59:12 +000096 *
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 /**
admine334c472006-10-21 19:44:22 +0000109 * Set Cache
adminb0dd10f2006-08-25 17:25:49 +0000110 *
111 * @access public
112 * @param integer
113 * @return void
114 */
115 function cache($time)
116 {
admin1cf89aa2006-09-03 18:24:39 +0000117 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
adminb0dd10f2006-08-25 17:25:49 +0000118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Display Output
124 *
admin08f60202006-10-03 05:28:00 +0000125 * All "view" data is automatically put into this variable by the controller class:
adminb0dd10f2006-08-25 17:25:49 +0000126 *
127 * $this->final_output
128 *
admin08f60202006-10-03 05:28:00 +0000129 * 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.
adminb0dd10f2006-08-25 17:25:49 +0000132 *
133 * @access public
admin08f60202006-10-03 05:28:00 +0000134 * @return mixed
adminb0dd10f2006-08-25 17:25:49 +0000135 */
136 function _display($output = '')
137 {
admine334c472006-10-21 19:44:22 +0000138 // Note: We use globals because we can't use $CI =& get_instance()
139 // since this function is sometimes called by the caching mechanism,
admin08f60202006-10-03 05:28:00 +0000140 // which happens before the CI super object is available.
adminb5a651c2006-09-23 17:39:41 +0000141 global $BM, $CFG;
admin08f60202006-10-03 05:28:00 +0000142
143 // --------------------------------------------------------------------
144
145 // Set the output data
adminb0dd10f2006-08-25 17:25:49 +0000146 if ($output == '')
147 {
148 $output =& $this->final_output;
149 }
150
admin08f60202006-10-03 05:28:00 +0000151 // --------------------------------------------------------------------
152
admin460f2672006-09-21 02:41:37 +0000153 // Do we need to write a cache file?
adminb0dd10f2006-08-25 17:25:49 +0000154 if ($this->cache_expiration > 0)
155 {
156 $this->_write_cache($output);
157 }
admin08f60202006-10-03 05:28:00 +0000158
159 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000160
admine334c472006-10-21 19:44:22 +0000161 // Parse out the elapsed time and memory usage,
admin08f60202006-10-03 05:28:00 +0000162 // then swap the pseudo-variables with the data
163
admine26611f2006-10-02 21:59:12 +0000164 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
adminb0dd10f2006-08-25 17:25:49 +0000165 $output = str_replace('{elapsed_time}', $elapsed, $output);
166
admin08f60202006-10-03 05:28:00 +0000167 $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
admin9aaa75e2006-09-21 04:45:20 +0000172 // Is compression requested?
adminb5a651c2006-09-23 17:39:41 +0000173 if ($CFG->item('compress_output') === TRUE)
admin9aaa75e2006-09-21 04:45:20 +0000174 {
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 }
admin08f60202006-10-03 05:28:00 +0000183
184 // --------------------------------------------------------------------
admin9aaa75e2006-09-21 04:45:20 +0000185
admin460f2672006-09-21 02:41:37 +0000186 // 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 }
admine26611f2006-10-02 21:59:12 +0000193 }
admin08f60202006-10-03 05:28:00 +0000194
195 // --------------------------------------------------------------------
admin460f2672006-09-21 02:41:37 +0000196
admin08f60202006-10-03 05:28:00 +0000197 // 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'))
adminc1fa0742006-09-21 23:50:23 +0000201 {
admin08f60202006-10-03 05:28:00 +0000202 echo $output;
203 log_message('debug', "Final output sent to browser");
admin3f643e62006-10-27 06:25:31 +0000204 log_message('debug', "Total execution time: ".$elapsed);
205 return TRUE;
admin08f60202006-10-03 05:28:00 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 // Grab the super object. We'll need it in a moment...
admin88a8ad12006-10-07 03:16:32 +0000211 $CI =& get_instance();
admine26611f2006-10-02 21:59:12 +0000212
admin08f60202006-10-03 05:28:00 +0000213 // Do we need to generate profile data?
214 // If so, load the Profile class and run it.
215 if ($this->enable_profiler == TRUE)
216 {
admin88a8ad12006-10-07 03:16:32 +0000217 $CI->load->library('profiler');
admin08f60202006-10-03 05:28:00 +0000218
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))
admine26611f2006-10-02 21:59:12 +0000222 {
admin08f60202006-10-03 05:28:00 +0000223 $output = preg_replace("|</body>.*?</html>|is", '', $output);
admin88a8ad12006-10-07 03:16:32 +0000224 $output .= $CI->profiler->run();
admin08f60202006-10-03 05:28:00 +0000225 $output .= '</body></html>';
admine26611f2006-10-02 21:59:12 +0000226 }
227 else
228 {
admin88a8ad12006-10-07 03:16:32 +0000229 $output .= $CI->profiler->run();
admine26611f2006-10-02 21:59:12 +0000230 }
adminc1fa0742006-09-21 23:50:23 +0000231 }
admin08f60202006-10-03 05:28:00 +0000232
233 // --------------------------------------------------------------------
234
235 // Does the controller contain a function named _output()?
236 // If so send the output there. Otherwise, echo it.
admin88a8ad12006-10-07 03:16:32 +0000237 if (method_exists($CI, '_output'))
admin08f60202006-10-03 05:28:00 +0000238 {
admin88a8ad12006-10-07 03:16:32 +0000239 $CI->_output($output);
admin08f60202006-10-03 05:28:00 +0000240 }
adminc1fa0742006-09-21 23:50:23 +0000241 else
admin08f60202006-10-03 05:28:00 +0000242 {
adminc1fa0742006-09-21 23:50:23 +0000243 echo $output; // Send it to the browser!
244 }
adminb0dd10f2006-08-25 17:25:49 +0000245
246 log_message('debug', "Final output sent to browser");
247 log_message('debug', "Total execution time: ".$elapsed);
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
admine334c472006-10-21 19:44:22 +0000253 * Write a Cache File
adminb0dd10f2006-08-25 17:25:49 +0000254 *
255 * @access public
256 * @return void
257 */
258 function _write_cache($output)
259 {
admin88a8ad12006-10-07 03:16:32 +0000260 $CI =& get_instance();
261 $path = $CI->config->item('cache_path');
adminb0dd10f2006-08-25 17:25:49 +0000262
263 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
264
265 if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
266 {
267 return;
268 }
269
admin88a8ad12006-10-07 03:16:32 +0000270 $uri = $CI->config->item('base_url').
271 $CI->config->item('index_page').
272 $CI->uri->uri_string();
adminb0dd10f2006-08-25 17:25:49 +0000273
274 $cache_path .= md5($uri);
275
admine334c472006-10-21 19:44:22 +0000276 if ( ! $fp = @fopen($cache_path, 'wb'))
277 {
adminb0dd10f2006-08-25 17:25:49 +0000278 log_message('error', "Unable to write ache file: ".$cache_path);
admine334c472006-10-21 19:44:22 +0000279 return;
adminb0dd10f2006-08-25 17:25:49 +0000280 }
281
282 $expire = time() + ($this->cache_expiration * 60);
283
admine334c472006-10-21 19:44:22 +0000284 flock($fp, LOCK_EX);
285 fwrite($fp, $expire.'TS--->'.$output);
286 flock($fp, LOCK_UN);
287 fclose($fp);
288 @chmod($cache_path, 0777);
adminb0dd10f2006-08-25 17:25:49 +0000289
290 log_message('debug', "Cache file written: ".$cache_path);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
admine334c472006-10-21 19:44:22 +0000296 * Update/serve a cached file
adminb0dd10f2006-08-25 17:25:49 +0000297 *
298 * @access public
299 * @return void
300 */
admin15c2bcc2006-09-18 08:17:56 +0000301 function _display_cache(&$CFG, &$RTR)
adminb0dd10f2006-08-25 17:25:49 +0000302 {
admin7099a582006-10-10 17:47:59 +0000303 $CFG =& load_class('Config');
admin1b0ab462006-10-10 23:30:21 +0000304 $RTR =& load_class('Router');
adminb0dd10f2006-08-25 17:25:49 +0000305
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
admin15c2bcc2006-09-18 08:17:56 +0000314 $uri = $CFG->item('base_url').
315 $CFG->item('index_page').
316 $RTR->uri_string;
admin1856f582006-09-15 20:48:15 +0000317
adminb0dd10f2006-08-25 17:25:49 +0000318 $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 = '';
admine334c472006-10-21 19:44:22 +0000333 if (filesize($filepath) > 0)
adminb0dd10f2006-08-25 17:25:49 +0000334 {
admine334c472006-10-21 19:44:22 +0000335 $cache = fread($fp, filesize($filepath));
adminb0dd10f2006-08-25 17:25:49 +0000336 }
337
338 flock($fp, LOCK_UN);
admine334c472006-10-21 19:44:22 +0000339 fclose($fp);
adminb0dd10f2006-08-25 17:25:49 +0000340
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
admine26611f2006-10-02 21:59:12 +0000361
adminb0dd10f2006-08-25 17:25:49 +0000362}
363// END Output Class
364?>