Completed
Push — master ( 974697...4b446d )
by Michael
9s
created

XoopsMediaUploader::fetchMedia()   F

Complexity

Conditions 25
Paths 578

Size

Total Lines 97
Code Lines 76

Duplication

Lines 8
Ratio 8.25 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 25
eloc 76
nc 578
nop 2
dl 8
loc 97
rs 2.3534
c 4
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 47 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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:
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
 * $uploader = new XoopsMediaUploader('/home/xoops/uploads', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
33
 * if ($uploader->fetchMedia($_POST['uploade_file_name'])) {
34
 *        if (!$uploader->upload()) {
35
 *           echo $uploader->getErrors();
36
 *        } else {
37
 *           echo '<h4>File uploaded successfully!</h4>'
38
 *           echo 'Saved as: ' . $uploader->getSavedFileName() . '<br>';
39
 *           echo 'Full path: ' . $uploader->getSavedDestination();
40
 *        }
41
 * } else {
42
 *        echo $uploader->getErrors();
43
 * }
44
 * </code>
45
 *
46
 */
47
class XoopsMediaUploader
48
{
49
    /**
50
     * Flag indicating if unrecognized mimetypes should be allowed (use with precaution ! may lead to security issues )
51
     */
52
53
    public $allowUnknownTypes       = false;
54
    public $mediaName;
55
    public $mediaType;
56
    public $mediaSize;
57
    public $mediaTmpName;
58
    public $mediaError;
59
    public $mediaRealType           = '';
60
    public $uploadDir               = '';
61
    public $allowedMimeTypes        = array();
62
    public $deniedMimeTypes         = array(
63
        'application/x-httpd-php');
64
    public $maxFileSize             = 0;
65
    public $maxWidth;
66
    public $maxHeight;
67
    public $targetFileName;
68
    public $prefix;
69
    public $errors                  = array();
70
    public $savedDestination;
71
    public $savedFileName;
72
    public $extensionToMime         = array();
73
    public $checkImageType          = true;
74
    public $extensionsToBeSanitized = array(
75
        'php',
76
        'phtml',
77
        'phtm',
78
        'php3',
79
        'php4',
80
        'cgi',
81
        'pl',
82
        'asp',
83
        'php5');
84
    // extensions needed image check (anti-IE Content-Type XSS)
85
    public $imageExtensions = array(
86
        1  => 'gif',
87
        2  => 'jpg',
88
        3  => 'png',
89
        4  => 'swf',
90
        5  => 'psd',
91
        6  => 'bmp',
92
        7  => 'tif',
93
        8  => 'tif',
94
        9  => 'jpc',
95
        10 => 'jp2',
96
        11 => 'jpx',
97
        12 => 'jb2',
98
        13 => 'swc',
99
        14 => 'iff',
100
        15 => 'wbmp',
101
        16 => 'xbm');
102
    public $randomFilename  = false;
103
104
    /**
105
     * Constructor
106
     *
107
     * @param string $uploadDir
108
     * @param array  $allowedMimeTypes
109
     * @param int    $maxFileSize
110
     * @param int    $maxWidth
111
     * @param int    $maxHeight
112
     * @param bool   $randomFilename
113
     */
114
115
    public function __construct($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = null, $maxHeight = null, $randomFilename = false)
116
    {
117
        $this->extensionToMime = include $GLOBALS['xoops']->path('include/mimetypes.inc.php');
118
        if (!is_array($this->extensionToMime)) {
119
            $this->extensionToMime = array();
120
121
            return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
122
        }
123
        if (is_array($allowedMimeTypes)) {
124
            $this->allowedMimeTypes =& $allowedMimeTypes;
125
        }
126
        $this->uploadDir = $uploadDir;
127
128
        $maxUploadInBytes   = $this->return_bytes(ini_get('upload_max_filesize'));
129
        $maxPostInBytes     = $this->return_bytes(ini_get('post_max_size'));
130
        $memoryLimitInBytes = $this->return_bytes(ini_get('memory_limit'));
131
        if ((int)$maxFileSize > 0) {
132
            $maxFileSizeInBytes = $this->return_bytes($maxFileSize);
133
            $newMaxFileSize     = min($maxFileSizeInBytes, $maxUploadInBytes, $maxPostInBytes, $memoryLimitInBytes);
134
        } else {
135
            $newMaxFileSize = min($maxUploadInBytes, $maxPostInBytes, $memoryLimitInBytes);
136
        }
137
        $this->maxFileSize = $newMaxFileSize;
138
139
        if (isset($maxWidth)) {
140
            $this->maxWidth = (int)$maxWidth;
141
        }
142
        if (isset($maxHeight)) {
143
            $this->maxHeight = (int)$maxHeight;
144
        }
145
        if (isset($randomFilename)) {
146
            $this->randomFilename = $randomFilename;
147
        }
148 View Code Duplication
        if (!include_once $GLOBALS['xoops']->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/uploader.php')) {
149
            include_once $GLOBALS['xoops']->path('language/english/uploader.php');
150
        }
151
    }
152
153
    /**
154
     * converts memory/file sizes as defined in php.ini to bytes
155
     *
156
     * @param $size_str
157
     *
158
     * @return int
159
     */
160
    public function return_bytes($size_str)
161
    {
162
        switch (substr($size_str, -1)) {
163
            case 'K':
164
            case 'k':
165
                return (int)$size_str * 1024;
166
            case 'M':
167
            case 'm':
168
                return (int)$size_str * 1048576;
169
            case 'G':
170
            case 'g':
171
                return (int)$size_str * 1073741824;
172
            default:
173
                return $size_str;
174
        }
175
    }
176
177
    /**
178
     * Fetch the uploaded file
179
     *
180
     * @param  string $media_name Name of the file field
181
     * @param  int    $index      Index of the file (if more than one uploaded under that name)
182
     * @return bool
183
     */
184
    public function fetchMedia($media_name, $index = null)
185
    {
186
        if (empty($this->extensionToMime)) {
187
            $this->setErrors(_ER_UP_MIMETYPELOAD);
188
189
            return false;
190
        }
191
        if (!isset($_FILES[$media_name])) {
192
            $this->setErrors(_ER_UP_FILENOTFOUND);
193
194
            return false;
195
        } elseif (is_array($_FILES[$media_name]['name']) && isset($index)) {
196
            $index           = (int)$index;
197
            $this->mediaName = get_magic_quotes_gpc() ? stripslashes($_FILES[$media_name]['name'][$index]) : $_FILES[$media_name]['name'][$index];
198 View Code Duplication
            if ($this->randomFilename) {
199
                $unique          = uniqid();
200
                $this->mediaName = '' . $unique . '--' . $this->mediaName;
201
            }
202
            $this->mediaType    = $_FILES[$media_name]['type'][$index];
203
            $this->mediaSize    = $_FILES[$media_name]['size'][$index];
204
            $this->mediaTmpName = $_FILES[$media_name]['tmp_name'][$index];
205
            $this->mediaError   = !empty($_FILES[$media_name]['error'][$index]) ? $_FILES[$media_name]['error'][$index] : 0;
206
        } else {
207
            $media_name      =& $_FILES[$media_name];
208
            $this->mediaName = get_magic_quotes_gpc() ? stripslashes($media_name['name']) : $media_name['name'];
209 View Code Duplication
            if ($this->randomFilename) {
210
                $unique          = uniqid();
211
                $this->mediaName = '' . $unique . '--' . $this->mediaName;
212
            }
213
            $this->mediaType    = $media_name['type'];
214
            $this->mediaSize    = $media_name['size'];
215
            $this->mediaTmpName = $media_name['tmp_name'];
216
            $this->mediaError   = !empty($media_name['error']) ? $media_name['error'] : 0;
217
        }
218
219
        if (($ext = strrpos($this->mediaName, '.')) !== false) {
220
            $ext = strtolower(substr($this->mediaName, $ext + 1));
221
            if (isset($this->extensionToMime[$ext])) {
222
                $this->mediaRealType = $this->extensionToMime[$ext];
223
            }
224
        }
225
        $this->errors = array();
226
        if ($this->mediaError > 0) {
227
            switch($this->mediaError){
228
                case UPLOAD_ERR_INI_SIZE:
229
                    $this->setErrors(_ER_UP_INISIZE);
230
                    return false;
231
                    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...
232
                case UPLOAD_ERR_FORM_SIZE:
233
                    $this->setErrors(_ER_UP_FORMSIZE);
234
                    return false;
235
                    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...
236
                case UPLOAD_ERR_PARTIAL:
237
                    $this->setErrors(_ER_UP_PARTIAL);
238
                    return false;
239
                    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...
240
                case UPLOAD_ERR_NO_FILE:
241
                    $this->setErrors(_ER_UP_NOFILE);
242
                    return false;
243
                    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...
244
                case UPLOAD_ERR_NO_TMP_DIR:
245
                    $this->setErrors(_ER_UP_NOTMPDIR);
246
                    return false;
247
                    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...
248
                case UPLOAD_ERR_CANT_WRITE:
249
                    $this->setErrors(_ER_UP_CANTWRITE);
250
                    return false;
251
                    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...
252
                case UPLOAD_ERR_EXTENSION:
253
                    $this->setErrors(_ER_UP_EXTENSION);
254
                    return false;
255
                    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...
256
                default:
257
                    $this->setErrors(_ER_UP_UNKNOWN);
258
                    return false;
259
                    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...
260
            }
261
        }
262
        
263
        if ((int)$this->mediaSize < 0) {
264
            $this->setErrors(_ER_UP_INVALIDFILESIZE);
265
266
            return false;
267
        }
268
        if ($this->mediaName == '') {
269
            $this->setErrors(_ER_UP_FILENAMEEMPTY);
270
271
            return false;
272
        }
273
        if ($this->mediaTmpName === 'none' || !is_uploaded_file($this->mediaTmpName)) {
274
            $this->setErrors(_ER_UP_NOFILEUPLOADED);
275
276
            return false;
277
        }
278
279
        return true;
280
    }
281
282
    /**
283
     * Set the target filename
284
     *
285
     * @param string $value
286
     */
287
    public function setTargetFileName($value)
288
    {
289
        $this->targetFileName = (string)trim($value);
290
    }
291
292
    /**
293
     * Set the prefix
294
     *
295
     * @param string $value
296
     */
297
    public function setPrefix($value)
298
    {
299
        $this->prefix = (string)trim($value);
300
    }
301
302
    /**
303
     * Get the uploaded filename
304
     *
305
     * @return string
306
     */
307
    public function getMediaName()
308
    {
309
        return $this->mediaName;
310
    }
311
312
    /**
313
     * Get the type of the uploaded file
314
     *
315
     * @return string
316
     */
317
    public function getMediaType()
318
    {
319
        return $this->mediaType;
320
    }
321
322
    /**
323
     * Get the size of the uploaded file
324
     *
325
     * @return int
326
     */
327
    public function getMediaSize()
328
    {
329
        return $this->mediaSize;
330
    }
331
332
    /**
333
     * Get the temporary name that the uploaded file was stored under
334
     *
335
     * @return string
336
     */
337
    public function getMediaTmpName()
338
    {
339
        return $this->mediaTmpName;
340
    }
341
342
    /**
343
     * Get the saved filename
344
     *
345
     * @return string
346
     */
347
    public function getSavedFileName()
348
    {
349
        return $this->savedFileName;
350
    }
351
352
    /**
353
     * Get the destination the file is saved to
354
     *
355
     * @return string
356
     */
357
    public function getSavedDestination()
358
    {
359
        return $this->savedDestination;
360
    }
361
362
    /**
363
     * Check the file and copy it to the destination
364
     *
365
     * @param  int $chmod
366
     * @return bool
367
     */
368
    public function upload($chmod = 0644)
369
    {
370
        if ($this->uploadDir == '') {
371
            $this->setErrors(_ER_UP_UPLOADDIRNOTSET);
372
373
            return false;
374
        }
375
        if (!is_dir($this->uploadDir)) {
376
            $this->setErrors(sprintf(_ER_UP_FAILEDOPENDIR, $this->uploadDir));
377
378
            return false;
379
        }
380
        if (!is_writable($this->uploadDir)) {
381
            $this->setErrors(sprintf(_ER_UP_FAILEDOPENDIRWRITE, $this->uploadDir));
382
383
            return false;
384
        }
385
        $this->sanitizeMultipleExtensions();
386
387
        if (!$this->checkMaxFileSize()) {
388
            return false;
389
        }
390
        if (!$this->checkMaxWidth()) {
391
            return false;
392
        }
393
        if (!$this->checkMaxHeight()) {
394
            return false;
395
        }
396
        if (!$this->checkMimeType()) {
397
            return false;
398
        }
399
        if (!$this->checkImageType()) {
400
            return false;
401
        }
402
        if (count($this->errors) > 0) {
403
            return false;
404
        }
405
406
        return $this->_copyFile($chmod);
407
    }
408
409
    /**
410
     * Copy the file to its destination
411
     *
412
     * @param $chmod
413
     * @return bool
414
     */
415
    public function _copyFile($chmod)
416
    {
417
        $matched = array();
418
        if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
419
            $this->setErrors(_ER_UP_INVALIDFILENAME);
420
421
            return false;
422
        }
423
        if (isset($this->targetFileName)) {
424
            $this->savedFileName = $this->targetFileName;
425
        } elseif (isset($this->prefix)) {
426
            $this->savedFileName = uniqid($this->prefix) . '.' . strtolower($matched[1]);
427
        } else {
428
            $this->savedFileName = strtolower($this->mediaName);
429
        }
430
431
        $this->savedFileName = iconv('UTF-8', 'ASCII//TRANSLIT', $this->savedFileName);
432
        $this->savedFileName = preg_replace('!\s+!', '_', $this->savedFileName);
433
        $this->savedFileName = preg_replace("/[^a-zA-Z0-9\._-]/", '', $this->savedFileName);
434
435
        $this->savedDestination = $this->uploadDir . '/' . $this->savedFileName;
436
        if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
437
            $this->setErrors(sprintf(_ER_UP_FAILEDSAVEFILE, $this->savedDestination));
438
439
            return false;
440
        }
441
        // Check IE XSS before returning success
442
        $ext = strtolower(substr(strrchr($this->savedDestination, '.'), 1));
443
        if (in_array($ext, $this->imageExtensions)) {
444
            $info = @getimagesize($this->savedDestination);
445
            if ($info === false || $this->imageExtensions[(int)$info[2]] != $ext) {
446
                $this->setErrors(_ER_UP_SUSPICIOUSREFUSED);
447
                @unlink($this->savedDestination);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
448
449
                return false;
450
            }
451
        }
452
        @chmod($this->savedDestination, $chmod);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
453
454
        return true;
455
    }
