forked from e107inc/e107
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumb.php
411 lines (326 loc) · 11 KB
/
thumb.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/*
* e107 website system
*
* Copyright (C) 2008-2010 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* On-the-fly thumbnail generator
*
* $URL$
* $Id$
*/
/**
* @package e107
* @subpackage core
* @author secretr
* @version $Id$
*
* @todo cache management - max age, max size, image cache manager (?), cron (?)
*
* On-the-fly thumbnail generator
*/
define('e107_INIT', true);
// error_reporting(E_ALL);
error_reporting(0); // suppress all errors or image will be corrupted.
ini_set('gd.jpeg_ignore_warning', 1);
//require_once './e107_handlers/benchmark.php';
//$bench = new e_benchmark();
//$bench->start();
$thumbpage = new e_thumbpage();
if(!$thumbpage->checkSrc())
{
die(' Access denied!');
}
$thumbpage->sendImage();
// Check your e_LOG folder
//$bench->end()->logResult('thumb.php', $_GET['src'].' - no cache');
exit;
class e_thumbpage
{
/**
* Page request
* @var array
*/
protected $_request = array();
/**
* @var string image source path (e107 path shortcode)
*/
protected $_src = null;
/**
* @var string source path modified/sanitized
*/
protected $_src_path = null;
/** Stores watermark prefs
*/
protected $_watermark = array();
private $_placeholder = false;
protected $_thumbQuality = null;
/**
* Constructor - init paths
* @todo FIX e107 (new folder structure), simplify all this, e.g. e107::getInstance()->initMinimal($path_to_e107_config);
*
* @return void
*/
public function __construct()
{
// initial path
$self = realpath(dirname(__FILE__));
// Config
include($self.'/e107_config.php');
// support early include feature
if(isset($CLASS2_INCLUDE) && !empty($CLASS2_INCLUDE))
{
require_once(realpath(dirname(__FILE__).'/'.$CLASS2_INCLUDE));
}
$tmp = $self.'/'.$HANDLERS_DIRECTORY;
//Core functions - now API independent
@require($tmp.'/core_functions.php');
//e107 class
@require($tmp.'/e107_class.php');
$e107_paths = compact(
'ADMIN_DIRECTORY',
'FILES_DIRECTORY',
'IMAGES_DIRECTORY',
'THEMES_DIRECTORY',
'PLUGINS_DIRECTORY',
'HANDLERS_DIRECTORY',
'LANGUAGES_DIRECTORY',
'HELP_DIRECTORY',
'DOWNLOADS_DIRECTORY',
'UPLOADS_DIRECTORY',
'MEDIA_DIRECTORY',
'CACHE_DIRECTORY',
'LOGS_DIRECTORY',
'WEB_DIRECTORY',
'SYSTEM_DIRECTORY',
'CORE_DIRECTORY'
);
// $sql_info = array(); //compact('mySQLserver', 'mySQLuser', 'mySQLpassword', 'mySQLdefaultdb', 'mySQLprefix', 'mySQLcharset');
//e107::getInstance()->initCore($e107_paths, $self, $sql_info, varset($e107_CONFIG, array()));
$e107 = e107::getInstance();
$e107->site_path = substr(md5($mySQLdefaultdb.".".$mySQLprefix),0,10);
$e107->prepare_request();
$e107->setDirs($e107_paths, varset($E107_CONFIG, array()));
$e107->set_constants();
$e107->set_paths();
$e107->file_path = $e107->fix_windows_paths($self)."/";
$e107->set_base_path();
$e107->set_request(false);
$e107->set_urls(false);
unset($tmp, $self);
// basic Admin area detection - required for proper path parsing
define('ADMIN', strpos(e_SELF, ($e107->getFolder('admin')) !== false || strpos(e_PAGE, 'admin') !== false));
$e107->set_urls(false);
$pref = $e107->getPref(); //TODO optimize/benchmark
$this->_watermark = array(
'activate' => vartrue($pref['watermark_activate'], false),
'text' => vartrue($pref['watermark_text']),
'size' => vartrue($pref['watermark_size'], 20),
'pos' => vartrue($pref['watermark_pos'],"BR"),
'color' => vartrue($pref['watermark_color'],'fff'),
'font' => vartrue($pref['watermark_font']),
'margin' => vartrue($pref['watermark_margin'],30),
'shadowcolor' => vartrue($pref['watermark_shadowcolor'], '000000'),
'opacity' => vartrue($pref['watermark_opacity'], 20)
);
$this->_thumbQuality = vartrue($pref['thumbnail_quality'],65);
// parse request
$this->parseRequest();
}
function parseRequest()
{
//echo 'e_query='.str_replace('&', '&', e_QUERY);
$e_QUERY = e_QUERY;
if(isset($_GET['id'])) // very-basic url-tampering prevention and path cloaking
{
$e_QUERY = base64_decode($_GET['id']);
}
parse_str(str_replace('&', '&', $e_QUERY), $this->_request);
// parse_str($_SERVER['QUERY_STRING'], $this->_request);
return $this;
}
function checkSrc()
{
if(!vartrue($this->_request['src'])) // display placeholder when src is missing.
{
$this->_placeholder = true;
return true;
}
$tp = e107::getParser();
// convert raw to SC path
$this->_request['src'] = str_replace($tp->getUrlConstants('raw'), $tp->getUrlConstants('sc'), $this->_request['src']);
// convert absolute and full url to SC URL
$this->_src = $tp->createConstants($this->_request['src'], 'mix');
if(preg_match('#^(https?|ftps?|file)://#i', $this->_request['src']))
{
return false;
}
if(!is_writeable(e_CACHE_IMAGE))
{
echo 'Cache folder not writeable! ';
return false;
}
// convert to relative server path
$path = $tp->replaceConstants(str_replace('..', '', $this->_src)); //should be safe enough
if(is_file($path) && is_readable($path))
{
$this->_src_path = $path;
return true;
}
else
{
$this->_placeholder = true;
return true;
}
// echo "path=".$path."<br />";
return false;
}
function sendImage()
{
//global $bench;
if($this->_placeholder == true)
{
$width = ($this->_request['aw']) ? $this->_request['aw'] : $this->_request['w'];
$height = ($this->_request['ah']) ? $this->_request['ah'] : $this->_request['h'];
$parm = array('size' => $width."x".$height);
$this->placeholder($parm);
return;
}
if(!$this->_src_path)
{
return $this;
}
$thumbnfo = pathinfo($this->_src_path);
$options = $this->getRequestOptions();
$cache_str = md5(serialize($options). $this->_src_path. $this->_thumbQuality);
$fname = strtolower('Thumb_'.$thumbnfo['filename'].'_'.$cache_str.'.'.$thumbnfo['extension']).'.cache.bin';
if(is_file(e_CACHE_IMAGE.$fname) && is_readable(e_CACHE_IMAGE.$fname))
{
$thumbnfo['lmodified'] = filemtime(e_CACHE_IMAGE.$fname);
$thumbnfo['md5s'] = md5_file(e_CACHE_IMAGE.$fname);
$thumbnfo['fsize'] = filesize(e_CACHE_IMAGE.$fname);
// Send required headers
$this->sendHeaders($thumbnfo);
// check browser cache
if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && ($thumbnfo['lmodified'] <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == $thumbnfo['md5s']))
{
header('HTTP/1.1 304 Not Modified');
//$bench->end()->logResult('thumb.php', $_GET['src'].' - 304 not modified');
exit;
}
// Send required headers
//$this->sendHeaders($thumbnfo);
@readfile(e_CACHE_IMAGE.$fname);
//$bench->end()->logResult('thumb.php', $_GET['src'].' - retrieve cache');
exit;
}
// TODO - wrap it around generic e107 thumb handler
@require(e_HANDLER.'phpthumb/ThumbLib.inc.php');
try
{
$thumb = PhpThumbFactory::create($this->_src_path);
$sizeUp = ($this->_request['w'] > 110) ? true : false; // don't resizeUp the icon images.
$thumb->setOptions(array('correctPermissions' => true, 'resizeUp' => $sizeUp, 'jpegQuality' => $this->_thumbQuality));
}
catch (Exception $e)
{
echo $e->getMessage();
return $this;
}
if(isset($this->_request['w']) || isset($this->_request['h']))
{
$thumb->resize((integer) vartrue($this->_request['w'], 0), (integer) vartrue($this->_request['h'], 0));
}
elseif(vartrue($this->_request['ah']))
{
//Typically gives a better result with images of people than adaptiveResize().
//TODO TBD Add Pref for Top, Bottom, Left, Right, Center?
$thumb->adaptiveResizeQuadrant((integer) vartrue($this->_request['aw'], 0), (integer) vartrue($this->_request['ah'], 0), 'T');
}
else
{
$thumb->adaptiveResize((integer) vartrue($this->_request['aw'], 0), (integer) vartrue($this->_request['ah'], 0));
}
// Watermark Option - See admin->MediaManager->prefs for details.
if(($this->_watermark['activate'] < $options['w']
|| $this->_watermark['activate'] < $options['aw']
|| $this->_watermark['activate'] < $options['h']
|| $this->_watermark['activate'] < $options['ah']
) && $this->_watermark['activate'] > 0 && $this->_watermark['font'] !='')
{
$tp = e107::getParser();
$this->_watermark['font'] = $tp->createConstants($this->_watermark['font'], 'mix');
$this->_watermark['font'] = realpath($tp->replaceConstants($this->_watermark['font'],'rel'));
$thumb->WatermarkText($this->_watermark);
}
// echo "hello";
//exit;
// set cache
$thumb->save(e_CACHE_IMAGE.$fname);
// show thumb
$thumb->show();
}
function getRequestOptions()
{
$ret = array();
$ret['w'] = isset($this->_request['w']) ? intval($this->_request['w']) : false;
$ret['h'] = isset($this->_request['h']) ? intval($this->_request['h']) : $ret['w'];
$ret['aw'] = isset($this->_request['aw']) ? intval($this->_request['aw']) : false;
$ret['ah'] = isset($this->_request['ah']) ? intval($this->_request['ah']) : $ret['aw'];
// $ret['wm'] = isset($this->_request['wm']) ? intval($this->_request['wm']) : $ret['wm'];
return $ret;
}
public function sendHeaders($thumbnfo)
{
if(headers_sent($filename, $linenum))
{
echo 'Headers already sent in '.$filename.' on line '.$linenum;
exit;
}
if (function_exists('date_default_timezone_set'))
{
date_default_timezone_set('UTC');
}
//header('Pragma:');
header('Cache-Control: must-revalidate');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $thumbnfo['lmodified']).' GMT');
header('Content-Length: '.$thumbnfo['fsize']);
header('Content-Disposition: filename='.$thumbnfo['basename']); // important for right-click save-as.
$ctype = self::ctype($thumbnfo['extension']);
if(null !== $ctype)
{
header('Content-Type: '.$ctype);
}
// Expire header - 1 year
$time = time() + 365 * 86400;
header('Expires: '.gmdate("D, d M Y H:i:s", $time).' GMT');
header("Etag: ".$thumbnfo['md5s']);
}
public static function ctype($ftype)
{
static $known_types = array(
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
//'bmp' => 'image/bmp',
);
$ftype = strtolower($ftype);
if(isset($known_types[$ftype]))
{
return $known_types[$ftype];
}
return null;
}
// Display a placeholder image.
function placeholder($parm)
{
$getsize = isset($parm['size']) ? $parm['size'] : '100x100';
header('location: http://placehold.it/'.$getsize);
header('Content-Length: 0');
exit();
}
}
?>