Improve the core Output library
diff --git a/system/core/Output.php b/system/core/Output.php
index e529f91..2725450 100755
--- a/system/core/Output.php
+++ b/system/core/Output.php
@@ -1,13 +1,13 @@
-<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /**
  * CodeIgniter
  *
  * An open source application development framework for PHP 5.1.6 or newer
  *
  * NOTICE OF LICENSE
- * 
+ *
  * Licensed under the Open Software License version 3.0
- * 
+ *
  * This source file is subject to the Open Software License (OSL 3.0) that is
  * bundled with this package in the files license.txt / license.rst.  It is
  * also available through the world wide web at this URL:
@@ -44,64 +44,52 @@
 	 * Current output string
 	 *
 	 * @var string
-	 * @access 	protected
 	 */
 	protected $final_output;
 	/**
 	 * Cache expiration time
 	 *
 	 * @var int
-	 * @access 	protected
 	 */
 	protected $cache_expiration	= 0;
 	/**
 	 * List of server headers
 	 *
 	 * @var array
-	 * @access 	protected
 	 */
 	protected $headers			= array();
 	/**
 	 * List of mime types
 	 *
 	 * @var array
-	 * @access 	protected
 	 */
 	protected $mime_types		= array();
 	/**
 	 * Determines wether profiler is enabled
 	 *
 	 * @var book
-	 * @access 	protected
 	 */
 	protected $enable_profiler	= FALSE;
 	/**
 	 * Determines if output compression is enabled
 	 *
 	 * @var bool
-	 * @access 	protected
 	 */
 	protected $_zlib_oc			= FALSE;
 	/**
 	 * List of profiler sections
 	 *
 	 * @var array
-	 * @access 	protected
 	 */
 	protected $_profiler_sections = array();
 	/**
 	 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
 	 *
 	 * @var bool
-	 * @access 	protected
 	 */
 	protected $parse_exec_vars	= TRUE;
 
-	/**
-	 * Constructor
-	 *
-	 */
-	function __construct()
+	public function __construct()
 	{
 		$this->_zlib_oc = @ini_get('zlib.output_compression');
 
@@ -117,8 +105,7 @@
 
 
 		$this->mime_types = $mimes;
-
-		log_message('debug', "Output Class Initialized");
+		log_message('debug', 'Output Class Initialized');
 	}
 
 	// --------------------------------------------------------------------
@@ -128,10 +115,9 @@
 	 *
 	 * Returns the current output string
 	 *
-	 * @access	public
 	 * @return	string
 	 */
-	function get_output()
+	public function get_output()
 	{
 		return $this->final_output;
 	}
@@ -147,10 +133,9 @@
 	 * @param	string
 	 * @return	void
 	 */
-	function set_output($output)
+	public function set_output($output)
 	{
 		$this->final_output = $output;
-
 		return $this;
 	}
 
@@ -161,11 +146,10 @@
 	 *
 	 * Appends data onto the output string
 	 *
-	 * @access	public
 	 * @param	string
 	 * @return	void
 	 */
