blob: 7e13316a17e3b8abd261ff3833592f32bb8094db [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +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 Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @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;
32 var $cache_expiration = 0;
33 var $headers = array();
34 var $enable_profiler = FALSE;
35
36
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 * 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 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * 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 /**
109 * Set Cache
110 *
111 * @access public
112 * @param integer
113 * @return void
114 */
115 function cache($time)
116 {
117 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Display Output
124 *
125 * All "view" data is automatically put into this variable by the controller class:
126 *
127 * $this->final_output
128 *
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.
132 *
133 * @access public
134 * @return mixed
135 */
136 function _display($output = '')
137 {
138 // Note: We use globals because we can't use $CI =& get_instance()
139 // since this function is sometimes called by the caching mechanism,
140 // which happens before the CI super object is available.
141 global $BM, $CFG;
142
143 // --------------------------------------------------------------------
144
145 // Set the output data
146 if ($output == '')
147 {
148 $output =& $this->final_output;
149 }
150
151 // --------------------------------------------------------------------
152
153 // Do we need to write a cache file?
154 if ($this->cache_expiration > 0)
155 {
156 $this->_write_cache($output);
157 }
158
159 // --------------------------------------------------------------------
160
161 // Parse out the elapsed time and memory usage,
162 // then swap the pseudo-variables with the data
163
164 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
165 $output = str_replace('{elapsed_time}', $elapsed, $output);
166
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
172 // Is compression requested?
173 if ($CFG->item('compress_output') === TRUE)
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 }
183
184 // --------------------------------------------------------------------
185
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 }
193 }
194
195 // --------------------------------------------------------------------
196
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'))
201 {
202 echo $output;
203 log_message('debug', "Final output sent to browser");
204 log_message('debug', "Total execution time: ".$elapsed);
205 return TRUE;
206 }
207
208 // --------------------------------------------------------------------
209
210 // Grab the super object. We'll need it in a moment...
211 $CI =& get_instance();
212
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 {
217 $CI->load->library('profiler');
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))
222 {
223 $output = preg_replace("|</body>.*?</html>|is", '', $output);
224 $output .= $CI->profiler->run();
225 $output .= '</body></html>';
226 }
227 else
228 {
229 $output .= $CI->profiler->run();
230 }
231 }
232
233 // --------------------------------------------------------------------
234
235 // Does the controller contain a function named _output()?
236 // If so send the output there. Otherwise, echo it.
237 if (method_exists($CI, '_output'))
238 {
239 $CI->_output($output);
240 }
241 else
242 {
243 echo $output; // Send it to the browser!
244 }
245
246 log_message('debug', "Final output sent to browser");
247 log_message('debug', "Total execution time: ".$elapsed);
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * Write a Cache File
254 *
255 * @access public
256 * @return void
257 */
258 function _write_cache($output)
259 {
260 $CI =& get_instance();
261 $path = $CI->config->item('cache_path');
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
270 $uri = $CI->config->item('base_url').
271 $CI->config->item('index_page').
272 $CI->uri->uri_string();
273
274 $cache_path .= md5($uri);
275
276 if ( ! $fp = @fopen($cache_path, 'wb'))
277 {
Derek Allardd8856802007-05-24 03:49:37 +0000278 log_message('error', "Unable to write cache file: ".$cache_path);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000279 return;
280 }
281
282 $expire = time() + ($this->cache_expiration * 60);
283
284 flock($fp, LOCK_EX);
285 fwrite($fp, $expire.'TS--->'.$output);
286 flock($fp, LOCK_UN);
287 fclose($fp);
288 @chmod($cache_path, 0777);
289
290 log_message('debug', "Cache file written: ".$cache_path);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Update/serve a cached file
297 *
298 * @access public
299 * @return void
300 */
301 function _display_cache(&$CFG, &$RTR)
302 {
303 $CFG =& load_class('Config');
Rick Ellis30b40152007-07-20 00:01:13 +0000304 $URI =& load_class('URI');
Derek Allardd2df9bc2007-04-15 17:41:17 +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
314 $uri = $CFG->item('base_url').
315 $CFG->item('index_page').
Rick Ellis30b40152007-07-20 00:01:13 +0000316 $URI->uri_string;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000317
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 = '';
333 if (filesize($filepath) > 0)
334 {
335 $cache = fread($fp, filesize($filepath));
336 }
337
338 flock($fp, LOCK_UN);
339 fclose($fp);
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
361
362}
363// END Output Class
adminb0dd10f2006-08-25 17:25:49 +0000364?>