File: request.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (394 lines) | stat: -rw-r--r-- 12,207 bytes parent folder | download
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
 * A request represents a http request
 *
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */

namespace web;

use ArrayAccess;
use breadcrumb;
use generic\variable;
use user;

/**
 * The request class is used to access request-related variables
 * such as $_GET, $_POST and $_SERVER.
 *
 * In the future $_FILES and $_COOKIE will be added.
 *
 * A variable can be accessed through ArrayAccess ($request["variable"] or object
 * access ($request->variable);
 *
 * @package Zoph
 * @author Jeroen Roos
 */
class request implements ArrayAccess {
    /** @var array holds $_GET variables */
    private $get;

    /** @var array holds $_POST variables */
    private $post;

    /** @var array holds $_SERVER variables */
    private $server;

    /** @var array holds $_FILES variables */
    private $files;

    /** @var array holds $_COOKIE variables */
    private $cookie;

    /** @var request vars, holds $_GET for GET requests and $_POST for POST requests
             actually, a POST request can have GET variables as well, but this has
             always been how Zoph works, so for now I am not changing this, note
             that this is *different* from the $_REQUEST superglobals - hence it's
             not called $request */
    private $requestVars;

    /** @var string queryString holds query string */
    private $queryString;

    /** @var array request path as array */
    private array $path;

    /** @var array always pass these fields when cleaning vars */
    private const FIELDS = array("_action", "_order", "_dir", "_cols", "_rows", "_random", "_filename", "_maxfiles", "_maxsize", "_off", "_filenum", "_index", "_qs");

    /**
     * Create object
     * @param array array of variables, can contain GET, POST and SERVER
     */
    public function __construct(array $vars) {
        foreach ([ "GET", "POST", "SERVER", "FILES", "COOKIE" ] as $var) {
            if (isset($vars[$var])) {
                $value=new variable($vars[$var]);
                $prop=strtolower($var);
                $this->$prop=$value->input();
            }
        }

        $this->buildRequest();

        $this->path = explode("/", $this->getServerVar("PATH_INFO") ?? "");

        $this->queryString = new queryString($this->getServerVar("QUERY_STRING"), $this["_qs"]);
    }

    public function getBasePath() {
        $scriptName = $this->getServerVar("SCRIPT_NAME");
        $scriptArray = explode("/", $scriptName);
        $script = end($scriptArray);
        return substr($scriptName, 0, strpos($scriptName, $script));
    }

    public function getController() {
        return $this->path[1] ?? "";
    }

    public function getAction() {
        return $this->path[2] ?? "";
    }

    /**
     * Create object and fill with superglobals
     * @return request new request
     */
    public static function create() : self {
        return new self(array(
            "GET"       =>  $_GET,
            "POST"      =>  $_POST,
            "SERVER"    => $_SERVER,
            "FILES"     => $_FILES,
            "COOKIE"    => $_COOKIE
        ));
    }

    /**
     * Fill the REQUESTVARS property with either the GET variables
     * OR the POST variables.
     * Note that this behaviour is different from PHP's $_REQUEST superglobal
     */
    private function buildRequest() : void {
        if (!empty($this->get)) {
            $this->requestVars=&$this->get;
        } else {
            $this->requestVars=&$this->post;
        }
    }

    /**
     * For ArrayAccess: does the offset exist
     * @param mixed offset
     * @return bool offset exists
     */
    public function offsetExists(mixed $off) : bool {
        return (isset($this->get[$off]) || isset($this->post[$off]));
    }

    /**
     * For ArrayAccess: Get value of parameter
     * if $_GET parameter is available, return it, if it is not but $_POST is available
     * return that, otherwise null
     * @param mixed offset
     * @return mixed value
     */
    public function offsetGet(mixed $off) : mixed {
        if (isset($this->get[$off])) {
            return $this->get[$off];
        } else if (isset($this->post[$off])) {
            return $this->post[$off];
        } else {
            return null;
        }
    }

    /**
     * For ArrayAccess: Set value of parameter
     * @param mixed offset
     * @param mixed value
     */
    public function offsetSet(mixed $off, mixed $val) : void {
        if (isset($this->post[$off])) {
            $this->post[$off]=$val;
        } else if (isset($this->get[$off])) {
            $this->get[$off]=$val;
        } else {
            $this->post[$off]=$val;
        }
    }

    /**
     * For ArrayAccess: Unset value of parameter
     * @param mixed offset
     */
    public function offsetUnset(mixed $off) : void {
        unset($this->get[$off]);
        unset($this->post[$off]);
    }

    /**
     * For ObjectAccess: Get value of parameter
     * if $_GET parameter is available, return it, if it is not but $_POST is available
     * return that, otherwise null
     * @param mixed offset
     * @return mixed value
     */
    public function __get(mixed $off) : mixed {
        return $this->offsetGet($off);
    }

    /**
     * Get RequestVars
     * @return array requestvars
     */
    public function getRequestVars() : array {
        return (array) $this->requestVars;
    }

