-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheZero.php
214 lines (195 loc) · 5.25 KB
/
CacheZero.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
<?php
/*
* A simple caching class inspired by Cache_Lite (http://pear.php.net/manual/en/package.caching.cache-lite.php)
*
* @author: George Borisov <george at gir dot me dot uk>
* @version: 20130102.03
* @licence: LGPL (https://www.gnu.org/licenses/lgpl-3.0.txt)
*
*/
class CacheZero {
private $options = array(
'cacheDir' => 'cache', // directory where to put the cache files (string),
'caching' => true, // enable / disable caching (boolean),
'fileLocking' => true, // enable / disable fileLocking (boolean),
'hashedDirectoryLevel' => 0, // level of the hashed directory system (int),
'lifeTime' => 3600, // cache lifetime in seconds (int),
'modeFile' => 0600, // mode for cache file (int),
'modeDir' => 0700, // mode for group / hash directory (int),
'exitOnError' => true, // exit on error (bool)
'protectID' => true, // enable / disable id protection in cache file name (boolean)
'protectGroup' => false, // enable / disable group protection in cache file name (boolean)
'verify' => true, // enable / disable verification of cached data (boolean)
'verifyMethod' => 'crc32', // type of verification 'crc32', 'md5', 'strlen' (string)
);
private $id;
private $group;
function __construct($options)
{
if (is_array($options)) {
foreach ($options as $k => $v) {
if (isset($this->options[$k])) {
$this->options[$k] = $v;
}
}
}
}
/// PUBLIC METHODS ///
function setOption($k, $v)
{
if (isset($this->options[$k])) {
$this->options[$k] = $v;
}
}
function get($id, $group = 'default', $expiredOK = false)
{
$data = false;
if ($this->options['caching']) {
$data = $this->read($id, $group);
}
return $data;
}
function save($data, $id, $group = 'default')
{
if ($this->options['caching']) {
$this->write($data, $id, $group);
}
}
function start($id, $group = 'default')
{
$this->id = $id;
$this->group = $group;
ob_start();
ob_implicit_flush(false);
}
function finish()
{
$data = ob_get_contents();
ob_end_clean();
$this->save($data, $this->id, $this->group);
echo $data;
}
/// PRIVATE METHODS ///
private function getFilePath($id, $group)
{
$a = array();
$a['id'] = 'cache_';
$regex = '/[^\w\-\?\&\.=]/';
if ($this->options['protectID']) {
$a['id'] .= md5($id);
} else {
$a['id'] .= preg_replace($regex, '', $id);
}
$a['path'] = $_SERVER['DOCUMENT_ROOT'] .'/'. $this->options['cacheDir'];
$a['prefix'] = array();
if ($this->options['protectGroup']) {
$a['prefix'][] = md5($group);
} else {
$a['prefix'][] = preg_replace($regex, '', $group);
}
if ($this->options['hashedDirectoryLevel']) {
$hash = ($this->options['protectID']) ? $id : md5($id);
for ($i=0; $i < $this->options['hashedDirectoryLevel']; ++$i) {
$a['prefix'][] = substr($hash, 0, $i + 1);
}
}
return $a;
}
private function createDir($path)
{
mkdir($path, $this->options['modeDir']);
if (!file_exists($path) || !is_dir($path)) {
$this->error("Unable to create directory ($path)");
}
}
private function read($id, $group)
{
$a = $this->getFilePath($id, $group);
$path = $a['path'] .'/'. implode('/', $a['prefix']) .'/'. $a['id'];
$result = false;
if (file_exists($path)) {
if ($this->options['lifeTime'] && filemtime($path) > (time() - $this->options['lifeTime'])) {
$fh = fopen($path, 'rb');
if ($fh) {
if ($this->options['verify']) {
$hash = trim(fgets($fh));
}
$data = '';
while(!feof($fh)){
$data .= fgets($fh); // TODO: test with lines > 8192 (https://bugs.php.net/bug.php?id=30936)
}
fclose($fh);
if ($this->options['verify']) {
if ((string)$hash === (string)$this->hash($data)) {
$result = $data;
} else {
unlink($path);
}
} else {
$result = $data;
}
}
}
}
return $result;
}
private function write($data, $id, $group)
{
$a = $this->getFilePath($id, $group);
$path = $a['path'];
if (!$path || !file_exists($path)) {
$this->error("Cache directory missing ($path)"); // paranoia
}
while (count($a['prefix'])) {
$path .= '/'. array_shift($a['prefix']);
if (!file_exists($path)) {
$this->createDir($path);
}
}
$path = "$path/$a[id]";
$fh = fopen($path, 'wb');
if (!$fh) {
$this->error("Unable to open cache file ($path)");
}
if ($this->options['caching']) { // check again in case caching was disabled by error handler
if ($this->options['fileLocking']) {
flock($fh, LOCK_EX);
}
if ($this->options['verify']) {
fwrite($fh, $this->hash($data) ."\n");
}
fwrite($fh, $data);
if ($this->options['fileLocking']) {
flock($fh, LOCK_UN);
}
fclose($fh);
chmod($path, $this->options['modeFile']);
}
}
private function hash($data)
{
$hash = FALSE;
$method = $this->options['verifyMethod'];
if ($method === 'crc32') {
$hash = crc32($data);
} elseif ($method === 'md5') {
$hash = md5($data);
} elseif ($method === 'strlen') {
$hash = strlen($data);
} else {
$this->error("Invalid verification method ($method)"); // paranoia
}
return $hash;
}
private function error($msg)
{
$msg = "ERROR: $msg\n";
if ($this->options['exitOnError']) {
exit($msg);
} else {
echo $msg;
$this->options['caching'] = false; // disable caching
}
}
}
?>