Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 197 lines (179 sloc) 5.839 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
/**
* CHttpCacheFilter class file.
*
* @author Da:Sourcerer <webmaster@dasourcerer.net>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CHttpCacheFilter implements http caching. It works a lot like {@link COutputCache}
* as a filter, except that content caching is being done on the client side.
*
* @author Da:Sourcerer <webmaster@dasourcerer.net>
* @version $Id$
* @package system.web.filters
* @since 1.1.11
*/
class CHttpCacheFilter extends CFilter
{
/**
* Timestamp for the last modification date. Must be either a string parsable by
* {@link http://php.net/strtotime strtotime()} or an integer representing a unix timestamp.
* @var string|integer
*/
public $lastModified;
/**
* Expression for the last modification date. If set, this takes precedence over {@link lastModified}.
* @var string|callback
*/
public $lastModifiedExpression;
/**
* Seed for the ETag. Can be anything that passes through {@link http://php.net/serialize serialize()}.
* @var mixed
*/
public $etagSeed;
/**
* Expression for the ETag seed. If set, this takes precedence over {@link etag}.
* @var string|callback
*/
public $etagSeedExpression;
/**
* Http cache control headers. Set this to an empty string in order to keep this
* header from being sent entirely.
* @var string
*/
public $cacheControl = 'max-age=3600, public';

/**
* Performs the pre-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on.
* @return boolean whether the filtering process should continue and the action should be executed.
*/
public function preFilter($filterChain)
{
// Only cache GET and HEAD requests
if(!in_array(Yii::app()->getRequest()->getRequestType(), array('GET', 'HEAD')))
return true;

$lastModified=$this->getLastModifiedValue();
$etag=$this->getEtagValue();

if($etag===false&&$lastModified===false)
return true;

if($etag)
header('ETag: '.$etag);

if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])&&isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
if($this->checkLastModified($lastModified)&&$this->checkEtag($etag))
{
$this->send304Header();
$this->sendCacheControlHeader();
return false;
}
}
else if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
if($this->checkLastModified($lastModified))
{
$this->send304Header();
$this->sendCacheControlHeader();
return false;
}
}
else if(isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
if($this->checkEtag($etag))
{
$this->send304Header();
$this->sendCacheControlHeader();
return false;
}

}

if($lastModified)
header('Last-Modified: '.date('r', $lastModified));

$this->sendCacheControlHeader();
return true;
}

/**
* Gets the last modified value from either {@link lastModifiedExpression} or {@link lastModified}
* and converts it into a unix timestamp if necessary
* @throws CException
* @return integer|boolean A unix timestamp or false if neither lastModified nor
* lastModifiedExpression have been set
*/
protected function getLastModifiedValue()
{
if($this->lastModifiedExpression)
{
$value=$this->evaluateExpression($this->lastModifiedExpression);
if(is_numeric($value)&&$value==(int)$value)
return $value;
else if(($lastModified=strtotime($value))===false)
throw new CException(Yii::t('yii','Invalid expression for CHttpCacheFilter.lastModifiedExpression: The evaluation result "{value}" could not be understood by strtotime()',
array('{value}'=>$value)));
return $lastModified;
}

if($this->lastModified)
{
if(is_numeric($this->lastModified)&&$this->lastModified==(int)$this->lastModified)
return $this->lastModified;
else if(($lastModified=strtotime($this->lastModified))===false)
throw new CException(Yii::t('yii','CHttpCacheFilter.lastModified contained a value that could not be understood by strtotime()'));
return $lastModified;
}
return false;
}

/**
* Gets the ETag out of either {@link etagSeedExpression} or {@link etagSeed}
* @return string|boolean Either a quoted string serving as ETag or false if neither etagSeed nor etagSeedExpression have been set
*/
protected function getEtagValue()
{
if($this->etagSeedExpression)
return $this->generateEtag($this->evaluateExpression($this->etagSeedExpression));
else if($this->etagSeed)
return $this->generateEtag($this->etagSeed);
return false;
}

/**
* Check if the etag supplied by the client matches our generated one
* @param string $etag the supplied etag
* @return boolean true if the supplied etag matches $etag
*/
protected function checkEtag($etag)
{
return isset($_SERVER['HTTP_IF_NONE_MATCH'])&&$_SERVER['HTTP_IF_NONE_MATCH']==$etag;
}

/**
* Checks if the last modified date supplied by the client is still up to date
* @param integer $lastModified the last modified date
* @return boolean true if the last modified date sent by the client is newer or equal to $lastModified
*/
protected function checkLastModified($lastModified)
{
return isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])&&@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])>=$lastModified;
}

/**
* Sends the 304 HTTP status code to the client
*/
protected function send304Header()
{
header('HTTP/1.1 304 Not Modified');
}

/**
* Sends the cache control header to the client
* @see cacheControl
* @since 1.1.12
*/
protected function sendCacheControlHeader()
{
header('Cache-Control: '.$this->cacheControl, true);
}

/**
* Generates a quoted string out of the seed
* @param mixed $seed Seed for the ETag
*/
protected function generateEtag($seed)
{
return '"'.base64_encode(sha1(serialize($seed), true)).'"';
}
}
Something went wrong with that request. Please try again.