Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.57% |
11 / 14 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
InputValidator | |
78.57% |
11 / 14 |
|
75.00% |
3 / 4 |
8.63 | |
0.00% |
0 / 1 |
checkMimeType | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
5.93 | |||
checkSource | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
checkDestination | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkSourceAndDestination | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
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 <it@rosell.dk> |
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 | } |