/
PageCache.php
320 lines (281 loc) · 9.92 KB
/
PageCache.php
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
namespace Concrete\Core\Cache\Page;
use Concrete\Core\Cache\FlushableInterface;
use Concrete\Core\Http\Request;
use Concrete\Core\Http\Response;
use Concrete\Core\Page\Page as ConcretePage;
use Concrete\Core\Page\View\PageView;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\User\User;
abstract class PageCache implements FlushableInterface
{
/**
* @deprecated what's deprecated is the "public" part: use the getLibrary() method to retrieve the library
*
* @var \Concrete\Core\Cache\Page\PageCache|null
*/
public static $library;
/**
* Build a Response object starting from a cached page.
*
* @param \Concrete\Core\Cache\Page\PageCacheRecord $record the cache record containing the cached page data
*
* @return \Concrete\Core\Http\Response
*/
public function deliver(PageCacheRecord $record)
{
$response = new Response();
$headers = [];
if (defined('APP_CHARSET')) {
$headers['Content-Type'] = 'text/html; charset=' . APP_CHARSET;
}
$headers = array_merge($headers, $record->getCacheRecordHeaders());
$response->headers->add($headers);
$response->setContent($record->getCacheRecordContent());
return $response;
}
/**
* Get the page cache library.
*
* @return \Concrete\Core\Cache\Page\PageCache
*/
public static function getLibrary()
{
if (!self::$library) {
$app = Application::getFacadeApplication();
$config = $app->make('config');
$adapter = $config->get('concrete.cache.page.adapter');
$class = overrideable_core_class(
'Core\\Cache\\Page\\' . camelcase($adapter) . 'PageCache',
DIRNAME_CLASSES . '/Cache/Page/' . camelcase($adapter) . 'PageCache.php'
);
self::$library = $app->build($class);
}
return self::$library;
}
/**
* Determine if we should check if a page is in the cache.
*
* @param \Concrete\Core\Http\Request $req
*
* @return bool
*/
public function shouldCheckCache(Request $req)
{
if ($req->getMethod() === $req::METHOD_POST) {
return false;
}
$app = Application::getFacadeApplication();
$config = $app->make('config');
$cookie = $app->make('cookie');
$loginCookie = sprintf('%s_LOGIN', $config->get('concrete.session.name'));
if ($cookie->has($loginCookie) && $cookie->get($loginCookie)) {
return false;
}
return true;
}
/**
* Send the cache-related HTTP headers for a page to the current response.
*
* @deprecated Headers are no longer set directly. Instead, use the
* <code>$pageCache->deliver()</code>
* method to retrieve a response object and either return it from a controller method or, if you must, use
* <code>$response->prepare($request)->send()</code>
*
* @param \Concrete\Core\Page\Page $c
*/
public function outputCacheHeaders(ConcretePage $c)
{
foreach ($this->getCacheHeaders($c) as $header) {
header($header);
}
}
/**
* Get the cache-related HTTP headers for a page.
*
* @param \Concrete\Core\Page\Page $c
*
* @return array Keys are the header names; values are the header values
*/
public function getCacheHeaders(ConcretePage $c)
{
$lifetime = $c->getCollectionFullPageCachingLifetimeValue();
$expires = gmdate('D, d M Y H:i:s', time() + $lifetime) . ' GMT';
$headers = [
'Pragma' => 'public',
'Cache-Control' => 'max-age=' . $lifetime . ',s-maxage=' . $lifetime,
'Expires' => $expires,
];
return $headers;
}
/**
* Check if a page contained in a PageView should be stored in the cache.
*
* @param \Concrete\Core\Page\View\PageView $v
*
* @return bool
*/
public function shouldAddToCache(PageView $v)
{
$c = $v->getPageObject();
if (!is_object($c)) {
return false;
}
$cp = new Checker($c);
if (!$cp->canViewPage()) {
return false;
}
if (is_object($v->controller)) {
$allowedControllerActions = ['view'];
if (!in_array($v->controller->getAction(), $allowedControllerActions)) {
return false;
}
if ($c->isGeneratedCollection() && !$v->controller->supportsPageCache()) {
return false;
}
}
if (!$c->getCollectionFullPageCaching()) {
return false;
}
$app = Application::getFacadeApplication();
$request = $app->make(Request::class);
if ($request->getMethod() === $request::METHOD_POST) {
return false;
}
$u = $app->make(User::class);
if ($u->isRegistered()) {
return false;
}
$config = $app->make('config');
if ($c->getCollectionFullPageCaching() == 1 || $config->get('concrete.cache.pages') === 'all') {
// this cache page at the page level
// this overrides any global settings
return true;
}
if ($config->get('concrete.cache.pages') !== 'blocks') {
// we are NOT specifically caching this page, and we don't
return false;
}
$blocks = $c->getBlocks();
$blocks = array_merge($c->getGlobalBlocks(), $blocks);
foreach ($blocks as $b) {
if (!$b->cacheBlockOutput()) {
return false;
}
}
return true;
}
/**
* Get the key that identifies the cache entry for a page or a request.
*
* @param \Concrete\Core\Page\Page|\Concrete\Core\Http\Request|\Concrete\Core\Cache\Page\PageCacheRecord|mixed $mixed
*
* @return string|null Returns NULL if $mixed is not a recognized type, a string otherwise
*/
public function getCacheKey($mixed)
{
if ($mixed instanceof ConcretePage) {
$host = $this->getCacheHost($mixed);
if (empty($host)) {
// Default to the request host. This should only happen in case
// the canonical URL has not been set for the site that the page
// belongs to.
$host = Request::getInstance()->getHttpHost();
}
$collectionPath = (string) $mixed->getCollectionPath();
// Add the "extra" parts to the path that can be added to the URL
// because the page/page type controller can have request actions.
$ctrl = $mixed->getPageController();
if (is_object($ctrl) &&
!empty($action = $ctrl->getRequestAction())
) {
$extra = [];
if ($action !== 'view') {
$extra[] = $action;
}
$extra = array_merge(
$extra,
$ctrl->getRequestActionParameters()
);
if (count($extra) > 0) {
$collectionPath .= '/' . implode('/', $extra);
}
}
$collectionPath = trim($collectionPath, '/');
if ($collectionPath !== '') {
return urlencode($host . '/' . $collectionPath);
}
if ($mixed->isHomePage()) {
return urlencode($host);
}
} elseif ($mixed instanceof Request) {
$host = $this->getCacheHost($mixed);
$path = trim((string) $mixed->getPath(), '/');
if ($path !== '') {
return urlencode($host . '/' . $path);
}
return urlencode($host);
} elseif ($mixed instanceof PageCacheRecord) {
return $mixed->getCacheRecordKey();
}
}
/**
* Get the host name under which the page or request belongs to.
*
* @param \Concrete\Core\Page\Page|\Concrete\Core\Http\Request|mixed $mixed
*
* @return string|null Returns NULL if $mixed is not a recognized type, a string otherwise
*/
public function getCacheHost($mixed)
{
if ($mixed instanceof ConcretePage) {
$site = $mixed->getSite();
if (is_object($site)) {
$host = $site->getSiteCanonicalURL();
if (!empty($host)) {
$host = preg_replace('#^https?://#', '', $host);
$host = trim($host, '/'); // Do not want trailing slashes in the host.
return $host;
}
}
} elseif ($mixed instanceof Request) {
return $mixed->getHttpHost();
}
return null;
}
/**
* Get the cached item for a page or a request.
*
* @param \Concrete\Core\Page\Page|\Concrete\Core\Http\Request|mixed $mixed
*
* @return \Concrete\Core\Cache\Page\PageCacheRecord|null Return NULL if $mixed is not a recognized type, or if it's the record is not in the cache
*/
abstract public function getRecord($mixed);
/**
* Store a page in the cache.
*
* @param \Concrete\Core\Page\Page $c The page to be stored in the cache
* @param string $content The whole HTML of the page to be stored in the cache
*/
abstract public function set(ConcretePage $c, $content);
/**
* Remove a cache entry given the record retrieved from the cache.
*
* @param \Concrete\Core\Cache\Page\PageCacheRecord $rec
*/
abstract public function purgeByRecord(PageCacheRecord $rec);
/**
* Remove a cache entry given the page.
*
* @param \Concrete\Core\Cache\Page\PageCacheRecord $rec
* @param ConcretePage $c
*/
abstract public function purge(ConcretePage $c);
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Cache\FlushableInterface::flush()
*/
abstract public function flush();
}