1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Provides access to the http request information |
5
|
|
|
*/ |
6
|
|
|
class Request |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $lang; |
10
|
|
|
private $clang; |
11
|
|
|
private $page; |
12
|
|
|
private $vocab; |
13
|
|
|
private $vocabids; |
14
|
|
|
private $uri; |
15
|
|
|
private $letter; |
16
|
|
|
private $model; |
17
|
|
|
private $queryParams; |
18
|
|
|
private $queryParamsPOST; |
19
|
|
|
private $serverConstants; |
20
|
|
|
private $cookies; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initializes the Request Object |
24
|
|
|
*/ |
25
|
|
|
public function __construct($model) |
26
|
|
|
{ |
27
|
|
|
$this->model = $model; |
28
|
|
|
|
29
|
|
|
// Store GET parameters in a local array, so we can mock them in tests. |
30
|
|
|
// We do not apply any filters at this point. |
31
|
|
|
$this->queryParams = []; |
32
|
|
|
foreach (filter_input_array(INPUT_GET) ?: [] as $key => $val) { |
33
|
|
|
$this->queryParams[$key] = $val; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Store POST parameters in a local array, so we can mock them in tests. |
37
|
|
|
// We do not apply any filters at this point. |
38
|
|
|
$this->queryParamsPOST = []; |
39
|
|
|
foreach (filter_input_array(INPUT_POST) ?: [] as $key => $val) { |
40
|
|
|
$this->queryParamsPOST[$key] = $val; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Store SERVER parameters in a local array, so we can mock them in tests. |
44
|
|
|
// We do not apply any filters at this point. |
45
|
|
|
$this->serverConstants = []; |
46
|
|
|
foreach (filter_input_array(INPUT_SERVER) ?: [] as $key => $val) { |
47
|
|
|
$this->serverConstants[$key] = $val; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Store cookies in a local array, so we can mock them in tests. |
51
|
|
|
// We do not apply any filters at this point. |
52
|
|
|
$this->cookies = []; |
53
|
|
|
foreach (filter_input_array(INPUT_COOKIE) ?: [] as $key => $val) { |
54
|
|
|
$this->cookies[$key] = $val; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set a GET query parameter to mock it in tests. |
60
|
|
|
* @param string $paramName parameter name |
61
|
|
|
* @param string $value parameter value |
62
|
|
|
*/ |
63
|
|
|
public function setQueryParam($paramName, $value) |
64
|
|
|
{ |
65
|
|
|
$this->queryParams[$paramName] = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set a POST query parameter to mock it in tests. |
70
|
|
|
* @param string $paramName parameter name |
71
|
|
|
* @param string $value parameter value |
72
|
|
|
*/ |
73
|
|
|
public function setQueryParamPOST($paramName, $value) |
74
|
|
|
{ |
75
|
|
|
$this->queryParamsPOST[$paramName] = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set a SERVER constant to mock it in tests. |
80
|
|
|
* @param string $paramName parameter name |
81
|
|
|
* @param string $value parameter value |
82
|
|
|
*/ |
83
|
|
|
public function setServerConstant($paramName, $value) |
84
|
|
|
{ |
85
|
|
|
$this->serverConstants[$paramName] = $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set a cookie to mock it in tests. |
90
|
|
|
* @param string $paramName parameter name |
91
|
|
|
* @param string $value parameter value |
92
|
|
|
*/ |
93
|
|
|
public function setCookie($paramName, $value) |
94
|
|
|
{ |
95
|
|
|
$this->cookies[$paramName] = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the requested GET query parameter as a string. Backslashes are stripped for security reasons. |
100
|
|
|
* @param string $paramName parameter name |
101
|
|
|
* @return string parameter content, or null if no parameter found |
102
|
|
|
*/ |
103
|
|
|
public function getQueryParam($paramName) |
104
|
|
|
{ |
105
|
|
|
if (!isset($this->queryParams[$paramName])) return null; |
106
|
|
|
$val = filter_var($this->queryParams[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
|
|
|
|
107
|
|
|
return ($val !== null ? str_replace('\\', '', $val) : null); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the requested GET query parameter as a string, with no sanitizing. |
112
|
|
|
* @param string $paramName parameter name |
113
|
|
|
* @return string parameter content, or null if no parameter found |
114
|
|
|
*/ |
115
|
|
|
public function getQueryParamRaw($paramName) |
116
|
|
|
{ |
117
|
|
|
return isset($this->queryParams[$paramName]) ? $this->queryParams[$paramName] : null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the requested POST query parameter as a string. Backslashes are stripped for security reasons. |
122
|
|
|
* @param string $paramName parameter name |
123
|
|
|
* @param int $maxlength maximum length of parameter, or null if unlimited |
124
|
|
|
* @return string parameter content, or null if no parameter found |
125
|
|
|
*/ |
126
|
|
|
public function getQueryParamPOST($paramName, $maxlength=null) |
127
|
|
|
{ |
128
|
|
|
if (!isset($this->queryParamsPOST[$paramName])) return null; |
129
|
|
|
$val = filter_var($this->queryParamsPOST[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
|
|
|
|
130
|
|
|
if ($maxlength !== null) { |
131
|
|
|
return substr($val, 0, $maxlength); |
132
|
|
|
} else { |
133
|
|
|
return $val; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getQueryParamBoolean($paramName, $default) |
138
|
|
|
{ |
139
|
|
|
$val = $this->getQueryParamRaw($paramName); |
140
|
|
|
if ($val !== NULL) { |
|
|
|
|
141
|
|
|
$val = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
142
|
|
|
} |
143
|
|
|
return ($val !== null) ? $val : $default; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getServerConstant($paramName) |
147
|
|
|
{ |
148
|
|
|
if (!isset($this->serverConstants[$paramName])) return null; |
149
|
|
|
return filter_var($this->serverConstants[$paramName], FILTER_SANITIZE_ADD_SLASHES); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getCookie($paramName) |
153
|
|
|
{ |
154
|
|
|
if (!isset($this->cookies[$paramName])) return null; |
155
|
|
|
return filter_var($this->cookies[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getLang() |
159
|
|
|
{ |
160
|
|
|
return $this->lang; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets the language variable |
165
|
|
|
* @param string $lang |
166
|
|
|
*/ |
167
|
|
|
public function setLang($lang) |
168
|
|
|
{ |
169
|
|
|
if ($lang !== '') { |
170
|
|
|
$this->lang = $lang; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getContentLang() |
176
|
|
|
{ |
177
|
|
|
return $this->clang; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Sets the language variable |
182
|
|
|
* @param string $clang |
183
|
|
|
*/ |
184
|
|
|
public function setContentLang($clang) |
185
|
|
|
{ |
186
|
|
|
$this->clang = $this->verifyContentLang($clang); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function verifyContentLang($lang) |
190
|
|
|
{ |
191
|
|
|
if ($this->vocab) { |
192
|
|
|
return $this->vocab->verifyVocabularyLanguage($lang); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $lang; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getPage() |
199
|
|
|
{ |
200
|
|
|
return $this->page; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets the page id variable eg. 'groups' |
205
|
|
|
* @param string $page |
206
|
|
|
*/ |
207
|
|
|
public function setPage($page) |
208
|
|
|
{ |
209
|
|
|
if ($page !== '') { |
210
|
|
|
$this->page = $page; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function getRequestUri() |
216
|
|
|
{ |
217
|
|
|
return $this->getServerConstant('HTTP_HOST') . $this->getServerConstant('REQUEST_URI'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat' |
222
|
|
|
* @param string $newlang new UI language to set |
223
|
|
|
* @return string the relative url of the page |
224
|
|
|
*/ |
225
|
|
|
public function getLangUrl($newlang=null) |
226
|
|
|
{ |
227
|
|
|
$script_name = str_replace('/index.php', '', $this->getServerConstant('SCRIPT_NAME')); |
228
|
|
|
$langurl = substr(str_replace($script_name, '', strval($this->getServerConstant('REQUEST_URI'))), 1); |
229
|
|
|
if ($newlang !== null) { |
230
|
|
|
$langurl = preg_replace("#^(.*/)?{$this->lang}/#", "$1{$newlang}/", $langurl); |
231
|
|
|
} |
232
|
|
|
// make sure that the resulting URL isn't interpreted as an absolute URL |
233
|
|
|
$langurl = str_replace(":", "", $langurl); |
234
|
|
|
return $langurl; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getLetter() |
238
|
|
|
{ |
239
|
|
|
return (isset($this->letter)) ? $this->letter : ''; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Sets the page id variable eg. 'B' |
244
|
|
|
* @param string $letter |
245
|
|
|
*/ |
246
|
|
|
public function setLetter($letter) |
247
|
|
|
{ |
248
|
|
|
if ($letter !== '') { |
249
|
|
|
$this->letter = $letter; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getURI() |
255
|
|
|
{ |
256
|
|
|
return $this->uri; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Sets the uri variable |
261
|
|
|
* @param string $uri |
262
|
|
|
*/ |
263
|
|
|
public function setURI($uri) |
264
|
|
|
{ |
265
|
|
|
if ($uri !== '') { |
266
|
|
|
$this->uri = rtrim(strval($uri)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso' |
273
|
|
|
* @param string $ids |
274
|
|
|
*/ |
275
|
|
|
public function setVocabids($ids) |
276
|
|
|
{ |
277
|
|
|
$this->vocabids = $ids; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function getVocabid() |
281
|
|
|
{ |
282
|
|
|
if ($this->vocabids) { |
283
|
|
|
return $this->vocabids; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return isset($this->vocab) ? $this->vocab->getId() : ''; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Creates a Vocabulary object |
291
|
|
|
* @param string $vocabid |
292
|
|
|
*/ |
293
|
|
|
public function setVocab($vocabid) |
294
|
|
|
{ |
295
|
|
|
if (strpos($vocabid, ' ') !== false) // if there are multiple vocabularies just storing the string |
296
|
|
|
{ |
297
|
|
|
$this->setVocabids($vocabid); |
298
|
|
|
} else { |
299
|
|
|
$this->vocab = $this->model->getVocabulary($vocabid); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return Vocabulary |
306
|
|
|
*/ |
307
|
|
|
public function getVocab() |
308
|
|
|
{ |
309
|
|
|
return $this->vocab; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function getVocabList() { |
313
|
|
|
return $this->model->getVocabularyList(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function getPlugins() { |
317
|
|
|
if ($this->vocab) { |
318
|
|
|
return $this->vocab->getConfig()->getPluginRegister(); |
319
|
|
|
} |
320
|
|
|
return new PluginRegister($this->model->getConfig()->getGlobalPlugins()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Return the version of this Skosmos installation, or "unknown" if |
325
|
|
|
* it cannot be determined. The version information is based on Git tags. |
326
|
|
|
* @return string version |
327
|
|
|
*/ |
328
|
|
|
public function getVersion() : string |
329
|
|
|
{ |
330
|
|
|
return $this->model->getVersion(); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|