PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / src / Helpers / InputValidator.php

InputValidator.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at vendor/rosell-dk/webp-convert/src/Helpers/InputValidator.php

62 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WebPConvert\Helpers;
4
5 use WebPConvert\Helpers\MimeType;
6 use WebPConvert\Helpers\PathChecker;
7 use WebPConvert\Exceptions\InvalidInputException;
8 use WebPConvert\Exceptions\InvalidInput\InvalidImageTypeException;
9
10 /**
11 * Functions for sanitizing.
12 *
13 * @package WebPConvert
14 * @author Bjørn Rosell <[email protected]>
15 * @since Class available since Release 2.0.6
16 */
17 class InputValidator
18 {
19
20 private static $allowedMimeTypes = [
21 'image/jpeg',
22 'image/png'
23 ];
24
25 /**
26 * Check mimetype and if file path is ok and exists
27 */
28 public static function checkMimeType($filePath, $allowedMimeTypes = null)
29 {
30 if (is_null($allowedMimeTypes)) {
31 $allowedMimeTypes = self::$allowedMimeTypes;
32 }
33 // the following also tests that file path is ok and file exists
34 $fileMimeType = MimeType::getMimeTypeDetectionResult($filePath);
35
36 if (is_null($fileMimeType)) {
37 throw new InvalidImageTypeException('Image type could not be detected');
38 } elseif ($fileMimeType === false) {
39 throw new InvalidImageTypeException('File seems not to be an image.');
40 } elseif (!in_array($fileMimeType, $allowedMimeTypes)) {
41 throw new InvalidImageTypeException('Unsupported mime type: ' . $fileMimeType);
42 }
43 }
44
45 public static function checkSource($source)
46 {
47 PathChecker::checkSourcePath($source);
48 self::checkMimeType($source);
49 }
50
51 public static function checkDestination($destination)
52 {
53 PathChecker::checkDestinationPath($destination);
54 }
55
56 public static function checkSourceAndDestination($source, $destination)
57 {
58 self::checkSource($source);
59 self::checkDestination($destination);
60 }
61 }
62