    /**
     * Return the query string, urlencoded, it can be passed via an URL
     */
    public function getEncodedQueryString() : string {
        return $this->queryString->encode();
    }

    /**
     * Get the query string that was used to get to this page
     */
    public function getQueryString() : string {
        return $this->queryString;
    }

    /**
     * Sometimes a form passes a previous query string as part of the data
     * this is needed to return to the original page. For example, if you have performed
     * a search and click on a photo, you're not simply sent to that photo,
     * but the query string for that photo contains the original search
     * to return to the search after the photo was updated, you need to retrieve that
     * query string through this function.
     */
    public function getPassedQueryString() : string {
        return (string) $this->queryString->getPassed();
    }

    /**
     * Clean the query string by passing regexes
     * For example removing "_crumb" and "_action":
     * this->cleanQueryString(array("/_crumb=\d+&?/","/_action=\w+&?/"))
     * @param array regex to use for cleaning
     * @return string cleaned query string
     */
    public function cleanQueryString(array $regexes) : string {
        $qs = $this->getQueryString();
        foreach ($regexes as $regex) {
            $qs = preg_replace($regex, "", $qs);
        }
        return $qs;
    }

    /**
     * Get the return query string
     * This could be the passed query string ("_qs") or this function could
     * clean the current query string, removing "_crumb" and "_action"
     */
    public function getReturnQueryString() : string {
        return $this->queryString->getReturn();
    }

    /**
     * Get $_SERVER variables
     * @param string Variable to return
     * @return mixed value
     */
    public function getServerVar(string $var) : mixed {
        if (isset($this->server[$var])) {
            return $this->server[$var];
        } else {
            return null;
        }
    }

    /**
     * Get uploaded files
     * @param string name of upload field
     * @return array uploaded files
     */
    public function getFiles(string $var) : ?array {
        if (isset($this->files[$var])) {
            return $this->files[$var];
        } else {
            return null;
        }
    }

    /**
     * Get $_POST variables
     * @param Variable to return
     * @return mixed value
     */
    public function getPostVar(mixed $var) : mixed {
        if (isset($this->post[$var])) {
            return $this->post[$var];
        } else {
            return null;
        }
    }

   /**
    * Update requestvars
    * Update a variable to a new value, remove ignored keys
    * always removes PHPSESSID and _crumb variables
    * @param mixed string variable (key) to add / update | array array of key => value pairs to update
    * @param mixed value of the new /updated variable | ignored when $new is array
    * @param array list of key names to remove
    * @return array updated variables
    */
    public function getUpdatedVars(mixed $new, mixed $val = null, array $ignore = array(), bool $clean = true) {
        $ignore[] = "PHPSESSID";
        $ignore[] = "_crumb";

        if ($clean) {
            $vars = $this->getRequestVarsClean();
        } else {
            $vars = $this->getRequestVars();
        }

        foreach ($ignore as $key) {
            unset($vars[$key]);
        }

        if (is_array($new)) {
            foreach ($new as $key => $value) {
                $vars[$key] = $value;
            }
        } else {
            $vars[$new] = $val;
        }

        return $vars;
    }

    /**
     * Remove any params without values and operator params without corresponding
     * fields (e.g. _album_id_op when there is no _album_id).  This can be called
     * once after a search is performed.  It allows for shorter urls that are
     * more readable and easier to debug.
     * @param array remove this field
     */
    public function getRequestVarsClean(array $remove = array()) : array {
        $cleanVars = array();

        foreach ($this->getRequestVars() as $var => $value) {
            if (in_array($var, $remove)) {
                continue;
            } else if (in_array($var, self::FIELDS)) {
                $cleanVars[$var] = $value;
                continue;
            } else if (substr($var, 0, 1) == "_") {
                continue;
            }

            if (is_array($value)) {
                foreach ($value as $i => $val) {
                    if (empty($val)) {
                        continue;
                    }
                    if (!isset($cleanVars[$var])) {
                        $cleanVars[$var]=array();
                    }
                    $cleanVars[$var][$i]=$val;
                    foreach ($this->getRelatedVars($var, $i) as $related => $value) {
                        if (!isset($cleanVars[$related])) {
                            $cleanVars[$related]=array();
                        }
                        $cleanVars[$related][$i]=$value;
                    }

                }
            } else {
                if (empty($value)) {
                    continue;
                }
                $cleanVars[$var]=$value;
                foreach ($this->getRelatedVars($var) as $related => $value) {
                    $cleanVars[$related]=$value;
                }
            }
        }
        return $cleanVars;

    }

    private function getRelatedVars(string $var, int $index = null) {
        $return = array();
        foreach ($this->getRequestVars() as $related => $value) {
            if (substr($related, 1, strlen($var)) == $var) {
                if (is_array($value) && $index !== null && isset($value[$index])) {
                    $return[$related] = $value[$index];
                } else if (!is_array($value) && $index === null && isset($value)) {
                    $return[$related] = $value;
                }
            }
        }
        return $return;
    }

}