ImageManager::_delFile()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 17
rs 9.9666
1
<?php
2
/**
3
 * ImageManager, list images, directories, and thumbnails.
4
 * @author  $Author:ray $
5
 * @version $Id:ImageManager.php 709 2007-01-30 23:22:04Z ray $
6
 * @package ImageManager
7
 */
8
9
require_once('../ImageManager/Classes/Files.php');
10
11
// uncomment to turn on debugging
12
13
// _ddtOn();
14
15
/**
16
 * ImageManager Class.
17
 * @author  $Author:ray $
18
 * @version $Id:ImageManager.php 709 2007-01-30 23:22:04Z ray $
19
 */
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()
55
    {
56
        Return $this->config['images_url'];
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        /** @scrutinizer ignore-call */ 
102
        $base = Files::fixPath($base);
Loading history...
102
        $dirs = [];
103
104
        if (false == $this->isValidBase()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
144
            return [$files, $dirs];
145
        }
146
147
        $path     = Files::fixPath($path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::fixPath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        /** @scrutinizer ignore-call */ 
148
        $path     = Files::fixPath($path);
Loading history...
148
        $base     = Files::fixPath($this->getImagesDir());
149
        $fullpath = Files::makePath($base, $path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        /** @scrutinizer ignore-call */ 
150
        $fullpath = Files::makePath($base, $path);
Loading history...
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)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
198
                    && false == $this->isTmpFile($entry)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
199
                    && false == $this->isThumb($entry)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return @getImageSize($file) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
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)
225
    {
226
        $len = strlen($this->config['thumbnail_prefix']);
227
        if (substr($file, 0, $len) == $this->config['thumbnail_prefix']) {
228
            Return true;
229
        } else {
230
            Return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
244
        } else {
245
            Return ($entry == $this->config['thumbnail_dir']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $entry == $this->config['thumbnail_dir'] returns the type boolean which is incompatible with the documented return type true.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

277
            Return Files::/** @scrutinizer ignore-call */ makeFile($path_parts['dirname'], $thumbnail);
Loading history...
278
        } else {
279
            $path = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
            /** @scrutinizer ignore-call */ 
280
            $path = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']);
Loading history...
280
            if (!is_dir($path)) {
281
                Files::createFolder($path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::createFolder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

281
                Files::/** @scrutinizer ignore-call */ 
282
                       createFolder($path);
Loading history...
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));
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

306
            Return Files::/** @scrutinizer ignore-call */ makeFile($this->getImagesURL(), rawurlencode($thumbnail));
Loading history...
307
        } else {
308
            if (strlen(trim($this->config['thumbnail_dir'])) > 0) {
309
                $path     = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

309
                /** @scrutinizer ignore-call */ 
310
                $path     = Files::makePath($path_parts['dirname'], $this->config['thumbnail_dir']);
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

338
            Return Files::/** @scrutinizer ignore-call */ makeFile($path_parts['dirname'], $thumbnail);
Loading history...
339
        } else {
340
            $path = Files::makePath($path_parts['dirname'], $this->config['resized_dir']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

340
            /** @scrutinizer ignore-call */ 
341
            $path = Files::makePath($path_parts['dirname'], $this->config['resized_dir']);
Loading history...
341
            if ($mkDir && !is_dir($path)) {
342
                Files::createFolder($path);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::createFolder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

342
                Files::/** @scrutinizer ignore-call */ 
343
                       createFolder($path);
Loading history...
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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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()
383
    {
384
        if (false == $this->isValidBase()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
385
            return;
386
        }
387
388
        $relative = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relative is dead and can be removed.
Loading history...
389
390
        if (isset($_POST['dir'])) {
391
            $relative = rawurldecode($_POST['dir']);
392
        } else {
393
            return;
394
        }
395
396
        //check for the file, and must have valid relative path
397
        if (isset($_FILES['upload']) && $this->validRelativePath($relative)) {
398
            $this->_processFiles($relative, $_FILES['upload']);
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']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

425
            Files::/** @scrutinizer ignore-call */ 
426
                   delFile($file['tmp_name']);
Loading history...
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.';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Cannot upload $e...es. Permission denied.' returns the type string which is incompatible with the documented return type boolean.
Loading history...
442
        }
443
444
        //now copy the file
445
        $path   = Files::makePath($this->getImagesDir(), $relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

445
        /** @scrutinizer ignore-call */ 
446
        $path   = Files::makePath($this->getImagesDir(), $relative);
Loading history...
446
        $result = Files::copyFile($file['tmp_name'], $path, $file['name']);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::copyFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

446
        /** @scrutinizer ignore-call */ 
447
        $result = Files::copyFile($file['tmp_name'], $path, $file['name']);
Loading history...
447
448
        //no copy error
449
        if (!is_int($result)) {
0 ignored issues
show
introduced by
The condition is_int($result) is always false.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

468
        Return Files::/** @scrutinizer ignore-call */ makeFile($this->getImagesURL(), $relative);
Loading history...
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);;
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

478
        Return Files::/** @scrutinizer ignore-call */ makeFile($this->getImagesDir(), $relative);;
Loading history...
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'])) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

522
        /** @scrutinizer ignore-call */ 
523
        $fullpath = Files::makeFile($this->getImagesDir(), $relative);
Loading history...
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()
576
    {
577
        if (isset($_GET['deld'])) {
578
            return $this->_delDir(rawurldecode($_GET['deld']));
579
        } else {
580
            Return false;
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)
590
    {
591
        $fullpath = Files::makeFile($this->getImagesDir(), $relative);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makeFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

591
        /** @scrutinizer ignore-call */ 
592
        $fullpath = Files::makeFile($this->getImagesDir(), $relative);
Loading history...
592
593
        //check that the file is an image
594
        if (true == $this->config['validate_images']) {
595
            if (!is_array($this->getImageInfo($fullpath))) {
0 ignored issues
show
introduced by
The condition is_array($this->getImageInfo($fullpath)) is always true.
Loading history...
596
                return false;
597
            } //hmmm not an Image!!???
598
        }
599
600
        $thumbnail = $this->getThumbName($fullpath);
601
602
        if (Files::delFile($fullpath)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

602
        if (Files::/** @scrutinizer ignore-call */ delFile($fullpath)) {
Loading history...
603
            Return Files::delFile($thumbnail);
604
        } else {
605
            Return false;
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);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

616
        /** @scrutinizer ignore-call */ 
617
        $fullpath = Files::makePath($this->getImagesDir(), $relative);
Loading history...
617
        if ($this->countFiles($fullpath) <= 0) {
618
            return Files::delFolder($fullpath, true);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::delFolder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

618
            return Files::/** @scrutinizer ignore-call */ delFolder($fullpath, true);
Loading history...
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()
631
    {
632
        if (true == $this->config['safe_mode']) {
633
            Return false;
634
        }
635
636
        if (isset($_GET['newDir']) && isset($_GET['dir'])) {
637
            $newDir   = rawurldecode($_GET['newDir']);
638
            $dir      = rawurldecode($_GET['dir']);
639
            $path     = Files::makePath($this->getImagesDir(), $dir);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::makePath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

639
            /** @scrutinizer ignore-call */ 
640
            $path     = Files::makePath($this->getImagesDir(), $dir);
Loading history...
640
            $fullpath = Files::makePath($path, Files::escape($newDir));
0 ignored issues
show
Bug Best Practice introduced by
The method Files::escape() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

640
            $fullpath = Files::makePath($path, Files::/** @scrutinizer ignore-call */ escape($newDir));
Loading history...
641
            if (is_dir($fullpath)) {
642
                Return false;
643
            }
644
645
            Return Files::createFolder($fullpath);
0 ignored issues
show
Bug Best Practice introduced by
The method Files::createFolder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

645
            Return Files::/** @scrutinizer ignore-call */ createFolder($fullpath);
Loading history...
646
        }
647
    }
648
}
649
650
651