Take two:
[www-register-wizard.git] / libraries / Output.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Output Class\r
20  *\r
21  * Responsible for sending final output to browser\r
22  *\r
23  * @package             CodeIgniter\r
24  * @subpackage  Libraries\r
25  * @category    Output\r
26  * @author              ExpressionEngine Dev Team\r
27  * @link                http://codeigniter.com/user_guide/libraries/output.html\r
28  */\r
29 class CI_Output {\r
30 \r
31         var $final_output;\r
32         var $cache_expiration   = 0;\r
33         var $headers                    = array();\r
34         var $enable_profiler    = FALSE;\r
35 \r
36 \r
37         function CI_Output()\r
38         {\r
39                 log_message('debug', "Output Class Initialized");\r
40         }\r
41         \r
42         // --------------------------------------------------------------------\r
43         \r
44         /**\r
45          * Get Output\r
46          *\r
47          * Returns the current output string\r
48          *\r
49          * @access      public\r
50          * @return      string\r
51          */     \r
52         function get_output()\r
53         {\r
54                 return $this->final_output;\r
55         }\r
56         \r
57         // --------------------------------------------------------------------\r
58         \r
59         /**\r
60          * Set Output\r
61          *\r
62          * Sets the output string\r
63          *\r
64          * @access      public\r
65          * @param       string\r
66          * @return      void\r
67          */     \r
68         function set_output($output)\r
69         {\r
70                 $this->final_output = $output;\r
71         }\r
72 \r
73         // --------------------------------------------------------------------\r
74 \r
75         /**\r
76          * Append Output\r
77          *\r
78          * Appends data onto the output string\r
79          *\r
80          * @access      public\r
81          * @param       string\r
82          * @return      void\r
83          */     \r
84         function append_output($output)\r
85         {\r
86                 if ($this->final_output == '')\r
87                 {\r
88                         $this->final_output = $output;\r
89                 }\r
90                 else\r
91                 {\r
92                         $this->final_output .= $output;\r
93                 }\r
94         }\r
95 \r
96         // --------------------------------------------------------------------\r
97 \r
98         /**\r
99          * Set Header\r
100          *\r
101          * Lets you set a server header which will be outputted with the final display.\r
102          *\r
103          * Note:  If a file is cached, headers will not be sent.  We need to figure out\r
104          * how to permit header data to be saved with the cache data...\r
105          *\r
106          * @access      public\r
107          * @param       string\r
108          * @return      void\r
109          */     \r
110         function set_header($header, $replace = TRUE)\r
111         {\r
112                 $this->headers[] = array($header, $replace);\r
113         }\r
114 \r
115         // --------------------------------------------------------------------\r
116         \r
117         /**\r
118          * Set HTTP Status Header\r
119          *\r
120          * @access      public\r
121          * @param       int     the status code\r
122          * @param       string  \r
123          * @return      void\r
124          */     \r
125         function set_status_header($code = '200', $text = '')\r
126         {\r
127                 $stati = array(\r
128                                                         '200'   => 'OK',\r
129                                                         '201'   => 'Created',\r
130                                                         '202'   => 'Accepted',\r
131                                                         '203'   => 'Non-Authoritative Information',\r
132                                                         '204'   => 'No Content',\r
133                                                         '205'   => 'Reset Content',\r
134                                                         '206'   => 'Partial Content',\r
135                                                         \r
136                                                         '300'   => 'Multiple Choices',\r
137                                                         '301'   => 'Moved Permanently',\r
138                                                         '302'   => 'Found',\r
139                                                         '304'   => 'Not Modified',\r
140                                                         '305'   => 'Use Proxy',\r
141                                                         '307'   => 'Temporary Redirect',\r
142                                                         \r
143                                                         '400'   => 'Bad Request',\r
144                                                         '401'   => 'Unauthorized',\r
145                                                         '403'   => 'Forbidden',\r
146                                                         '404'   => 'Not Found',\r
147                                                         '405'   => 'Method Not Allowed',\r
148                                                         '406'   => 'Not Acceptable',\r
149                                                         '407'   => 'Proxy Authentication Required',\r
150                                                         '408'   => 'Request Timeout',\r
151                                                         '409'   => 'Conflict',\r
152                                                         '410'   => 'Gone',\r
153                                                         '411'   => 'Length Required',\r
154                                                         '412'   => 'Precondition Failed',\r
155                                                         '413'   => 'Request Entity Too Large',\r
156                                                         '414'   => 'Request-URI Too Long',\r
157                                                         '415'   => 'Unsupported Media Type',\r
158                                                         '416'   => 'Requested Range Not Satisfiable',\r
159                                                         '417'   => 'Expectation Failed',\r
160                 \r
161                                                         '500'   => 'Internal Server Error',\r
162                                                         '501'   => 'Not Implemented',\r
163                                                         '502'   => 'Bad Gateway',\r
164                                                         '503'   => 'Service Unavailable',\r
165                                                         '504'   => 'Gateway Timeout',\r
166                                                         '505'   => 'HTTP Version Not Supported'\r
167                                                 );\r
168 \r
169                 if ($code == '' OR ! is_numeric($code))\r
170                 {\r
171                         show_error('Status codes must be numeric');\r
172                 }\r
173 \r
174                 if (isset($stati[$code]) AND $text == '')\r
175                 {                               \r
176                         $text = $stati[$code];\r
177                 }\r
178                 \r
179                 if ($text == '')\r
180                 {\r
181                         show_error('No status text available.  Please check your status code number or supply your own message text.');\r
182                 }\r
183                 \r
184                 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;\r
185         \r
186                 if (substr(php_sapi_name(), 0, 3) == 'cgi')\r
187                 {\r
188                         header("Status: {$code} {$text}", TRUE);\r
189                 }\r
190                 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')\r
191                 {\r
192                         header($server_protocol." {$code} {$text}", TRUE, $code);\r
193                 }\r
194                 else\r
195                 {\r
196                         header("HTTP/1.1 {$code} {$text}", TRUE, $code);\r
197                 }\r
198         }\r
199         \r
200         // --------------------------------------------------------------------\r
201         \r
202         /**\r
203          * Enable/disable Profiler\r
204          *\r
205          * @access      public\r
206          * @param       bool\r
207          * @return      void\r
208          */     \r
209         function enable_profiler($val = TRUE)\r
210         {\r
211                 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;\r
212         }\r
213         \r
214         // --------------------------------------------------------------------\r
215         \r
216         /**\r
217          * Set Cache\r
218          *\r
219          * @access      public\r
220          * @param       integer\r
221          * @return      void\r
222          */     \r
223         function cache($time)\r
224         {\r
225                 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;\r
226         }\r
227         \r
228         // --------------------------------------------------------------------\r
229         \r
230         /**\r
231          * Display Output\r
232          *\r
233          * All "view" data is automatically put into this variable by the controller class:\r
234          *\r
235          * $this->final_output\r
236          *\r
237          * This function sends the finalized output data to the browser along\r
238          * with any server headers and profile data.  It also stops the\r
239          * benchmark timer so the page rendering speed and memory usage can be shown.\r
240          *\r
241          * @access      public\r
242          * @return      mixed\r
243          */             \r
244         function _display($output = '')\r
245         {       \r
246                 // Note:  We use globals because we can't use $CI =& get_instance()\r
247                 // since this function is sometimes called by the caching mechanism,\r
248                 // which happens before the CI super object is available.\r
249                 global $BM, $CFG;\r
250                 \r
251                 // --------------------------------------------------------------------\r
252                 \r
253                 // Set the output data\r
254                 if ($output == '')\r
255                 {\r
256                         $output =& $this->final_output;\r
257                 }\r
258                 \r
259                 // --------------------------------------------------------------------\r
260                 \r
261                 // Do we need to write a cache file?\r
262                 if ($this->cache_expiration > 0)\r
263                 {\r
264                         $this->_write_cache($output);\r
265                 }\r
266                 \r
267                 // --------------------------------------------------------------------\r
268 \r
269                 // Parse out the elapsed time and memory usage,\r
270                 // then swap the pseudo-variables with the data\r
271 \r
272                 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');         \r
273                 $output = str_replace('{elapsed_time}', $elapsed, $output);\r
274                 \r
275                 $memory  = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';\r
276                 $output = str_replace('{memory_usage}', $memory, $output);              \r
277 \r
278                 // --------------------------------------------------------------------\r
279                 \r
280                 // Is compression requested?\r
281                 if ($CFG->item('compress_output') === TRUE)\r
282                 {\r
283                         if (extension_loaded('zlib'))\r
284                         {\r
285                                 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)\r
286                                 {\r
287                                         ob_start('ob_gzhandler');\r
288                                 }\r
289                         }\r
290                 }\r
291 \r
292                 // --------------------------------------------------------------------\r
293                 \r
294                 // Are there any server headers to send?\r
295                 if (count($this->headers) > 0)\r
296                 {\r
297                         foreach ($this->headers as $header)\r
298                         {\r
299                                 @header($header[0], $header[1]);\r
300                         }\r
301                 }               \r
302 \r
303                 // --------------------------------------------------------------------\r
304                 \r
305                 // Does the get_instance() function exist?\r
306                 // If not we know we are dealing with a cache file so we'll\r
307                 // simply echo out the data and exit.\r
308                 if ( ! function_exists('get_instance'))\r
309                 {\r
310                         echo $output;\r
311                         log_message('debug', "Final output sent to browser");\r
312                         log_message('debug', "Total execution time: ".$elapsed);\r
313                         return TRUE;\r
314                 }\r
315         \r
316                 // --------------------------------------------------------------------\r
317 \r
318                 // Grab the super object.  We'll need it in a moment...\r
319                 $CI =& get_instance();\r
320                 \r
321                 // Do we need to generate profile data?\r
322                 // If so, load the Profile class and run it.\r
323                 if ($this->enable_profiler == TRUE)\r
324                 {\r
325                         $CI->load->library('profiler');                         \r
326                                                                                 \r
327                         // If the output data contains closing </body> and </html> tags\r
328                         // we will remove them and add them back after we insert the profile data\r
329                         if (preg_match("|</body>.*?</html>|is", $output))\r
330                         {\r
331                                 $output  = preg_replace("|</body>.*?</html>|is", '', $output);\r
332                                 $output .= $CI->profiler->run();\r
333                                 $output .= '</body></html>';\r
334                         }\r
335                         else\r
336                         {\r
337                                 $output .= $CI->profiler->run();\r
338                         }\r
339                 }\r
340                 \r
341                 // --------------------------------------------------------------------\r
342 \r
343                 // Does the controller contain a function named _output()?\r
344                 // If so send the output there.  Otherwise, echo it.\r
345                 if (method_exists($CI, '_output'))\r
346                 {\r
347                         $CI->_output($output);\r
348                 }\r
349                 else\r
350                 {\r
351                         echo $output;  // Send it to the browser!\r
352                 }\r
353                 \r
354                 log_message('debug', "Final output sent to browser");\r
355                 log_message('debug', "Total execution time: ".$elapsed);                \r
356         }\r
357         \r
358         // --------------------------------------------------------------------\r
359         \r
360         /**\r
361          * Write a Cache File\r
362          *\r
363          * @access      public\r
364          * @return      void\r
365          */     \r
366         function _write_cache($output)\r
367         {\r
368                 $CI =& get_instance();  \r
369                 $path = $CI->config->item('cache_path');\r
370         \r
371                 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;\r
372                 \r
373                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))\r
374                 {\r
375                         return;\r
376                 }\r
377                 \r
378                 $uri =  $CI->config->item('base_url').\r
379                                 $CI->config->item('index_page').\r
380                                 $CI->uri->uri_string();\r
381                 \r
382                 $cache_path .= md5($uri);\r
383 \r
384                 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))\r
385                 {\r
386                         log_message('error', "Unable to write cache file: ".$cache_path);\r
387                         return;\r
388                 }\r
389                 \r
390                 $expire = time() + ($this->cache_expiration * 60);\r
391                 \r
392                 if (flock($fp, LOCK_EX))\r
393                 {\r
394                         fwrite($fp, $expire.'TS--->'.$output);\r
395                         flock($fp, LOCK_UN);\r
396                 }\r
397                 else\r
398                 {\r
399                         log_message('error', "Unable to secure a file lock for file at: ".$cache_path);\r
400                         return;\r
401                 }\r
402                 fclose($fp);\r
403                 @chmod($cache_path, DIR_WRITE_MODE);\r
404 \r
405                 log_message('debug', "Cache file written: ".$cache_path);\r
406         }\r
407 \r
408         // --------------------------------------------------------------------\r
409         \r
410         /**\r
411          * Update/serve a cached file\r
412          *\r
413          * @access      public\r
414          * @return      void\r
415          */     \r
416         function _display_cache(&$CFG, &$URI)\r
417         {\r
418                 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');\r
419                         \r
420                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))\r
421                 {\r
422                         return FALSE;\r
423                 }\r
424                 \r
425                 // Build the file path.  The file name is an MD5 hash of the full URI\r
426                 $uri =  $CFG->item('base_url').\r
427                                 $CFG->item('index_page').\r
428                                 $URI->uri_string;\r
429                                 \r
430                 $filepath = $cache_path.md5($uri);\r
431                 \r
432                 if ( ! @file_exists($filepath))\r
433                 {\r
434                         return FALSE;\r
435                 }\r
436         \r
437                 if ( ! $fp = @fopen($filepath, FOPEN_READ))\r
438                 {\r
439                         return FALSE;\r
440                 }\r
441                         \r
442                 flock($fp, LOCK_SH);\r
443                 \r
444                 $cache = '';\r
445                 if (filesize($filepath) > 0)\r
446                 {\r
447                         $cache = fread($fp, filesize($filepath));\r
448                 }\r
449         \r
450                 flock($fp, LOCK_UN);\r
451                 fclose($fp);\r
452                                         \r
453                 // Strip out the embedded timestamp             \r
454                 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))\r
455                 {\r
456                         return FALSE;\r
457                 }\r
458                 \r
459                 // Has the file expired? If so we'll delete it.\r
460                 if (time() >= trim(str_replace('TS--->', '', $match['1'])))\r
461                 {               \r
462                         @unlink($filepath);\r
463                         log_message('debug', "Cache file has expired. File deleted");\r
464                         return FALSE;\r
465                 }\r
466 \r
467                 // Display the cache\r
468                 $this->_display(str_replace($match['0'], '', $cache));\r
469                 log_message('debug', "Cache file is current. Sending it to browser.");          \r
470                 return TRUE;\r
471         }\r
472 \r
473 \r
474 }\r
475 // END Output Class\r
476 \r
477 /* End of file Output.php */\r
478 /* Location: ./system/libraries/Output.php */