blob: ad92accd6105b7e3886df07e3710906f4db8a3e9 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://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 ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/output.html
28 */
29class CI_Output {
30
31 var $final_output;
32 var $cache_expiration = 0;
33 var $headers = array();
34 var $enable_profiler = FALSE;
Derek Jones46520492010-03-02 13:53:25 -060035 var $parse_exec_vars = TRUE; // whether or not to parse variables like {elapsed_time} and {memory_usage}
Derek Allard2067d1a2008-11-13 22:59:24 +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 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Append Output
77 *
78 * Appends data onto the output string
79 *
80 * @access public
81 * @param string
82 * @return void
83 */
84 function append_output($output)
85 {
86 if ($this->final_output == '')
87 {
88 $this->final_output = $output;
89 }
90 else
91 {
92 $this->final_output .= $output;
93 }
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Set Header
100 *
101 * Lets you set a server header which will be outputted with the final display.
102 *
103 * Note: If a file is cached, headers will not be sent. We need to figure out
104 * how to permit header data to be saved with the cache data...
105 *
106 * @access public
107 * @param string
108 * @return void
109 */
110 function set_header($header, $replace = TRUE)
111 {
112 $this->headers[] = array($header, $replace);
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000119 * moved to Common procedural functions in 1.7.2
120 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * @access public
122 * @param int the status code
123 * @param string
124 * @return void
125 */
Derek Jones46520492010-03-02 13:53:25 -0600126 function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Derek Jones817163a2009-07-11 17:05:58 +0000128 set_status_header($code, $text);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Enable/disable Profiler
135 *
136 * @access public
137 * @param bool
138 * @return void
139 */
140 function enable_profiler($val = TRUE)
141 {
142 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Set Cache
149 *
150 * @access public
151 * @param integer
152 * @return void
153 */
154 function cache($time)
155 {
156 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Display Output
163 *
164 * All "view" data is automatically put into this variable by the controller class:
165 *
166 * $this->final_output
167 *
168 * This function sends the finalized output data to the browser along
169 * with any server headers and profile data. It also stops the
170 * benchmark timer so the page rendering speed and memory usage can be shown.
171 *
172 * @access public
173 * @return mixed
174 */
175 function _display($output = '')
176 {
177 // Note: We use globals because we can't use $CI =& get_instance()
178 // since this function is sometimes called by the caching mechanism,
179 // which happens before the CI super object is available.
180 global $BM, $CFG;
181
182 // --------------------------------------------------------------------
183
184 // Set the output data
185 if ($output == '')
186 {
187 $output =& $this->final_output;
188 }
189
190 // --------------------------------------------------------------------
191
192 // Do we need to write a cache file?
193 if ($this->cache_expiration > 0)
194 {
195 $this->_write_cache($output);
196 }
197
198 // --------------------------------------------------------------------
199
200 // Parse out the elapsed time and memory usage,
201 // then swap the pseudo-variables with the data
Derek Allard2067d1a2008-11-13 22:59:24 +0000202
Derek Jones46520492010-03-02 13:53:25 -0600203 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
204
205 if ($this->parse_exec_vars === TRUE)
206 {
207 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
208
209 $output = str_replace('{elapsed_time}', $elapsed, $output);
210 $output = str_replace('{memory_usage}', $memory, $output);
211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213 // --------------------------------------------------------------------
214
215 // Is compression requested?
216 if ($CFG->item('compress_output') === TRUE)
217 {
218 if (extension_loaded('zlib'))
219 {
220 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
221 {
222 ob_start('ob_gzhandler');
223 }
224 }
225 }
226
227 // --------------------------------------------------------------------
228
229 // Are there any server headers to send?
230 if (count($this->headers) > 0)
231 {
232 foreach ($this->headers as $header)
233 {
234 @header($header[0], $header[1]);
235 }
236 }
237
238 // --------------------------------------------------------------------
239
240 // Does the get_instance() function exist?
241 // If not we know we are dealing with a cache file so we'll
242 // simply echo out the data and exit.
243 if ( ! function_exists('get_instance'))
244 {
245 echo $output;
246 log_message('debug', "Final output sent to browser");
247 log_message('debug', "Total execution time: ".$elapsed);
248 return TRUE;
249 }
250
251 // --------------------------------------------------------------------
252
253 // Grab the super object. We'll need it in a moment...
254 $CI =& get_instance();
255
256 // Do we need to generate profile data?
257 // If so, load the Profile class and run it.
258 if ($this->enable_profiler == TRUE)
259 {
260 $CI->load->library('profiler');
261
262 // If the output data contains closing </body> and </html> tags
263 // we will remove them and add them back after we insert the profile data
264 if (preg_match("|</body>.*?</html>|is", $output))
265 {
266 $output = preg_replace("|</body>.*?</html>|is", '', $output);
267 $output .= $CI->profiler->run();
268 $output .= '</body></html>';
269 }
270 else
271 {
272 $output .= $CI->profiler->run();
273 }
274 }
275
276 // --------------------------------------------------------------------
277
278 // Does the controller contain a function named _output()?
279 // If so send the output there. Otherwise, echo it.
280 if (method_exists($CI, '_output'))
281 {
282 $CI->_output($output);
283 }
284 else
285 {
286 echo $output; // Send it to the browser!
287 }
288
289 log_message('debug', "Final output sent to browser");
290 log_message('debug', "Total execution time: ".$elapsed);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Write a Cache File
297 *
298 * @access public
299 * @return void
300 */
301 function _write_cache($output)
302 {
303 $CI =& get_instance();
304 $path = $CI->config->item('cache_path');
305
306 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
307
308 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
309 {
310 return;
311 }
312
313 $uri = $CI->config->item('base_url').
314 $CI->config->item('index_page').
315 $CI->uri->uri_string();
316
317 $cache_path .= md5($uri);
318
319 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
320 {
321 log_message('error', "Unable to write cache file: ".$cache_path);
322 return;
323 }
324
325 $expire = time() + ($this->cache_expiration * 60);
326
327 if (flock($fp, LOCK_EX))
328 {
329 fwrite($fp, $expire.'TS--->'.$output);
330 flock($fp, LOCK_UN);
331 }
332 else
333 {
334 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
335 return;
336 }
337 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000338 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000339
340 log_message('debug', "Cache file written: ".$cache_path);
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Update/serve a cached file
347 *
348 * @access public
349 * @return void
350 */
351 function _display_cache(&$CFG, &$URI)
352 {
353 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
354
355 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
356 {
357 return FALSE;
358 }
359
360 // Build the file path. The file name is an MD5 hash of the full URI
361 $uri = $CFG->item('base_url').
362 $CFG->item('index_page').
363 $URI->uri_string;
364
365 $filepath = $cache_path.md5($uri);
366
367 if ( ! @file_exists($filepath))
368 {
369 return FALSE;
370 }
371
372 if ( ! $fp = @fopen($filepath, FOPEN_READ))
373 {
374 return FALSE;
375 }
376
377 flock($fp, LOCK_SH);
378
379 $cache = '';
380 if (filesize($filepath) > 0)
381 {
382 $cache = fread($fp, filesize($filepath));
383 }
384
385 flock($fp, LOCK_UN);
386 fclose($fp);
387
388 // Strip out the embedded timestamp
389 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
390 {
391 return FALSE;
392 }
393
394 // Has the file expired? If so we'll delete it.
395 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
396 {
397 @unlink($filepath);
398 log_message('debug', "Cache file has expired. File deleted");
399 return FALSE;
400 }
401
402 // Display the cache
403 $this->_display(str_replace($match['0'], '', $cache));
404 log_message('debug', "Cache file is current. Sending it to browser.");
405 return TRUE;
406 }
407
408
409}
410// END Output Class
411
412/* End of file Output.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600413/* Location: ./system/core/Output.php */