Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ExtendedFileManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ExtendedFileManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ExtendedFileManager |
||
23 | { |
||
24 | /** |
||
25 | * Configuration array. |
||
26 | */ |
||
27 | var $config; |
||
28 | |||
29 | /** |
||
30 | * Array of directory information. |
||
31 | */ |
||
32 | var $dirs; |
||
33 | |||
34 | /** |
||
35 | * Manager mode - image | link |
||
36 | */ |
||
37 | var $mode; |
||
38 | |||
39 | /** |
||
40 | * Constructor. Create a new Image Manager instance. |
||
41 | * @param array $config configuration array, see config.inc.php |
||
42 | */ |
||
43 | function ExtendedFileManager($config, $mode = null) |
||
|
|||
44 | { |
||
45 | $this->config = $config; |
||
46 | |||
47 | $this->mode = empty($mode) ? (empty($config['insert_mode']) ? 'image' : $config['insert_mode']): $mode; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get the base directory. |
||
52 | * @return string base dir, see config.inc.php |
||
53 | */ |
||
54 | function getImagesDir() |
||
55 | { |
||
56 | if ($this->mode == 'link' && isset($this->config['files_dir'])) |
||
57 | Return $this->config['files_dir']; |
||
58 | else Return $this->config['images_dir']; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get the base URL. |
||
63 | * @return string base url, see config.inc.php |
||
64 | */ |
||
65 | function getImagesURL() |
||
66 | { |
||
67 | if ($this->mode == 'link' && isset($this->config['files_url'])) |
||
68 | Return $this->config['files_url']; |
||
69 | else Return $this->config['images_url']; |
||
70 | } |
||
71 | |||
72 | function isValidBase() |
||
73 | { |
||
74 | return is_dir($this->getImagesDir()); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the tmp file prefix. |
||
79 | * @return string tmp file prefix. |
||
80 | */ |
||
81 | function getTmpPrefix() |
||
82 | { |
||
83 | Return $this->config['tmp_prefix']; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get the sub directories in the base dir. |
||
88 | * Each array element contain |
||
89 | * the relative path (relative to the base dir) as key and the |
||
90 | * full path as value. |
||
91 | * @return array of sub directries |
||
92 | * <code>array('path name' => 'full directory path', ...)</code> |
||
93 | */ |
||
94 | function getDirs() |
||
95 | { |
||
96 | if(is_null($this->dirs)) |
||
97 | { |
||
98 | $dirs = $this->_dirs($this->getImagesDir(),'/'); |
||
99 | ksort($dirs); |
||
100 | $this->dirs = $dirs; |
||
101 | } |
||
102 | return $this->dirs; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Recursively travese the directories to get a list |
||
107 | * of accessable directories. |
||
108 | * @param string $base the full path to the current directory |
||
109 | * @param string $path the relative path name |
||
110 | * @return array of accessiable sub-directories |
||
111 | * <code>array('path name' => 'full directory path', ...)</code> |
||
112 | */ |
||
113 | function _dirs($base, $path) |
||
114 | { |
||
115 | $base = Files::fixPath($base); |
||
116 | $dirs = array(); |
||
117 | |||
118 | if($this->isValidBase() == false) |
||
119 | return $dirs; |
||
120 | |||
121 | $d = @dir($base); |
||
122 | |||
123 | while (false !== ($entry = $d->read())) |
||
124 | { |
||
125 | //If it is a directory, and it doesn't start with |
||
126 | // a dot, and if is it not the thumbnail directory |
||
127 | if(is_dir($base.$entry) |
||
128 | && substr($entry,0,1) != '.' |
||
129 | && $this->isThumbDir($entry) == false) |
||
130 | { |
||
131 | $relative = Files::fixPath($path.$entry); |
||
132 | $fullpath = Files::fixPath($base.$entry); |
||
133 | $dirs[$relative] = $fullpath; |
||
134 | $dirs = array_merge($dirs, $this->_dirs($fullpath, $relative)); |
||
135 | } |
||
136 | } |
||
137 | $d->close(); |
||
138 | |||
139 | Return $dirs; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get all the files and directories of a relative path. |
||
144 | * @param string $path relative path to be base path. |
||
145 | * @return array of file and path information. |
||
146 | * <code>array(0=>array('relative'=>'fullpath',...), 1=>array('filename'=>fileinfo array(),...)</code> |
||
147 | * fileinfo array: <code>array('url'=>'full url', |
||
148 | * 'relative'=>'relative to base', |
||
149 | * 'fullpath'=>'full file path', |
||
150 | * 'image'=>imageInfo array() false if not image, |
||
151 | * 'stat' => filestat)</code> |
||
152 | */ |
||
153 | function getFiles($path) |
||
154 | { |
||
155 | $files = array(); |
||
156 | $dirs = array(); |
||
157 | |||
158 | $valid_extensions = $this->mode == 'image' ? $this->config['allowed_image_extensions'] : $this->config['allowed_link_extensions']; |
||
159 | |||
160 | if($this->isValidBase() == false) |
||
161 | return array($files,$dirs); |
||
162 | |||
163 | $path = Files::fixPath($path); |
||
164 | $base = Files::fixPath($this->getImagesDir()); |
||
165 | $fullpath = Files::makePath($base,$path); |
||
166 | |||
167 | |||
168 | $d = @dir($fullpath); |
||
169 | |||
170 | while (false !== ($entry = $d->read())) |
||
171 | { |
||
172 | //not a dot file or directory |
||
173 | if(substr($entry,0,1) != '.') |
||
174 | { |
||
175 | if(is_dir($fullpath.$entry) |
||
176 | && $this->isThumbDir($entry) == false) |
||
177 | { |
||
178 | $relative = Files::fixPath($path.$entry); |
||
179 | $full = Files::fixPath($fullpath.$entry); |
||
180 | $count = $this->countFiles($full); |
||
181 | $dirs[$relative] = array('fullpath'=>$full,'entry'=>$entry,'count'=>$count, 'stat'=>stat($fullpath.$entry)); |
||
182 | } |
||
183 | |||
184 | else if(is_file($fullpath.$entry) && $this->isThumb($entry)==false && $this->isTmpFile($entry) == false) |
||
185 | { |
||
186 | $afruext = strtolower(substr(strrchr($entry, "."), 1)); |
||
187 | |||
188 | if(in_array($afruext,$valid_extensions)) |
||
189 | { |
||
190 | |||
191 | $file['url'] = Files::makePath($this->config['base_url'],$path).$entry; |
||
192 | $file['relative'] = $path.$entry; |
||
193 | $file['fullpath'] = $fullpath.$entry; |
||
194 | $img = $this->getImageInfo($fullpath.$entry); |
||
195 | if(!is_array($img)) $img[0]=$img[1]=0; |
||
196 | $file['image'] = $img; |
||
197 | $file['stat'] = stat($fullpath.$entry); |
||
198 | $file['ext'] = $afruext; |
||
199 | $files[$entry] = $file; |
||
200 | } |
||
201 | |||
202 | } |
||
203 | } |
||
204 | } |
||
205 | $d->close(); |
||
206 | ksort($dirs); |
||
207 | ksort($files); |
||
208 | |||
209 | Return array($dirs, $files); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Count the number of files and directories in a given folder |
||
214 | * minus the thumbnail folders and thumbnails. |
||
215 | */ |
||
216 | function countFiles($path) |
||
217 | { |
||
218 | $total = 0; |
||
219 | |||
220 | if(is_dir($path)) |
||
221 | { |
||
222 | $d = @dir($path); |
||
223 | |||
224 | while (false !== ($entry = $d->read())) |
||
225 | { |
||
226 | //echo $entry."<br>"; |
||
227 | if(substr($entry,0,1) != '.' |
||
228 | && $this->isThumbDir($entry) == false |
||
229 | && $this->isTmpFile($entry) == false |
||
230 | && $this->isThumb($entry) == false) |
||
231 | { |
||
232 | $total++; |
||
233 | } |
||
234 | } |
||
235 | $d->close(); |
||
236 | } |
||
237 | return $total; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Get image size information. |
||
242 | * @param string $file the image file |
||
243 | * @return array of getImageSize information, |
||
244 | * false if the file is not an image. |
||
245 | */ |
||
246 | function getImageInfo($file) |
||
247 | { |
||
248 | Return @getImageSize($file); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Check if the file contains the thumbnail prefix. |
||
253 | * @param string $file filename to be checked |
||
254 | * @return true if the file contains the thumbnail prefix, false otherwise. |
||
255 | */ |
||
256 | function isThumb($file) |
||
257 | { |
||
258 | $len = strlen($this->config['thumbnail_prefix']); |
||
259 | if(substr($file,0,$len)==$this->config['thumbnail_prefix']) |
||
260 | Return true; |
||
261 | else |
||
262 | Return false; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Check if the given directory is a thumbnail directory. |
||
267 | * @param string $entry directory name |
||
268 | * @return true if it is a thumbnail directory, false otherwise |
||
269 | */ |
||
270 | function isThumbDir($entry) |
||
271 | { |
||
272 | if($this->config['thumbnail_dir'] == false |
||
273 | || strlen(trim($this->config['thumbnail_dir'])) == 0) |
||
274 | Return false; |
||
275 | else |
||
276 | Return ($entry == $this->config['thumbnail_dir']); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Check if the given file is a tmp file. |
||
281 | * @param string $file file name |
||
282 | * @return boolean true if it is a tmp file, false otherwise |
||
283 | */ |
||
284 | function isTmpFile($file) |
||
285 | { |
||
286 | $len = strlen($this->config['tmp_prefix']); |
||
287 | if(substr($file,0,$len)==$this->config['tmp_prefix']) |
||
288 | Return true; |
||
289 | else |
||
290 | Return false; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * For a given image file, get the respective thumbnail filename |
||
295 | * no file existence check is done. |
||
296 | * @param string $fullpathfile the full path to the image file |
||
297 | * @return string of the thumbnail file |
||
298 | */ |
||
299 | function getThumbName($fullpathfile) |
||
300 | { |
||
301 | $path_parts = pathinfo($fullpathfile); |
||
302 | |||
303 | $thumbnail = $this->config['thumbnail_prefix'].$path_parts['basename']; |
||
304 | |||
305 | if($this->config['safe_mode'] == true |
||
306 | || strlen(trim($this->config['thumbnail_dir'])) == 0) |
||
307 | { |
||
308 | Return Files::makeFile($path_parts['dirname'],$thumbnail); |
||
309 | } |
||
310 | else |
||
311 | { |
||
312 | if(strlen(trim($this->config['thumbnail_dir'])) > 0) |
||
313 | { |
||
314 | $path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']); |
||
315 | if(!is_dir($path)) |
||
316 | Files::createFolder($path); |
||
317 | Return Files::makeFile($path,$thumbnail); |
||
318 | } |
||
319 | else //should this ever happen? |
||
320 | { |
||
321 | //error_log('ExtendedFileManager: Error in creating thumbnail name'); |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Similar to getThumbName, but returns the URL, base on the |
||
328 | * given base_url in config.inc.php |
||
329 | * @param string $relative the relative image file name, |
||
330 | * relative to the base_dir path |
||
331 | * @return string the url of the thumbnail |
||
332 | */ |
||
333 | function getThumbURL($relative) |
||
354 | //error_log('ExtendedFileManager: Error in creating thumbnail url'); |
||
355 | } |
||
356 | |||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * For a given image file, get the respective resized filename |
||
362 | * no file existence check is done. |
||
363 | * @param string $fullpathfile the full path to the image file |
||
364 | * @param integer $width the intended width |
||
365 | * @param integer $height the intended height |
||
366 | * @param boolean $mkDir whether to attempt to make the resized_dir if it doesn't exist |
||
367 | * @return string of the resized filename |
||
368 | */ |
||
369 | function getResizedName($fullpathfile, $width, $height, $mkDir = TRUE) |
||
370 | { |
||
371 | $path_parts = pathinfo($fullpathfile); |
||
372 | |||
373 | $thumbnail = $this->config['resized_prefix']."_{$width}x{$height}_{$path_parts['basename']}"; |
||
374 | |||
375 | if( strlen(trim($this->config['resized_dir'])) == 0 || $this->config['safe_mode'] == true ) |
||
376 | { |
||
377 | Return Files::makeFile($path_parts['dirname'],$thumbnail); |
||
378 | } |
||
379 | else |
||
380 | { |
||
381 | $path = Files::makePath($path_parts['dirname'],$this->config['resized_dir']); |
||
382 | if($mkDir && !is_dir($path)) |
||
383 | Files::createFolder($path); |
||
384 | Return Files::makeFile($path,$thumbnail); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Check if the given path is part of the subdirectories |
||
390 | * under the base_dir. |
||
391 | * @param string $path the relative path to be checked |
||
392 | * @return boolean true if the path exists, false otherwise |
||
393 | */ |
||
394 | function validRelativePath($path) |
||
395 | { |
||
396 | $dirs = $this->getDirs(); |
||
397 | if($path == '/') |
||
398 | Return true; |
||
399 | //check the path given in the url against the |
||
400 | //list of paths in the system. |
||
401 | for($i = 0; $i < count($dirs); $i++) |
||
402 | { |
||
403 | $key = key($dirs); |
||
404 | //we found the path |
||
405 | if($key == $path) |
||
406 | Return true; |
||
407 | |||
408 | next($dirs); |
||
409 | } |
||
410 | Return false; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Process uploaded files, assumes the file is in |
||
415 | * $_FILES['upload'] and $_POST['dir'] is set. |
||
416 | * The dir must be relative to the base_dir and exists. |
||
417 | * @return null |
||
418 | */ |
||
419 | function processUploads() |
||
420 | { |
||
421 | if($this->isValidBase() == false) |
||
422 | return; |
||
423 | |||
424 | $relative = null; |
||
425 | |||
426 | if(isset($_POST['dir'])) |
||
427 | $relative = rawurldecode($_POST['dir']); |
||
428 | else |
||
429 | return; |
||
430 | |||
431 | //check for the file, and must have valid relative path |
||
432 | if(isset($_FILES['upload']) && $this->validRelativePath($relative)) |
||
433 | { |
||
434 | Return $this->_processFiles($relative, $_FILES['upload']); |
||
435 | } |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Process upload files. The file must be an |
||
440 | * uploaded file. Any duplicate |
||
441 | * file will be renamed. See Files::copyFile for details |
||
442 | * on renaming. |
||
443 | * @param string $relative the relative path where the file |
||
444 | * should be copied to. |
||
445 | * @param array $file the uploaded file from $_FILES |
||
446 | * @return boolean true if the file was processed successfully, |
||
447 | * false otherwise |
||
448 | */ |
||
449 | function _processFiles($relative, $file) |
||
450 | { |
||
451 | |||
452 | if($file['error']!=0) |
||
453 | { |
||
454 | Return false; |
||
455 | } |
||
456 | |||
457 | if(!is_uploaded_file($file['tmp_name'])) |
||
458 | { |
||
459 | Files::delFile($file['tmp_name']); |
||
460 | Return false; |
||
461 | } |
||
462 | |||
463 | $valid_extensions = $this->mode == 'image' ? $this->config['allowed_image_extensions'] : $this->config['allowed_link_extensions']; |
||
464 | $max_size = $this->mode == 'image' ? $this->config['max_filesize_kb_image'] : $this->config['max_filesize_kb_link']; |
||
465 | $afruext = strtolower(substr(strrchr($file['name'], "."), 1)); |
||
466 | |||
467 | if(!in_array($afruext, $valid_extensions)) |
||
468 | { |
||
469 | Files::delFile($file['tmp_name']); |
||
470 | Return 'Cannot upload $extension='.$afruext.'$ Files. Permission denied.'; |
||
471 | } |
||
472 | |||
473 | if($file['size']>($max_size*1024)) |
||
474 | { |
||
475 | Files::delFile($file['tmp_name']); |
||
476 | Return 'Unble to upload file. Maximum file size [$max_size='.$max_size.'$ KB] exceeded.'; |
||
477 | } |
||
478 | |||
479 | if(!empty($this->config['max_foldersize_mb']) && (Files::dirSize($this->getImagesDir()))+$file['size']> ($this->config['max_foldersize_mb']*1048576)) |
||
480 | { |
||
481 | Files::delFile($file['tmp_name']); |
||
482 | Return ("Cannot upload. Maximum folder size reached. Delete unwanted files and try again."); |
||
483 | } |
||
484 | |||
485 | //now copy the file |
||
486 | $path = Files::makePath($this->getImagesDir(),$relative); |
||
487 | $result = Files::copyFile($file['tmp_name'], $path, $file['name']); |
||
488 | |||
489 | //no copy error |
||
490 | if(!is_int($result)) |
||
491 | { |
||
492 | Files::delFile($file['tmp_name']); |
||
493 | Return 'File "$file='.$file['name'].'$" successfully uploaded.'; |
||
494 | } |
||
495 | |||
496 | //delete tmp files. |
||
497 | Files::delFile($file['tmp_name']); |
||
498 | Return false; |
||
499 | |||
500 | } |
||
501 | |||
502 | |||
503 | function getDiskInfo() |
||
504 | { |
||
505 | if (empty($this->config['max_foldersize_mb'])) |
||
506 | return ''; |
||
507 | |||
508 | $tmpFreeSize=($this->config['max_foldersize_mb']*1048576)-Files::dirSize($this->getImagesDir()); |
||
509 | |||
510 | if(!is_numeric($tmpFreeSize) || $tmpFreeSize<0) $tmpFreeSize=0; |
||
511 | |||
512 | Return 'Total Size : $max_foldersize_mb='.$this->config['max_foldersize_mb'].'$ MB, Free Space: $free_space='.Files::formatSize($tmpFreeSize).'$'; |
||
513 | } |
||
514 | |||
515 | |||
516 | |||
517 | /** |
||
518 | * Get the URL of the relative file. |
||
519 | * basically appends the relative file to the |
||
520 | * base_url given in config.inc.php |
||
521 | * @param string $relative a file the relative to the base_dir |
||
522 | * @return string the URL of the relative file. |
||
523 | */ |
||
524 | function getFileURL($relative) |
||
525 | { |
||
526 | Return Files::makeFile($this->getImagesURL(),$relative); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Get the fullpath to a relative file. |
||
531 | * @param string $relative the relative file. |
||
532 | * @return string the full path, .ie. the base_dir + relative. |
||
533 | */ |
||
534 | function getFullPath($relative) |
||
535 | { |
||
536 | Return Files::makeFile($this->getImagesDir(),$relative);; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Get the default thumbnail. |
||
541 | * @return string default thumbnail, empty string if |
||
542 | * the thumbnail doesn't exist. |
||
543 | */ |
||
544 | function getDefaultThumb() |
||
545 | { |
||
546 | if(is_file($this->config['default_thumbnail'])) |
||
547 | Return $this->config['default_thumbnail']; |
||
548 | else |
||
549 | Return ''; |
||
550 | } |
||
551 | |||
552 | |||
553 | /** |
||
554 | * Checks image size. If the image size is less than default size |
||
555 | * returns the original size else returns default size to display thumbnail |
||
556 | */ |
||
557 | function checkImageSize($relative) |
||
558 | { |
||
559 | $fullpath = Files::makeFile($this->getImagesDir(),$relative); |
||
560 | |||
561 | $afruext = strtolower(substr(strrchr($relative, "."), 1)); |
||
562 | |||
563 | if(!in_array($afruext,$this->config['thumbnail_extensions'])) |
||
564 | { |
||
565 | $imgInfo=array(0,0); |
||
566 | Return $imgInfo; |
||
567 | } |
||
568 | else |
||
569 | { |
||
570 | $imgInfo = @getImageSize($fullpath); |
||
571 | //not an image |
||
572 | if(!is_array($imgInfo)) |
||
573 | { |
||
574 | $imgInfo=array(0,0); |
||
575 | Return $imgInfo; |
||
576 | } |
||
577 | else |
||
578 | { |
||
579 | if($imgInfo[0] > $this->config['thumbnail_width']) |
||
580 | $imgInfo[0] = $this->config['thumbnail_width']; |
||
581 | |||
582 | if($imgInfo[1] > $this->config['thumbnail_height']) |
||
583 | $imgInfo[1] = $this->config['thumbnail_height']; |
||
584 | |||
585 | Return $imgInfo; |
||
586 | } |
||
587 | } |
||
588 | |||
589 | } |
||
590 | |||
591 | |||
592 | /** |
||
593 | * Get the thumbnail url to be displayed. |
||
594 | * If the thumbnail exists, and it is up-to-date |
||
595 | * the thumbnail url will be returns. If the |
||
596 | * file is not an image, a default image will be returned. |
||
597 | * If it is an image file, and no thumbnail exists or |
||
598 | * the thumbnail is out-of-date (i.e. the thumbnail |
||
599 | * modified time is less than the original file) |
||
600 | * then a thumbs.php?img=filename.jpg is returned. |
||
601 | * The thumbs.php url will generate a new thumbnail |
||
602 | * on the fly. If the image is less than the dimensions |
||
603 | * of the thumbnails, the image will be display instead. |
||
604 | * @param string $relative the relative image file. |
||
605 | * @return string the url of the thumbnail, be it |
||
606 | * actually thumbnail or a script to generate the |
||
607 | * thumbnail on the fly. |
||
608 | */ |
||
609 | function getThumbnail($relative) |
||
610 | { |
||
611 | global $IMConfig; |
||
612 | |||
613 | $fullpath = Files::makeFile($this->getImagesDir(),$relative); |
||
614 | |||
615 | //not a file??? |
||
616 | if(!is_file($fullpath)) |
||
617 | Return $this->getDefaultThumb(); |
||
618 | |||
619 | $afruext = strtolower(substr(strrchr($relative, "."), 1)); |
||
620 | |||
621 | if(!in_array($afruext,$this->config['thumbnail_extensions'])) |
||
622 | { |
||
623 | if(is_file('icons/'.$afruext.'.gif')) |
||
624 | Return('icons/'.$afruext.'.gif'); |
||
625 | else |
||
626 | Return $this->getDefaultThumb(); |
||
627 | } |
||
628 | |||
629 | $imgInfo = @getImageSize($fullpath); |
||
630 | |||
631 | //not an image |
||
632 | if(!is_array($imgInfo)) |
||
633 | Return $this->getDefaultThumb(); |
||
634 | |||
635 | |||
636 | //Returning original image as thumbnail without Image Library by Afru |
||
637 | if(!$this->config['img_library']) Return $this->getFileURL($relative); |
||
638 | |||
639 | |||
640 | //the original image is smaller than thumbnails, |
||
641 | //so just return the url to the original image. |
||
642 | if ($imgInfo[0] <= $this->config['thumbnail_width'] |
||
643 | && $imgInfo[1] <= $this->config['thumbnail_height']) |
||
644 | Return $this->getFileURL($relative); |
||
645 | |||
646 | $thumbnail = $this->getThumbName($fullpath); |
||
647 | |||
648 | //check for thumbnails, if exists and |
||
649 | // it is up-to-date, return the thumbnail url |
||
650 | if(is_file($thumbnail)) |
||
651 | { |
||
652 | if(filemtime($thumbnail) >= filemtime($fullpath)) |
||
653 | Return $this->getThumbURL($relative); |
||
654 | } |
||
655 | |||
656 | //well, no thumbnail was found, so ask the thumbs.php |
||
657 | //to generate the thumbnail on the fly. |
||
658 | Return $IMConfig['backend_url'] . '__function=thumbs&img='.rawurlencode($relative)."&mode=$this->mode"; |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * Delete and specified files. |
||
663 | * @return boolean true if delete, false otherwise |
||
664 | */ |
||
665 | function deleteFiles() |
||
666 | { |
||
667 | if(isset($_GET['delf'])) |
||
668 | return $this->_delFile(rawurldecode($_GET['delf'])); |
||
669 | return false; |
||
670 | } |
||
671 | |||
672 | /** |
||
673 | * Delete and specified directories. |
||
674 | * @return boolean true if delete, false otherwise |
||
675 | */ |
||
676 | function deleteDirs() |
||
677 | { |
||
678 | if(isset($_GET['deld'])) |
||
679 | return $this->_delDir(rawurldecode($_GET['deld'])); |
||
680 | else |
||
681 | Return false; |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * Delete the relative file, and any thumbnails. |
||
686 | * @param string $relative the relative file. |
||
687 | * @return boolean true if deleted, false otherwise. |
||
688 | */ |
||
689 | function _delFile($relative) |
||
690 | { |
||
691 | $fullpath = Files::makeFile($this->getImagesDir(),$relative); |
||
692 | |||
693 | $afruext = strtolower(substr(strrchr($relative, "."), 1)); |
||
694 | |||
695 | $valid_extensions = $this->mode == 'image' ? $this->config['allowed_image_extensions'] : $this->config['allowed_link_extensions']; |
||
696 | |||
697 | if(!in_array($afruext,$valid_extensions)) |
||
698 | { |
||
699 | return false; |
||
700 | } |
||
701 | |||
702 | //check that the file is an image |
||
703 | if(is_array($this->getImageInfo($fullpath))) |
||
704 | { |
||
705 | $thumbnail = $this->getThumbName($fullpath); |
||
706 | Files::delFile($thumbnail); |
||
707 | } |
||
708 | |||
709 | Return Files::delFile($fullpath); |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * Delete directories recursively. |
||
714 | * @param string $relative the relative path to be deleted. |
||
715 | * @return boolean true if deleted, false otherwise. |
||
716 | */ |
||
717 | function _delDir($relative) |
||
718 | { |
||
719 | $fullpath = Files::makePath($this->getImagesDir(),$relative); |
||
720 | // if($this->countFiles($fullpath) <= 0) |
||
721 | return Files::delFolder($fullpath,true); //delete recursively. |
||
722 | //else |
||
723 | //Return false; |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Create new directories. |
||
728 | * If in safe_mode, nothing happens. |
||
729 | * @return boolean true if created, false otherwise. |
||
730 | */ |
||
731 | function processNewDir() |
||
732 | { |
||
733 | if($this->config['safe_mode'] == true) |
||
734 | Return false; |
||
735 | |||
736 | if(isset($_GET['newDir']) && isset($_GET['dir'])) |
||
737 | { |
||
738 | $newDir = rawurldecode($_GET['newDir']); |
||
739 | $dir = rawurldecode($_GET['dir']); |
||
740 | $path = Files::makePath($this->getImagesDir(),$dir); |
||
741 | $fullpath = Files::makePath($path, Files::escape($newDir)); |
||
742 | if(is_dir($fullpath)) |
||
743 | Return false; |
||
744 | |||
745 | Return Files::createFolder($fullpath); |
||
746 | } |
||
747 | } |
||
748 | |||
749 | /** |
||
750 | * Renames files if certain GET variables are set |
||
751 | * @return bool |
||
752 | */ |
||
753 | function processRenames() |
||
754 | { |
||
755 | if(!empty($_GET['rename']) && !empty($_GET['renameTo'])) |
||
756 | { |
||
757 | // new file name (without path and extension) |
||
758 | $newName = Files::escape(rawurldecode($_GET['renameTo'])); |
||
759 | $newName = str_replace('.', '', $newName); |
||
760 | |||
761 | // path to file (from base images directory) |
||
762 | $oldName = rawurldecode($_GET['rename']); |
||
763 | |||
764 | // strip parent dir ("..") to avoid escaping from base directiory |
||
765 | $oldName = preg_replace('#\.\.#', '', $oldName); |
||
766 | |||
767 | if (is_dir($oldPath = Files::makeFile($this->getImagesDir(), $_GET['dir'].$oldName))) |
||
768 | { |
||
769 | $newPath = Files::makeFile($this->getImagesDir(), $_GET['dir'].$newName); |
||
770 | return Files::rename($oldPath,$newPath); |
||
771 | } |
||
772 | else |
||
773 | { |
||
774 | // path to old file |
||
775 | $oldPath = Files::makeFile($this->getImagesDir(), $oldName); |
||
776 | |||
777 | $ret = Files::renameFile($oldPath, $newName); |
||
778 | if ($ret === true) { |
||
779 | // delete old thumbnail |
||
780 | Files::delFile($this->getThumbname($oldPath)); |
||
781 | } |
||
782 | } |
||
783 | return $ret; |
||
784 | } |
||
785 | |||
786 | return null; |
||
787 | } |
||
788 | |||
789 | function processPaste() |
||
815 | } |
||
816 | } |
||
817 | } |
||
818 | |||
819 | ?> |
||
820 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.