Total Complexity | 96 |
Total Lines | 626 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ImageManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ImageManager |
||
21 | { |
||
22 | /** |
||
23 | * Configuration array. |
||
24 | */ |
||
25 | public $config; |
||
26 | |||
27 | /** |
||
28 | * Array of directory information. |
||
29 | */ |
||
30 | public $dirs; |
||
31 | |||
32 | /** |
||
33 | * Constructor. Create a new Image Manager instance. |
||
34 | * @param array $config configuration array, see config.inc.php |
||
35 | */ |
||
36 | public function __construct($config) |
||
37 | { |
||
38 | $this->config = $config; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get the images base directory. |
||
43 | * @return string base dir, see config.inc.php |
||
44 | */ |
||
45 | public function getImagesDir() |
||
46 | { |
||
47 | Return $this->config['images_dir']; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get the images base URL. |
||
52 | * @return string base url, see config.inc.php |
||
53 | */ |
||
54 | public function getImagesURL() |
||
57 | } |
||
58 | |||
59 | public function isValidBase() |
||
60 | { |
||
61 | return is_dir($this->getImagesDir()); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the tmp file prefix. |
||
66 | * @return string tmp file prefix. |
||
67 | */ |
||
68 | public function getTmpPrefix() |
||
69 | { |
||
70 | Return $this->config['tmp_prefix']; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the sub directories in the base dir. |
||
75 | * Each array element contain |
||
76 | * the relative path (relative to the base dir) as key and the |
||
77 | * full path as value. |
||
78 | * @return array of sub directries |
||
79 | * <code>array('path name' => 'full directory path', ...)</code> |
||
80 | */ |
||
81 | public function getDirs() |
||
82 | { |
||
83 | if (is_null($this->dirs)) { |
||
84 | $dirs = $this->_dirs($this->getImagesDir(), '/'); |
||
85 | ksort($dirs); |
||
86 | $this->dirs = $dirs; |
||
87 | } |
||
88 | return $this->dirs; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Recursively travese the directories to get a list |
||
93 | * of accessable directories. |
||
94 | * @param string $base the full path to the current directory |
||
95 | * @param string $path the relative path name |
||
96 | * @return array of accessiable sub-directories |
||
97 | * <code>array('path name' => 'full directory path', ...)</code> |
||
98 | */ |
||
99 | public function _dirs($base, $path) |
||
100 | { |
||
101 | $base = Files::fixPath($base); |
||
|
|||
102 | $dirs = []; |
||
103 | |||
104 | if (false == $this->isValidBase()) { |
||
105 | return $dirs; |
||
106 | } |
||
107 | |||
108 | $d = @dir($base); |
||
109 | |||
110 | while (false !== ($entry = $d->read())) { |
||
111 | //If it is a directory, and it doesn't start with |
||
112 | // a dot, and if is it not the thumbnail directory |
||
113 | if (is_dir($base . $entry) |
||
114 | && '.' != substr($entry, 0, 1) |
||
115 | && false == $this->isThumbDir($entry)) { |
||
116 | $relative = Files::fixPath($path . $entry); |
||
117 | $fullpath = Files::fixPath($base . $entry); |
||
118 | $dirs[$relative] = $fullpath; |
||
119 | $dirs = array_merge($dirs, $this->_dirs($fullpath, $relative)); |
||
120 | } |
||
121 | } |
||
122 | $d->close(); |
||
123 | |||
124 | Return $dirs; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get all the files and directories of a relative path. |
||
129 | * @param string $path relative path to be base path. |
||
130 | * @return array of file and path information. |
||
131 | * <code>array(0=>array('relative'=>'fullpath',...), 1=>array('filename'=>fileinfo array(),...)</code> |
||
132 | * fileinfo array: <code>array('url'=>'full url', |
||
133 | * 'relative'=>'relative to base', |
||
134 | * 'fullpath'=>'full file path', |
||
135 | * 'image'=>imageInfo array() false if not image, |
||
136 | * 'stat' => filestat)</code> |
||
137 | */ |
||
138 | public function getFiles($path) |
||
139 | { |
||
140 | $files = []; |
||
141 | $dirs = []; |
||
142 | |||
143 | if (false == $this->isValidBase()) { |
||
144 | return [$files, $dirs]; |
||
145 | } |
||
146 | |||
147 | $path = Files::fixPath($path); |
||
148 | $base = Files::fixPath($this->getImagesDir()); |
||
149 | $fullpath = Files::makePath($base, $path); |
||
150 | |||
151 | $d = @dir($fullpath); |
||
152 | |||
153 | while (false !== ($entry = $d->read())) { |
||
154 | //not a dot file or directory |
||
155 | if ('.' != substr($entry, 0, 1)) { |
||
156 | if (is_dir($fullpath . $entry) |
||
157 | && false == $this->isThumbDir($entry)) { |
||
158 | $relative = Files::fixPath($path . $entry); |
||
159 | $full = Files::fixPath($fullpath . $entry); |
||
160 | $count = $this->countFiles($full); |
||
161 | $dirs[$relative] = ['fullpath' => $full, 'entry' => $entry, 'count' => $count]; |
||
162 | } elseif (is_file($fullpath . $entry) && false == $this->isThumb($entry) && false == $this->isTmpFile($entry)) { |
||
163 | $img = $this->getImageInfo($fullpath . $entry); |
||
164 | |||
165 | if (!(!is_array($img) && $this->config['validate_images'])) { |
||
166 | $file['url'] = Files::makePath($this->config['base_url'], $path) . $entry; |
||
167 | $file['relative'] = $path . $entry; |
||
168 | $file['fullpath'] = $fullpath . $entry; |
||
169 | $file['image'] = $img; |
||
170 | $file['stat'] = stat($fullpath . $entry); |
||
171 | $files[$entry] = $file; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | $d->close(); |
||
177 | ksort($dirs); |
||
178 | ksort($files); |
||
179 | |||
180 | Return [$dirs, $files]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Count the number of files and directories in a given folder |
||
185 | * minus the thumbnail folders and thumbnails. |
||
186 | */ |
||
187 | public function countFiles($path) |
||
188 | { |
||
189 | $total = 0; |
||
190 | |||
191 | if (is_dir($path)) { |
||
192 | $d = @dir($path); |
||
193 | |||
194 | while (false !== ($entry = $d->read())) { |
||
195 | //echo $entry."<br>"; |
||
196 | if ('.' != substr($entry, 0, 1) |
||
197 | && false == $this->isThumbDir($entry) |
||
198 | && false == $this->isTmpFile($entry) |
||
199 | && false == $this->isThumb($entry)) { |
||
200 | $total++; |
||
201 | } |
||
202 | } |
||
203 | $d->close(); |
||
204 | } |
||
205 | return $total; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Get image size information. |
||
210 | * @param string $file the image file |
||
211 | * @return array of getImageSize information, |
||
212 | * false if the file is not an image. |
||
213 | */ |
||
214 | public function getImageInfo($file) |
||
215 | { |
||
216 | Return @getImageSize($file); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Check if the file contains the thumbnail prefix. |
||
221 | * @param string $file filename to be checked |
||
222 | * @return true if the file contains the thumbnail prefix, false otherwise. |
||
223 | */ |
||
224 | public function isThumb($file) |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Check if the given directory is a thumbnail directory. |
||
236 | * @param string $entry directory name |
||
237 | * @return true if it is a thumbnail directory, false otherwise |
||
238 | */ |
||
239 | public function isThumbDir($entry) |
||
240 | { |
||
241 | if (false == $this->config['thumbnail_dir'] |
||
242 | || 0 == strlen(trim($this->config['thumbnail_dir']))) { |
||
243 | Return false; |
||
244 | } else { |
||
245 | Return ($entry == $this->config['thumbnail_dir']); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Check if the given file is a tmp file. |
||
251 | * @param string $file file name |
||
252 | * @return bool true if it is a tmp file, false otherwise |
||
253 | */ |
||
254 | public function isTmpFile($file) |
||
255 | { |
||
256 | $len = strlen($this->config['tmp_prefix']); |
||
257 | if (substr($file, 0, $len) == $this->config['tmp_prefix']) { |
||
258 | Return true; |
||
259 | } else { |
||
260 | Return false; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * For a given image file, get the respective thumbnail filename |
||
266 | * no file existence check is done. |
||
267 | * @param string $fullpathfile the full path to the image file |
||
268 | * @return string of the thumbnail file |
||
269 | */ |
||
270 | public function getThumbName($fullpathfile) |
||
271 | { |
||
272 | $path_parts = pathinfo($fullpathfile); |
||
273 | |||
274 | $thumbnail = $this->config['thumbnail_prefix'] . $path_parts['basename']; |
||
275 | |||
276 | if (0 == strlen(trim($this->config['thumbnail_dir'])) || true == $this->config['safe_mode']) { |
||
277 | Return Files::makeFile($path_parts['dirname'], $thumbnail); |
||
278 | } else { |
||
279 | $path = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']); |
||
280 | if (!is_dir($path)) { |
||
281 | Files::createFolder($path); |
||
282 | } |
||
283 | Return Files::makeFile($path, $thumbnail); |
||
284 | } |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Similar to getThumbName, but returns the URL, base on the |
||
289 | * given base_url in config.inc.php |
||
290 | * @param string $relative the relative image file name, |
||
291 | * relative to the base_dir path |
||
292 | * @return string the url of the thumbnail |
||
293 | */ |
||
294 | public function getThumbURL($relative) |
||
295 | { |
||
296 | _ddt(__FILE__, __LINE__, "getThumbURL(): relative is '$relative'"); |
||
297 | |||
298 | $path_parts = pathinfo($relative); |
||
299 | $thumbnail = $this->config['thumbnail_prefix'] . $path_parts['basename']; |
||
300 | if ('\\' == $path_parts['dirname']) { |
||
301 | $path_parts['dirname'] = '/'; |
||
302 | } |
||
303 | |||
304 | if (true == $this->config['safe_mode'] |
||
305 | || 0 == strlen(trim($this->config['thumbnail_dir']))) { |
||
306 | Return Files::makeFile($this->getImagesURL(), rawurlencode($thumbnail)); |
||
307 | } else { |
||
308 | if (strlen(trim($this->config['thumbnail_dir'])) > 0) { |
||
309 | $path = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']); |
||
310 | $url_path = Files::makePath($this->getImagesURL(), $path); |
||
311 | |||
312 | _ddt(__FILE__, __LINE__, "getThumbURL(): url_path is '$url_path'"); |
||
313 | |||
314 | Return Files::makeFile($url_path, rawurlencode($thumbnail)); |
||
315 | } else //should this ever happen? |
||
316 | { |
||
317 | //error_log('ImageManager: Error in creating thumbnail url'); |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * For a given image file, get the respective resized filename |
||
324 | * no file existence check is done. |
||
325 | * @param string $fullpathfile the full path to the image file |
||
326 | * @param int $width the intended width |
||
327 | * @param int $height the intended height |
||
328 | * @param bool $mkDir whether to attempt to make the resized_dir if it doesn't exist |
||
329 | * @return string of the resized filename |
||
330 | */ |
||
331 | public function getResizedName($fullpathfile, $width, $height, $mkDir = true) |
||
332 | { |
||
333 | $path_parts = pathinfo($fullpathfile); |
||
334 | |||
335 | $thumbnail = $this->config['resized_prefix'] . "_{$width}x{$height}_{$path_parts['basename']}"; |
||
336 | |||
337 | if (0 == strlen(trim($this->config['resized_dir'])) || true == $this->config['safe_mode']) { |
||
338 | Return Files::makeFile($path_parts['dirname'], $thumbnail); |
||
339 | } else { |
||
340 | $path = Files::makePath($path_parts['dirname'], $this->config['resized_dir']); |
||
341 | if ($mkDir && !is_dir($path)) { |
||
342 | Files::createFolder($path); |
||
343 | } |
||
344 | Return Files::makeFile($path, $thumbnail); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Check if the given path is part of the subdirectories |
||
350 | * under the base_dir. |
||
351 | * @param string $path the relative path to be checked |
||
352 | * @return bool true if the path exists, false otherwise |
||
353 | */ |
||
354 | public function validRelativePath($path) |
||
355 | { |
||
356 | $dirs = $this->getDirs(); |
||
357 | if ('/' == $path) { |
||
358 | Return true; |
||
359 | } |
||
360 | //check the path given in the url against the |
||
361 | //list of paths in the system. |
||
362 | for ($i = 0; $i < count($dirs); $i++) { |
||
363 | $key = key($dirs); |
||
364 | //we found the path |
||
365 | if ($key == $path) { |
||
366 | Return true; |
||
367 | } |
||
368 | |||
369 | next($dirs); |
||
370 | } |
||
371 | Return false; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Process uploaded files, assumes the file is in |
||
376 | * $_FILES['upload'] and $_POST['dir'] is set. |
||
377 | * The dir must be relative to the base_dir and exists. |
||
378 | * If 'validate_images' is set to true, only file with |
||
379 | * image dimensions will be accepted. |
||
380 | * @return null |
||
381 | */ |
||
382 | public function processUploads() |
||
399 | } |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Process upload files. The file must be an |
||
404 | * uploaded file. If 'validate_images' is set to |
||
405 | * true, only images will be processed. Any duplicate |
||
406 | * file will be renamed. See Files::copyFile for details |
||
407 | * on renaming. |
||
408 | * @param string $relative the relative path where the file |
||
409 | * should be copied to. |
||
410 | * @param array $file the uploaded file from $_FILES |
||
411 | * @return bool true if the file was processed successfully, |
||
412 | * false otherwise |
||
413 | */ |
||
414 | public function _processFiles($relative, $file) |
||
415 | { |
||
416 | if (0 != $file['error']) { |
||
417 | Return false; |
||
418 | } |
||
419 | |||
420 | if (!is_file($file['tmp_name'])) { |
||
421 | Return false; |
||
422 | } |
||
423 | |||
424 | if (!is_uploaded_file($file['tmp_name'])) { |
||
425 | Files::delFile($file['tmp_name']); |
||
426 | Return false; |
||
427 | } |
||
428 | |||
429 | if (true == $this->config['validate_images']) { |
||
430 | $imgInfo = @getImageSize($file['tmp_name']); |
||
431 | if (!is_array($imgInfo)) { |
||
432 | Files::delFile($file['tmp_name']); |
||
433 | Return false; |
||
434 | } |
||
435 | } |
||
436 | |||
437 | $valid_extensions = $this->config['allowed_image_extensions']; |
||
438 | $afruext = strtolower(substr(strrchr($file['name'], '.'), 1)); |
||
439 | if (!in_array($afruext, $valid_extensions)) { |
||
440 | Files::delFile($file['tmp_name']); |
||
441 | Return 'Cannot upload $extension=' . $afruext . '$ Files. Permission denied.'; |
||
442 | } |
||
443 | |||
444 | //now copy the file |
||
445 | $path = Files::makePath($this->getImagesDir(), $relative); |
||
446 | $result = Files::copyFile($file['tmp_name'], $path, $file['name']); |
||
447 | |||
448 | //no copy error |
||
449 | if (!is_int($result)) { |
||
450 | Files::delFile($file['tmp_name']); |
||
451 | Return true; |
||
452 | } |
||
453 | |||
454 | //delete tmp files. |
||
455 | Files::delFile($file['tmp_name']); |
||
456 | Return false; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Get the URL of the relative file. |
||
461 | * basically appends the relative file to the |
||
462 | * base_url given in config.inc.php |
||
463 | * @param string $relative a file the relative to the base_dir |
||
464 | * @return string the URL of the relative file. |
||
465 | */ |
||
466 | public function getFileURL($relative) |
||
467 | { |
||
468 | Return Files::makeFile($this->getImagesURL(), $relative); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Get the fullpath to a relative file. |
||
473 | * @param string $relative the relative file. |
||
474 | * @return string the full path, .ie. the base_dir + relative. |
||
475 | */ |
||
476 | public function getFullPath($relative) |
||
477 | { |
||
478 | Return Files::makeFile($this->getImagesDir(), $relative);; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Get the default thumbnail. |
||
483 | * @return string default thumbnail, empty string if |
||
484 | * the thumbnail doesn't exist. |
||
485 | */ |
||
486 | public function getDefaultThumb() |
||
487 | { |
||
488 | // FIXME: hack |
||
489 | |||
490 | Return $this->config['default_thumbnail']; |
||
491 | |||
492 | if (is_file($this->config['default_thumbnail'])) { |
||
493 | Return $this->config['default_thumbnail']; |
||
494 | } else { |
||
495 | Return ''; |
||
496 | } |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Get the thumbnail url to be displayed. |
||
501 | * If the thumbnail exists, and it is up-to-date |
||
502 | * the thumbnail url will be returns. If the |
||
503 | * file is not an image, a default image will be returned. |
||
504 | * If it is an image file, and no thumbnail exists or |
||
505 | * the thumbnail is out-of-date (i.e. the thumbnail |
||
506 | * modified time is less than the original file) |
||
507 | * then a thumbs.php?img=filename.jpg is returned. |
||
508 | * The thumbs.php url will generate a new thumbnail |
||
509 | * on the fly. If the image is less than the dimensions |
||
510 | * of the thumbnails, the image will be display instead. |
||
511 | * @param string $relative the relative image file. |
||
512 | * @return string the url of the thumbnail, be it |
||
513 | * actually thumbnail or a script to generate the |
||
514 | * thumbnail on the fly. |
||
515 | */ |
||
516 | public function getThumbnail($relative) |
||
517 | { |
||
518 | global $IMConfig; |
||
519 | |||
520 | _ddt(__FILE__, __LINE__, "getThumbnail(): top with '$relative'"); |
||
521 | |||
522 | $fullpath = Files::makeFile($this->getImagesDir(), $relative); |
||
523 | |||
524 | //not a file??? |
||
525 | if (!is_file($fullpath)) { |
||
526 | Return $this->getDefaultThumb(); |
||
527 | } |
||
528 | |||
529 | $imgInfo = @getImageSize($fullpath); |
||
530 | |||
531 | //not an image |
||
532 | if (!is_array($imgInfo)) { |
||
533 | Return $this->getDefaultThumb(); |
||
534 | } |
||
535 | |||
536 | //the original image is smaller than thumbnails, |
||
537 | //so just return the url to the original image. |
||
538 | if ($imgInfo[0] <= $this->config['thumbnail_width'] |
||
539 | && $imgInfo[1] <= $this->config['thumbnail_height']) { |
||
540 | Return $this->getFileURL($relative); |
||
541 | } |
||
542 | |||
543 | $thumbnail = $this->getThumbName($fullpath); |
||
544 | |||
545 | //check for thumbnails, if exists and |
||
546 | // it is up-to-date, return the thumbnail url |
||
547 | if (is_file($thumbnail)) { |
||
548 | if (filemtime($thumbnail) >= filemtime($fullpath)) { |
||
549 | _ddt(__FILE__, __LINE__, "getThumbnail(): returning url '" . $this->getThumbURL($relative) . "'"); |
||
550 | |||
551 | Return $this->getThumbURL($relative); |
||
552 | } |
||
553 | } |
||
554 | |||
555 | //well, no thumbnail was found, so ask the thumbs.php |
||
556 | //to generate the thumbnail on the fly. |
||
557 | Return $IMConfig['backend_url'] . '__function=thumbs&img=' . rawurlencode($relative); |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Delete and specified files. |
||
562 | * @return bool true if delete, false otherwise |
||
563 | */ |
||
564 | public function deleteFiles() |
||
565 | { |
||
566 | if (isset($_GET['delf'])) { |
||
567 | $this->_delFile(rawurldecode($_GET['delf'])); |
||
568 | } |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Delete and specified directories. |
||
573 | * @return bool true if delete, false otherwise |
||
574 | */ |
||
575 | public function deleteDirs() |
||
581 | } |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Delete the relative file, and any thumbnails. |
||
586 | * @param string $relative the relative file. |
||
587 | * @return bool true if deleted, false otherwise. |
||
588 | */ |
||
589 | public function _delFile($relative) |
||
606 | } |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Delete directories recursively. |
||
611 | * @param string $relative the relative path to be deleted. |
||
612 | * @return bool true if deleted, false otherwise. |
||
613 | */ |
||
614 | public function _delDir($relative) |
||
615 | { |
||
616 | $fullpath = Files::makePath($this->getImagesDir(), $relative); |
||
617 | if ($this->countFiles($fullpath) <= 0) { |
||
618 | return Files::delFolder($fullpath, true); |
||
619 | } //delete recursively. |
||
620 | else { |
||
621 | Return false; |
||
622 | } |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Create new directories. |
||
627 | * If in safe_mode, nothing happens. |
||
628 | * @return bool true if created, false otherwise. |
||
629 | */ |
||
630 | public function processNewDir() |
||
646 | } |
||
647 | } |
||
648 | } |
||
649 | |||
650 | |||
651 |