Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/environment/response.php

Documentation is available at response.php

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

Documentation generated on Sun, 21 Dec 2008 20:48:57 +0000 by phpDocumentor 1.3.1