Passed
Push — master ( b5dddf...91d417 )
by Richard
09:12
created

XoopsMediaUploader   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 595
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 103
eloc 288
c 2
b 0
f 0
dl 0
loc 595
rs 2

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setTargetFileName() 0 3 1
A getMediaType() 0 3 1
B upload() 0 39 10
A getErrors() 0 14 4
A countMedia() 0 6 2
A setPrefix() 0 3 1
A getSavedFileName() 0 3 1
B return_bytes() 0 14 7
A checkMaxFileSize() 0 12 3
A checkImageType() 0 15 6
B __construct() 0 35 8
A sanitizeMultipleExtensions() 0 13 3
A getMediaSize() 0 3 1
B _copyFile() 0 40 8
F fetchMedia() 0 100 27
A setErrors() 0 3 1
A getSavedDestination() 0 3 1
A checkMaxWidth() 0 16 4
A getMediaTmpName() 0 3 1
B checkMimeType() 0 23 8
A getMediaName() 0 3 1
A checkMaxHeight() 0 16 4

How to fix   Complexity   

Complex Class

Complex classes like XoopsMediaUploader 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 XoopsMediaUploader, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * XOOPS file uploader
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (http://www.myweb.ne.jp/, http://jp.xoops.org/)
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
/**
23
 * Upload Media files
24
 *
25
 * Example of usage (single file):
26
 * <code>
27
 * include_once 'uploader.php';
28
 * $allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png');
29
 * $maxfilesize = 50000;
30
 * $maxfilewidth = 120;
31
 * $maxfileheight = 120;
32
 * $randomFilename = true;
33
 * $uploader = new XoopsMediaUploader('/home/xoops/uploads', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight, $randomFilename);
34
 * if ($uploader->fetchMedia('single_file_name')) {
35
 *     if (!$uploader->upload()) {
36
 *         echo $uploader->getErrors();
37
 *     } else {
38
 *         echo '<h4>File uploaded successfully!</h4>'
39
 *         echo 'Saved as: ' . $uploader->getSavedFileName() . '<br>';
40
 *         echo 'Full path: ' . $uploader->getSavedDestination();
41
 *     }
42
 * } else {
43
 *        echo $uploader->getErrors();
44
 * }
45
 * </code>
46
 *
47
 * Example of usage (multiple file):
48
 * <code>
49
 * include_once 'uploader.php';
50
 * $allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png');
51
 * $maxfilesize = 50000;
52
 * $maxfilewidth = 120;
53
 * $maxfileheight = 120;
54
 * $randomFilename = true;
55
 * $uploader = new XoopsMediaUploader('/home/xoops/uploads', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight, $randomFilename);
56
 * for ($i = 0; $i < $uploader->countMedia('multiple_file_name'); $i++) {
57
 *     if ($uploader->fetchMedia('miltiple_file_name')) {
58
 *        if (!$uploader->upload()) {
59
 *           echo $uploader->getErrors();
60
 *        } else {
61
 *           echo '<h4>File uploaded successfully!</h4>'
62
 *           echo 'Saved as: ' . $uploader->getSavedFileName() . '<br>';
63
 *           echo 'Full path: ' . $uploader->getSavedDestination();
64
 *        }
65
 *     } else {
66
 *        echo $uploader->getErrors();
67
 *     }
68
 * }
69
 * </code>
70
 *
71
 */
72
class XoopsMediaUploader
73
{
74
    /**
75
     * Flag indicating if unrecognized mimetypes should be allowed (use with precaution ! may lead to security issues )
76
     */
77
78
    public $allowUnknownTypes       = false;
79
    public $mediaName;
80
    public $mediaType;
81
    public $mediaSize;
82
    public $mediaTmpName;
83
    public $mediaError;
84
    public $mediaRealType           = '';
85
    public $uploadDir               = '';
86
    public $allowedMimeTypes        = array();
87
    public $deniedMimeTypes         = array(
88
        'application/x-httpd-php');
89
    public $maxFileSize             = 0;
90
    public $maxWidth;
91
    public $maxHeight;
92
    public $targetFileName;
93
    public $prefix;
94
    public $errors                  = array();
95
    public $savedDestination;
96
    public $savedFileName;
97
    public $extensionToMime         = array();
98
    public $checkImageType          = true;
99
    public $extensionsToBeSanitized = array(
100
        'php',
101
        'phtml',
102
        'phtm',
103
        'php3',
104
        'php4',
105
        'cgi',
106
        'pl',
107
        'asp',
108
        'php5',
109
        'php7',
110
    );
111
    // extensions needed image check (anti-IE Content-Type XSS)
112
    public $imageExtensions = array(
113
        1  => 'gif',
114
        2  => 'jpg',
115
        3  => 'png',
116
        4  => 'swf',
117
        5  => 'psd',
118
        6  => 'bmp',
119
        7  => 'tif',
120
        8  => 'tif',
121
        9  => 'jpc',
122
        10 => 'jp2',
123
        11 => 'jpx',
124
        12 => 'jb2',
125
        13 => 'swc',
126
        14 => 'iff',
127
        15 => 'wbmp',
128
        16 => 'xbm');
129
    public $randomFilename  = false;
130
131
    /**
132
     * Constructor
133
     *
134
     * @param string $uploadDir
135
     * @param array  $allowedMimeTypes
136
     * @param int    $maxFileSize
137
     * @param int    $maxWidth
138
     * @param int    $maxHeight
139
     * @param bool   $randomFilename
140
     */
141
142
    public function __construct($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = null, $maxHeight = null, $randomFilename = false)
143
    {
144
        $this->extensionToMime = include $GLOBALS['xoops']->path('include/mimetypes.inc.php');
145
        if (!is_array($this->extensionToMime)) {
146
            $this->extensionToMime = array();
147
148
            return false;
149
        }
150
        if (is_array($allowedMimeTypes)) {
0 ignored issues
show
introduced by
The condition is_array($allowedMimeTypes) is always true.
Loading history...
151
            $this->allowedMimeTypes =& $allowedMimeTypes;
152
        }
153
        $this->uploadDir = $uploadDir;
154
155
        $maxUploadInBytes   = $this->return_bytes(ini_get('upload_max_filesize'));
156
        $maxPostInBytes     = $this->return_bytes(ini_get('post_max_size'));
157
        $memoryLimitInBytes = $this->return_bytes(ini_get('memory_limit'));
158
        if ((int)$maxFileSize > 0) {
159
            $maxFileSizeInBytes = $this->return_bytes($maxFileSize);
160
            $newMaxFileSize     = min($maxFileSizeInBytes, $maxUploadInBytes, $maxPostInBytes, $memoryLimitInBytes);
161
        } else {
162
            $newMaxFileSize = min($maxUploadInBytes, $maxPostInBytes, $memoryLimitInBytes);
163
        }
164
        $this->maxFileSize = $newMaxFileSize;
165
166
        if (isset($maxWidth)) {
167
            $this->maxWidth = (int)$maxWidth;
168
        }
169
        if (isset($maxHeight)) {
170
            $this->maxHeight = (int)$maxHeight;
171
        }
172
        if (isset($randomFilename)) {
173
            $this->randomFilename = $randomFilename;
174
        }
175
        if (!include_once $GLOBALS['xoops']->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/uploader.php')) {
176
            include_once $GLOBALS['xoops']->path('language/english/uploader.php');
177
        }
178
    }
179
180
    /**
181
     * converts memory/file sizes as defined in php.ini to bytes
182
     *
183
     * @param $size_str
184
     *
185
     * @return int
186
     */
187
    public function return_bytes($size_str)
188
    {
189
        switch (substr($size_str, -1)) {
190
            case 'K':
191
            case 'k':
192
                return (int)$size_str * 1024;
193
            case 'M':
194
            case 'm':
195
                return (int)$size_str * 1048576;
196
            case 'G':
197
            case 'g':
198
                return (int)$size_str * 1073741824;
199
            default:
200
                return $size_str;
201
        }
202
    }
203
204
    /**
205
     * Count the uploaded files (in case of miltiple upload)
206
     *
207
     * @param  string $media_name Name of the file field
208
     * @return int
209
     */
210
    public function countMedia($media_name) {
211
        if (!isset($_FILES[$media_name])) {
212
            $this->setErrors(_ER_UP_FILENOTFOUND);
213
            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 integer.
Loading history...
214
        }
215
        return count($_FILES[$media_name]['name']);
216
    }
217
    
218
    /**
219
     * Fetch the uploaded file
220
     *
221
     * @param  string $media_name Name of the file field
222
     * @param  int    $index      Index of the file (if more than one uploaded under that name)
223
     * @return bool
224
     */
225
    public function fetchMedia($media_name, $index = null)
226
    {
227
        if (empty($this->extensionToMime)) {
228
            $this->setErrors(_ER_UP_MIMETYPELOAD);
229
230
            return false;
231
        }
232
        if (!isset($_FILES[$media_name])) {
233
            $this->setErrors(_ER_UP_FILENOTFOUND);
234
235
            return false;
236
        } elseif (is_array($_FILES[$media_name]['name']) && isset($index)) {
237
            $index           = (int)$index;
238
            $this->mediaName = get_magic_quotes_gpc() ? stripslashes($_FILES[$media_name]['name'][$index]) : $_FILES[$media_name]['name'][$index];
239
            if ($this->randomFilename) {
240
                $unique          = uniqid();
241
                $this->mediaName = '' . $unique . '--' . $this->mediaName;
242
            }
243
            $this->mediaType    = $_FILES[$media_name]['type'][$index];
244
            $this->mediaSize    = $_FILES[$media_name]['size'][$index];
245
            $this->mediaTmpName = $_FILES[$media_name]['tmp_name'][$index];
246
            $this->mediaError   = !empty($_FILES[$media_name]['error'][$index]) ? $_FILES[$media_name]['error'][$index] : 0;
247
        } elseif (is_array($_FILES[$media_name]['name']) && !isset($index)) {
248
            $this->setErrors(_ER_UP_INDEXNOTSET);
249
250
            return false;
251
        } else {
252
            $media_name      =& $_FILES[$media_name];
253
            $this->mediaName = get_magic_quotes_gpc() ? stripslashes($media_name['name']) : $media_name['name'];
254
            if ($this->randomFilename) {
255
                $unique          = uniqid();
256
                $this->mediaName = '' . $unique . '--' . $this->mediaName;
257
            }
258
            $this->mediaType    = $media_name['type'];
259
            $this->mediaSize    = $media_name['size'];
260
            $this->mediaTmpName = $media_name['tmp_name'];
261
            $this->mediaError   = !empty($media_name['error']) ? $media_name['error'] : 0;
262
        }
263
264
        if (($ext = strrpos($this->mediaName, '.')) !== false) {
265
            $ext = strtolower(substr($this->mediaName, $ext + 1));
266
            if (isset($this->extensionToMime[$ext])) {
267
                $this->mediaRealType = $this->extensionToMime[$ext];
268
            }
269
        }
270
        $this->errors = array();
271
        if ($this->mediaError > 0) {
272
            switch($this->mediaError){
273
                case UPLOAD_ERR_INI_SIZE:
274
                    $this->setErrors(_ER_UP_INISIZE);
275
                    return false;
276
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
277
                case UPLOAD_ERR_FORM_SIZE:
278
                    $this->setErrors(_ER_UP_FORMSIZE);
279
                    return false;
280
                    break;
281
                case UPLOAD_ERR_PARTIAL:
282
                    $this->setErrors(_ER_UP_PARTIAL);
283
                    return false;
284
                    break;
285
                case UPLOAD_ERR_NO_FILE:
286
                    $this->setErrors(_ER_UP_NOFILE);
287
                    return false;
288
                    break;
289
                case UPLOAD_ERR_NO_TMP_DIR:
290
                    $this->setErrors(_ER_UP_NOTMPDIR);
291
                    return false;
292
                    break;
293
                case UPLOAD_ERR_CANT_WRITE:
294
                    $this->setErrors(_ER_UP_CANTWRITE);
295
                    return false;
296
                    break;
297
                case UPLOAD_ERR_EXTENSION:
298
                    $this->setErrors(_ER_UP_EXTENSION);
299
                    return false;
300
                    break;
301
                default:
302
                    $this->setErrors(_ER_UP_UNKNOWN);
303
                    return false;
304
                    break;
305
            }
306
        }
307
308
        if ((int)$this->mediaSize < 0) {
309
            $this->setErrors(_ER_UP_INVALIDFILESIZE);
310
311
            return false;
312
        }
313
        if ($this->mediaName == '') {
314
            $this->setErrors(_ER_UP_FILENAMEEMPTY);
315
316
            return false;
317
        }
318
        if ($this->mediaTmpName === 'none' || !is_uploaded_file($this->mediaTmpName)) {
319
            $this->setErrors(_ER_UP_NOFILEUPLOADED);
320
321
            return false;
322
        }
323
324
        return true;
325
    }
326
327
    /**
328
     * Set the target filename
329
     *
330
     * @param string $value
331
     */
332
    public function setTargetFileName($value)
333
    {
334
        $this->targetFileName = (string)trim($value);
335
    }
336
337
    /**
338
     * Set the prefix
339
     *
340
     * @param string $value
341
     */
342
    public function setPrefix($value)
343
    {
344
        $this->prefix = (string)trim($value);
345
    }
346
347
    /**
348
     * Get the uploaded filename
349
     *
350
     * @return string
351
     */
352
    public function getMediaName()
353
    {
354
        return $this->mediaName;
355
    }
356
357
    /**
358
     * Get the type of the uploaded file
359
     *
360
     * @return string
361
     */
362
    public function getMediaType()
363
    {
364
        return $this->mediaType;
365
    }
366
367
    /**
368
     * Get the size of the uploaded file
369
     *
370
     * @return int
371
     */
372
    public function getMediaSize()
373
    {
374
        return $this->mediaSize;
375
    }
376
377
    /**
378
     * Get the temporary name that the uploaded file was stored under
379
     *
380
     * @return string
381
     */
382
    public function getMediaTmpName()
383
    {
384
        return $this->mediaTmpName;
385
    }
386
387
    /**
388
     * Get the saved filename
389
     *
390
     * @return string
391
     */
392
    public function getSavedFileName()
393
    {
394
        return $this->savedFileName;
395
    }
396
397
    /**
398
     * Get the destination the file is saved to
399
     *
400
     * @return string
401
     */
402
    public function getSavedDestination()
403
    {
404
        return $this->savedDestination;
405
    }
406
407
    /**
408
     * Check the file and copy it to the destination
409
     *
410
     * @param  int $chmod
411
     * @return bool
412
     */
413
    public function upload($chmod = 0644)
414
    {
415
        if ($this->uploadDir == '') {
416
            $this->setErrors(_ER_UP_UPLOADDIRNOTSET);
417
418
            return false;
419
        }
420
        if (!is_dir($this->uploadDir)) {
421
            $this->setErrors(sprintf(_ER_UP_FAILEDOPENDIR, $this->uploadDir));
422
423
            return false;
424
        }
425
        if (!is_writable($this->uploadDir)) {
426
            $this->setErrors(sprintf(_ER_UP_FAILEDOPENDIRWRITE, $this->uploadDir));
427
428
            return false;
429
        }
430
        $this->sanitizeMultipleExtensions();
431
432
        if (!$this->checkMaxFileSize()) {
433
            return false;
434
        }
435
        if (!$this->checkMaxWidth()) {
436
            return false;
437
        }
438
        if (!$this->checkMaxHeight()) {
439
            return false;
440
        }
441
        if (!$this->checkMimeType()) {
442
            return false;
443
        }
444
        if (!$this->checkImageType()) {
445
            return false;
446
        }
447
        if (count($this->errors) > 0) {
448
            return false;
449
        }
450
451
        return $this->_copyFile($chmod);
452
    }
453
454
    /**
455
     * Copy the file to its destination
456
     *
457
     * @param $chmod
458
     * @return bool
459
     */
460
    public function _copyFile($chmod)
461
    {
462
        $matched = array();
463
        if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
464
            $this->setErrors(_ER_UP_INVALIDFILENAME);
465
466
            return false;
467
        }
468
        if (isset($this->targetFileName)) {
469
            $this->savedFileName = $this->targetFileName;
470
        } elseif (isset($this->prefix)) {
471
            $this->savedFileName = uniqid($this->prefix) . '.' . strtolower($matched[1]);
472
        } else {
473
            $this->savedFileName = strtolower($this->mediaName);
474
        }
475
476
        $this->savedFileName = iconv('UTF-8', 'ASCII//TRANSLIT', $this->savedFileName);
477
        $this->savedFileName = preg_replace('!\s+!', '_', $this->savedFileName);
478
        $this->savedFileName = preg_replace("/[^a-zA-Z0-9\._-]/", '', $this->savedFileName);
479
480
        $this->savedDestination = $this->uploadDir . '/' . $this->savedFileName;
481
        if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
482
            $this->setErrors(sprintf(_ER_UP_FAILEDSAVEFILE, $this->savedDestination));
483
484
            return false;
485
        }
486
        // Check IE XSS before returning success
487
        $ext = strtolower(substr(strrchr($this->savedDestination, '.'), 1));
488
        if (in_array($ext, $this->imageExtensions)) {
489
            $info = @getimagesize($this->savedDestination);
490
            if ($info === false || $this->imageExtensions[(int)$info[2]] != $ext) {
491
                $this->setErrors(_ER_UP_SUSPICIOUSREFUSED);
492
                @unlink($this->savedDestination);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

492
                /** @scrutinizer ignore-unhandled */ @unlink($this->savedDestination);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
493
494
                return false;
495
            }
496
        }
497
        @chmod($this->savedDestination, $chmod);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

497
        /** @scrutinizer ignore-unhandled */ @chmod($this->savedDestination, $chmod);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
498
499
        return true;
500
    }
