|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Intervention\Image\Traits; |
| 6 | + |
| 7 | +use Intervention\Image\Interfaces\DataUriInterface; |
| 8 | +use Stringable; |
| 9 | + |
| 10 | +trait CanDetectImageSources |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Returns true if the specified content could be base64 encoded. |
| 14 | + * |
| 15 | + * This does not necessarily mean that the content actually meets this |
| 16 | + * assumption, but only serves as an initial filter. |
| 17 | + */ |
| 18 | + protected function couldBeBase64Data(mixed $input): bool |
| 19 | + { |
| 20 | + if (!is_string($input) && !$input instanceof Stringable) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + $input = (string) $input; |
| 25 | + |
| 26 | + if (str_ends_with($input, '=')) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + return preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $input) === 1; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns true if the specified content could be binary data. |
| 35 | + * |
| 36 | + * This does not necessarily mean that the content actually meets this |
| 37 | + * assumption, but only serves as an initial filter. |
| 38 | + */ |
| 39 | + protected function couldBeBinaryData(mixed $input): bool |
| 40 | + { |
| 41 | + if (!is_string($input) && !$input instanceof Stringable) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + $input = (string) $input; |
| 46 | + |
| 47 | + // contains non printable ascii |
| 48 | + if (preg_match('/[^ -~]/', $input) === 1) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + // contains only printable ascii |
| 53 | + if (preg_match('/^[ -~]+$/', $input) === 1) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns true if the specified content could be a data uri. |
| 62 | + * |
| 63 | + * This does not necessarily mean that the content actually meets this |
| 64 | + * assumption, but only serves as an initial filter. |
| 65 | + */ |
| 66 | + protected function couldBeDataUrl(mixed $input): bool |
| 67 | + { |
| 68 | + if ($input instanceof DataUriInterface) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + return is_string($input) && str_starts_with($input, 'data:'); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns true if the specified content could be a file path. |
| 77 | + * |
| 78 | + * This does not necessarily mean that the content actually meets this |
| 79 | + * assumption, but only serves as an initial filter. |
| 80 | + */ |
| 81 | + protected function couldBeFilePath(mixed $input): bool |
| 82 | + { |
| 83 | + if (!is_string($input) && !$input instanceof Stringable) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $input = (string) $input; |
| 88 | + |
| 89 | + if (strlen($input) > PHP_MAXPATHLEN) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + if (str_starts_with($input, DIRECTORY_SEPARATOR)) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + if (preg_match('/[^ -~]/', $input) === 1) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | +} |
0 commit comments