Repository navigation
Expand file tree
/
Copy pathbootstrapcache.php
More file actions
172 lines (131 loc) · 3.66 KB
/
Copy pathbootstrapcache.php
File metadata and controls
172 lines (131 loc) · 3.66 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
<?php
class BootstrapCache {
protected $logger;
protected $driver;
static $pages_exclude = array(
'login.*',
'tools/.*',
'dashboard/.*'
);
static $pages_purge = array(
'login/logout'
);
static $base_dir;
const DEFAULT_CACHE_TIME = 604800; // 60 * 60 * 24 * 7 -- 7 days
function __construct() {
}
public static function factory($driver) {
$basename = strtolower($driver);
if (!preg_match('/^[a-z0-9]+$/mis', $basename)) {
throw new BootstrapCacheException("Driver name '$driver' contains invalid characters (alphanumeric only)");
}
$filename = (static::$base_dir ?: realpath(__DIR__) . '/') . 'drivers/' . $basename . '.php';
if (!file_exists($filename)) {
throw new BootstrapCacheException('Driver file not found: ' . $filename);
}
include_once $filename;
return 'BootstrapCache_Driver_' . $driver;
}
public function render($page=null) {
// If no caching engine is specifed, skip
if (is_null($this->driver)) {
$this->log('debug', 'skipping cache as no engine is specified');
return;
}
// If there was a request to clear the caching system, clear it
if (isset($_GET['purgecache'])) {
return $this->purge();
}
// Don't cache if the user is logged in
if (isset($_SESSION['uID'])) {
$this->log('debug', 'no caching as user is logged in');
return;
}
// Don't use a cache if there is user data
if (!empty($_GET)) {
$this->log('debug', 'not caching as request contains GET params');
return;
}
if (!empty($_POST)) {
$this->log('debug', 'not caching as request contains POST params');
return;
}
// Identify the page
if (is_null($page)) {
$page = $_SERVER['REQUEST_URI'];
}
// Clean the page
$page = str_replace('/index.php', '', $page);
$page = trim($page, '/');
$page = '/' . $page;
$this->log('debug', 'Caching for page: ' . $page);
// If this is an excluded page, exclude it
if (static::page_matches($page, static::$pages_exclude)) {
$this->log('debug', 'skipped page, in exclusion list');
return;
}
// If this is a purge page, purge it
if (static::page_matches($page, static::$pages_purge)) {
return $this->purge();
}
// Test if a cache is available and (if yes) return it to the browser.
// Else, the output buffering is activated.
$this->driver->setKey($_SERVER['HTTP_HOST'], $page);
$has_cached = $this->driver->start();
if (!$has_cached) {
$this->startup();
return;
}
$this->log('debug', 'Outputting cached file');
exit;
}
protected function startup() {
$this->log('debug', 'No cached file, attaching shutdown function');
return register_shutdown_function(array($this, 'shutdown'));
}
public function purge() {
if (is_null($this->driver)) {
return false;
}
$this->log('debug', 'purged cache');
return $this->driver->purge();
}
/**
* Check to see if a page exists in a list
*/
public static function page_matches($needle, $haystack) {
foreach ($haystack as $straw) {
if (preg_match('#^(\/index.php)?\/' . $straw . '$#i', $needle)) {
return true;
}
}
return false;
}
public function shutdown() {
if (is_null($this->driver)) {
return;
}
$this->driver->end();
$this->log('debug', 'Cache ended');
}
/* Logging */
public function setLogger($logger) {
$this->logger = $logger;
}
public function getLogger($logger) {
return $this->logger;
}
protected function log($level, $message, array $context = array()) {
if (is_null($this->logger)) {
return;
}
$this->logger->log($level, '[BOOTSTRAPCACHE] ' . $message, $context);
}
/* Cache engine */
public function setDriver($driver) {
$this->driver = $driver;
}
public function getDriver($driver) {
return $this->driver;
}
}