-	function append_output($output)
+	public function append_output($output)
 	{
 		if ($this->final_output == '')
 		{
@@ -189,25 +173,22 @@
 	 * Note:  If a file is cached, headers will not be sent.  We need to figure out
 	 * how to permit header data to be saved with the cache data...
 	 *
-	 * @access	public
 	 * @param	string
 	 * @param 	bool
 	 * @return	void
 	 */
-	function set_header($header, $replace = TRUE)
+	public function set_header($header, $replace = TRUE)
 	{
 		// If zlib.output_compression is enabled it will compress the output,
 		// but it will not modify the content-length header to compensate for
 		// the reduction, causing the browser to hang waiting for more data.
 		// We'll just skip content-length in those cases.
-
 		if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
 		{
 			return;
 		}
 
 		$this->headers[] = array($header, $replace);
-
 		return $this;
 	}
 
@@ -216,11 +197,10 @@
 	/**
 	 * Set Content Type Header
 	 *
-	 * @access	public
 	 * @param	string	extension of the file we're outputting
 	 * @return	void
 	 */
-	function set_content_type($mime_type)
+	public function set_content_type($mime_type)
 	{
 		if (strpos($mime_type, '/') === FALSE)
 		{
@@ -241,7 +221,6 @@
 		$header = 'Content-Type: '.$mime_type;
 
 		$this->headers[] = array($header, TRUE);
-
 		return $this;
 	}
 
@@ -251,15 +230,13 @@
 	 * Set HTTP Status Header
 	 * moved to Common procedural functions in 1.7.2
 	 *
-	 * @access	public
 	 * @param	int		the status code
 	 * @param	string
 	 * @return	void
 	 */
-	function set_status_header($code = 200, $text = '')
+	public function set_status_header($code = 200, $text = '')
 	{
 		set_status_header($code, $text);
-
 		return $this;
 	}
 
@@ -268,14 +245,12 @@
 	/**
 	 * Enable/disable Profiler
 	 *
-	 * @access	public
 	 * @param	bool
 	 * @return	void
 	 */
-	function enable_profiler($val = TRUE)
+	public function enable_profiler($val = TRUE)
 	{
 		$this->enable_profiler = (is_bool($val)) ? $val : TRUE;
-
 		return $this;
 	}
 
@@ -286,11 +261,10 @@
 	 *
 	 * Allows override of default / config settings for Profiler section display
 	 *
-	 * @access	public
 	 * @param	array
 	 * @return	void
 	 */
-	function set_profiler_sections($sections)
+	public function set_profiler_sections($sections)
 	{
 		foreach ($sections as $section => $enable)
 		{
@@ -305,14 +279,12 @@
 	/**
 	 * Set Cache
 	 *
-	 * @access	public
 	 * @param	integer
 	 * @return	void
 	 */
-	function cache($time)
+	publi function cache($time)
 	{
 		$this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
-
 		return $this;
 	}
 
@@ -329,11 +301,10 @@
 	 * with any server headers and profile data.  It also stops the
 	 * benchmark timer so the page rendering speed and memory usage can be shown.
 	 *
-	 * @access	public
 	 * @param 	string
 	 * @return	mixed
 	 */
-	function _display($output = '')
+	public function _display($output = '')
 	{
 		// Note:  We use globals because we can't use $CI =& get_instance()
 		// since this function is sometimes called by the caching mechanism,
@@ -375,22 +346,17 @@
 		{
 			$memory	 = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
 
-			$output = str_replace('{elapsed_time}', $elapsed, $output);
-			$output = str_replace('{memory_usage}', $memory, $output);
+			$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
 		}
 
 		// --------------------------------------------------------------------
 
 		// Is compression requested?
-		if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
+		if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE
+			&& extension_loaded('zlib')
+			&& isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
 		{
-			if (extension_loaded('zlib'))
-			{
-				if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
-				{
-					ob_start('ob_gzhandler');
-				}
-			}
+			ob_start('ob_gzhandler');
 		}
 
 		// --------------------------------------------------------------------
@@ -412,8 +378,8 @@
 		if ( ! isset($CI))
 		{
 			echo $output;
-			log_message('debug', "Final output sent to browser");
-			log_message('debug', "Total execution time: ".$elapsed);
+			log_message('debug', 'Final output sent to browser');
+			log_message('debug', 'Total execution time: '.$elapsed);
 			return TRUE;
 		}
 
@@ -424,7 +390,6 @@
 		if ($this->enable_profiler == TRUE)
 		{
 			$CI->load->library('profiler');
-
 			if ( ! empty($this->_profiler_sections))
 			{
 				$CI->profiler->set_sections($this->_profiler_sections);
@@ -432,16 +397,11 @@
 
 			// If the output data contains closing </body> and </html> tags
 			// we will remove them and add them back after we insert the profile data
-			if (preg_match("|</body>.*?</html>|is", $output))
+			$output = preg_replace('|</body>.*?</html>|is', '', $output, $count).$CI->profiler->run();
+			if ($count > 0)
 			{
-				$output  = preg_replace("|</body>.*?</html>|is", '', $output);
-				$output .= $CI->profiler->run();
 				$output .= '</body></html>';
 			}
-			else
-			{
-				$output .= $CI->profiler->run();
-			}
 		}
 
 		// --------------------------------------------------------------------
@@ -457,8 +417,8 @@
 			echo $output;  // Send it to the browser!
 		}
 
-		log_message('debug', "Final output sent to browser");
-		log_message('debug', "Total execution time: ".$elapsed);
+		log_message('debug', 'Final output sent to browser');
+		log_message('debug', 'Total execution time: '.$elapsed);
 	}
 
 	// --------------------------------------------------------------------
@@ -466,20 +426,18 @@
 	/**
 	 * Write a Cache File
 	 *
-	 * @access	public
 	 * @param 	string
 	 * @return	void
 	 */
-	function _write_cache($output)
+	public function _write_cache($output)
 	{
 		$CI =& get_instance();
 		$path = $CI->config->item('cache_path');
-
 		$cache_path = ($path == '') ? APPPATH.'cache/' : $path;
 
 		if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
 		{
-			log_message('error', "Unable to write cache file: ".$cache_path);
+			log_message('error', 'Unable to write cache file: '.$cache_path);
 			return;
 		}
 
@@ -491,7 +449,7 @@
 
 		if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
 		{
-			log_message('error', "Unable to write cache file: ".$cache_path);
+			log_message('error', 'Unable to write cache file: '.$cache_path);
 			return;
 		}
 
@@ -504,13 +462,13 @@
 		}
 		else
 		{
-			log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
+			log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
 			return;
 		}
 		fclose($fp);
 		@chmod($cache_path, FILE_WRITE_MODE);
 
-		log_message('debug', "Cache file written: ".$cache_path);
+		log_message('debug', 'Cache file written: '.$cache_path);
 	}
 
 	// --------------------------------------------------------------------
@@ -518,69 +476,53 @@
 	/**
 	 * Update/serve a cached file
 	 *
-	 * @access	public
 	 * @param 	object	config class
 	 * @param 	object	uri class
 	 * @return	void
 	 */
-	function _display_cache(&$CFG, &$URI)
+	public function _display_cache(&$CFG, &$URI)
 	{
 		$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
 
-		// Build the file path.  The file name is an MD5 hash of the full URI
-		$uri =	$CFG->item('base_url').
-				$CFG->item('index_page').
-				$URI->uri_string;
-
+		// Build the file path. The file name is an MD5 hash of the full URI
+		$uri =	$CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
 		$filepath = $cache_path.md5($uri);
 
-		if ( ! @file_exists($filepath))
-		{
-			return FALSE;
-		}
-
-		if ( ! $fp = @fopen($filepath, FOPEN_READ))
+		if ( ! @file_exists($filepath)
+			OR ! $fp = @fopen($filepath, FOPEN_READ))
 		{
 			return FALSE;
 		}
 
 		flock($fp, LOCK_SH);
 
-		$cache = '';
-		if (filesize($filepath) > 0)
-		{
-			$cache = fread($fp, filesize($filepath));
-		}
+		$cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
 
 		flock($fp, LOCK_UN);
 		fclose($fp);
 
 		// Strip out the embedded timestamp
-		if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
+		if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
 		{
 			return FALSE;
 		}
 
 		// Has the file expired? If so we'll delete it.
-		if (time() >= trim(str_replace('TS--->', '', $match['1'])))
+		if (time() >= trim(str_replace('TS--->', '', $match[1]))
+			AND is_really_writable($cache_path))
 		{
-			if (is_really_writable($cache_path))
-			{
-				@unlink($filepath);
-				log_message('debug', "Cache file has expired. File deleted");
-				return FALSE;
-			}
+			@unlink($filepath);
+			log_message('debug', 'Cache file has expired. File deleted.');
+			return FALSE;
 		}
 
 		// Display the cache
-		$this->_display(str_replace($match['0'], '', $cache));
-		log_message('debug', "Cache file is current. Sending it to browser.");
+		$this->_display(str_replace($match[0], '', $cache));
+		log_message('debug', 'Cache file is current. Sending it to browser.');
 		return TRUE;
 	}
 
-
 }
-// END Output Class
 
 /* End of file Output.php */
-/* Location: ./system/core/Output.php */
\ No newline at end of file
+/* Location: ./system/core/Output.php */