456
457
    /**
458
     * Is the file the right size?
459
     *
460
     * @return bool
461
     */
462
    public function checkMaxFileSize()
463
    {
464
        if (!isset($this->maxFileSize)) {
465
            return true;
466
        }
467
        if ($this->mediaSize > $this->maxFileSize) {
468
            $this->setErrors(sprintf(_ER_UP_FILESIZETOOLARGE, $this->maxFileSize, $this->mediaSize));
469
470
            return false;
471
        }
472
473
        return true;
474
    }
475
476
    /**
477
     * Is the picture the right width?
478
     *
479
     * @return bool
480
     */
481 View Code Duplication
    public function checkMaxWidth()
482
    {
483
        if (!isset($this->maxWidth)) {
484
            return true;
485
        }
486
        if (false !== $dimension = getimagesize($this->mediaTmpName)) {
487
            if ($dimension[0] > $this->maxWidth) {
488
                $this->setErrors(sprintf(_ER_UP_FILEWIDTHTOOLARGE, $this->maxWidth, $dimension[0]));
489
490
                return false;
491
            }
492
        } else {
493
            trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
494
        }
495
496
        return true;
497
    }
498
499
    /**
500
     * Is the picture the right height?
501
     *
502
     * @return bool
503
     */
504 View Code Duplication
    public function checkMaxHeight()
