[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/environment/ -> response.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: response.php 21044 2011-03-31 16:03:23Z dextercowley $
   4   * @package        Joomla.Framework
   5   * @subpackage    Environment
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  /**
  16   * Create the response global object
  17   */
  18  defined('JPATH_BASE') or die();
  19  $GLOBALS['_JRESPONSE'] = new stdClass();
  20  $GLOBALS['_JRESPONSE']->cachable = false;
  21  $GLOBALS['_JRESPONSE']->headers  = array();
  22  $GLOBALS['_JRESPONSE']->body     = array();
  23  
  24   /**
  25   * JResponse Class
  26   *
  27   * This class serves to provide the Joomla Framework with a common interface to access
  28   * response variables.  This includes header and body.
  29   *
  30   * @static
  31   * @package        Joomla.Framework
  32   * @subpackage    Environment
  33   * @since        1.5
  34   */
  35  class JResponse
  36  {
  37      /**
  38       * Set/get cachable state for the response
  39       *
  40       * If $allow is set, sets the cachable state of the response.  Always returns current state
  41       *
  42       * @static
  43       * @param    boolean    $allow
  44       * @return    boolean     True of browser caching should be allowed
  45       * @since    1.5
  46       */
  47  	function allowCache($allow = null)
  48      {
  49          if (!is_null($allow)) {
  50              $GLOBALS['_JRESPONSE']->cachable = (bool) $allow;
  51          }
  52          return $GLOBALS['_JRESPONSE']->cachable;
  53      }
  54  
  55      /**
  56       * Set a header
  57       *
  58       * If $replace is true, replaces any headers already defined with that
  59       * $name.
  60       *
  61       * @access public
  62       * @param string     $name
  63       * @param string     $value
  64       * @param boolean     $replace
  65       */
  66  	function setHeader($name, $value, $replace = false)
  67      {
  68          $name    = (string) $name;
  69          $value    = (string) $value;
  70  
  71          if ($replace)
  72          {
  73              foreach ($GLOBALS['_JRESPONSE']->headers as $key => $header) {
  74                  if ($name == $header['name']) {
  75                      unset($GLOBALS['_JRESPONSE']->headers[$key]);
  76                  }
  77              }
  78          }
  79  
  80          $GLOBALS['_JRESPONSE']->headers[] = array(
  81              'name'    => $name,
  82              'value'    => $value
  83          );
  84      }
  85  
  86      /**
  87       * Return array of headers;
  88       *
  89       * @access public
  90       * @return array
  91       */
  92  	function getHeaders() {
  93          return  $GLOBALS['_JRESPONSE']->headers;
  94      }
  95  
  96      /**
  97       * Clear headers
  98       *
  99       * @access public
 100       */
 101  	function clearHeaders() {
 102          $GLOBALS['_JRESPONSE']->headers = array();
 103      }
 104  
 105      /**
 106       * Send all headers
 107       *
 108       * @access public
 109       * @return void
 110       */
 111  	function sendHeaders()
 112      {
 113          if (!headers_sent())
 114          {
 115              foreach ($GLOBALS['_JRESPONSE']->headers as $header)
 116              {
 117                  if ('status' == strtolower($header['name']))
 118                  {
 119                      // 'status' headers indicate an HTTP status, and need to be handled slightly differently
 120                      header(ucfirst(strtolower($header['name'])) . ': ' . $header['value'], null, (int) $header['value']);
 121                  } else {
 122                      header($header['name'] . ': ' . $header['value']);
 123                  }
 124              }
 125          }
 126      }
 127  
 128      /**
 129       * Set body content
 130       *
 131       * If body content already defined, this will replace it.
 132       *
 133       * @access public
 134       * @param string $content
 135       */
 136  	function setBody($content) {
 137          $GLOBALS['_JRESPONSE']->body = array((string) $content);
 138      }
 139  
 140       /**
 141       * Prepend content to the body content
 142       *
 143       * @access public
 144       * @param string $content
 145       */
 146  	function prependBody($content) {
 147          array_unshift($GLOBALS['_JRESPONSE']->body, (string) $content);
 148      }
 149  
 150      /**
 151       * Append content to the body content
 152       *
 153       * @access public
 154       * @param string $content
 155       */
 156  	function appendBody($content) {
 157          array_push($GLOBALS['_JRESPONSE']->body, (string) $content);
 158      }
 159  
 160      /**
 161       * Return the body content
 162       *
 163       * @access public
 164       * @param boolean $toArray Whether or not to return the body content as an
 165       * array of strings or as a single string; defaults to false
 166       * @return string|array
 167       */
 168  	function getBody($toArray = false)
 169      {
 170          if ($toArray) {
 171              return $GLOBALS['_JRESPONSE']->body;
 172          }
 173  
 174          ob_start();
 175          foreach ($GLOBALS['_JRESPONSE']->body as $content) {
 176              echo $content;
 177          }
 178          return ob_get_clean();
 179      }
 180  
 181      /**
 182       * Sends all headers prior to returning the string
 183       *
 184       * @access public
 185       * @param boolean     $compress    If true, compress the data
 186       * @return string
 187       */
 188  	function toString($compress = false)
 189      {
 190          $data = JResponse::getBody();
 191  
 192          // Don't compress something if the server is going todo it anyway. Waste of time.
 193          if($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler') {
 194              $data = JResponse::_compress($data);
 195          }
 196  
 197          if (JResponse::allowCache() === false)
 198          {
 199              JResponse::setHeader( 'Expires', 'Mon, 1 Jan 2001 00:00:00 GMT', true );                 // Expires in the past
 200              JResponse::setHeader( 'Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT', true );         // Always modified
 201              JResponse::setHeader( 'Cache-Control', 'no-store, no-cache, must-revalidate', true );     // Extra CYA
 202              JResponse::setHeader( 'Cache-Control', 'post-check=0, pre-check=0', false );            // HTTP/1.1
 203              JResponse::setHeader( 'Pragma', 'no-cache' );                                             // HTTP 1.0
 204          }
 205  
 206          JResponse::sendHeaders();
 207          return $data;
 208      }
 209  
 210      /**
 211      * Compress the data
 212      *
 213      * Checks the accept encoding of the browser and compresses the data before
 214      * sending it to the client.
 215      *
 216      * @access    public
 217      * @param    string        data
 218      * @return    string        compressed data
 219      */
 220  	function _compress( $data )
 221      {
 222          $encoding = JResponse::_clientEncoding();
 223  
 224          if (!$encoding)
 225              return $data;
 226  
 227          if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
 228              return $data;
 229          }
 230  
 231          if (headers_sent())
 232              return $data;
 233  
 234          if (connection_status() !== 0)
 235              return $data;
 236  
 237  
 238          $level = 4; //ideal level
 239  
 240          /*
 241          $size        = strlen($data);
 242          $crc        = crc32($data);
 243  
 244          $gzdata        = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
 245          $gzdata        .= gzcompress($data, $level);
 246  
 247          $gzdata     = substr($gzdata, 0, strlen($gzdata) - 4);
 248          $gzdata     .= pack("V",$crc) . pack("V", $size);
 249          */
 250  
 251          $gzdata = gzencode($data, $level);
 252  
 253          JResponse::setHeader('Content-Encoding', $encoding);
 254          JResponse::setHeader('X-Content-Encoded-By', 'Joomla! 1.5');
 255  
 256          return $gzdata;
 257      }
 258  
 259       /**
 260      * check, whether client supports compressed data
 261      *
 262      * @access    private
 263      * @return    boolean
 264      */
 265  	function _clientEncoding()
 266      {
 267          if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
 268              return false;
 269          }
 270  
 271          $encoding = false;
 272  
 273          if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
 274              $encoding = 'gzip';
 275          }
 276  
 277          if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip')) {
 278              $encoding = 'x-gzip';
 279          }
 280  
 281          return $encoding;
 282      }
 283  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1