-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.gallery.php
More file actions
670 lines (622 loc) · 23.9 KB
/
Copy pathclass.gallery.php
File metadata and controls
670 lines (622 loc) · 23.9 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* imageTweak
*
* @author Ralf Hertsch <ralf.hertsch@phpmanufaktur.de>
* @link http://phpmanufaktur.de
* @copyright 2008-2013
* @license MIT License (MIT) http://www.opensource.org/licenses/MIT
*/
// include class.secure.php to protect this file and the whole CMS!
if (defined('WB_PATH')) {
if (defined('LEPTON_VERSION'))
include(WB_PATH.'/framework/class.secure.php');
}
else {
$oneback = "../";
$root = $oneback;
$level = 1;
while (($level < 10) && (!file_exists($root.'/framework/class.secure.php'))) {
$root .= $oneback;
$level += 1;
}
if (file_exists($root.'/framework/class.secure.php')) {
include($root.'/framework/class.secure.php');
}
else {
trigger_error(sprintf("[ <b>%s</b> ] Can't include class.secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
}
// end include class.secure.php
// include language file for imageTweak
if(!file_exists(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/' .LANGUAGE .'.php')) {
// default language is DE !!!
require_once(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/DE.php');
}
else {
require_once(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/' .LANGUAGE .'.php');
}
global $parser;
if (!class_exists('Dwoo')) {
// search lib_dwoo (LEPTON only)
if ( is_dir( WB_PATH.'/modules/lib_dwoo' ) ) {
// as of version 1.2, LEPTON will autocreate the $parser object; this is
// for backward compatibility
if (!is_object($parser)) {
require_once(WB_PATH.'/modules/lib_dwoo/dwoo/dwooAutoload.php');
$cache_path = WB_PATH.'/temp/cache';
if (!file_exists($cache_path)) mkdir($cache_path, 0755, true);
$compiled_path = WB_PATH.'/temp/compiled';
if (!file_exists($compiled_path)) mkdir($compiled_path, 0755, true);
$parser = new Dwoo($compiled_path,$cache_path);
set_include_path (
implode(
PATH_SEPARATOR,
array(
realpath(WB_PATH.'/modules/lib_dwoo/dwoo'),
get_include_path(),
)
)
);
}
}
// search dwoo module (WB only)
elseif( is_dir( WB_PATH.'/modules/dwoo' ) ) {
require_once(WB_PATH.'/modules/dwoo/include.php');
if (!is_object($parser)) {
$cache_path = WB_PATH.'/temp/cache';
if (!file_exists($cache_path)) mkdir($cache_path, 0755, true);
$compiled_path = WB_PATH.'/temp/compiled';
if (!file_exists($compiled_path)) mkdir($compiled_path, 0755, true);
$parser = new Dwoo($compiled_path,$cache_path);
set_include_path (
implode(
PATH_SEPARATOR,
array(
realpath(WB_PATH.'/modules/dwoo'),
get_include_path(),
)
)
);
}
}
else {
trigger_error(sprintf("[ <b>%s</b> ] Can't include Dwoo Template Engine!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
}
if (!is_object($parser)) {
$cache_path = WB_PATH.'/temp/cache';
if (!file_exists($cache_path)) mkdir($cache_path, 0755, true);
$compiled_path = WB_PATH.'/temp/compiled';
if (!file_exists($compiled_path)) mkdir($compiled_path, 0755, true);
$parser = new Dwoo($compiled_path,$cache_path);
set_include_path (
implode(PATH_SEPARATOR,
array(
realpath(WB_PATH.'/modules/dwoo'),
get_include_path(),
))
);
}
require_once(WB_PATH.'/framework/functions.php');
/**
* FilterIterator for parsing images in the /MEDIA directory
*
* @author phpManufaktur, ralf.hertsch@phpmanufaktur.de
*/
class imageExtensionFilter extends FilterIterator {
private $extensions;
/**
* Constructor imageExtensionFilter
* Specify the allowed file extensions in the array $allowed_extensions
*
* @param iterator $iterator
* @param array $allowed_extensions
*/
public function __construct($iterator, $allowed_extensions = array('jpg','png','gif','jpeg')) {
$this->setExtensions(implode('|', $allowed_extensions));
parent::__construct($iterator);
} // __construct()
/**
* @return the $extensions
*/
protected function getExtensions ()
{
return $this->extensions;
}
/**
* @param field_type $extensions
*/
protected function setExtensions ($extensions)
{
$this->extensions = $extensions;
}
/**
* Set the accepted filter to the desired extension array and to directories
* to enable recursing through the /MEDIA directory.
*
* @see FilterIterator::accept()
*/
public function accept() {
$filter = sprintf('%%.(%s)%%si', $this->getExtensions());
return (($this->current()->isFile() &&
preg_match($filter, $this->current()->getBasename()) ||
$this->current()->isDir()));
} // accept()
} // imageExtensionFilter
class imageTweakGallery {
const PARAM_CSS = 'css';
const PARAM_FOLDER = 'folder';
const PARAM_RECURSIVE = 'recursive';
const PARAM_WIDTH = 'width';
private $params = array(
self::PARAM_CSS => true,
self::PARAM_FOLDER => '',
self::PARAM_RECURSIVE => true,
self::PARAM_WIDTH => 200
);
const ZOOM_MAX_WIDTH = 800;
private $tempPath;
private $tempURL;
private $templatePath;
/**
* classes added for compatibility to imageTweak
* @see http://phpmanufaktur.de/image_tweak
*/
const CLASS_CROP = 'crop';
const CLASS_TOP = 'top';
const CLASS_BOTTOM = 'bottom';
const CLASS_LEFT = 'left';
const CLASS_RIGHT = 'right';
const CLASS_ZOOM = 'zoom';
const CLASS_NO_CACHE = 'no-cache';
public function __construct() {
$this->setTemplatePath(WB_PATH.'/modules/image_tweak/htt/');
$this->setTempPath(WB_PATH.'/temp/image_tweak/gallery/');
$this->setTempURL(WB_URL.'/temp/image_tweak/gallery/');
if (!file_exists($this->getTempPath())) {
try {
mkdir($this->getTempPath(), 0755, true);
}
catch(ErrorException $ex) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(tweak_error_mkdir, $this->getTempPath(), $ex->getMessage())));
return false;
}
}
} // __construct()
/**
* @return the $templatePath
*/
protected function getTemplatePath ()
{
return $this->templatePath;
}
/**
* @param field_type $templatePath
*/
protected function setTemplatePath ($templatePath)
{
$this->templatePath = $templatePath;
}
/**
* @return the $tempURL
*/
protected function getTempURL ()
{
return $this->tempURL;
}
/**
* @param field_type $tempURL
*/
protected function setTempURL ($tempURL)
{
$this->tempURL = $tempURL;
}
/**
* @return the $tempPath
*/
protected function getTempPath ()
{
return $this->tempPath;
}
/**
* @param field_type $tempPath
*/
protected function setTempPath ($tempPath)
{
$this->tempPath = $tempPath;
}
/**
* Return the params available for the droplet [[kit_idea]] as array
*
* @return ARRAY $params
*/
public function getParams ()
{
return $this->params;
} // getParams()
/**
* Set the params for the droplet [[gallery]]
*
* @param ARRAY $params
* @return BOOL
*/
public function setParams($params = array()) {
$this->params = $params;
$this->params[self::PARAM_FOLDER] = $this->removeLeadingSlash($this->params[self::PARAM_FOLDER]);
return true;
} // setParams()
/**
* Set $this->error to $error
*
* @param STR $error
*/
public function setError($error) {
$this->error = $error;
} // setError()
/**
* Get Error from $this->error;
*
* @return STR $this->error
*/
public function getError() {
return $this->error;
} // getError()
/**
* Check if $this->error is empty
*
* @return BOOL
*/
public function isError() {
return (bool) !empty($this->error);
} // isError
/**
* Process the desired template and returns the result as string
*
* @param STR $template
* @param ARRAY $template_data
* @return STR $result
*/
public function getTemplate($template, $template_data) {
global $parser;
try {
$result = $parser->get($this->getTemplatePath().$template, $template_data);
} catch (Exception $e) {
$this->setError(sprintf(TSG_ERROR_TEMPLATE_ERROR, $template, $e->getMessage()));
return false;
}
return $result;
} // getTemplate()
/**
* removes a leading backslash from $path
*
* @param string $path
* @return string $path
*/
public function removeLeadingSlash($path) {
$path = substr($path, 0, 1) == DIRECTORY_SEPARATOR ? substr($path, 1, strlen($path)) : $path;
return $path;
}
/**
* Haengt einen Slash an das Ende des uebergebenen Strings
* wenn das letzte Zeichen noch kein Slash ist
*
* @param string $path
* @return string
*/
public function addSlash($path) {
$path = substr($path, strlen($path)-1, 1) == DIRECTORY_SEPARATOR ? $path : $path.DIRECTORY_SEPARATOR;
return $path;
}
/**
* Action handler of the class
*
* @return string formatted gallery on success or error prompt
*/
public function action() {
if (empty($this->params[self::PARAM_FOLDER])) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, tweak_error_param_folder_missing));
return $this->show();
}
$images_path = WB_PATH.MEDIA_DIRECTORY.DIRECTORY_SEPARATOR.$this->params[self::PARAM_FOLDER];
$images_path = $this->addSlash($images_path);
if (!file_exists($images_path)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(tweak_error_param_folder_invalid, $this->params[self::PARAM_FOLDER])));
return $this->show();
}
$temp_path_preview = $this->getTempPath().$this->params[self::PARAM_FOLDER].'/preview/';
$temp_url_preview = $this->getTempURL().$this->params[self::PARAM_FOLDER].'/preview/';
if (!file_exists($temp_path_preview)) {
try {
mkdir($temp_path_preview, 0755, true);
}
catch(ErrorException $ex) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(tweak_error_mkdir, $temp_path_preview, $ex->getMessage())));
return $this->show();
}
}
$temp_path_zoom = $this->getTempPath().$this->params[self::PARAM_FOLDER].'/zoom/';
$temp_url_zoom = $this->getTempURL().$this->params[self::PARAM_FOLDER].'/zoom/';
if (!file_exists($temp_path_zoom)) {
try {
mkdir($temp_path_zoom, 0755, true);
}
catch(ErrorException $ex) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(tweak_error_mkdir, $temp_path_zoom, $ex->getMessage())));
return $this->show();
}
}
$first = '';
$links = '';
$start = true;
$description = false;
$description_array = array();
$start_file = '';
$items = array();
if (file_exists($images_path.'images.lst')) {
if (false === ($fa = file($images_path.'images.lst', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES))) {
// error reading file
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(tweak_error_reading_file, 'images.lst')));
return $this->show();
}
foreach ($fa as $line) {
$x = substr_count($line, '|');
if ($x == 1) {
list($file, $title) = explode('|', $line);
$description_array[trim($file)] = trim($title);
}
elseif ($x == 2) {
list($file, $title, $start) = explode('|', $line);
$description_array[trim($file)] = trim($title);
if ((strtolower(trim($start)) == 'true') && (file_exists($images_path.$file))) {
$start_file = trim($file);
}
}
}
}
$iterator = new imageExtensionFilter(new RecursiveDirectoryIterator($images_path));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
list($width, $height, $type) = getimagesize($fileinfo->__toString());
$rewrite = false;
if (file_exists($temp_path_preview.$fileinfo->getBasename())) {
// compare size and date
list($previewWidth, $previewHeight) = getimagesize($temp_path_preview.$fileinfo->getBasename());
if ($previewWidth != $this->params[self::PARAM_WIDTH]) $rewrite = true;
if (filemtime($temp_path_preview.$fileinfo->getBasename()) != $fileinfo->getMTime()) $rewrite = true;
}
// create preview image
if (!file_exists($temp_path_preview.$fileinfo->getBasename()) || $rewrite) {
// create preview image
if ($width > $this->params[self::PARAM_WIDTH]) {
// calculate size for icon
$percent = (int) ($this->params[self::PARAM_WIDTH]/($width/100));
$previewWidth = $this->params[self::PARAM_WIDTH];
$previewHeight = (int) ($height/(100)*$percent);
}
else {
// use orginal image dimensions
$previewWidth = $width;
$previewHeight = $height;
}
if (false == ($tweaked_file = $this->createTweakedFile(
$fileinfo->getBasename(),
strtolower(substr($fileinfo->getBasename(), strrpos($fileinfo->getBasename(), '.')+1)),
$fileinfo->__toString(),
$previewWidth,
$previewHeight,
$width,
$height,
$fileinfo->getMTime(),
$temp_path_preview))) {
// error creating the tweaked file
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $this->getError()));
return $this->show();
}
}
// create zoom image
if (!file_exists($temp_path_zoom.$fileinfo->getBasename()) || $rewrite) {
// create preview image
if ($width > self::ZOOM_MAX_WIDTH) {
// calculate size for icon
$percent = (int) (self::ZOOM_MAX_WIDTH/($width/100));
$zoomWidth = self::ZOOM_MAX_WIDTH;
$zoomHeight = (int) ($height/(100)*$percent);
}
else {
// use orginal image dimensions
$zoomWidth = $width;
$zoomHeight = $height;
}
if (false == ($tweaked_file = $this->createTweakedFile(
$fileinfo->getBasename(),
strtolower(substr($fileinfo->getBasename(), strrpos($fileinfo->getBasename(), '.')+1)),
$fileinfo->__toString(),
$zoomWidth,
$zoomHeight,
$width,
$height,
$fileinfo->getMTime(),
$temp_path_zoom))) {
// error creating the tweaked file
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $this->getError()));
return $this->show();
}
}
else {
// get width and height from zoom image
list($zoomWidth, $zoomHeight) = getimagesize($temp_path_zoom.$fileinfo->getBasename());
}
$items[] = array(
'is_first' => ($start && (empty($start_file)) || ($fileinfo->getBasename() == $start_file)) ? 1 : 0,
'class' => media_filename($this->params[self::PARAM_FOLDER]),
'title' => isset($description_array[$fileinfo->getBasename()]) ? $description_array[$fileinfo->getBasename()] : '',
'zoom' => array(
'url' => $temp_url_zoom.$fileinfo->getBasename(),
'width' => $zoomWidth,
'height' => $zoomHeight
),
'preview' => array(
'url' => $temp_url_preview.$fileinfo->getBasename(),
'width' => $previewWidth,
'height' => $previewHeight
)
);
// set start tag to false ...
if ($start && (empty($start_file))) $start = false;
}
}
return $this->show($items);
} // action()
/**
* prompt the formatted result
*
* @param STR $content - content to show
*
* @return STR dialog
*/
public function show($data=array()) {
if ($this->isError() || !is_array($data)) {
$data = array(
'is_error' => 1,
'error' => $this->getError()
);
}
else {
$data = array(
'is_error' => 0,
'gallery' => $data
);
}
// return the result
return $this->getTemplate('frontend.gallery.lte', $data);
} // show_main()
/**
* Master routine from imageTweak to create optimized images.
* @see http://phpmanufaktur.de/image_tweak
*
* @param string $filename - basename of the image
* @param string $extension - extension of the image
* @param string $file_path - complete path to the image
* @param integer $new_width - the new width in pixel
* @param integer $new_height - the new height in pixel
* @param integer $origin_width - the original width in pixel
* @param integer $origin_height - the original height in pixel
* @param integer $origin_filemtime - the FileMTime of the image
* @param string $new_path - the path to the tweaked image
* @param array $classes - optional es to force image operations
* @return mixed - path to the new file on succes, boolean false on error
*/
public function createTweakedFile($filename, $extension, $file_path,
$new_width, $new_height, $origin_width, $origin_height,
$origin_filemtime, $new_path, $classes=array()) {
$extension = strtolower($extension);
switch ($extension):
case 'gif':
$origin_image = imagecreatefromgif($file_path);
break;
case 'jpeg':
case 'jpg':
$origin_image = imagecreatefromjpeg($file_path);
break;
case 'png':
$origin_image = imagecreatefrompng($file_path);
break;
default:
// unsupported image type
echo $extension;
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(TSG_ERROR_TWEAK_INVALID_EXTENSION, $extension)));
return false;
endswitch;
// create new image of $new_width and $new_height
$new_image = imagecreatetruecolor($new_width, $new_height);
// Check if this image is PNG or GIF, then set if Transparent
if (($extension == 'gif') OR ($extension == 'png')) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $new_width, $new_height, $transparent);
}
if (in_array(self::CLASS_CROP, $classes)) {
// don't change image size...
$zoom = 100;
foreach ($classes as $class) {
if (stripos($class, self::CLASS_ZOOM.'[') !== false) {
$x = substr($class, strpos($class, '[')+1, (strpos($class, ']') - (strpos($class, '[')+1)));
$zoom = (int) $x;
if ($zoom < 1) $zoom = 1;
if ($zoom > 100) $zoom = 100;
}
}
// crop image
if (in_array(self::CLASS_LEFT, $classes)) {
$x_pos = 0;
}
elseif (in_array(self::CLASS_RIGHT, $classes)) {
$x_pos = $origin_width-$new_width;
}
else {
$x_pos = ((int) $origin_width/2)-((int) $new_width/2);
}
if (in_array(self::CLASS_TOP, $classes)) {
$y_pos = 0;
}
elseif (in_array(self::CLASS_BOTTOM, $classes)) {
$y_pos = $origin_height-$new_height;
}
else {
$y_pos = ((int) $origin_height/2) - ((int)$new_height/2);
}
if ($zoom !== 100) {
// change image size and crop image
$faktor = $zoom/100;
$zoom_width = (int) ($origin_width*$faktor);
$zoom_height = (int) ($origin_height*$faktor);
imagecopyresampled($new_image, $origin_image, 0, 0, $x_pos, $y_pos, $new_width, $new_height, $zoom_width, $zoom_height);
}
else {
// only crop image
imagecopy($new_image, $origin_image, 0, 0, $x_pos, $y_pos, $new_width, $new_height);
}
}
else {
// resample image
imagecopyresampled($new_image, $origin_image, 0, 0, 0, 0, $new_width, $new_height, $origin_width, $origin_height);
}
if (!file_exists($new_path)) {
try {
mkdir($new_path, 0755, true);
}
catch(ErrorException $ex) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(TSG_ERROR_MKDIR, $new_path, $ex->getMessage())));
return false;
}
}
$new_file = $new_path.$filename;
//Generate the file, and rename it to $newfilename
switch ($extension):
case 'gif':
imagegif($new_image, $new_file);
break;
case 'jpg':
case 'jpeg':
imagejpeg($new_image, $new_file, 90); // static setting for the JPEG Quality
break;
case 'png':
imagepng($new_image, $new_file);
break;
default:
// unsupported image type
return false;
endswitch;
if (!chmod($new_file, 0644)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(TSG_ERROR_CHMOD, basename($new_file))));
return false;
}
if (($origin_filemtime !== false) && (touch($new_file, $origin_filemtime) === false)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(TSG_ERROR_TOUCH, basename($new_file))));
return false;
}
return $new_file;
} // createTweakedFile()
} // class imageTweakGallery