505
    {
506
        if (!isset($this->maxHeight)) {
507
            return true;
508
        }
509
        if (false !== $dimension = getimagesize($this->mediaTmpName)) {
510
            if ($dimension[1] > $this->maxHeight) {
511
                $this->setErrors(sprintf(_ER_UP_FILEHEIGHTTOOLARGE, $this->maxHeight, $dimension[1]));
512
513
                return false;
514
            }
515
        } else {
516
            trigger_error(sprintf(_ER_UP_FAILEDFETCHIMAGESIZE, $this->mediaTmpName), E_USER_WARNING);
517
        }
518
519
        return true;
520
    }
521
522
    /**
523
     * Check whether or not the uploaded file type is allowed
524
     *
525
     * @return bool
526
     */
527
    public function checkMimeType()
528
    {
529
        if (empty($this->mediaRealType) && empty($this->allowUnknownTypes)) {
530
            $this->setErrors(_ER_UP_UNKNOWNFILETYPEREJECTED);
531
532
            return false;
533
        }
534
535
        if ((!empty($this->allowedMimeTypes) && !in_array($this->mediaRealType, $this->allowedMimeTypes)) || (!empty($this->deniedMimeTypes) && in_array($this->mediaRealType, $this->deniedMimeTypes))) {
536
            $this->setErrors(sprintf(_ER_UP_MIMETYPENOTALLOWED, $this->mediaType));
537
538
            return false;
539
        }
540
541
        return true;
542
    }
