PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/system/codeigniter/core/Output.php

https://github.com/ikhattab/pyrocms
PHP | 450 lines | 206 code | 74 blank | 170 comment | 34 complexity | c458d3513d21a74dd572e2db4e74e1d5 MD5 | raw file
  1. <?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
  9. * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Output Class
  18. *
  19. * Responsible for sending final output to browser
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category Output
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/output.html
  26. */
  27. class CI_Output {
  28. var $final_output;
  29. var $cache_expiration = 0;
  30. var $headers = array();
  31. var $enable_profiler = FALSE;
  32. var $parse_exec_vars = TRUE; // whether or not to parse variables like {elapsed_time} and {memory_usage}
  33. var $_zlib_oc = FALSE;
  34. var $_profiler_sections = array();
  35. function CI_Output()
  36. {
  37. $this->_zlib_oc = @ini_get('zlib.output_compression');
  38. log_message('debug', "Output Class Initialized");
  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. * Set Output
  56. *
  57. * Sets the output string
  58. *
  59. * @access public
  60. * @param string
  61. * @return void
  62. */
  63. function set_output($output)
  64. {
  65. $this->final_output = $output;
  66. }
  67. // --------------------------------------------------------------------
  68. /**
  69. * Append Output
  70. *
  71. * Appends data onto the output string
  72. *
  73. * @access public
  74. * @param string
  75. * @return void
  76. */
  77. function append_output($output)
  78. {
  79. if ($this->final_output == '')
  80. {
  81. $this->final_output = $output;
  82. }
  83. else
  84. {
  85. $this->final_output .= $output;
  86. }
  87. }
  88. // --------------------------------------------------------------------
  89. /**
  90. * Set Header
  91. *
  92. * Lets you set a server header which will be outputted with the final display.
  93. *
  94. * Note: If a file is cached, headers will not be sent. We need to figure out
  95. * how to permit header data to be saved with the cache data...
  96. *
  97. * @access public
  98. * @param string
  99. * @return void
  100. */
  101. function set_header($header, $replace = TRUE)
  102. {
  103. // If zlib.output_compression is enabled it will compress the output,
  104. // but it will not modify the content-length header to compensate for
  105. // the reduction, causing the browser to hang waiting for more data.
  106. // We'll just skip content-length in those cases.
  107. if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
  108. {
  109. return;
  110. }
  111. $this->headers[] = array($header, $replace);
  112. }
  113. // --------------------------------------------------------------------
  114. /**
  115. * Set HTTP Status Header
  116. * moved to Common procedural functions in 1.7.2
  117. *
  118. * @access public
  119. * @param int the status code
  120. * @param string
  121. * @return void
  122. */
  123. function set_status_header($code = 200, $text = '')
  124. {
  125. set_status_header($code, $text);
  126. }
  127. // --------------------------------------------------------------------
  128. /**
  129. * Enable/disable Profiler
  130. *
  131. * @access public
  132. * @param bool
  133. * @return void
  134. */
  135. function enable_profiler($val = TRUE)
  136. {
  137. $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Set Profiler Sections
  142. *
  143. * Allows override of default / config settings for Profiler section display
  144. *
  145. * @access public
  146. * @param array
  147. * @return void
  148. */
  149. function set_profiler_sections($sections)
  150. {
  151. foreach ($sections as $section => $enable)
  152. {
  153. $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
  154. }
  155. }
  156. // --------------------------------------------------------------------
  157. /**
  158. * Set Cache
  159. *
  160. * @access public
  161. * @param integer
  162. * @return void
  163. */
  164. function cache($time)
  165. {
  166. $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
  167. }
  168. // --------------------------------------------------------------------
  169. /**
  170. * Display Output
  171. *
  172. * All "view" data is automatically put into this variable by the controller class:
  173. *
  174. * $this->final_output
  175. *
  176. * This function sends the finalized output data to the browser along
  177. * with any server headers and profile data. It also stops the
  178. * benchmark timer so the page rendering speed and memory usage can be shown.
  179. *
  180. * @access public
  181. * @return mixed
  182. */
  183. function _display($output = '')
  184. {
  185. // Note: We use globals because we can't use $CI =& get_instance()
  186. // since this function is sometimes called by the caching mechanism,
  187. // which happens before the CI super object is available.
  188. global $BM, $CFG;
  189. // --------------------------------------------------------------------
  190. // Set the output data
  191. if ($output == '')
  192. {
  193. $output =& $this->final_output;
  194. }
  195. // --------------------------------------------------------------------
  196. // Do we need to write a cache file?
  197. if ($this->cache_expiration > 0)
  198. {
  199. $this->_write_cache($output);
  200. }
  201. // --------------------------------------------------------------------
  202. // Parse out the elapsed time and memory usage,
  203. // then swap the pseudo-variables with the data
  204. $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
  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. $output = str_replace('{elapsed_time}', $elapsed, $output);
  209. $output = str_replace('{memory_usage}', $memory, $output);
  210. }
  211. // --------------------------------------------------------------------
  212. // Is compression requested?
  213. if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
  214. {
  215. if (extension_loaded('zlib'))
  216. {
  217. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
  218. {
  219. ob_start('ob_gzhandler');
  220. }
  221. }
  222. }
  223. // --------------------------------------------------------------------
  224. // Are there any server headers to send?
  225. if (count($this->headers) > 0)
  226. {
  227. foreach ($this->headers as $header)
  228. {
  229. @header($header[0], $header[1]);
  230. }
  231. }
  232. // --------------------------------------------------------------------
  233. // Does the get_instance() function exist?
  234. // If not we know we are dealing with a cache file so we'll
  235. // simply echo out the data and exit.
  236. if ( ! function_exists('get_instance'))
  237. {
  238. echo $output;
  239. log_message('debug', "Final output sent to browser");
  240. log_message('debug', "Total execution time: ".$elapsed);
  241. return TRUE;
  242. }
  243. // --------------------------------------------------------------------
  244. // Grab the super object. We'll need it in a moment...
  245. $CI =& get_instance();
  246. // Do we need to generate profile data?
  247. // If so, load the Profile class and run it.
  248. if ($this->enable_profiler == TRUE)
  249. {
  250. $CI->load->library('profiler');
  251. if ( ! empty($this->_profiler_sections))
  252. {
  253. $CI->profiler->set_sections($this->_profiler_sections);
  254. }
  255. // If the output data contains closing </body> and </html> tags
  256. // we will remove them and add them back after we insert the profile data
  257. if (preg_match("|</body>.*?</html>|is", $output))
  258. {
  259. $output = preg_replace("|</body>.*?</html>|is", '', $output);
  260. $output .= $CI->profiler->run();
  261. $output .= '</body></html>';
  262. }
  263. else
  264. {
  265. $output .= $CI->profiler->run();
  266. }
  267. }
  268. // --------------------------------------------------------------------
  269. // Does the controller contain a function named _output()?
  270. // If so send the output there. Otherwise, echo it.
  271. if (method_exists($CI, '_output'))
  272. {
  273. $CI->_output($output);
  274. }
  275. else
  276. {
  277. echo $output; // Send it to the browser!
  278. }
  279. log_message('debug', "Final output sent to browser");
  280. log_message('debug', "Total execution time: ".$elapsed);
  281. }
  282. // --------------------------------------------------------------------
  283. /**
  284. * Write a Cache File
  285. *
  286. * @access public
  287. * @return void
  288. */
  289. function _write_cache($output)
  290. {
  291. $CI =& get_instance();
  292. $path = $CI->config->item('cache_path');
  293. $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
  294. if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
  295. {
  296. return;
  297. }
  298. $uri = $CI->config->item('base_url').
  299. $CI->config->item('index_page').
  300. $CI->uri->uri_string();
  301. $cache_path .= md5($uri);
  302. if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
  303. {
  304. log_message('error', "Unable to write cache file: ".$cache_path);
  305. return;
  306. }
  307. $expire = time() + ($this->cache_expiration * 60);
  308. if (flock($fp, LOCK_EX))
  309. {
  310. fwrite($fp, $expire.'TS--->'.$output);
  311. flock($fp, LOCK_UN);
  312. }
  313. else
  314. {
  315. log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
  316. return;
  317. }
  318. fclose($fp);
  319. @chmod($cache_path, FILE_WRITE_MODE);
  320. log_message('debug', "Cache file written: ".$cache_path);
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Update/serve a cached file
  325. *
  326. * @access public
  327. * @return void
  328. */
  329. function _display_cache(&$CFG, &$URI)
  330. {
  331. $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
  332. // Build the file path. The file name is an MD5 hash of the full URI
  333. $uri = $CFG->item('base_url').
  334. $CFG->item('index_page').
  335. $URI->uri_string;
  336. $filepath = $cache_path.md5($uri);
  337. if ( ! @file_exists($filepath))
  338. {
  339. return FALSE;
  340. }
  341. if ( ! $fp = @fopen($filepath, FOPEN_READ))
  342. {
  343. return FALSE;
  344. }
  345. flock($fp, LOCK_SH);
  346. $cache = '';
  347. if (filesize($filepath) > 0)
  348. {
  349. $cache = fread($fp, filesize($filepath));
  350. }
  351. flock($fp, LOCK_UN);
  352. fclose($fp);
  353. // Strip out the embedded timestamp
  354. if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
  355. {
  356. return FALSE;
  357. }
  358. // Has the file expired? If so we'll delete it.
  359. if (time() >= trim(str_replace('TS--->', '', $match['1'])))
  360. {
  361. if (is_really_writable($cache_path))
  362. {
  363. @unlink($filepath);
  364. log_message('debug', "Cache file has expired. File deleted");
  365. return FALSE;
  366. }
  367. }
  368. // Display the cache
  369. $this->_display(str_replace($match['0'], '', $cache));
  370. log_message('debug', "Cache file is current. Sending it to browser.");
  371. return TRUE;
  372. }
  373. }
  374. // END Output Class
  375. /* End of file Output.php */
  376. /* Location: ./system/core/Output.php */