501
502
    /**
503
     * Is the file the right size?
504
     *
505
     * @return bool
506
     */
507
    public function checkMaxFileSize()
508
    {
509
        if (!isset($this->maxFileSize)) {
510
            return true;
511
        }
512
        if ($this->mediaSize > $this->maxFileSize) {
513
            $this->setErrors(sprintf(_ER_UP_FILESIZETOOLARGE, $this->maxFileSize, $this->mediaSize));
514
515
            return false;
516
        }
517
518
        return true;
519
    }
520
521
    /**
522
     * Is the picture the right width?
523
     *
524
     * @return bool
525
     */
526
    public function checkMaxWidth()
527
    {
528
        if (!isset($this->maxWidth)) {
529
            return true;
530
        }
531
        if (false !== $dimension = getimagesize($this->mediaTmpName)) {
532
            if ($dimension[0] > $this->maxWidth) {
533
                $this->setErrors(sprintf(_ER_UP_FILEWIDTHTOOLARGE, $this->maxWidth, $dimension[0]));
534
535
                return false;
536
            }
537
        } else {
538
            trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
539
        }
540
541
        return true;
542
    }
543
544
    /**
545
     * Is the picture the right height?
546
     *
547
     * @return bool
548
     */
549
    public function checkMaxHeight()
