1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File Validation |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Files |
10
|
|
|
* @author Daniel Schalla <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\files; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* File Validate |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Files |
23
|
|
|
* @author Daniel Schalla <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Validate |
31
|
|
|
* |
32
|
|
|
* @category Core |
33
|
|
|
* @package Files |
34
|
|
|
* @author Daniel Schalla <[email protected]> |
35
|
|
|
* @copyright 2013 cSphere Team |
36
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
37
|
|
|
* @link http://www.csphere.eu |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
class Validate |
41
|
|
|
{ |
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var array File Location |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
private $_file; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Saves the File which must be validated |
50
|
|
|
* |
51
|
|
|
* @param array $file file Location |
52
|
|
|
*/ |
53
|
|
|
public function __construct($file) |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
if (file_exists($file['tmp_name'])) { |
57
|
|
|
$this->_file = $file; |
58
|
|
|
return true; |
|
|
|
|
59
|
|
|
} else { |
60
|
|
|
return false; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check the file in dependency on a validationSet |
67
|
|
|
* on its mime and file extension |
68
|
|
|
* |
69
|
|
|
* @param string $validationSet Defines the ValidationSet (e.g. image) |
70
|
|
|
* @param bool $mime Flag for Mime Check |
71
|
|
|
* @param bool $fileEnding Flag for File EndingCheck |
72
|
|
|
* |
73
|
|
|
* @throws \ErrorException |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
**/ |
|
|
|
|
77
|
|
|
|
78
|
|
|
public function check($validationSet, $mime = true, $fileEnding = true) |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
$validate = true; |
82
|
|
|
|
83
|
|
|
if (!empty(Validate::$_fileEnding[$validationSet]) |
84
|
|
|
&& !empty(Validate::$_mime[$validationSet]) |
85
|
|
|
) { |
86
|
|
|
if ($mime) { |
87
|
|
|
$validate = $this->_mimeCheck($validationSet); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($fileEnding && $validate) { |
91
|
|
|
$validate = $this->_fileEndingCheck($validationSet); |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
throw new \ErrorException('Validation Set does not exist!'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $validate; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Compares file mime with filter set |
102
|
|
|
* |
103
|
|
|
* @param string $validationSet Defines the ValidationSet (e.g. image) |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
|
|
|
|
107
|
|
|
|
108
|
|
|
private function _mimeCheck($validationSet) |
109
|
|
|
{ |
110
|
|
|
$validate = false; |
111
|
|
|
$whiteList = Validate::$_mime[$validationSet]; |
112
|
|
|
|
113
|
|
|
$finfo = new \finfo(FILEINFO_MIME_TYPE); |
114
|
|
|
$mime = $finfo->file($this->_file['tmp_name']); |
115
|
|
|
|
116
|
|
|
foreach ($whiteList as $entry) { |
117
|
|
|
if ($mime == $entry) { |
118
|
|
|
$validate = true; |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $validate; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Compares file extension with filter set |
128
|
|
|
* |
129
|
|
|
* @param string $validationSet Defines the ValidationSet (e.g. image) |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
private function _fileEndingCheck($validationSet) |
134
|
|
|
{ |
135
|
|
|
$validate = false; |
136
|
|
|
$whiteList = Validate::$_fileEnding[$validationSet]; |
137
|
|
|
|
138
|
|
|
$ext = pathinfo($this->_file['name'], PATHINFO_EXTENSION); |
139
|
|
|
|
140
|
|
|
foreach ($whiteList as $entry) { |
141
|
|
|
if ($ext == $entry) { |
142
|
|
|
$validate = true; |
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $validate; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Defines several set of allowed file endings depending on filter |
153
|
|
|
* |
154
|
|
|
* @var array |
155
|
|
|
*/ |
156
|
|
|
/*private static $_fileEnding = [ |
157
|
|
|
"image" => ["jpeg", "jpg", "png", "gif"], |
158
|
|
|
];*/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Defines several set of allowed mime types depending on filter |
162
|
|
|
* |
163
|
|
|
* @var array |
164
|
|
|
*/ |
165
|
|
|
/* |
166
|
|
|
private static $_mime = [ |
167
|
|
|
"image" => ["image/jpeg", "image/png", "image/gif"], |
168
|
|
|
]; |
169
|
|
|
*/ |
170
|
|
|
} |
171
|
|
|
|