Passed
Pull Request — master (#1542)
by
unknown
03:45
created

Request::getLangUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
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 SERVER constant to mock it in tests.
70
     * @param string $paramName parameter name
71
     * @param string $value parameter value
72
     */
73
    public function setServerConstant($paramName, $value)
74
    {
75
        $this->serverConstants[$paramName] = $value;
76
    }
77
78
    /**
79
     * Set a cookie to mock it in tests.
80
     * @param string $paramName parameter name
81
     * @param string $value parameter value
82
     */
83
    public function setCookie($paramName, $value)
84
    {
85
        $this->cookies[$paramName] = $value;
86
    }
87
88
    /**
89
     * Return the requested GET query parameter as a string. Backslashes are stripped for security reasons.
90
     * @param string $paramName parameter name
91
     * @return string parameter content, or null if no parameter found
92
     */
93
    public function getQueryParam($paramName)
94
    {
95
        if (!isset($this->queryParams[$paramName])) return null;
96
        $val = filter_var($this->queryParams[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
0 ignored issues
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
        return ($val !== null ? str_replace('\\', '', $val) : null);
98
    }
99
100
    /**
101
     * Return the requested GET query parameter as a string, with no sanitizing.
102
     * @param string $paramName parameter name
103
     * @return string parameter content, or null if no parameter found
104
     */
105
    public function getQueryParamRaw($paramName)
106
    {
107
        return isset($this->queryParams[$paramName]) ? $this->queryParams[$paramName] : null;
108
    }
109
110
    public function getQueryParamPOST($paramName)
111
    {
112
        if (!isset($this->queryParamsPOST[$paramName])) return null;
113
        return filter_var($this->queryParamsPOST[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
0 ignored issues
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
114
    }
115
116
    public function getQueryParamBoolean($paramName, $default)
117
    {
118
        $val = $this->getQueryParamRaw($paramName);
119
        if ($val !== NULL) {
0 ignored issues
show
introduced by
The condition $val !== NULL is always true.
Loading history...
120
            $val = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
121
        }
122
        return ($val !== null) ? $val : $default;
123
    }
124
125
    public function getServerConstant($paramName)
126
    {
127
        if (!isset($this->serverConstants[$paramName])) return null;
128
        return filter_var($this->serverConstants[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
0 ignored issues
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
    }
130
131
    public function getCookie($paramName)
132
    {
133
        if (!isset($this->cookies[$paramName])) return null;
134
        return filter_var($this->cookies[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
0 ignored issues
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
135
    }
136
137
    public function getLang()
138
    {
139
        return $this->lang;
140
    }
141
142
    /**
143
     * Sets the language variable
144
     * @param string $lang
145
     */
146
    public function setLang($lang)
147
    {
148
        if ($lang !== '') {
149
            $this->lang = $lang;
150
        }
151
152
    }
153
154
    public function getContentLang()
155
    {
156
        return $this->clang;
157
    }
158
159
    /**
160
     * Sets the language variable
161
     * @param string $clang
162
     */
163
    public function setContentLang($clang)
164
    {
165
        $this->clang = $this->verifyContentLang($clang);
166
    }
167
168
    private function verifyContentLang($lang)
169
    {
170
        if ($this->vocab) {
171
            return $this->vocab->verifyVocabularyLanguage($lang);
172
        }
173
174
        return $lang;
175
    }
176
177
    public function getPage()
178
    {
179
        return $this->page;
180
    }
181
182
    /**
183
     * Sets the page id variable eg. 'groups'
184
     * @param string $page
185
     */
186
    public function setPage($page)
187
    {
188
        if ($page !== '') {
189
            $this->page = $page;
190
        }
191
192
    }
193
194
    public function getRequestUri()
195
    {
196
        return $this->getServerConstant('HTTP_HOST') . $this->getServerConstant('REQUEST_URI');
197
    }
198
199
    /**
200
     * Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat'
201
     * @param string $newlang new UI language to set
202
     * @return string the relative url of the page
203
     */
204
    public function getLangUrl($newlang=null)
205
    {
206
        $script_name = str_replace('/index.php', '', $this->getServerConstant('SCRIPT_NAME'));
207
        $langurl = substr(str_replace($script_name, '', strval($this->getServerConstant('REQUEST_URI'))), 1);
208
        if ($newlang !== null) {
209
            $langurl = preg_replace("#^(.*/)?{$this->lang}/#", "$1{$newlang}/", $langurl);
210
        }
211
        if (str_contains($langurl, '?')) {
212
            $urlparts = explode('?', $langurl);
213
            $langurl = $urlparts[0] . '?' . urlencode($urlparts[1]);
214
            $langurl = str_replace("%3D", "=", $langurl);
215
        }
216
        // make sure that the resulting URL isn't interpreted as an absolute URL
217
        $langurl = str_replace(":", "", $langurl);
218
        return $langurl;
219
    }
220
221
    public function getLetter()
222
    {
223
        return (isset($this->letter)) ? $this->letter : '';
224
    }
225
226
    /**
227
     * Sets the page id variable eg. 'B'
228
     * @param string $letter
229
     */
230
    public function setLetter($letter)
231
    {
232
        if ($letter !== '') {
233
            $this->letter = $letter;
234
        }
235
236
    }
237
238
    public function getURI()
239
    {
240
        return $this->uri;
241
    }
242
243
    /**
244
     * Sets the uri variable
245
     * @param string $uri
246
     */
247
    public function setURI($uri)
248
    {
249
        if ($uri !== '') {
250
            $this->uri = rtrim(strval($uri));
251
        }
252
253
    }
254
255
    /**
256
     * Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso'
257
     * @param string $ids
258
     */
259
    public function setVocabids($ids)
260
    {
261
        $this->vocabids = $ids;
262
    }
263
264
    public function getVocabid()
265
    {
266
        if ($this->vocabids) {
267
            return $this->vocabids;
268
        }
269
270
        return isset($this->vocab) ? $this->vocab->getId() : '';
271
    }
272
273
    /**
274
     * Creates a Vocabulary object
275
     * @param string $vocabid
276
     */
277
    public function setVocab($vocabid)
278
    {
279
        if (strpos($vocabid, ' ') !== false) // if there are multiple vocabularies just storing the string
280
        {
281
            $this->setVocabids($vocabid);
282
        } else {
283
            $this->vocab = $this->model->getVocabulary($vocabid);
284
        }
285
286
    }
287
288
    /**
289
     * @return Vocabulary
290
     */
291
    public function getVocab()
292
    {
293
        return $this->vocab;
294
    }
295
296
    public function getVocabList() {
297
        return $this->model->getVocabularyList();
298
    }
299
300
    public function getPlugins() {
301
        if ($this->vocab) {
302
            return $this->vocab->getConfig()->getPluginRegister();
303
        }
304
        return new PluginRegister($this->model->getConfig()->getGlobalPlugins());
305
    }
306
307
    /**
308
     * Return the version of this Skosmos installation, or "unknown" if
309
     * it cannot be determined. The version information is based on Git tags.
310
     * @return string version
311
     */
312
    public function getVersion() : string
313
    {
314
        return $this->model->getVersion();
315
    }
316
}
317