550
    {
551
        if (!isset($this->maxHeight)) {
552
            return true;
553
        }
554
        if (false !== $dimension = getimagesize($this->mediaTmpName)) {
555
            if ($dimension[1] > $this->maxHeight) {
556
                $this->setErrors(sprintf(_ER_UP_FILEHEIGHTTOOLARGE, $this->maxHeight, $dimension[1]));
557
558
                return false;
559
            }
560
        } else {
561
            trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
562
        }
563
564
        return true;
565
    }
566
567
    /**
568
     * Check whether or not the uploaded file type is allowed
569
     *
570
     * @return bool
571
     */
572
    public function checkMimeType()
573
    {
574
        // if the browser supplied mime type looks suspicious, refuse it
575
        $structureCheck = (bool) preg_match('/^\w+\/[-+.\w]+$/', $this->mediaType);
576
        if (false === $structureCheck) {
577
            $this->mediaType = 'invalid';
578
            $this->setErrors(_ER_UP_UNKNOWNFILETYPEREJECTED);
579
            return false;
580
        }
581
582
        if (empty($this->mediaRealType) && empty($this->allowUnknownTypes)) {
583
            $this->setErrors(_ER_UP_UNKNOWNFILETYPEREJECTED);
584
585
            return false;
586
        }
587
588
        if ((!empty($this->allowedMimeTypes) && !in_array($this->mediaRealType, $this->allowedMimeTypes)) || (!empty($this->deniedMimeTypes) && in_array($this->mediaRealType, $this->deniedMimeTypes))) {
589
            $this->setErrors(sprintf(_ER_UP_MIMETYPENOTALLOWED, htmlspecialchars($this->mediaRealType, ENT_QUOTES)));
590
591
            return false;
592
        }
593
594
        return true;
595
    }
