-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathclass.jit.php
363 lines (306 loc) · 12.6 KB
/
class.jit.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace JIT;
use Symphony;
use SymphonyErrorPage;
require_once __DIR__ . '/interface.imagefilter.php';
require_once __DIR__ . '/class.imagefilter.php';
require_once __DIR__ . '/class.jitfiltermanager.php';
require_once __DIR__ . '/class.image.php';
class JIT extends Symphony
{
/**
* @var array
*/
private $settings = array();
/**
* @var boolean
*/
private $caching = false;
/**
* @var array
*/
public static $available_filters = array();
/**
* This function returns an instance of the Frontend
* class. It is the only way to create a new Frontend, as
* it implements the Singleton interface
*
* @return JIT
*/
public static function instance()
{
if (!(self::$_instance instanceof JIT)) {
self::$_instance = new JIT;
}
return self::$_instance;
}
public static function getAvailableFilters()
{
if (empty(self::$available_filters)) {
$filters = JitFilterManager::listAll();
foreach ($filters as $filter) {
self::$available_filters[] = JitFilterManager::create($filter['handle']);
}
}
return self::$available_filters;
}
public function display($page = null)
{
$this->settings = Symphony::Configuration()->get('image');
$this->caching = ($this->settings['cache'] == 1 ? true : false);
// is this thing cached?
if ($image = $this->isImageAlreadyCached($_GET['param'])) {
return $this->displayImage($image);
}
// process the parameters
$param = $this->parseParameters($_GET['param']);
// get the actual image
$image = $this->fetchImagePath($param);
if ($image) {
// prepare caching headers, potentially 304.
//$this->sendImageHeaders($param);
$image_resource = $this->fetchImage($image, $param);
// apply the filter
$image = $this->applyFilterToImage($image_resource, $param);
// figure out whether to cache the image or not
if ($this->caching) {
$this->cacheImage($image, $param);
}
// display the image
return $this->displayImage($image);
}
}
/**
* Given the parameters, check to see if this image is already in
* the cache.
*
* @param string $parameter_string
* @return boolean
*/
public function isImageAlreadyCached($parameter_string)
{
return false;
}
/**
* Given the parameters, this function will attempt to parse them
* to determine the mode, it's settings and the image path.
*
* @param string $parameter_string
* @return array
*/
public function parseParameters($parameter_string)
{
$settings = array();
$mode = false;
$image_path = false;
$filters = self::getAvailableFilters();
foreach ($filters as $filter) {
if ($params = $filter->parseParameters($parameter_string)) {
extract($params);
break;
}
}
// Did the delegate resolve anything?
if (($mode && $image_path) === false) {
throw new JITParseParametersException('No JIT filter was found for this request.');
}
// If the background has been set, ensure that it's not mistakenly
// a folder. This is rare edge case in that if a folder is named like
// a hexcode, JIT will interpret it as the background colour instead of
// the filepath.
// @link https://github.com/symphonycms/jit_image_manipulation/issues/8
if (($settings['background'] !== 0 || empty($settings['background'])) && $settings['external'] === false) {
// Also check the case of `bbbbbb/bbbbbb/file.png`, which should resolve
// as background = bbbbbb, file = bbbbbb/file.png (if that's the correct path of
// course)
if (is_dir(WORKSPACE . '/'. $settings['background'])
&& (!is_file(WORKSPACE . '/' . $image_path) && is_file(WORKSPACE . '/' . $settings['background'] . '/' . $image_path))
) {
$image_path = $settings['background'] . '/' . $image_path;
$settings['background'] = 0;
}
}
return array(
'settings' => $settings,
'mode' => $mode,
'image' => $image_path
);
}
/**
* Given the parsed parameters, this function will go and grab
* the desired image, whether it be local, or external.
*
* @param array $parameters
* @return Resource
*/
public function fetchImagePath(array &$parameters)
{
// Fetch external images
if ($parameters['external'] === true) {
$image_path = "http://{$parameters['image']}";
// Image is external, check to see that it is a trusted source
$rules = file(WORKSPACE . '/jit-image-manipulation/trusted-sites', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$allowed = false;
$rules = array_map('trim', $rules);
if (count($rules) > 0) {
foreach ($rules as $rule) {
$rule = str_replace(array('http://', 'https://'), null, $rule);
// Wildcard
if ($rule == '*') {
$allowed = true;
break;
} // Wildcard after domain
elseif (substr($rule, -1) == '*' && strncasecmp($parameters['image'], $rule, strlen($rule) - 1) == 0) {
$allowed = true;
break;
} // Match the start of the rule with file path
elseif (strncasecmp($rule, $parameters['image'], strlen($rule)) == 0) {
$allowed = true;
break;
} // Match subdomain wildcards
elseif (substr($rule, 0, 1) == '*' && preg_match("/(".substr((substr($rule, -1) == '*' ? rtrim($rule, "/*") : $rule), 2).")/", $param->file)) {
$allowed = true;
break;
}
}
}
if ($allowed == false) {
\Page::renderStatusCode(Page::HTTP_STATUS_FORBIDDEN);
exit(sprintf('Error: Connecting to %s is not permitted.', $parameters['image']));
}
$parameters['last_modified'] = strtotime(\Image::getHttpHeaderFieldValue($image_path, 'Last-Modified'));
// If the image is not external check to see when the image was last modified
} else {
$image_path = WORKSPACE . "/" . $parameters['image'];
$parameters['last_modified'] = is_file($image_path) ? filemtime($image_path) : null;
}
// If $this->caching is enabled, check to see that the cached file is still valid.
if ($this->caching === true) {
$cache_file = sprintf('%s/%s_%s', CACHE, md5($_GET['param'] . intval($this->settings['quality'])), basename($image_path));
// Set the cached image path, worst case scenario an image will be saved here
// if no valid cache exists.
$parameters['cached_image'] = $cache_file;
// Cache has expired or doesn't exist
if (is_file($cache_file) && (filemtime($cache_file) < $parameters['last_modified'])) {
unlink($cache_file);
} elseif (is_file($cache_file)) {
touch($cache_file);
}
}
return $image_path;
}
public function sendImageHeaders($parameters)
{
// if there is no `$last_modified` value, params should be NULL and headers
// should not be set. Otherwise, set caching headers for the browser.
if ($parameters['last_modified']) {
$last_modified_gmt = gmdate('D, d M Y H:i:s', $parameters['last_modified']) . ' GMT';
$etag = md5($parameters['last_modified'] . $image_path);
$cacheControl = 'public';
// Add no-transform in order to prevent ISPs to
// serve image over http through a compressing proxy
// See #79
if ($this->settings['disable_proxy_transform'] == 'yes') {
$cacheControl .= ', no-transform';
}
header('Last-Modified: ' . $last_modified_gmt);
header(sprintf('ETag: "%s"', $etag));
header('Cache-Control: '. $cacheControl);
} else {
$last_modified_gmt = null;
$etag = null;
}
// Check to see if the requested image needs to be generated or if a 304
// can just be returned to the browser to use it's cached version.
if ($this->caching === true && (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH']))) {
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_modified_gmt || str_replace('"', null, stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $etag) {
\Page::renderStatusCode(\Page::HTTP_NOT_MODIFIED);
exit;
}
}
}
public function fetchImage($image_path, $parameters)
{
// There is mode, or the image to JIT is external, so call `Image::load` or
// `Image::loadExternal` to load the image into the Image class
try {
$method = 'load' . ($parameters['external'] === true ? 'External' : null);
$image = call_user_func_array(array('Image', $method), array($image_path));
if (!$image instanceof \Image) {
throw new JITGenerationError('Could not load image');
}
} catch (Exception $ex) {
throw new JITGenerationError($ex->getMessage());
}
return $image;
}
/**
* Given a filter name, this function will attempt to load the filter
* from the `/filters` folder and call it's `run` method. The JIT core
* provides filters for 'crop', 'resize' and 'scale'.
*
* @param \Image $image
* @param array $parameters
* @return \Image
*/
public function applyFilterToImage(\Image $image, $parameters)
{
// Calculate the correct dimensions. If necessary, avoid upscaling the image.
$src_w = $image->Meta()->width;
$src_h = $image->Meta()->height;
if ($this->settings['disable_upscaling'] == 'yes') {
$parameters['meta']['width'] = min($parameters['settings']['width'], $src_w);
$parameters['meta']['height'] = min($parameters['settings']['height'], $src_h);
} else {
$parameters['meta']['width'] = $parameters['settings']['width'];
$parameters['meta']['height'] = $parameters['settings']['height'];
}
$filters = self::getAvailableFilters();
foreach ($filters as $filter) {
if ($filter->mode === $parameters['mode']) {
$resource = $filter->run($image, $parameters);
break;
}
}
return $resource;
}
public function cacheImage(\Image $image, $parameters)
{
// If $this->caching is enabled, and a cache file doesn't already exist,
// save the JIT image to CACHE using the Quality setting from Symphony's
// Configuration.
if (!is_file($parameters['cached_image'])) {
if (!$image->save($parameters['cached_image'], intval($this->settings['quality']))) {
throw new JITGenerationError('Error generating image, failed to create cache file.');
}
}
}
public function displayImage($image)
{
// Display the image in the browser using the Quality setting from Symphony's
// Configuration. If this fails, trigger an error.
if (!$image->display(intval($this->settings['quality']))) {
throw new JITGenerationError('Error generating image');
}
}
}
class JITException extends SymphonyErrorPage
{
public function __construct($message, $heading = 'JIT Error', $template = 'generic', array $additional = array(), $status = \Page::HTTP_STATUS_ERROR)
{
return parent::__construct($message, $heading, $template, $additional, $status);
}
}
class JITImageNotFound extends JITException
{
public function __construct($message, $heading = 'JIT Image Not Found', $template = 'generic', array $additional = array(), $status = \Page::HTTP_STATUS_NOT_FOUND)
{
return parent::__construct($message, $heading, $template, $additional, $status);
}
}
class JITGenerationError extends JITException
{
}
class JITParseParametersException extends JITException
{
}