-
Notifications
You must be signed in to change notification settings - Fork 452
/
PageCache.php
177 lines (146 loc) · 5.01 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
<?php
namespace Concrete\Core\Cache\Page;
use Concrete\Core\Cache\FlushableInterface;
use Concrete\Core\Http\Response;
use Config;
use Concrete\Core\Http\Request;
use Concrete\Core\Page\Page as ConcretePage;
use \Concrete\Core\Page\View\PageView;
use Permissions;
use User;
use Session;
abstract class PageCache implements FlushableInterface
{
static $library;
public function deliver(PageCacheRecord $record)
{
$response = new Response();
$headers = array();
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;
}
public static function getLibrary()
{
if (!PageCache::$library) {
$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'
);
PageCache::$library = new $class();
}
return PageCache::$library;
}
public function shouldCheckCache(Request $req)
{
if ($req->isPost()) {
return false;
}
$app = \Core::make('app');
$config = $app['config'];
$cookie = $app['cookie'];
$loginCookie = sprintf('%s_LOGIN', $config->get('concrete.session.name'));
if ($cookie->has($loginCookie) && $cookie->get($loginCookie)) {
return false;
}
return true;
}
public function outputCacheHeaders(ConcretePage $c)
{
foreach ($this->getCacheHeaders($c) as $header) {
header($header);
}
}
public function getCacheHeaders(ConcretePage $c)
{
$lifetime = $c->getCollectionFullPageCachingLifetimeValue();
$expires = gmdate('D, d M Y H:i:s', time() + $lifetime) . ' GMT';
$headers = array(
'Pragma' => 'public',
'Cache-Control' => 'max-age=' . $lifetime . ',s-maxage=' . $lifetime,
'Expires' => $expires
);
return $headers;
}
public function shouldAddToCache(PageView $v)
{
$c = $v->getCollectionObject();
if (!is_object($c)) {
return false;
}
$cp = new Permissions($c);
if (!$cp->canViewPage()) {
return false;
}
$u = new User();
$allowedControllerActions = array('view');
if (is_object($v->controller)) {
if (!in_array($v->controller->getTask(), $allowedControllerActions)) {
return false;
}
}
if (!$c->getCollectionFullPageCaching()) {
return false;
}
if ($u->isRegistered() || $_SERVER['REQUEST_METHOD'] == 'POST') {
return false;
}
if ($c->isGeneratedCollection()) {
if ((is_object($v->controller) && (!$v->controller->supportsPageCache())) || (!is_object($v->controller))) {
return false;
}
}
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;
}
public function getCacheKey($mixed)
{
$homeCID = 1;
if ($mixed instanceof ConcretePage) {
if ($mixed->getCollectionPath() != '') {
return urlencode(trim($mixed->getCollectionPath(), '/'));
} else {
if ($mixed->getCollectionID() == $homeCID) {
return '!' . $homeCID;
}
}
} else {
if ($mixed instanceof \Concrete\Core\Http\Request) {
if ($mixed->getPath() != '') {
return urlencode(trim($mixed->getPath(), '/'));
} else {
return '!' . $homeCID;
}
} else {
if ($mixed instanceof PageCacheRecord) {
return $mixed->getCacheRecordKey();
}
}
}
}
abstract public function getRecord($mixed);
abstract public function set(ConcretePage $c, $content);
abstract public function purgeByRecord(\Concrete\Core\Cache\Page\PageCacheRecord $rec);
abstract public function purge(ConcretePage $c);
abstract public function flush();
}