|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Enable a fast track cache with a json representation of the image delivery. |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
class CFastTrackCache |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Cache is disabled to start with. |
|
10
|
|
|
*/ |
|
11
|
|
|
private $enabled = false; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Path to the cache directory. |
|
17
|
|
|
*/ |
|
18
|
|
|
private $path; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Filename of current cache item. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $filename; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Container with items to store as cached item. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $container; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Enable or disable cache. |
|
38
|
|
|
* |
|
39
|
|
|
* @param boolean $enable set to true to enable, false to disable |
|
|
|
|
|
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function enable($enabled) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->enabled = $enabled; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set the path to the cache dir which must exist. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $path to the cache dir. |
|
55
|
|
|
* |
|
56
|
|
|
* @throws Exception when $path is not a directory. |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setCacheDir($path) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!is_dir($path)) { |
|
63
|
|
|
throw new Exception("Cachedir is not a directory."); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->path = rtrim($path, "/"); |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set the filename to store in cache, use the querystring to create that |
|
75
|
|
|
* filename. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $clear items to clear in $_GET when creating the filename. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string as filename created. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setFilename($clear) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$query = $_GET; |
|
84
|
|
|
|
|
85
|
|
|
// Remove parts from querystring that should not be part of filename |
|
86
|
|
|
foreach ($clear as $value) { |
|
87
|
|
|
unset($query[$value]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
arsort($query); |
|
91
|
|
|
$queryAsString = http_build_query($query); |
|
92
|
|
|
|
|
93
|
|
|
$this->filename = md5($queryAsString); |
|
94
|
|
|
|
|
95
|
|
|
if (CIMAGE_DEBUG) { |
|
96
|
|
|
$this->container["query-string"] = $queryAsString; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->filename; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add header items. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $header add this as header. |
|
108
|
|
|
* |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function addHeader($header) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->container["header"][] = $header; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add header items on output, these are not output when 304. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $header add this as header. |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function addHeaderOnOutput($header) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->container["header-output"][] = $header; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set path to source image to. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $source path to source image file. |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setSource($source) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->container["source"] = $source; |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set last modified of source image, use to check for 304. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $lastModified |
|
153
|
|
|
* |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setLastModified($lastModified) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->container["last-modified"] = $lastModified; |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get filename of cached item. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string as filename. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getFilename() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->path . "/" . $this->filename; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Write current item to cache. |
|
178
|
|
|
* |
|
179
|
|
|
* @return boolean if cache file was written. |
|
180
|
|
|
*/ |
|
181
|
|
|
public function writeToCache() |
|
182
|
|
|
{ |
|
183
|
|
|
if (!$this->enabled) { |
|
184
|
|
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (is_dir($this->path) && is_writable($this->path)) { |
|
188
|
|
|
$filename = $this->getFilename(); |
|
189
|
|
|
return file_put_contents($filename, json_encode($this->container)) !== false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Output current item from cache, if available. |
|
199
|
|
|
* |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function output() |
|
|
|
|
|
|
203
|
|
|
{ |
|
204
|
|
|
$filename = $this->getFilename(); |
|
205
|
|
|
if (!is_readable($filename)) { |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$item = json_decode(file_get_contents($filename), true); |
|
210
|
|
|
|
|
211
|
|
|
if (!is_readable($item["source"])) { |
|
212
|
|
|
return; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
foreach ($item["header"] as $value) { |
|
216
|
|
|
header($value); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) |
|
220
|
|
|
&& strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) == $item["last-modified"]) { |
|
221
|
|
|
header("HTTP/1.0 304 Not Modified"); |
|
222
|
|
|
if (CIMAGE_DEBUG) { |
|
223
|
|
|
trace(__CLASS__ . " 304"); |
|
224
|
|
|
} |
|
225
|
|
|
exit; |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
foreach ($item["header-output"] as $value) { |
|
229
|
|
|
header($value); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if (CIMAGE_DEBUG) { |
|
233
|
|
|
trace(__CLASS__ . " 200"); |
|
234
|
|
|
} |
|
235
|
|
|
readfile($item["source"]); |
|
236
|
|
|
exit; |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.