Completed
Push — master ( 20d994...e21309 )
by Osma
24s queued 11s
created

Request::setQueryParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
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
21
    /**
22
     * Initializes the Request Object
23
     */
24
    public function __construct($model)
25
    {
26
        $this->model = $model;
27
28
        // Store GET parameters in a local array, so we can mock them in tests.
29
        // We do not apply any filters at this point.
30
        $this->queryParams = [];
31
        foreach (filter_input_array(INPUT_GET) ?: [] as $key => $val) {
32
            $this->queryParams[$key] = $val;
33
        }
34
35
        // Store POST parameters in a local array, so we can mock them in tests.
36
        // We do not apply any filters at this point.
37
        $this->queryParamsPOST = [];
38
        foreach (filter_input_array(INPUT_POST) ?: [] as $key => $val) {
39
            $this->queryParamsPOST[$key] = $val;
40
        }
41
42
        // Store SERVER parameters in a local array, so we can mock them in tests.
43
        // We do not apply any filters at this point.
44
        $this->serverConstants = [];
45
        foreach (filter_input_array(INPUT_SERVER) ?: [] as $key => $val) {
46
            $this->serverConstants[$key] = $val;
47
        }
48
    }
49
50
    /**
51
     * Set a GET query parameter to mock it in tests.
52
     * @param string $paramName parameter name
53
     * @param string $value parameter value
54
     */
55
    public function setQueryParam($paramName, $value)
56
    {
57
        $this->queryParams[$paramName] = $value;
58
    }
59
60
    /**
61
     * Set a SERVER constant to mock it in tests.
62
     * @param string $paramName parameter name
63
     * @param string $value parameter value
64
     */
65
    public function setServerConstant($paramName, $value)
66
    {
67
        $this->serverConstants[$paramName] = $value;
68
    }
69
70
    /**
71
     * Return the requested GET query parameter as a string. Backslashes are stripped for security reasons.
72
     * @param string $paramName parameter name
73
     * @return string parameter content, or null if no parameter found
74
     */
75
    public function getQueryParam($paramName)
76
    {
77
        if (!isset($this->queryParams[$paramName])) return null;
78
        $val = filter_var($this->queryParams[$paramName], FILTER_SANITIZE_STRING);
79
        return ($val !== null ? str_replace('\\', '', $val) : null);
80
    }
81
82
    /**
83
     * Return the requested GET query parameter as a string, with no sanitizing.
84
     * @param string $paramName parameter name
85
     * @return string parameter content, or null if no parameter found
86
     */
87
    public function getQueryParamRaw($paramName)
88
    {
89
        return isset($this->queryParams[$paramName]) ? $this->queryParams[$paramName] : null;
90
    }
91
92
    public function getQueryParamPOST($paramName)
93
    {
94
        if (!isset($this->queryParamsPOST[$paramName])) return null;
95
        return filter_var($this->queryParamsPOST[$paramName], FILTER_SANITIZE_STRING);
96
    }
97
98
    public function getQueryParamBoolean($paramName, $default)
99
    {
100
        $val = $this->getQueryParamRaw($paramName);
101
        if ($val !== NULL) {
102
            $val = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
103
        }
104
        return ($val !== null) ? $val : $default;
105
    }
106
107
    public function getServerConstant($paramName)
108
    {
109
        if (!isset($this->serverConstants[$paramName])) return null;
110
        return filter_var($this->serverConstants[$paramName], FILTER_SANITIZE_STRING);
111
    }
112
113
    public function getLang()
114
    {
115
        return $this->lang;
116
    }
117
118
    /**
119
     * Sets the language variable
120
     * @param string $lang
121
     */
122
    public function setLang($lang)
123
    {
124
        if ($lang !== '') {
125
            $this->lang = $lang;
126
        }
127
128
    }
129
130
    public function getContentLang()
131
    {
132
        return $this->clang;
133
    }
134
135
    /**
136
     * Sets the language variable
137
     * @param string $clang
138
     */
139
    public function setContentLang($clang)
140
    {
141
        $this->clang = $this->verifyContentLang($clang);
142
    }
143
144
    private function verifyContentLang($lang)
145
    {
146
        if ($this->vocab) {
147
            return $this->vocab->verifyVocabularyLanguage($lang);
148
        }
149
150
        return $lang;
151
    }
152
153
    public function getPage()
154
    {
155
        return $this->page;
156
    }
157
158
    /**
159
     * Sets the page id variable eg. 'groups'
160
     * @param string $page
161
     */
162
    public function setPage($page)
163
    {
164
        if ($page !== '') {
165
            $this->page = $page;
166
        }
167
168
    }
169
170
    public function getRequestUri()
171
    {
172
        return $this->getServerConstant('HTTP_HOST') . $this->getServerConstant('REQUEST_URI');
173
    }
174
175
    /**
176
     * Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat'
177
     * @return string the relative url of the page
178
     */
179
    public function getLangUrl()
180
    {
181
        return substr(str_replace(str_replace('/index.php', '', $this->getServerConstant('SCRIPT_NAME')), '', $this->getServerConstant('REQUEST_URI')), 1);
182
    }
183
184
    public function getLetter()
185
    {
186
        return (isset($this->letter)) ? $this->letter : '';
187
    }
188
189
    /**
190
     * Sets the page id variable eg. 'B'
191
     * @param string $letter
192
     */
193
    public function setLetter($letter)
194
    {
195
        if ($letter !== '') {
196
            $this->letter = $letter;
197
        }
198
199
    }
200
201
    public function getURI()
202
    {
203
        return $this->uri;
204
    }
205
206
    /**
207
     * Sets the uri variable
208
     * @param string $uri
209
     */
210
    public function setURI($uri)
211
    {
212
        if ($uri !== '') {
213
            $this->uri = rtrim($uri);
214
        }
215
216
    }
217
218
    /**
219
     * Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso'
220
     * @param string $ids
221
     */
222
    public function setVocabids($ids)
223
    {
224
        $this->vocabids = $ids;
225
    }
226
227
    public function getVocabid()
228
    {
229
        if ($this->vocabids) {
230
            return $this->vocabids;
231
        }
232
233
        return isset($this->vocab) ? $this->vocab->getId() : '';
234
    }
235
236
    /**
237
     * Creates a Vocabulary object
238
     * @param string $vocabid
239
     */
240
    public function setVocab($vocabid)
241
    {
242
        if (strpos($vocabid, ' ') !== false) // if there are multiple vocabularies just storing the string
243
        {
244
            $this->setVocabids($vocabid);
245
        } else {
246
            $this->vocab = $this->model->getVocabulary($vocabid);
247
        }
248
249
    }
250
251
    /**
252
     * @return Vocabulary
253
     */
254
    public function getVocab()
255
    {
256
        return $this->vocab;
257
    }
258
259
    public function getVocabList() {
260
        return $this->model->getVocabularyList();
261
    }
262
263
    public function getPlugins() {
264
        if ($this->vocab) {
265
            return $this->vocab->getConfig()->getPlugins();
266
        }
267
        return new PluginRegister($this->model->getConfig()->getGlobalPlugins());
268
    }
269
270
    /**
271
     * Return the version of this Skosmos installation, or "unknown" if
272
     * it cannot be determined. The version information is based on Git tags.
273
     * @return string version
274
     */
275
    public function getVersion() : string
276
    {
277
        return $this->model->getVersion();
278
    }
279
}
280