Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/environment/response.php

Documentation is available at response.php

  1. <?php
  2. /**
  3.  * @version        $Id: request.php 5921 2006-12-04 16:12:21Z chrisdavenport $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Environment
  6.  * @copyright    Copyright (C) 2005 - 2007 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 true;
  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.  * @author        Johan Janssens <[email protected]>
  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.         // 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.             JResponse::setHeader'Expires''Mon, 1 Jan 2001 00:00:00 GMT'true )// Expires in the past
  198.             JResponse::setHeader'Last-Modified'gmdate("D, d M Y H:i:s"' GMT'true )// Always modified
  199.             JResponse::setHeader'Cache-Control''no-store, no-cache, must-revalidate'true )// Extra CYA
  200.             JResponse::setHeader'Cache-Control''post-check=0, pre-check=0'false )// HTTP/1.1
  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 Mon, 05 Mar 2007 21:20:17 +0000 by phpDocumentor 1.3.1