543
544
    /**
545
     * Check whether or not the uploaded image type is valid
546
     *
547
     * @return bool
548
     */
549
    public function checkImageType()
550
    {
551
        if (empty($this->checkImageType)) {
552
            return true;
553
        }
554
555
        if (('image' === substr($this->mediaType, 0, strpos($this->mediaType, '/'))) || (!empty($this->mediaRealType) && 'image' === substr($this->mediaRealType, 0, strpos($this->mediaRealType, '/')))) {
556
            if (!($info = @getimagesize($this->mediaTmpName))) {
557
                $this->setErrors(_ER_UP_INVALIDIMAGEFILE);
558
559
                return false;
560
            }
561
        }
562
563
        return true;
564
    }
565
566
    /**
567
     * Sanitize executable filename with multiple extensions
568
     */
569
    public function sanitizeMultipleExtensions()
570
    {
571
        if (empty($this->extensionsToBeSanitized)) {
572
            return null;
573
        }
574
575
        $patterns = array();
576
        $replaces = array();
577
        foreach ($this->extensionsToBeSanitized as $ext) {
578
            $patterns[] = "/\." . preg_quote($ext) . "\./i";
579
            $replaces[] = '_' . $ext . '.';
580
        }
581
        $this->mediaName = preg_replace($patterns, $replaces, $this->mediaName);
582
    }
583
584
    /**
585
     * Add an error
586
     *
587
     * @param string $error
588
     */
589
    public function setErrors($error)
590
    {
591
        $this->errors[] = trim($error);
592
    }
593
594
    /**
595
     * Get generated errors
596
     *
597
     * @param  bool $ashtml Format using HTML?
598
     * @return array |string    Array of array messages OR HTML string
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
599
     */
600
    public function &getErrors($ashtml = true)
601
    {
602
        if (!$ashtml) {
603
            return $this->errors;
604
        } else {
605
            $ret = '';
606
            if (count($this->errors) > 0) {
607
                $ret = '<h4>' . sprintf(_ER_UP_ERRORSRETURNED, htmlspecialchars($this->mediaName, ENT_QUOTES)) . '</h4>';
608
                foreach ($this->errors as $error) {
609
                    $ret .= $error . '<br>';
610
                }
611
            }
612
613
            return $ret;
614
        }
615
    }
616
}
617