blob: 4ab5dd23c72312f7cd2135e550b38c7ab47dc749 [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.
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 */
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 /**
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 }
admin460f2672006-09-21 02:41:37 +000072
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 }
adminb0dd10f2006-08-25 17:25:49 +000091
92 // --------------------------------------------------------------------
93
94 /**
admine26611f2006-10-02 21:59:12 +000095 * 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 /**
adminb0dd10f2006-08-25 17:25:49 +0000109 * Set Cache
110 *
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 {
admin08f60202006-10-03 05:28:00 +0000138 // Note: We use globals because we can't use $obj =& get_instance()
139 // since this function is sometimes called by the caching mechanism,
140 // 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
admin08f60202006-10-03 05:28:00 +0000161 // Parse out the elapsed time and memory usage,
162 // 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");
204 log_message('debug', "Total execution time: ".$elapsed);
205 }
206
207 // --------------------------------------------------------------------
208
209 // Grab the super object. We'll need it in a moment...
210 $obj =& get_instance();
admine26611f2006-10-02 21:59:12 +0000211
admin08f60202006-10-03 05:28:00 +0000212 // Do we need to generate profile data?
213 // If so, load the Profile class and run it.
214 if ($this->enable_profiler == TRUE)
215 {
216 $obj->load->library('profiler');
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))
admine26611f2006-10-02 21:59:12 +0000221 {
admin08f60202006-10-03 05:28:00 +0000222 $output = preg_replace("|</body>.*?</html>|is", '', $output);
223 $output .= $obj->profiler->run();
224 $output .= '</body></html>';
admine26611f2006-10-02 21:59:12 +0000225 }
226 else
227 {
admin08f60202006-10-03 05:28:00 +0000228 $output .= $obj->profiler->run();
admine26611f2006-10-02 21:59:12 +0000229 }
adminc1fa0742006-09-21 23:50:23 +0000230 }
admin08f60202006-10-03 05:28:00 +0000231
232 // --------------------------------------------------------------------
233
234 // Does the controller contain a function named _output()?
235 // If so send the output there. Otherwise, echo it.
236 if (method_exists($obj, '_output'))
237 {
238 $obj->_output($output);
239 }
adminc1fa0742006-09-21 23:50:23 +0000240 else
admin08f60202006-10-03 05:28:00 +0000241 {
adminc1fa0742006-09-21 23:50:23 +0000242 echo $output; // Send it to the browser!
243 }
adminb0dd10f2006-08-25 17:25:49 +0000244
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 {
259 $obj =& get_instance();
260 $path = $obj->config->item('cache_path');
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
admin1856f582006-09-15 20:48:15 +0000269 $uri = $obj->config->item('base_url').
adminb0dd10f2006-08-25 17:25:49 +0000270 $obj->config->item('index_page').
271 $obj->uri->uri_string();
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 */
admin15c2bcc2006-09-18 08:17:56 +0000300 function _display_cache(&$CFG, &$RTR)
adminb0dd10f2006-08-25 17:25:49 +0000301 {
admin33de9a12006-09-28 06:50:16 +0000302 $CFG =& _load_class('Config');
303 $RTR =& _load_class('Router');
adminb0dd10f2006-08-25 17:25:49 +0000304
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
admin15c2bcc2006-09-18 08:17:56 +0000313 $uri = $CFG->item('base_url').
314 $CFG->item('index_page').
315 $RTR->uri_string;
admin1856f582006-09-15 20:48:15 +0000316
adminb0dd10f2006-08-25 17:25:49 +0000317 $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
admine26611f2006-10-02 21:59:12 +0000360
adminb0dd10f2006-08-25 17:25:49 +0000361}
362// END Output Class
363?>