forked from appwrite/appwrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileType.php
More file actions
91 lines (75 loc) 路 1.7 KB
/
Copy pathFileType.php
File metadata and controls
91 lines (75 loc) 路 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Storage\Validators;
use Exception;
use Utopia\Validator;
class FileType extends Validator
{
/**
* File Types Constants.
*/
const FILE_TYPE_JPEG = 'jpeg';
const FILE_TYPE_GIF = 'gif';
const FILE_TYPE_PNG = 'png';
/**
* File Type Binaries.
*
* @var array
*/
protected $types = array(
self::FILE_TYPE_JPEG => "\xFF\xD8\xFF",
self::FILE_TYPE_GIF => 'GIF',
self::FILE_TYPE_PNG => "\x89\x50\x4e\x47\x0d\x0a",
);
/**
* @var array
*/
protected $whiteList;
/**
* @param array $whiteList
*
* @throws Exception
*/
public function __construct(array $whiteList)
{
foreach ($whiteList as $key) {
if (!isset($this->types[$key])) {
throw new Exception('Unknown file mime type');
}
}
$this->whiteList = $whiteList;
}
public function getDescription()
{
return 'File mime-type is not allowed ';
}
/**
* Is Valid.
*
* Binary check to finds whether a file is of valid type
*
* @see http://stackoverflow.com/a/3313196
*
* @param string $path
*
* @return bool
*/
public function isValid($path)
{
if(!\is_readable($path)) {
return false;
}
$handle = fopen($path, 'r');
if (!$handle) {
return false;
}
$bytes = fgets($handle, 8);
foreach ($this->whiteList as $key) {
if (strpos($bytes, $this->types[$key]) === 0) {
fclose($handle);
return true;
}
}
fclose($handle);
return false;
}
}