blob: b09bf2a53d4ff1ac5026d05861e7deff1176f9ca [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;
32 var $cache_expiration = 0;
admin460f2672006-09-21 02:41:37 +000033 var $headers = array();
adminb0dd10f2006-08-25 17:25:49 +000034
35 function CI_Output()
36 {
37 log_message('debug', "Output Class Initialized");
38 }
39
40 // --------------------------------------------------------------------
41
42 /**
43 * Get Output
44 *
45 * Returns the current output string
46 *
47 * @access public
48 * @return string
49 */
50 function get_output()
51 {
52 return $this->final_output;
53 }
54
55 // --------------------------------------------------------------------
56
57 /**
58 * Set Output
59 *
60 * Sets the output string
61 *
62 * @access public
63 * @param string
64 * @return void
65 */
66 function set_output($output)
67 {
68 $this->final_output = $output;
69 }
admin460f2672006-09-21 02:41:37 +000070
71 // --------------------------------------------------------------------
72
73 /**
74 * Set Header
75 *
76 * Lets you set a server header which will be outputted with the final display.
77 *
78 * Note: If a file is cached, headers will not be sent. We need to figure out
79 * how to permit header data to be saved with the cache data...
80 *
81 * @access public
82 * @param string
83 * @return void
84 */
85 function set_header($header)
86 {
87 $this->headers[] = $header;
88 }
89
adminb0dd10f2006-08-25 17:25:49 +000090
91 // --------------------------------------------------------------------
92
93 /**
94 * Set Cache
95 *
96 * @access public
97 * @param integer
98 * @return void
99 */
100 function cache($time)
101 {
admin1cf89aa2006-09-03 18:24:39 +0000102 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
adminb0dd10f2006-08-25 17:25:49 +0000103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Display Output
109 *
110 * All "view" data is automatically put into this variable
111 * by the controller class:
112 *
113 * $this->final_output
114 *
115 * This function simply echos the variable out. It also does the following:
116 *
117 * Stops the benchmark timer so the page rendering speed can be shown.
118 *
119 * Determines if the "memory_get_usage' function is available so that
120 * the memory usage can be shown.
121 *
122 * @access public
123 * @return void
124 */
125 function _display($output = '')
126 {
127 $BM =& _load_class('CI_Benchmark');
128
129 if ($output == '')
130 {
131 $output =& $this->final_output;
132 }
133
admin460f2672006-09-21 02:41:37 +0000134 // Do we need to write a cache file?
adminb0dd10f2006-08-25 17:25:49 +0000135 if ($this->cache_expiration > 0)
136 {
137 $this->_write_cache($output);
138 }
139
admin460f2672006-09-21 02:41:37 +0000140 // Parse out the elapsed time and memory usage, and
141 // swap the pseudo-variables with the data
adminb0dd10f2006-08-25 17:25:49 +0000142 $elapsed = $BM->elapsed_time('code_igniter_start', 'code_igniter_end');
143 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
144
145 $output = str_replace('{memory_usage}', $memory, $output);
146 $output = str_replace('{elapsed_time}', $elapsed, $output);
147
admin460f2672006-09-21 02:41:37 +0000148 // Are there any server headers to send?
149 if (count($this->headers) > 0)
150 {
151 foreach ($this->headers as $header)
152 {
153 @header($header);
154 }
155 }
156
157 // Send it to the browser!
adminb0dd10f2006-08-25 17:25:49 +0000158 echo $output;
159
160 log_message('debug', "Final output sent to browser");
161 log_message('debug', "Total execution time: ".$elapsed);
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Write a Cache File
168 *
169 * @access public
170 * @return void
171 */
172 function _write_cache($output)
173 {
174 $obj =& get_instance();
175 $path = $obj->config->item('cache_path');
176
177 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
178
179 if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
180 {
181 return;
182 }
183
admin1856f582006-09-15 20:48:15 +0000184 $uri = $obj->config->item('base_url').
adminb0dd10f2006-08-25 17:25:49 +0000185 $obj->config->item('index_page').
186 $obj->uri->uri_string();
187
188 $cache_path .= md5($uri);
189
190 if ( ! $fp = @fopen($cache_path, 'wb'))
191 {
192 log_message('error', "Unable to write ache file: ".$cache_path);
193 return;
194 }
195
196 $expire = time() + ($this->cache_expiration * 60);
197
198 flock($fp, LOCK_EX);
199 fwrite($fp, $expire.'TS--->'.$output);
200 flock($fp, LOCK_UN);
201 fclose($fp);
202 @chmod($cache_path, 0777);
203
204 log_message('debug', "Cache file written: ".$cache_path);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Update/serve a cached file
211 *
212 * @access public
213 * @return void
214 */
admin15c2bcc2006-09-18 08:17:56 +0000215 function _display_cache(&$CFG, &$RTR)
adminb0dd10f2006-08-25 17:25:49 +0000216 {
217 $CFG =& _load_class('CI_Config');
218 $RTR =& _load_class('CI_Router');
219
220 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
221
222 if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
223 {
224 return FALSE;
225 }
226
227 // Build the file path. The file name is an MD5 hash of the full URI
admin15c2bcc2006-09-18 08:17:56 +0000228 $uri = $CFG->item('base_url').
229 $CFG->item('index_page').
230 $RTR->uri_string;
admin1856f582006-09-15 20:48:15 +0000231
adminb0dd10f2006-08-25 17:25:49 +0000232 $filepath = $cache_path.md5($uri);
233
234 if ( ! @file_exists($filepath))
235 {
236 return FALSE;
237 }
238
239 if ( ! $fp = @fopen($filepath, 'rb'))
240 {
241 return FALSE;
242 }
243
244 flock($fp, LOCK_SH);
245
246 $cache = '';
247 if (filesize($filepath) > 0)
248 {
249 $cache = fread($fp, filesize($filepath));
250 }
251
252 flock($fp, LOCK_UN);
253 fclose($fp);
254
255 // Strip out the embedded timestamp
256 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
257 {
258 return FALSE;
259 }
260
261 // Has the file expired? If so we'll delete it.
262 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
263 {
264 @unlink($filepath);
265 log_message('debug', "Cache file has expired. File deleted");
266 return FALSE;
267 }
268
269 // Display the cache
270 $this->_display(str_replace($match['0'], '', $cache));
271 log_message('debug', "Cache file is current. Sending it to browser.");
272 return TRUE;
273 }
274
275}
276// END Output Class
277?>