PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Output.php

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