/
Request.php
313 lines (274 loc) · 8.11 KB
/
Request.php
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
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2013
* @license http://opensource.org/licenses/mit-license.php
*/
namespace Munee;
use Munee\ErrorException;
use Munee\Asset\Registry;
/**
* Munee Request Class
*
* @author Cody Lundquist
*/
class Request
{
/**
* @var string
*/
public $webroot = WEBROOT;
/**
* @var string
*/
public $ext;
/**
* @var array
*/
public $params = array();
/**
* @var array
*/
public $files = array();
/**
* @var array
*/
public $options = array();
/**
* @var array
*/
protected $rawParams = array();
/**
* @var array
*/
protected $allowedParams = array();
/**
* @var string
*/
protected $rawFiles;
/**
* Constructor
*
* @param array $options
*/
public function __construct($options = array())
{
$this->options = $options;
$this->rawFiles = isset($_GET['files']) ? $_GET['files'] : '';
unset($_GET['files']);
$this->rawParams = $_GET;
}
/**
* Sets the document root.
*
* @param string $path
*
* @return object
*/
public function setWebroot($path)
{
$this->webroot = $path;
return $this;
}
/**
* Sets either an individual _rawParams key - or overwrites the whole array.
*
* @param mixed $key
* @param mixed $value
*
* @return object
*/
public function setRawParam($key, $value = null)
{
if (is_array($key)) {
$this->rawParams = $key;
} else {
$this->rawParams[$key] = $value;
}
return $this;
}
/**
* Returns the pre-parsed raw params
*
* @return array
*/
public function getRawParams()
{
return $this->rawParams;
}
/**
* Sets the $rawFiles.
*
* @param string $files
*
* @return object
*/
public function setFiles($files)
{
$this->rawFiles = $files;
return $this;
}
/**
* Parses the $rawFiles and does sanity checks
*
* @throws ErrorException
* @throws Asset\NotFoundException
*/
public function init()
{
if (empty($this->rawFiles)) {
throw new ErrorException('No file specified; make sure you are using the correct .htaccess rules.');
}
// Handle legacy code for minifying
if (preg_match('%^/minify/%', $this->rawFiles)) {
$this->rawFiles = substr($this->rawFiles, 7);
$this->setRawParam('minify', 'true');
}
$this->ext = strtolower(pathinfo($this->rawFiles, PATHINFO_EXTENSION));
$supportedExtensions = Registry::getSupportedExtensions($this->ext);
// Suppressing errors because Exceptions thrown in the callback cause Warnings.
$webroot = $this->webroot;
$this->files = @array_map(function($v) use ($supportedExtensions, $webroot) {
// Make sure all the file extensions are supported
if (! in_array(strtolower(pathinfo($v, PATHINFO_EXTENSION)), $supportedExtensions)) {
throw new ErrorException('All requested files need to be: ' . implode(', ', $supportedExtensions));
}
// Strip any parent directory slugs (../) - loop through until they are all gone
$count = 1;
while ($count > 0) {
$v = preg_replace('%(/\\.\\.?|\\.\\.?/)%', '', $v, -1, $count);
// If there is no slash prefix, add it back in
if (substr($v, 0, 1) != '/') {
$v = '/' . $v;
}
}
return $webroot . $v;
}, explode(',', $this->rawFiles));
}
/**
* Parse query string parameter arguments based on mapped allowed params
*
* @param array $allowedParams
*/
public function parseParams($allowedParams)
{
$this->allowedParams = $allowedParams;
$this->setDefaultParams();
foreach ($this->rawParams as $checkParam => $value) {
if (! $paramOptions = $this->getParamOptions($checkParam)) {
continue;
}
$param = $paramOptions['param'];
$options = $paramOptions['options'];
$paramValue = $this->getParamValue($param, $options, $value);
if (isset($this->params[$param]) && is_array($this->params[$param])) {
$this->params[$param] = array_merge($this->params[$param], $paramValue);
} else {
$this->params[$param] = $paramValue;
}
}
}
/**
* Setup the default values for the allowed parameters
*/
protected function setDefaultParams()
{
foreach ($this->allowedParams as $param => $options) {
$this->params[$param] = null;
if (! empty($options['arguments'])) {
$this->params[$param] = array();
foreach ($options['arguments'] as $arg => $opts) {
if (! empty($opts['default'])) {
$cast = ! empty($opts['cast']) ? $opts['cast'] : 'string';
$this->params[$param][$arg] = $this->castValue($cast, $opts['default']);
}
}
} elseif (! empty($options['default'])) {
$cast = ! empty($options['cast']) ? $options['cast'] : 'string';
$this->params[$param] = $this->castValue($cast, $options['default']);
}
}
}
/**
* Grabs the params options taking into account any aliases
*
* @param $checkParam
*
* @return bool|array
*/
protected function getParamOptions($checkParam)
{
if (isset($this->allowedParams[$checkParam])) {
return array('param' => $checkParam, 'options' => $this->allowedParams[$checkParam]);
} else {
foreach ($this->allowedParams as $param => $options) {
if (! empty($options['alias']) && in_array($checkParam, (array) $options['alias'])) {
return compact('param', 'options');
}
}
}
return false;
}
/**
* Grabs a value from a param by running it through supplied regex
*
* @param $param
* @param $paramOptions
* @param $value
*
* @return string|array
*
* @throws \Munee\ErrorException
*/
protected function getParamValue($param, $paramOptions, $value)
{
if (! empty($paramOptions['arguments'])) {
$ret = array();
foreach ($paramOptions['arguments'] as $arg => $opts) {
$p = $arg;
if (! empty($opts['alias'])) {
$alias = implode('|', (array) $opts['alias']);
$p .= "|\\b{$alias}";
}
$regex = "(\\b{$p})\\[(.*?)\\]";
if (preg_match("%{$regex}%", $value, $match)) {
$ret[$arg] = $this->getParamValue($arg, $opts, $match[2]);
}
}
return $ret;
} else {
// Using RegEx?
if (! empty($paramOptions['regex'])) {
if (! preg_match("%^(?:{$paramOptions['regex']})$%", $value)) {
throw new ErrorException("'{$value}' is not a valid value for: {$param}");
}
}
$cast = ! empty($paramOptions['cast']) ? $paramOptions['cast'] : 'string';
return $this->castValue($cast, $value);
}
}
/**
* Helper function to cast values
*
* @param $cast
* @param $value
*
* @return bool|int|string
*/
protected function castValue($cast, $value)
{
switch ($cast) {
case 'integer';
$value = (integer) $value;
break;
case 'boolean';
$value = in_array($value, array('true', 't', 'yes', 'y'));
break;
case 'string':
default:
$value = (string) $value;
}
return $value;
}
}