Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
17 / 21
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Request
80.95% covered (warning)
80.95%
17 / 21
60.00% covered (warning)
60.00%
3 / 5
19.00
0.00% covered (danger)
0.00%
0 / 1
 getQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cleanup
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
9.49
 isValid
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
5.58
1<?php
2
3/**
4 * HTTP Request class
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2019.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  HTTP
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Http\PhpEnvironment;
31
32use function is_array;
33use function is_string;
34
35/**
36 * HTTP Request class
37 *
38 * @category VuFind
39 * @package  HTTP
40 * @author   Ere Maijala <ere.maijala@helsinki.fi>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class Request extends \Laminas\Http\PhpEnvironment\Request
45{
46    /**
47     * Return the parameter container responsible for query parameters or a single
48     * query parameter
49     *
50     * @param string|null $name    Parameter name to retrieve, or null to get the
51     * whole container.
52     * @param mixed|null  $default Default value to use when the parameter is
53     * missing.
54     *
55     * @return \Laminas\Stdlib\ParametersInterface|mixed
56     */
57    public function getQuery($name = null, $default = null)
58    {
59        return $this->cleanup(parent::getQuery($name, $default));
60    }
61
62    /**
63     * Return the parameter container responsible for post parameters or a single
64     * post parameter.
65     *
66     * @param string|null $name    Parameter name to retrieve, or null to get the
67     * whole container.
68     * @param mixed|null  $default Default value to use when the parameter is
69     * missing.
70     *
71     * @return \Laminas\Stdlib\ParametersInterface|mixed
72     */
73    public function getPost($name = null, $default = null)
74    {
75        return $this->cleanup(parent::getPost($name, $default));
76    }
77
78    /**
79     * Return the parameter container responsible for server parameters or a single
80     * parameter value.
81     *
82     * @param string|null $name    Parameter name to retrieve, or null to get the
83     * whole container.
84     * @param mixed|null  $default Default value to use when the parameter is
85     * missing.
86     *
87     * @see    http://www.faqs.org/rfcs/rfc3875.html
88     * @return \Laminas\Stdlib\ParametersInterface|mixed
89     */
90    public function getServer($name = null, $default = null)
91    {
92        return $this->cleanup(parent::getServer($name, $default));
93    }
94
95    /**
96     * Clean up a parameter
97     *
98     * @param \Laminas\Stdlib\ParametersInterface|mixed $param Parameter
99     *
100     * @return \Laminas\Stdlib\ParametersInterface|mixed
101     */
102    protected function cleanup($param)
103    {
104        if (
105            is_array($param)
106            || $param instanceof \Laminas\Stdlib\ParametersInterface
107        ) {
108            foreach ($param as $key => &$value) {
109                if (is_array($value)) {
110                    $value = $this->cleanup($value);
111                } elseif (!$this->isValid($key) || !$this->isValid($value)) {
112                    unset($param[$key]);
113                }
114            }
115            return $param;
116        }
117
118        if (is_string($param) && !$this->isValid($param)) {
119            return '';
120        }
121
122        return $param;
123    }
124
125    /**
126     * Check if a parameter is valid
127     *
128     * @param mixed $param Parameter to check
129     *
130     * @return bool
131     */
132    protected function isValid($param)
133    {
134        if (!is_string($param)) {
135            return true;
136        }
137        // Check if the string is UTF-8:
138        if ($param !== '' && !preg_match('/^./su', $param)) {
139            return false;
140        }
141        // Check for null in string:
142        if (str_contains($param, "\x00")) {
143            return false;
144        }
145        return true;
146    }
147}