home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTTP / Header / Cache.php
Encoding:
PHP Script  |  2008-07-02  |  6.6 KB  |  239 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * HTTP::Header::Cache
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * @category    HTTP
  10.  * @package     HTTP_Header
  11.  * @author      Wolfram Kriesing <wk@visionp.de>
  12.  * @author      Michael Wallner <mike@php.net>
  13.  * @copyright   2003-2005 The Authors
  14.  * @license     BSD, revised
  15.  * @version     CVS: $Id: Cache.php,v 1.24 2005/11/08 19:06:14 mike Exp $
  16.  * @link        http://pear.php.net/package/HTTP_Header
  17.  */
  18.  
  19. /**
  20.  * Requires HTTP_Header
  21.  */
  22. require_once 'HTTP/Header.php';
  23.  
  24. /**
  25.  * HTTP_Header_Cache
  26.  * 
  27.  * This package provides methods to easier handle caching of HTTP pages.  That 
  28.  * means that the pages can be cached at the client (user agent or browser) and 
  29.  * your application only needs to send "hey client you already have the pages".
  30.  * 
  31.  * Which is done by sending the HTTP-Status "304 Not Modified", so that your
  32.  * application load and the network traffic can be reduced, since you only need
  33.  * to send the complete page once.  This is really an advantage e.g. for 
  34.  * generated style sheets, or simply pages that do only change rarely.
  35.  * 
  36.  * Usage:
  37.  * <code>
  38.  *  require_once 'HTTP/Header/Cache.php';
  39.  *  $httpCache = new HTTP_Header_Cache(4, 'weeks');
  40.  *  $httpCache->sendHeaders();
  41.  *  // your code goes here
  42.  * </code>
  43.  * 
  44.  * @package     HTTP_Header
  45.  * @category    HTTP
  46.  * @access      public
  47.  * @version     $Revision: 1.24 $
  48.  */
  49. class HTTP_Header_Cache extends HTTP_Header
  50. {
  51.     /**
  52.      * Constructor
  53.      * 
  54.      * Set the amount of time to cache.
  55.      * 
  56.      * @access  public
  57.      * @return  object  HTTP_Header_Cache
  58.      * @param   int     $expires 
  59.      * @param   string  $unit
  60.      */
  61.     function HTTP_Header_Cache($expires = 0, $unit = 'seconds')
  62.     {
  63.         parent::HTTP_Header();
  64.         $this->setHeader('Pragma', 'cache');
  65.         $this->setHeader('Last-Modified', $this->getCacheStart());
  66.         $this->setHeader('Cache-Control', 'private, must-revalidate, max-age=0');
  67.         
  68.         if ($expires) {
  69.             if (!$this->isOlderThan($expires, $unit)) {
  70.                 $this->exitCached();
  71.             }
  72.             $this->setHeader('Last-Modified', time());
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * Get Cache Start
  78.      * 
  79.      * Returns the unix timestamp of the If-Modified-Since HTTP header or the
  80.      * current time if the header was not sent by the client.
  81.      * 
  82.      * @access  public
  83.      * @return  int     unix timestamp
  84.      */
  85.     function getCacheStart()
  86.     {
  87.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && !$this->isPost()) {
  88.             return strtotime(current($array = explode(';', 
  89.                 $_SERVER['HTTP_IF_MODIFIED_SINCE'])));
  90.         }
  91.         return time();
  92.     }
  93.  
  94.     /**
  95.      * Is Older Than
  96.      * 
  97.      * You can call it like this:
  98.      * <code>
  99.      *  $httpCache->isOlderThan(1, 'day');
  100.      *  $httpCache->isOlderThan(47, 'days');
  101.      * 
  102.      *  $httpCache->isOlderThan(1, 'week');
  103.      *  $httpCache->isOlderThan(3, 'weeks');
  104.      * 
  105.      *  $httpCache->isOlderThan(1, 'hour');
  106.      *  $httpCache->isOlderThan(5, 'hours');
  107.      * 
  108.      *  $httpCache->isOlderThan(1, 'minute');
  109.      *  $httpCache->isOlderThan(15, 'minutes');
  110.      * 
  111.      *  $httpCache->isOlderThan(1, 'second');
  112.      *  $httpCache->isOlderThan(15);
  113.      * </code>
  114.      * 
  115.      * If you specify something greater than "weeks" as time untit, it just 
  116.      * works approximatly, because a month is taken to consist of 4.3 weeks.
  117.      * 
  118.      * @access  public
  119.      * @return  bool    Returns true if requested page is older than specified.
  120.      * @param   int     $time The amount of time.
  121.      * @param   string  $unit The unit of the time amount - (year[s], month[s], 
  122.      *                  week[s], day[s], hour[s], minute[s], second[s]).
  123.      */
  124.     function isOlderThan($time = 0, $unit = 'seconds')
  125.     {
  126.         if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || $this->isPost()) {
  127.             return true;
  128.         }
  129.         if (!$time) {
  130.             return false;
  131.         }
  132.         
  133.         switch (strtolower($unit))
  134.         {
  135.             case 'year':
  136.             case 'years':
  137.                 $time *= 12;
  138.             case 'month':
  139.             case 'months':
  140.                 $time *= 4.3;
  141.             case 'week':
  142.             case 'weeks':
  143.                 $time *= 7;
  144.             case 'day':
  145.             case 'days':
  146.                 $time *= 24;
  147.             case 'hour':
  148.             case 'hours':
  149.                 $time *= 60;
  150.             case 'minute':
  151.             case 'minutes':
  152.                 $time *= 60;
  153.         }
  154.         
  155.         return (time() - $this->getCacheStart()) > $time;
  156.     }
  157.  
  158.     /**
  159.      * Is Cached
  160.      * 
  161.      * Check whether we can consider to be cached on the client side.
  162.      * 
  163.      * @access  public
  164.      * @return  bool    Whether the page/resource is considered to be cached.
  165.      * @param   int     $lastModified Unix timestamp of last modification.
  166.      */
  167.     function isCached($lastModified = 0)
  168.     {
  169.         if ($this->isPost()) {
  170.             return false;
  171.         }
  172.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && !$lastModified) {
  173.             return true;
  174.         }
  175.         if (!$seconds = time() - $lastModified) {
  176.             return false;
  177.         }
  178.         return !$this->isOlderThan($seconds);
  179.     }
  180.     
  181.     /**
  182.      * Is Post
  183.      * 
  184.      * Check if request method is "POST".
  185.      * 
  186.      * @access  public
  187.      * @return  bool
  188.      */
  189.     function isPost()
  190.     {
  191.         return  isset($_SERVER['REQUEST_METHOD']) and
  192.             'POST' == $_SERVER['REQUEST_METHOD'];
  193.     }
  194.     
  195.     /**
  196.      * Exit If Cached
  197.      * 
  198.      * Exit with "HTTP 304 Not Modified" if we consider to be cached.
  199.      * 
  200.      * @access  public
  201.      * @return  void
  202.      * @param   int     $lastModified Unix timestamp of last modification.
  203.      */
  204.     function exitIfCached($lastModified = 0)
  205.     {
  206.         if ($this->isCached($lastModified)) {
  207.             $this->exitCached();
  208.         }
  209.     }
  210.     
  211.     /**
  212.      * Exit Cached
  213.      * 
  214.      * Exit with "HTTP 304 Not Modified".
  215.      * 
  216.      * @access  public
  217.      * @return  void
  218.      */
  219.     function exitCached()
  220.     {
  221.         $this->sendHeaders();
  222.         $this->sendStatusCode(304);
  223.         exit;
  224.     }
  225.     
  226.     /**
  227.      * Set Last Modified
  228.      * 
  229.      * @access  public
  230.      * @return  void
  231.      * @param   int     $lastModified The unix timestamp of last modification.
  232.      */
  233.     function setLastModified($lastModified = null)
  234.     {
  235.         $this->setHeader('Last-Modified', $lastModified);
  236.     }
  237. }
  238. ?>
  239.