blob: 7a03cf9c71229fdb8474ed4c2b33d9075a1f8238 [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;
33
34 function CI_Output()
35 {
36 log_message('debug', "Output Class Initialized");
37 }
38
39 // --------------------------------------------------------------------
40
41 /**
42 * Get Output
43 *
44 * Returns the current output string
45 *
46 * @access public
47 * @return string
48 */
49 function get_output()
50 {
51 return $this->final_output;
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Set Output
58 *
59 * Sets the output string
60 *
61 * @access public
62 * @param string
63 * @return void
64 */
65 function set_output($output)
66 {
67 $this->final_output = $output;
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Set Cache
74 *
75 * @access public
76 * @param integer
77 * @return void
78 */
79 function cache($time)
80 {
admin1cf89aa2006-09-03 18:24:39 +000081 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
adminb0dd10f2006-08-25 17:25:49 +000082 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Display Output
88 *
89 * All "view" data is automatically put into this variable
90 * by the controller class:
91 *
92 * $this->final_output
93 *
94 * This function simply echos the variable out. It also does the following:
95 *
96 * Stops the benchmark timer so the page rendering speed can be shown.
97 *
98 * Determines if the "memory_get_usage' function is available so that
99 * the memory usage can be shown.
100 *
101 * @access public
102 * @return void
103 */
104 function _display($output = '')
105 {
106 $BM =& _load_class('CI_Benchmark');
107
108 if ($output == '')
109 {
110 $output =& $this->final_output;
111 }
112
113 if ($this->cache_expiration > 0)
114 {
115 $this->_write_cache($output);
116 }
117
118 $elapsed = $BM->elapsed_time('code_igniter_start', 'code_igniter_end');
119 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
120
121 $output = str_replace('{memory_usage}', $memory, $output);
122 $output = str_replace('{elapsed_time}', $elapsed, $output);
123
124 echo $output;
125
126 log_message('debug', "Final output sent to browser");
127 log_message('debug', "Total execution time: ".$elapsed);
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Write a Cache File
134 *
135 * @access public
136 * @return void
137 */
138 function _write_cache($output)
139 {
140 $obj =& get_instance();
141 $path = $obj->config->item('cache_path');
142
143 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
144
145 if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
146 {
147 return;
148 }
149
admin2ed76d52006-09-02 17:34:52 +0000150 $uri = $obj->config->slash_item('base_url').
adminb0dd10f2006-08-25 17:25:49 +0000151 $obj->config->item('index_page').
152 $obj->uri->uri_string();
153
154 $cache_path .= md5($uri);
155
156 if ( ! $fp = @fopen($cache_path, 'wb'))
157 {
158 log_message('error', "Unable to write ache file: ".$cache_path);
159 return;
160 }
161
162 $expire = time() + ($this->cache_expiration * 60);
163
164 flock($fp, LOCK_EX);
165 fwrite($fp, $expire.'TS--->'.$output);
166 flock($fp, LOCK_UN);
167 fclose($fp);
168 @chmod($cache_path, 0777);
169
170 log_message('debug', "Cache file written: ".$cache_path);
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Update/serve a cached file
177 *
178 * @access public
179 * @return void
180 */
181 function _display_cache()
182 {
183 $CFG =& _load_class('CI_Config');
184 $RTR =& _load_class('CI_Router');
185
186 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
187
188 if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
189 {
190 return FALSE;
191 }
192
193 // Build the file path. The file name is an MD5 hash of the full URI
194 $uri = $CFG->item('base_url', 1).$CFG->item('index_page').$RTR->uri_string;
195
196 $filepath = $cache_path.md5($uri);
197
198 if ( ! @file_exists($filepath))
199 {
200 return FALSE;
201 }
202
203 if ( ! $fp = @fopen($filepath, 'rb'))
204 {
205 return FALSE;
206 }
207
208 flock($fp, LOCK_SH);
209
210 $cache = '';
211 if (filesize($filepath) > 0)
212 {
213 $cache = fread($fp, filesize($filepath));
214 }
215
216 flock($fp, LOCK_UN);
217 fclose($fp);
218
219 // Strip out the embedded timestamp
220 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
221 {
222 return FALSE;
223 }
224
225 // Has the file expired? If so we'll delete it.
226 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
227 {
228 @unlink($filepath);
229 log_message('debug', "Cache file has expired. File deleted");
230 return FALSE;
231 }
232
233 // Display the cache
234 $this->_display(str_replace($match['0'], '', $cache));
235 log_message('debug', "Cache file is current. Sending it to browser.");
236 return TRUE;
237 }
238
239}
240// END Output Class
241?>