|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package File manager |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\file_manager\handlers\validators; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\handlers\validator\Element; |
|
13
|
|
|
use gplcart\core\models\UserRole; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Provides methods to validate module settings |
|
17
|
|
|
*/ |
|
18
|
|
|
class Settings extends Element |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* User role model instance |
|
23
|
|
|
* @var \gplcart\core\models\UserRole $role |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $role; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Settings constructor. |
|
29
|
|
|
* @param UserRole $role |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(UserRole $role) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->role = $role; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Validates module settings |
|
40
|
|
|
* @param array $submitted |
|
41
|
|
|
* @param array $options |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
public function validateSettings(array &$submitted, array $options = array()) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->options = $options; |
|
47
|
|
|
$this->submitted = &$submitted; |
|
48
|
|
|
|
|
49
|
|
|
$this->validateInitialPathSettings(); |
|
50
|
|
|
$this->validateLimitSettings(); |
|
51
|
|
|
$this->validateAccessSettings(); |
|
52
|
|
|
$this->validateExtensionSettings(); |
|
53
|
|
|
$this->validateFileSizeSettings(); |
|
54
|
|
|
|
|
55
|
|
|
return $this->getResult(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Validates the initial path field |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function validateInitialPathSettings() |
|
63
|
|
|
{ |
|
64
|
|
|
$field = 'initial_path'; |
|
65
|
|
|
$path = $this->getSubmitted($field); |
|
66
|
|
|
|
|
67
|
|
|
if (strlen($path) > 0 && preg_match('/^[\w-]+[\w-\/]*[\w-]+$|^[\w-]$/', $path) !== 1) { |
|
68
|
|
|
$this->setErrorInvalid($field, $this->translation->text('Initial path')); |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$destination = gplcart_file_absolute($path); |
|
73
|
|
|
|
|
74
|
|
|
if (!file_exists($destination)) { |
|
75
|
|
|
$this->setError($field, $this->translation->text('Destination does not exist')); |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (!is_readable($destination)) { |
|
80
|
|
|
$this->setError($field, $this->translation->text('Destination is not readable')); |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Validates limit field |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function validateLimitSettings() |
|
92
|
|
|
{ |
|
93
|
|
|
$field = 'limit'; |
|
94
|
|
|
$limit = $this->getSubmitted($field); |
|
95
|
|
|
|
|
96
|
|
|
if (!ctype_digit($limit) || strlen($limit) > 3) { |
|
97
|
|
|
$this->setErrorInteger($field, $this->translation->text('Limit')); |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Validates the path access field |
|
106
|
|
|
* @return boolean |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function validateAccessSettings() |
|
109
|
|
|
{ |
|
110
|
|
|
$field = 'access'; |
|
111
|
|
|
$rules = $this->getSubmitted($field); |
|
112
|
|
|
|
|
113
|
|
|
$errors = $converted = array(); |
|
114
|
|
|
foreach (gplcart_string_explode_multiline($rules) as $line => $rule) { |
|
115
|
|
|
|
|
116
|
|
|
$line++; |
|
117
|
|
|
|
|
118
|
|
|
$parts = gplcart_string_explode_whitespace($rule, 2); |
|
119
|
|
|
|
|
120
|
|
|
if (count($parts) != 2) { |
|
121
|
|
|
$errors[] = $line; |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
list($role_id, $pattern) = $parts; |
|
126
|
|
|
|
|
127
|
|
|
if (strlen(trim($pattern, '\\/')) != strlen($pattern)) { |
|
128
|
|
|
$errors[] = $line; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (!$this->isValidRoleSettings($role_id)) { |
|
132
|
|
|
$errors[] = $line; |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$normalized_pattern = gplcart_path_normalize($pattern); |
|
137
|
|
|
|
|
138
|
|
|
if (!gplcart_string_is_regexp($normalized_pattern) || strlen($normalized_pattern) > 250) { |
|
139
|
|
|
$errors[] = $line; |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$converted[$role_id][] = $normalized_pattern; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (!empty($errors)) { |
|
147
|
|
|
$error = $this->translation->text('Error on line @num', array('@num' => implode(',', $errors))); |
|
148
|
|
|
$this->setError($field, $error); |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$this->setSubmitted($field, $converted); |
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Validates the allowed extensions field |
|
158
|
|
|
* @return boolean |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function validateExtensionSettings() |
|
161
|
|
|
{ |
|
162
|
|
|
$field = 'extension_limit'; |
|
163
|
|
|
$rules = $this->getSubmitted($field); |
|
164
|
|
|
|
|
165
|
|
|
$errors = $converted = array(); |
|
166
|
|
|
foreach (gplcart_string_explode_multiline($rules) as $line => $rule) { |
|
167
|
|
|
|
|
168
|
|
|
$line++; |
|
169
|
|
|
|
|
170
|
|
|
$parts = gplcart_string_explode_whitespace($rule, 2); |
|
171
|
|
|
|
|
172
|
|
|
if (count($parts) != 2) { |
|
173
|
|
|
$errors[] = $line; |
|
174
|
|
|
continue; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
list($role_id, $extensions) = $parts; |
|
178
|
|
|
|
|
179
|
|
|
if (!$this->isValidRoleSettings($role_id)) { |
|
180
|
|
|
$errors[] = $line; |
|
181
|
|
|
continue; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$filtered_extensions = array_filter(array_map('trim', explode(',', $extensions))); |
|
185
|
|
|
|
|
186
|
|
|
foreach ($filtered_extensions as $extension) { |
|
187
|
|
|
if (!ctype_alnum($extension) || strlen($extension) > 250) { |
|
188
|
|
|
$errors[] = $line; |
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$converted[$role_id] = $filtered_extensions; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (!empty($errors)) { |
|
197
|
|
|
$error = $this->translation->text('Error on line @num', array('@num' => implode(',', $errors))); |
|
198
|
|
|
$this->setError($field, $error); |
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->setSubmitted($field, $converted); |
|
203
|
|
|
return true; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Validates the file size limit field |
|
208
|
|
|
* @return boolean |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function validateFileSizeSettings() |
|
211
|
|
|
{ |
|
212
|
|
|
$field = 'filesize_limit'; |
|
213
|
|
|
$rules = $this->getSubmitted($field); |
|
214
|
|
|
|
|
215
|
|
|
$errors = $converted = array(); |
|
216
|
|
|
foreach (gplcart_string_explode_multiline($rules) as $line => $rule) { |
|
217
|
|
|
|
|
218
|
|
|
$line++; |
|
219
|
|
|
|
|
220
|
|
|
$parts = gplcart_string_explode_whitespace($rule, 2); |
|
221
|
|
|
|
|
222
|
|
|
if (count($parts) != 2) { |
|
223
|
|
|
$errors[] = $line; |
|
224
|
|
|
continue; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
list($role_id, $filesize) = $parts; |
|
228
|
|
|
|
|
229
|
|
|
if (!$this->isValidRoleSettings($role_id)) { |
|
230
|
|
|
$errors[] = $line; |
|
231
|
|
|
continue; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if (!ctype_digit($filesize) || strlen($filesize) > 12) { |
|
235
|
|
|
$errors[] = $line; |
|
236
|
|
|
break; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$converted[$role_id] = $filesize; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if (!empty($errors)) { |
|
243
|
|
|
$error = $this->translation->text('Error on line @num', array('@num' => implode(',', $errors))); |
|
244
|
|
|
$this->setError($field, $error); |
|
245
|
|
|
return false; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$this->setSubmitted($field, $converted); |
|
249
|
|
|
return true; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Check whether the value is an existing role |
|
254
|
|
|
* @param string $role_id |
|
255
|
|
|
* @return bool |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function isValidRoleSettings($role_id) |
|
258
|
|
|
{ |
|
259
|
|
|
return ctype_digit($role_id) && $this->role->get($role_id); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|