forked from SimoneS93/xtremecache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxtremecache.php
274 lines (239 loc) · 8.9 KB
/
xtremecache.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
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* Serve cached pages with no request processing
* @author Pavol Ďurko
* @based on Salerno Simone
* @version 1.0.8
* @license MIT
*/
require __DIR__.DS.'config.php';
require __DIR__.DS.'classes'.DS.'PSCache.php';
class XtremeCache extends Module {
private $cacheKey;
private $ps_cache;
private $_activeCache;
public function __construct()
{
$this->name = 'xtremecache';
$this->tab = 'front_office_features';
$this->version = '1.0.8';
$this->author = 'Pavol Ďurko';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Xtreme cache');
$this->description = $this->l('Cache all front office pages.');
}
/**
* Handle non-explicitly handled hooks
* @param string $name hook name
* @param array $arguments
*/
public function __call($name, $arguments)
{
if (0 === strpos(strtolower($name), 'hookaction')) {
$this->_clearCache();
}
}
/**
* Install and register hooks
* @return bool
*/
public function install()
{
return parent::install() &&
$this->registerHook('actionDispatcher') &&
$this->registerHook('actionRequestComplete') &&
$this->registerHook('actionCategoryAdd') &&
$this->registerHook('actionCategoryUpdate') &&
$this->registerHook('actionCategoryDelete') &&
$this->registerHook('actionProductAdd') &&
$this->registerHook('actionProductUpdate') &&
$this->registerHook('actionProductDelete') &&
$this->registerHook('actionProductSave');
}
/**
* Uninstall and clear cache
* @return bool
*/
public function uninstall()
{
//delete all cached files
$this->_clearCache(null, null, null, true);
return $this->unregisterHook('actionDispatcher') &&
$this->unregisterHook('actionRequestComplete') &&
parent::uninstall();
}
/**
* Check if page exists in cache
* If it exists, serve and abort
* @param array $params
*/
public function hookActionDispatcher(&$params)
{
if (!$this->isActive())
return;
//if not in the checkout process, probably not necessary, checkout cant continue without products in cart (isActive)
if ($params['controller_class'] !== 'OrderController' &&
$params['controller_class'] !== 'OrderOpcController')
{
$cached = $this->getFromCache();
if ($cached !== false)
{
exit($cached);
}
}
}
/**
* Cache page content for front pages
* @param string $params
*/
public function hookActionRequestComplete(&$params)
{
if (!$this->isActive())
return;
if (!is_subclass_of($this->context->controller, 'OrderController') &&
!is_subclass_of($this->context->controller, 'OrderOpcController') &&
!is_subclass_of($this->context->controller, 'PageNotFoundController') // do not cache 404
&& !$this->isMaintenance() // comment this line to cache pages during maintenance too
)
{
if ($this->saveToCache($params['output']))
{}
else {
//echo 'Unable to write cache.'; // inform about cache issues, misconfigurations..
} // we can do stats
}
}
private function getFromCache()
{
return $this->ps_cache->retrieve($this->cacheKey);
}
private function saveToCache(&$data)
{
$debugInfo = sprintf(
'<!-- [%s from %s on %s] -->',
$this->cacheKey,
str_replace('Cache', '', (DRIVER === 'prestashop') ? _PS_CACHING_SYSTEM_ : DRIVER),
date('Y-m-d H:i:s'));
return $this->ps_cache->store($this->cacheKey, $debugInfo . chr(0x0D) . chr(0x0A) . $data, CACHE_TTL);
}
/**
* Check if we should use cache
* checks for: dev mode, profilling, front controller, maintenance mode?, customer, shopping cart, AJAX and POST requests
* @return boolean
*/
private function isActive()
{
//turn off if we are not in front office
if($this->context->controller->controller_type !== 'front')
{
// i dont like unnecessary overrides, this is workaround for clearing cache
if ($_GET['empty_smarty_cache'] == 1 || $_GET['empty_sf2_cache'] == 1)
$this->_clearCache();
return $this->stopCache();
}
// make sure processing occurs only once and whole code will not execute in hookActionRequestComplete again
// we get all information at first time
if ($this->_activeCache !== true || $this->_activeCache !== false)
{
//turn off on debug mode and in profilling
if (_PS_MODE_DEV_ || _PS_DEBUG_PROFILING_)
return $this->stopCache();
//disable on ajax and non-GET requests
$active = !(isset($this->context->controller->ajax) ? $this->ajax : false);
$active = $active && $_SERVER['REQUEST_METHOD'] === 'GET';
if (!$active)
return $this->stopCache();
// if enabled, during maintenance mode there will be no cache
if (CHECK_FOR_MAINTENANCE && !((bool)Configuration::get('PS_SHOP_ENABLE', true)))
return $this->stopCache();
//check that customer is not logged in
if (isset($this->context->customer))
{
$customer = $this->context->customer;
if ($customer && $customer instanceof Customer && $customer->id > 0)
return $this->stopCache();
}
//for guest checkout, check that cart is empty
if (isset($this->context->cookie->id_cart))
{
$cart = new Cart($this->context->cookie->id_cart);
if ($cart && $cart instanceof Cart && $cart->nbProducts() > 0)
return $this->stopCache();
}
// we will be working with cache, so we get key and cache handler
$this->initCache();
}
return $this->_activeCache;
}
private function initCache()
{
$this->cacheKey = $this->getCacheKey();
$this->ps_cache = new PSCache();
$this->_activeCache = true;
}
private function stopCache()
{
$this->_activeCache = false;
return false;
}
/**
* Map lang, shop, currency, device and url to create cache key
* @return md5 string
*/
public function getCacheKey($url = null)
{
if ($url === null)
$url = $_SERVER['REQUEST_URI'];
$device = (SEPARATE_MOBILE_AND_DESKTOP) ? 'device-'.$this->context->getDevice().'|' : '';
$currency = (MULTICURRENCY) ? 'currency-'.$this->getCurrencyId().'|' : '';
$url = $device.
'lang-'.$this->context->language->id.
'|shop-'.$this->context->shop->id.
'|theme-'.$this->context->shop->theme_name.
'|puri-'.$this->context->shop->physical_uri.
'|vuri-'.$this->context->shop->virtual_uri.
'|domain-'.$this->context->shop->domain.'|'.
$currency.
'url-'.$url;
return md5($url);
}
/**
* Hack to get protected variable
* Are we in maintenance?
*/
private function isMaintenance()
{
$reflection = new ReflectionClass($this->context->controller);
$property = $reflection->getProperty('maintenance');
$property->setAccessible(true);
return (bool)$property->getValue($this->context->controller);
}
/**
* Look if currency is set in cookies
* if not, return default currency ID
* @return integer
*/
private function getCurrencyId()
{
// get currency from cookies
if (isset($this->context->cookie->id_currency))
$currency = $this->context->cookie->id_currency;
else // get PrestaShop default currency
{
$defaultCurrency = Currency::getDefaultCurrency(); // query for default
$currency = ($defCurrency === false) ? 1 : $defaultCurrency; // fallback, set currency ID to 1 if not found
}
return (int) $currency;
}
// clear whole cache
public function _clearCache($template = null, $cache_id = null, $compile_id = null, $deleteAll = false)
{
if (!isset($this->ps_cache))
$this->ps_cache = new PSCache();
$this->ps_cache->flush();
}
}