Page.php
9.6 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @subpackage Frontend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Zend_Cache_Core
*/
require_once 'Zend/Cache/Core.php';
/**
* @package Zend_Cache
* @subpackage Frontend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Cache_Frontend_Page extends Zend_Cache_Core
{
/**
* This frontend specific options
*
* ====> (boolean) httpConditional :
* - if true, http conditional mode is on
* WARNING : httpConditional OPTION IS NOT IMPLEMENTED FOR THE MOMENT (TODO)
*
* ====> (boolean) debugHeader :
* - if true, a debug text is added before each cached pages
*
* ====> (array) defaultOptions :
* - an associative array of default options :
* - (boolean) cache : cache is on by default if true
* - (boolean) cacheWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
* if true, cache is still on even if there are some variables in this superglobal array
* if false, cache is off if there are some variables in this superglobal array
* - (boolean) makeIdWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
* if true, we have to use the content of this superglobal array to make a cache id
* if false, the cache id won't be dependent of the content of this superglobal array
*
* ====> (array) regexps :
* - an associative array to set options only for some REQUEST_URI
* - keys are (pcre) regexps
* - values are associative array with specific options to set if the regexp matchs on $_SERVER['REQUEST_URI']
* (see defaultOptions for the list of available options)
* - if several regexps match the $_SERVER['REQUEST_URI'], only the last one will be used
*
* @var array options
*/
protected $_specificOptions = array(
'httpConditional' => false,
'debugHeader' => false,
'defaultOptions' => array(
'cacheWithGetVariables' => false,
'cacheWithPostVariables' => false,
'cacheWithSessionVariables' => false,
'cacheWithFilesVariables' => false,
'cacheWithCookieVariables' => false,
'makeIdWithGetVariables' => true,
'makeIdWithPostVariables' => true,
'makeIdWithSessionVariables' => true,
'makeIdWithFilesVariables' => true,
'makeIdWithCookieVariables' => true,
'cache' => true
),
'regexps' => array()
);
/**
* Internal array to store some options
*
* @var array associative array of options
*/
protected $_activeOptions = array();
/**
* Constructor
*
* @param array $options associative array of options
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
*/
public function __construct($options = array())
{
if (isset($options['httpConditional'])) {
if ($options['httpConditional']) {
Zend_Cache::throwException('httpConditional is not implemented for the moment !');
}
}
while (list($name, $value) = each($options)) {
switch ($name) {
case 'regexps':
$this->_setRegexps($value);
break;
case 'defaultOptions':
$this->_setDefaultOptions($value);
break;
default:
$this->setOption($name, $value);
}
}
}
/**
* Specific setter for the 'defaultOptions' option (with some additional tests)
*
* @param array $options associative array
*/
protected function _setDefaultOptions($options)
{
if (!is_array($options)) {
Zend_Cache::throwException('defaultOptions must be an array !');
}
foreach ($options as $key=>$value) {
if (!isset($this->_specificOptions['defaultOptions'][$key])) {
Zend_Cache::throwException("unknown option [$key] !");
} else {
$this->_specificOptions['defaultOptions'][$key] = $value;
}
}
}
/**
* Specific setter for the 'regexps' option (with some additional tests)
*
* @param array $options associative array
*/
protected function _setRegexps($regexps)
{
if (!is_array($regexps)) {
Zend_Cache::throwException('regexps option must be an array !');
}
foreach ($regexps as $regexp=>$conf) {
if (!is_array($conf)) {
Zend_Cache::throwException('regexps option must be an array of arrays !');
}
$validKeys = array_keys($this->_specificOptions['defaultOptions']);
foreach ($conf as $key=>$value) {
if (!in_array($key, $validKeys)) {
Zend_Cache::throwException("unknown option [$key] !");
}
}
}
$this->setOption('regexps', $regexps);
}
/**
* Start the cache
*
* @param string $id (optional) a cache id (if you set a value here, maybe you have to use Output frontend instead)
* @param boolean $doNotDie for unit testing only !
* @return boolean true if the cache is hit (false else)
*/
public function start($id = false, $doNotDie = false)
{
$lastMatchingRegexp = null;
foreach ($this->_specificOptions['regexps'] as $regexp => $conf) {
if (preg_match("`$regexp`", $_SERVER['REQUEST_URI'])) {
$lastMatchingRegexp = $regexp;
}
}
$this->_activeOptions = $this->_specificOptions['defaultOptions'];
if (!is_null($lastMatchingRegexp)) {
$conf = $this->_specificOptions['regexps'][$lastMatchingRegexp];
foreach ($conf as $key=>$value) {
$this->_activeOptions[$key] = $value;
}
}
if (!($this->_activeOptions['cache'])) {
return false;
}
if (!$id) {
$id = $this->_makeId();
if (!$id) {
return false;
}
}
$data = $this->load($id);
if ($data !== false) {
if ($this->_specificOptions['debugHeader']) {
echo 'DEBUG HEADER : This is a cached page !';
}
echo $data;
if ($doNotDie) {
return true;
}
die();
}
ob_start(array($this, '_flush'));
ob_implicit_flush(false);
return false;
}
/**
* callback for output buffering
* (shouldn't really be called manually)
*
* @param string $data buffered output
* @return string data to send to browser
*/
public function _flush($data)
{
$this->save($data);
return $data;
}
/**
* Make an id depending on REQUEST_URI and superglobal arrays (depending on options)
*
* @return mixed a cache id (string), false if the cache should have not to be used
*/
private function _makeId()
{
$tmp = $_SERVER['REQUEST_URI'];
foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
$tmp2 = $this->_makePartialId($arrayName, $this->_activeOptions['cacheWith' . $arrayName . 'Variables'], $this->_activeOptions['makeIdWith' . $arrayName . 'Variables']);
if ($tmp2===false) {
return false;
}
$tmp = $tmp . $tmp2;
}
return md5($tmp);
}
/**
* Make a partial id depending on options
*
* @var string $arrayName superglobal array name
* @var $bool1 if true, cache is still on even if there are some variables in the superglobal array
* @var $bool2 if true, we have to use the content of the superglobal array to make a partial id
* @return mixed partial id (string) or false if the cache should have not to be used
*/
private function _makePartialId($arrayName, $bool1, $bool2)
{
switch ($arrayName) {
case 'Get':
$var = $_GET;
break;
case 'Post':
$var = $_POST;
break;
case 'Session':
if (isset($_SESSION)) {
$var = $_SESSION;
} else {
$var = null;
}
break;
case 'Cookie':
if (isset($_COOKIE)) {
$var = $_COOKIE;
} else {
$var = null;
}
break;
case 'Files':
$var = $_FILES;
break;
default:
return false;
}
if ($bool1) {
if ($bool2) {
return serialize($var);
}
return '';
}
if (count($var) > 0) {
return false;
}
return '';
}
}