CI_Output Class Reference

List of all members.


Public Member Functions

 CI_Output ()
 get_output ()
 Get Output.
 set_output ($output)
 Set Output.
 append_output ($output)
 Append Output.
 set_header ($header, $replace=TRUE)
 Set Header.
 set_status_header ($code= '200', $text= '')
 Set HTTP Status Header.
 enable_profiler ($val=TRUE)
 Enable/disable Profiler.
 cache ($time)
 Set Cache.
 _display ($output= '')
 Display Output.
 _write_cache ($output)
 Write a Cache File.
 _display_cache (&$CFG, &$URI)
 Update/serve a cached file.

Public Attributes

 $final_output
 $cache_expiration = 0
 $headers = array()
 $enable_profiler = FALSE

Detailed Description

Definition at line 29 of file Output.php.


Member Function Documentation

CI_Output::_display ( output = ''  ) 

Display Output.

All "view" data is automatically put into this variable by the controller class:

$this->final_output

This function sends the finalized output data to the browser along with any server headers and profile data. It also stops the benchmark timer so the page rendering speed and memory usage can be shown.

public

Returns:
mixed

Definition at line 244 of file Output.php.

References $BM, $CFG, $CI, _write_cache(), enable_profiler(), get_instance(), and log_message().

Referenced by _display_cache().