596
597
    /**
598
     * Check whether or not the uploaded image type is valid
599
     *
600
     * @return bool
601
     */
602
    public function checkImageType()
603
    {
604
        if (empty($this->checkImageType)) {
605
            return true;
606
        }
607
608
        if (('image' === substr($this->mediaType, 0, strpos($this->mediaType, '/'))) || (!empty($this->mediaRealType) && 'image' === substr($this->mediaRealType, 0, strpos($this->mediaRealType, '/')))) {
609
            if (!($info = @getimagesize($this->mediaTmpName))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $info is dead and can be removed.
Loading history...
610
                $this->setErrors(_ER_UP_INVALIDIMAGEFILE);
611
612
                return false;
613
            }
614
        }
615
616
        return true;
617
    }
618
619
    /**
620
     * Sanitize executable filename with multiple extensions
621
     */
622
    public function sanitizeMultipleExtensions()
623
    {
624
        if (empty($this->extensionsToBeSanitized)) {
625
            return null;
626
        }
627
628
        $patterns = array();
629
        $replaces = array();
630
        foreach ($this->extensionsToBeSanitized as $ext) {
631
            $patterns[] = "/\." . preg_quote($ext) . "\./i";
632
            $replaces[] = '_' . $ext . '.';
633
        }
634
        $this->mediaName = preg_replace($patterns, $replaces, $this->mediaName);
635
    }
636
637
    /**
638
     * Add an error
639
     *
640
     * @param string $error
641
     */
642
    public function setErrors($error)
643
    {
644
        $this->errors[] = trim($error);
645
    }
646
647
    /**
648
     * Get generated errors
649
     *
650
     * @param  bool $ashtml Format using HTML?
651
     * @return array |string    Array of array messages OR HTML string
652
     */
653
    public function &getErrors($ashtml = true)
654
    {
655
        if (!$ashtml) {
656
            return $this->errors;
657
        } else {
658
            $ret = '';
659
            if (count($this->errors) > 0) {
660
                $ret = '<h4>' . sprintf(_ER_UP_ERRORSRETURNED, htmlspecialchars($this->mediaName, ENT_QUOTES)) . '</h4>';
661
                foreach ($this->errors as $error) {
662
                    $ret .= $error . '<br>';
663
                }
664
            }
665
666
            return $ret;
667
        }
668
    }
669
}
670