Repository navigation
Expand file tree
/
Copy pathvalidator.php
More file actions
320 lines (279 loc) · 7.69 KB
/
Copy pathvalidator.php
File metadata and controls
320 lines (279 loc) · 7.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\skeleton\helper;
use phpbb\exception\runtime_exception;
use phpbb\language\language;
class validator
{
/** @var language */
protected $language;
/**
* Constructor
*
* @param language $language
*/
public function __construct(language $language)
{
$this->language = $language;
}
/**
* Validate the number of authors
* Should be between 1 and 20
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_num_authors($value)
{
if ($value > 0 && $value <= 20 && ctype_digit($value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_NUM_AUTHORS'));
}
/**
* Validate and require the extension name
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_name($value)
{
if (preg_match('#^[a-z][a-z0-9]*$#', $value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_PACKAGE_NAME'));
}
/**
* Validate and require the extension display name
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_display_name($value)
{
if ((string) $value !== '')
{
return $this->validate_generated_string($value, 'SKELETON_INVALID_DISPLAY_NAME');
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_DISPLAY_NAME'));
}
/**
* Validate the extension description for insertion into composer.json
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_description($value)
{
return $this->validate_generated_string($value, 'SKELETON_INVALID_EXTENSION_DESCRIPTION');
}
/**
* Validate and require the extension date/time
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_time($value)
{
if (preg_match('#^\d{4}-\d{2}-\d{2}$#', $value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_EXTENSION_TIME'));
}
/**
* Validate and require the extension version number
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_version($value)
{
if (preg_match('#^\d+(\.\d){1,3}(-(((?:a|b|RC|pl)\d+)|dev))?$#', $value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_EXTENSION_VERSION'));
}
/**
* Validate and require the extension vendor name
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_vendor_name($value)
{
if ($value !== 'core' && preg_match('#^[a-z][a-z0-9]*$#', $value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_VENDOR_NAME'));
}
/**
* Validate the extension homepage URL
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_extension_homepage($value)
{
$value = $this->validate_generated_string($value, 'SKELETON_INVALID_EXTENSION_URL');
if ($value !== '' && filter_var($value, FILTER_VALIDATE_URL) === false)
{
throw new runtime_exception($this->language->lang('SKELETON_INVALID_EXTENSION_URL'));
}
return $value;
}
/**
* Validate the author name
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_author_name($value)
{
return $this->validate_generated_string($value, 'SKELETON_INVALID_AUTHOR_NAME');
}
/**
* Validate the author homepage URL
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_author_homepage($value)
{
$value = $this->validate_generated_string($value, 'SKELETON_INVALID_AUTHOR_URL');
if ($value !== '' && filter_var($value, FILTER_VALIDATE_URL) === false)
{
throw new runtime_exception($this->language->lang('SKELETON_INVALID_AUTHOR_URL'));
}
return $value;
}
/**
* Validate the author email
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_author_email($value)
{
$value = $this->validate_generated_string($value, 'SKELETON_INVALID_AUTHOR_EMAIL');
if ($value !== '' && filter_var($value, FILTER_VALIDATE_EMAIL) === false)
{
throw new runtime_exception($this->language->lang('SKELETON_INVALID_AUTHOR_EMAIL'));
}
return $value;
}
/**
* Validate the author role
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_author_role($value)
{
return $this->validate_generated_string($value, 'SKELETON_INVALID_AUTHOR_ROLE');
}
/**
* Validate and require the phpBB minimum version
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_phpbb_version_min($value)
{
if ($this->check_version($value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_PHPBB_MIN_VERSION'));
}
/**
* Validate and require the phpBB maximum version
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_phpbb_version_max($value)
{
if ($this->check_version($value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_PHPBB_MAX_VERSION'));
}
/**
* Validate and require the PHP version
*
* @param string $value The value to validate
* @return string The valid value
* @throws runtime_exception
*/
public function validate_php_version($value)
{
if ($this->check_version($value))
{
return $value;
}
throw new runtime_exception($this->language->lang('SKELETON_INVALID_PHP_VERSION'));
}
/**
* Validate a string inserted without escaping into generated files.
* Values must be safe inside a JSON string and must not contain a PHP comment
* terminator. Request values are HTML escaped, so inspect their decoded form
* and return safe characters without HTML entities.
*
* @param string $value The value to validate
* @param string $error Language key used for invalid input
* @return string The valid value
* @throws runtime_exception
*/
protected function validate_generated_string($value, $error)
{
$decoded_value = htmlspecialchars_decode((string) $value, ENT_QUOTES);
if (preg_match('/["\\\\\x00-\x1F]/', $decoded_value) || strpos($decoded_value, '*/') !== false)
{
throw new runtime_exception($this->language->lang($error));
}
return htmlspecialchars_decode((string) $value, ENT_NOQUOTES);
}
/**
* Version value check. Checks for values like:
* 1.0.0-RC1
* >=1.0.0
* <1.0@dev
*
* @param string $value The value to check
* @return bool True if valid, false if not
*/
protected function check_version($value)
{
return (bool) preg_match('/^[<>=]*[\d+][\w.@-]+$/', htmlspecialchars_decode($value, ENT_NOQUOTES));
}
}