00245         {       
00246                 // Note:  We use globals because we can't use $CI =& get_instance()
00247                 // since this function is sometimes called by the caching mechanism,
00248                 // which happens before the CI super object is available.
00249                 global $BM, $CFG;
00250                 
00251                 // --------------------------------------------------------------------
00252                 
00253                 // Set the output data
00254                 if ($output == '')
00255                 {
00256                         $output =& $this->final_output;
00257                 }
00258                 
00259                 // --------------------------------------------------------------------
00260                 
00261                 // Do we need to write a cache file?
00262                 if ($this->cache_expiration > 0)
00263                 {
00264                         $this->_write_cache($output);
00265                 }
00266                 
00267                 // --------------------------------------------------------------------
00268 
00269                 // Parse out the elapsed time and memory usage,
00270                 // then swap the pseudo-variables with the data
00271 
00272                 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');         
00273                 $output = str_replace('{elapsed_time}', $elapsed, $output);
00274                 
00275                 $memory  = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
00276                 $output = str_replace('{memory_usage}', $memory, $output);              
00277 
00278                 // --------------------------------------------------------------------
00279                 
00280                 // Is compression requested?
00281                 if ($CFG->item('compress_output') === TRUE)
00282                 {
00283                         if (extension_loaded('zlib'))
00284                         {
00285                                 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
00286                                 {
00287                                         ob_start('ob_gzhandler');
00288                                 }
00289                         }
00290                 }
00291 
00292                 // --------------------------------------------------------------------
00293                 
00294                 // Are there any server headers to send?
00295                 if (count($this->headers) > 0)
00296                 {
00297                         foreach ($this->headers as $header)
00298                         {
00299                                 @header($header[0], $header[1]);
00300                         }
00301                 }               
00302 
00303                 // --------------------------------------------------------------------
00304                 
00305                 // Does the get_instance() function exist?
00306                 // If not we know we are dealing with a cache file so we'll
00307                 // simply echo out the data and exit.
00308                 if ( ! function_exists('get_instance'))
00309                 {
00310                         echo $output;
00311                         log_message('debug', "Final output sent to browser");
00312                         log_message('debug', "Total execution time: ".$elapsed);
00313                         return TRUE;
00314                 }
00315         
00316                 // --------------------------------------------------------------------
00317 
00318                 // Grab the super object.  We'll need it in a moment...
00319                 $CI =& get_instance();
00320                 
00321                 // Do we need to generate profile data?
00322                 // If so, load the Profile class and run it.
00323                 if ($this->enable_profiler == TRUE)
00324                 {
00325                         $CI->load->library('profiler');                         
00326                                                                                 
00327                         // If the output data contains closing </body> and </html> tags
00328                         // we will remove them and add them back after we insert the profile data
00329                         if (preg_match("|</body>.*?</html>|is", $output))
00330                         {
00331                                 $output  = preg_replace("|</body>.*?</html>|is", '', $output);
00332                                 $output .= $CI->profiler->run();
00333                                 $output .= '</body></html>';
00334                         }
00335                         else
00336                         {
00337                                 $output .= $CI->profiler->run();
00338                         }
00339                 }
00340                 
00341                 // --------------------------------------------------------------------
00342 
00343                 // Does the controller contain a function named _output()?
00344                 // If so send the output there.  Otherwise, echo it.
00345                 if (method_exists($CI, '_output'))
00346                 {
00347                         $CI->_output($output);
00348                 }
00349                 else
00350                 {
00351                         echo $output;  // Send it to the browser!
00352                 }
00353                 
00354                 log_message('debug', "Final output sent to browser");
00355                 log_message('debug', "Total execution time: ".$elapsed);                
00356         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Output::_display_cache ( &$  CFG,
&$  URI 
)

Update/serve a cached file.

public

Returns:
void

Definition at line 416 of file Output.php.

References $CFG, $URI, _display(), is_really_writable(), and log_message().

00417         {
00418                 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
00419                         
00420                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00421                 {
00422                         return FALSE;
00423                 }
00424                 
00425                 // Build the file path.  The file name is an MD5 hash of the full URI
00426                 $uri =  $CFG->item('base_url').
00427                                 $CFG->item('index_page').
00428                                 $URI->uri_string;
00429                                 
00430                 $filepath = $cache_path.md5($uri);
00431                 
00432                 if ( ! @file_exists($filepath))
00433                 {
00434                         return FALSE;
00435                 }
00436         
00437                 if ( ! $fp = @fopen($filepath, FOPEN_READ))
00438                 {
00439                         return FALSE;
00440                 }
00441                         
00442                 flock($fp, LOCK_SH);
00443                 
00444                 $cache = '';
00445                 if (filesize($filepath) > 0)
00446                 {
00447                         $cache = fread($fp, filesize($filepath));
00448                 }
00449         
00450                 flock($fp, LOCK_UN);
00451                 fclose($fp);
00452                                         
00453                 // Strip out the embedded timestamp             
00454                 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
00455                 {
00456                         return FALSE;
00457                 }
00458                 
00459                 // Has the file expired? If so we'll delete it.
00460                 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
00461                 {               
00462                         @unlink($filepath);
00463                         log_message('debug', "Cache file has expired. File deleted");
00464                         return FALSE;
00465                 }
00466 
00467                 // Display the cache
00468                 $this->_display(str_replace($match['0'], '', $cache));
00469                 log_message('debug', "Cache file is current. Sending it to browser.");          
00470                 return TRUE;
00471         }

Here is the call graph for this function:

CI_Output::_write_cache ( output  ) 

Write a Cache File.

public

Returns:
void

Definition at line 366 of file Output.php.

References $CI, get_instance(), if, is_really_writable(), and log_message().

Referenced by _display().

00367         {
00368                 $CI =& get_instance();  
00369                 $path = $CI->config->item('cache_path');
00370         
00371                 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
00372                 
00373                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00374                 {
00375                         return;
00376                 }
00377                 
00378                 $uri =  $CI->config->item('base_url').
00379                                 $CI->config->item('index_page').
00380                                 $CI->uri->uri_string();
00381                 
00382                 $cache_path .= md5($uri);
00383 
00384                 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
00385                 {
00386                         log_message('error', "Unable to write cache file: ".$cache_path);
00387                         return;
00388                 }
00389                 
00390                 $expire = time() + ($this->cache_expiration * 60);
00391                 
00392                 if (flock($fp, LOCK_EX))
00393                 {
00394                         fwrite($fp, $expire.'TS--->'.$output);
00395                         flock($fp, LOCK_UN);
00396                 }
00397                 else
00398                 {
00399                         log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
00400                         return;
00401                 }
00402                 fclose($fp);
00403                 @chmod($cache_path, DIR_WRITE_MODE);
00404 
00405                 log_message('debug', "Cache file written: ".$cache_path);
00406         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Output::append_output ( output  ) 

Append Output.

Appends data onto the output string

public

Parameters:
string 
Returns:
void

Definition at line 84 of file Output.php.

00085         {
00086                 if ($this->final_output == '')
00087                 {
00088                         $this->final_output = $output;
00089                 }
00090                 else
00091                 {
00092                         $this->final_output .= $output;
00093                 }
00094         }

CI_Output::cache ( time  ) 

Set Cache.

public

Parameters:
integer 
Returns:
void

Definition at line 223 of file Output.php.

00224         {
00225                 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
00226         }

CI_Output::CI_Output (  ) 

Definition at line 37 of file Output.php.

References log_message().

00038         {
00039                 log_message('debug', "Output Class Initialized");
00040         }

Here is the call graph for this function:

CI_Output::enable_profiler ( val = TRUE  ) 

Enable/disable Profiler.

public

Parameters:
bool 
Returns:
void

Definition at line 209 of file Output.php.

Referenced by _display().

00210         {
00211                 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
00212         }

Here is the caller graph for this function:

CI_Output::get_output (  ) 

Get Output.

Returns the current output string

public

Returns:
string

Definition at line 52 of file Output.php.

00053         {
00054                 return $this->final_output;
00055         }

CI_Output::set_header ( header,
replace = TRUE 
)

Set Header.

Lets you set a server header which will be outputted with the final display.

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...

public

Parameters:
string 
Returns:
void

Definition at line 110 of file Output.php.

00111         {
00112                 $this->headers[] = array($header, $replace);
00113         }

CI_Output::set_output ( output  ) 

Set Output.

Sets the output string

public

Parameters:
string 
Returns:
void

Definition at line 68 of file Output.php.

00069         {
00070                 $this->final_output = $output;
00071         }

CI_Output::set_status_header ( code = '200',
text = '' 
)

Set HTTP Status Header.

public

Parameters:
int the status code
string 
Returns:
void

Definition at line 125 of file Output.php.

References show_error().

00126         {
00127                 $stati = array(
00128                                                         '200'   => 'OK',
00129                                                         '201'   => 'Created',
00130                                                         '202'   => 'Accepted',
00131                                                         '203'   => 'Non-Authoritative Information',
00132                                                         '204'   => 'No Content',
00133                                                         '205'   => 'Reset Content',
00134                                                         '206'   => 'Partial Content',
00135                                                         
00136                                                         '300'   => 'Multiple Choices',
00137                                                         '301'   => 'Moved Permanently',
00138                                                         '302'   => 'Found',
00139                                                         '304'   => 'Not Modified',
00140                                                         '305'   => 'Use Proxy',
00141                                                         '307'   => 'Temporary Redirect',
00142                                                         
00143                                                         '400'   => 'Bad Request',
00144                                                         '401'   => 'Unauthorized',
00145                                                         '403'   => 'Forbidden',
00146                                                         '404'   => 'Not Found',
00147                                                         '405'   => 'Method Not Allowed',
00148                                                         '406'   => 'Not Acceptable',
00149                                                         '407'   => 'Proxy Authentication Required',
00150                                                         '408'   => 'Request Timeout',
00151                                                         '409'   => 'Conflict',
00152                                                         '410'   => 'Gone',
00153                                                         '411'   => 'Length Required',
00154                                                         '412'   => 'Precondition Failed',
00155                                                         '413'   => 'Request Entity Too Large',
00156                                                         '414'   => 'Request-URI Too Long',
00157                                                         '415'   => 'Unsupported Media Type',
00158                                                         '416'   => 'Requested Range Not Satisfiable',
00159                                                         '417'   => 'Expectation Failed',
00160                 
00161                                                         '500'   => 'Internal Server Error',
00162                                                         '501'   => 'Not Implemented',
00163                                                         '502'   => 'Bad Gateway',
00164                                                         '503'   => 'Service Unavailable',
00165                                                         '504'   => 'Gateway Timeout',
00166                                                         '505'   => 'HTTP Version Not Supported'
00167                                                 );
00168 
00169                 if ($code == '' OR ! is_numeric($code))
00170                 {
00171                         show_error('Status codes must be numeric');
00172                 }
00173 
00174                 if (isset($stati[$code]) AND $text == '')
00175                 {                               
00176                         $text = $stati[$code];
00177                 }
00178                 
00179                 if ($text == '')
00180                 {
00181                         show_error('No status text available.  Please check your status code number or supply your own message text.');
00182                 }
00183                 
00184                 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
00185         
00186                 if (substr(php_sapi_name(), 0, 3) == 'cgi')
00187                 {
00188                         header("Status: {$code} {$text}", TRUE);
00189                 }
00190                 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
00191                 {
00192                         header($server_protocol." {$code} {$text}", TRUE, $code);
00193                 }
00194                 else
00195                 {
00196                         header("HTTP/1.1 {$code} {$text}", TRUE, $code);
00197                 }
00198         }

Here is the call graph for this function:


Member Data Documentation

CI_Output::$cache_expiration = 0

Definition at line 32 of file Output.php.

CI_Output::$enable_profiler = FALSE

Definition at line 34 of file Output.php.

CI_Output::$final_output

Definition at line 31 of file Output.php.

CI_Output::$headers = array()

Definition at line 33 of file Output.php.


The documentation for this class was generated from the following file: