7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
17
* @subpackage Zend_Cache_Frontend
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
24
* @see Zend_Cache_Core
26
require_once 'Zend/Cache/Core.php';
31
* @subpackage Zend_Cache_Frontend
32
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
33
* @license http://framework.zend.com/license/new-bsd New BSD License
35
class Zend_Cache_Frontend_Page extends Zend_Cache_Core
38
* This frontend specific options
40
* ====> (boolean) http_conditional :
41
* - if true, http conditional mode is on
42
* WARNING : http_conditional OPTION IS NOT IMPLEMENTED FOR THE MOMENT (TODO)
44
* ====> (boolean) debug_header :
45
* - if true, a debug text is added before each cached pages
47
* ====> (boolean) content_type_memorization :
48
* - deprecated => use memorize_headers instead
49
* - if the Content-Type header is sent after the cache was started, the
50
* corresponding value can be memorized and replayed when the cache is hit
51
* (if false (default), the frontend doesn't take care of Content-Type header)
53
* ====> (array) memorize_headers :
54
* - an array of strings corresponding to some HTTP headers name. Listed headers
55
* will be stored with cache datas and "replayed" when the cache is hit
57
* ====> (array) default_options :
58
* - an associative array of default options :
59
* - (boolean) cache : cache is on by default if true
60
* - (boolean) cacheWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
61
* if true, cache is still on even if there are some variables in this superglobal array
62
* if false, cache is off if there are some variables in this superglobal array
63
* - (boolean) makeIdWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
64
* if true, we have to use the content of this superglobal array to make a cache id
65
* if false, the cache id won't be dependent of the content of this superglobal array
66
* - (int) specific_lifetime : cache specific lifetime
67
* (false => global lifetime is used, null => infinite lifetime,
68
* integer => this lifetime is used), this "lifetime" is probably only
69
* usefull when used with "regexps" array
70
* - (array) tags : array of tags (strings)
71
* - (int) priority : integer between 0 (very low priority) and 10 (maximum priority) used by
72
* some particular backends
74
* ====> (array) regexps :
75
* - an associative array to set options only for some REQUEST_URI
76
* - keys are (pcre) regexps
77
* - values are associative array with specific options to set if the regexp matchs on $_SERVER['REQUEST_URI']
78
* (see default_options for the list of available options)
79
* - if several regexps match the $_SERVER['REQUEST_URI'], only the last one will be used
83
protected $_specificOptions = array(
84
'http_conditional' => false,
85
'debug_header' => false,
86
'content_type_memorization' => false,
87
'memorize_headers' => array(),
88
'default_options' => array(
89
'cache_with_get_variables' => false,
90
'cache_with_post_variables' => false,
91
'cache_with_session_variables' => false,
92
'cache_with_files_variables' => false,
93
'cache_with_cookie_variables' => false,
94
'make_id_with_get_variables' => true,
95
'make_id_with_post_variables' => true,
96
'make_id_with_session_variables' => true,
97
'make_id_with_files_variables' => true,
98
'make_id_with_cookie_variables' => true,
100
'specific_lifetime' => false,
108
* Internal array to store some options
110
* @var array associative array of options
112
protected $_activeOptions = array();
115
* If true, the page won't be cached
119
private $_cancel = false;
124
* @param array $options Associative array of options
125
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
126
* @throws Zend_Cache_Exception
129
public function __construct(array $options = array())
131
while (list($name, $value) = each($options)) {
132
$name = strtolower($name);
135
$this->_setRegexps($value);
137
case 'default_options':
138
$this->_setDefaultOptions($value);
140
case 'content_type_memorization':
141
$this->_setContentTypeMemorization($value);
144
$this->setOption($name, $value);
147
if (isset($this->_specificOptions['http_conditional'])) {
148
if ($this->_specificOptions['http_conditional']) {
149
Zend_Cache::throwException('http_conditional is not implemented for the moment !');
152
$this->setOption('automatic_serialization', true);
156
* Specific setter for the 'default_options' option (with some additional tests)
158
* @param array $options Associative array
159
* @throws Zend_Cache_Exception
162
protected function _setDefaultOptions($options)
164
if (!is_array($options)) {
165
Zend_Cache::throwException('default_options must be an array !');
167
foreach ($options as $key=>$value) {
168
if (!is_string($key)) {
169
Zend_Cache::throwException("invalid option [$key] !");
171
$key = strtolower($key);
172
if (isset($this->_specificOptions['default_options'][$key])) {
173
$this->_specificOptions['default_options'][$key] = $value;
179
* Set the deprecated contentTypeMemorization option
181
* @param boolean $value value
185
protected function _setContentTypeMemorization($value)
188
foreach ($this->_specificOptions['memorize_headers'] as $key => $value) {
189
if (strtolower($value) == 'content-type') {
195
$this->_specificOptions['memorize_headers'][] = 'Content-Type';
199
unset($this->_specificOptions['memorize_headers'][$found]);
205
* Specific setter for the 'regexps' option (with some additional tests)
207
* @param array $options Associative array
208
* @throws Zend_Cache_Exception
211
protected function _setRegexps($regexps)
213
if (!is_array($regexps)) {
214
Zend_Cache::throwException('regexps option must be an array !');
216
foreach ($regexps as $regexp=>$conf) {
217
if (!is_array($conf)) {
218
Zend_Cache::throwException('regexps option must be an array of arrays !');
220
$validKeys = array_keys($this->_specificOptions['default_options']);
221
foreach ($conf as $key=>$value) {
222
if (!is_string($key)) {
223
Zend_Cache::throwException("unknown option [$key] !");
225
$key = strtolower($key);
226
if (!in_array($key, $validKeys)) {
227
unset($regexps[$regexp][$key]);
231
$this->setOption('regexps', $regexps);
237
* @param string $id (optional) A cache id (if you set a value here, maybe you have to use Output frontend instead)
238
* @param boolean $doNotDie For unit testing only !
239
* @return boolean True if the cache is hit (false else)
241
public function start($id = false, $doNotDie = false)
243
$this->_cancel = false;
244
$lastMatchingRegexp = null;
245
foreach ($this->_specificOptions['regexps'] as $regexp => $conf) {
246
if (preg_match("`$regexp`", $_SERVER['REQUEST_URI'])) {
247
$lastMatchingRegexp = $regexp;
250
$this->_activeOptions = $this->_specificOptions['default_options'];
251
if (!is_null($lastMatchingRegexp)) {
252
$conf = $this->_specificOptions['regexps'][$lastMatchingRegexp];
253
foreach ($conf as $key=>$value) {
254
$this->_activeOptions[$key] = $value;
257
if (!($this->_activeOptions['cache'])) {
261
$id = $this->_makeId();
266
$array = $this->load($id);
267
if ($array !== false) {
268
$data = $array['data'];
269
$headers = $array['headers'];
270
if ($this->_specificOptions['debug_header']) {
271
echo 'DEBUG HEADER : This is a cached page !';
273
if (!headers_sent()) {
274
foreach ($headers as $key=>$headerCouple) {
275
$name = $headerCouple[0];
276
$value = $headerCouple[1];
277
header("$name: $value");
286
ob_start(array($this, '_flush'));
287
ob_implicit_flush(false);
292
* Cancel the current caching process
294
public function cancel()
296
$this->_cancel = true;
300
* callback for output buffering
301
* (shouldn't really be called manually)
303
* @param string $data Buffered output
304
* @return string Data to send to browser
306
public function _flush($data)
308
if ($this->_cancel) {
312
$storedHeaders = array();
313
$headersList = headers_list();
314
foreach($this->_specificOptions['memorize_headers'] as $key=>$headerName) {
315
foreach ($headersList as $headerSent) {
316
$tmp = split(':', $headerSent);
317
$headerSentName = trim(array_shift($tmp));
318
if (strtolower($headerName) == strtolower($headerSentName)) {
319
$headerSentValue = trim(implode(':', $tmp));
320
$storedHeaders[] = array($headerSentName, $headerSentValue);
326
'headers' => $storedHeaders
328
$this->save($array, null, $this->_activeOptions['tags'], $this->_activeOptions['specific_lifetime'], $this->_activeOptions['priority']);
333
* Make an id depending on REQUEST_URI and superglobal arrays (depending on options)
335
* @return mixed|false a cache id (string), false if the cache should have not to be used
337
protected function _makeId()
339
$tmp = $_SERVER['REQUEST_URI'];
340
foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
341
$tmp2 = $this->_makePartialId($arrayName, $this->_activeOptions['cache_with_' . strtolower($arrayName) . '_variables'], $this->_activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']);
351
* Make a partial id depending on options
353
* @param string $arrayName Superglobal array name
354
* @param bool $bool1 If true, cache is still on even if there are some variables in the superglobal array
355
* @param bool $bool2 If true, we have to use the content of the superglobal array to make a partial id
356
* @return mixed|false Partial id (string) or false if the cache should have not to be used
358
protected function _makePartialId($arrayName, $bool1, $bool2)
360
switch ($arrayName) {
368
if (isset($_SESSION)) {
375
if (isset($_COOKIE)) {
389
return serialize($var);
393
if (count($var) > 0) {