Completed
Push — master ( a6f179...f74bc3 )
by Osma
39s queued 15s
created

Request::getQueryParamRaw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $1 seems to be never defined.
Loading history...
210
        }
211
        // make sure that the resulting URL isn't interpreted as an absolute URL
212
        $langurl = str_replace(":", "", $langurl);
213
        return $langurl;
214
    }
215
216
    public function getLetter()
217
    {
218
        return (isset($this->letter)) ? $this->letter : '';
219
    }
220
221
    /**
222
     * Sets the page id variable eg. 'B'
223
     * @param string $letter
224
     */
225
    public function setLetter($letter)
226
    {
227
        if ($letter !== '') {
228
            $this->letter = $letter;
229
        }
230
231
    }
232
233
    public function getURI()
234
    {
235
        return $this->uri;
236
    }
237
238
    /**
239
     * Sets the uri variable
240
     * @param string $uri
241
     */
242
    public function setURI($uri)
243
    {
244
        if ($uri !== '') {
245
            $this->uri = rtrim(strval($uri));
246
        }
247
248
    }
249
250
    /**
251
     * Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso'
252
     * @param string $ids
253
     */
254
    public function setVocabids($ids)
255
    {
256
        $this->vocabids = $ids;
257
    }
258
259
    public function getVocabid()
260
    {
261
        if ($this->vocabids) {
262
            return $this->vocabids;
263
        }
264
265
        return isset($this->vocab) ? $this->vocab->getId() : '';
266
    }
267
268
    /**
269
     * Creates a Vocabulary object
270
     * @param string $vocabid
271
     */
272
    public function setVocab($vocabid)
273
    {
274
        if (strpos($vocabid, ' ') !== false) // if there are multiple vocabularies just storing the string
275
        {
276
            $this->setVocabids($vocabid);
277
        } else {
278
            $this->vocab = $this->model->getVocabulary($vocabid);
279
        }
280
281
    }
282
283
    /**
284
     * @return Vocabulary
285
     */
286
    public function getVocab()
287
    {
288
        return $this->vocab;
289
    }
290
291
    public function getVocabList() {
292
        return $this->model->getVocabularyList();
293
    }
294
295
    public function getPlugins() {
296
        if ($this->vocab) {
297
            return $this->vocab->getConfig()->getPluginRegister();
298
        }
299
        return new PluginRegister($this->model->getConfig()->getGlobalPlugins());
300
    }
301
302
    /**
303
     * Return the version of this Skosmos installation, or "unknown" if
304
     * it cannot be determined. The version information is based on Git tags.
305
     * @return string version
306
     */
307
    public function getVersion() : string
308
    {
309
        return $this->model->getVersion();
310
    }
311
}
312