Passed
Push — master ( 2d4bff...43944f )
by Lars
03:45
created

Bootup   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 184
ccs 64
cts 68
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filterRequestInputs() 0 36 5
A get_random_bytes() 0 13 3
A is_php() 0 11 2
A initAll() 0 3 1
B filterRequestUri() 0 55 7
A filterString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
class Bootup
8
{
9
    /**
10
     * filter request inputs
11
     *
12
     * Ensures inputs are well formed UTF-8
13
     * When not, assumes Windows-1252 and converts to UTF-8
14
     * Tests only values, not keys
15
     *
16
     * @param int    $normalization_form
17
     * @param string $leading_combining
18
     */
19 1
    public static function filterRequestInputs($normalization_form = 4 /* n::NFC */, $leading_combining = '◌')
20
    {
21
        $a = [
22 1
            &$_FILES,
23 1
            &$_ENV,
24 1
            &$_GET,
25 1
            &$_POST,
26 1
            &$_COOKIE,
27 1
            &$_SERVER,
28 1
            &$_REQUEST,
29
        ];
30
31
        /** @noinspection ReferenceMismatchInspection */
32
        /** @noinspection ForeachSourceInspection */
33 1
        foreach ($a[0] as &$r) {
34 1
            $a[] = [
35 1
                &$r['name'],
36 1
                &$r['type'],
37
            ];
38
        }
39 1
        unset($r, $a[0]);
40
41 1
        $len = \count($a) + 1;
42 1
        for ($i = 1; $i < $len; ++$i) {
43
            /** @noinspection ReferenceMismatchInspection */
44
            /** @noinspection ForeachSourceInspection */
45 1
            foreach ($a[$i] as &$r) {
46
                /** @noinspection ReferenceMismatchInspection */
47 1
                $s = $r; // $r is a reference, $s a copy
48 1
                if (\is_array($s)) {
49 1
                    $a[$len++] = &$r;
50
                } else {
51 1
                    $r = self::filterString($s, $normalization_form, $leading_combining);
52
                }
53
            }
54 1
            unset($r, $a[$i]);
55
        }
56 1
    }
57
58
    /**
59
     * Filter current REQUEST_URI .
60
     *
61
     * @param string|null $uri  <p>If null is set, then the server REQUEST_URI will be used.</p>
62
     * @param bool        $exit
63
     *
64
     * @return mixed
65
     */
66 1
    public static function filterRequestUri($uri = null, $exit = true)
67
    {
68 1
        if ($uri === null) {
69 1
            if (!isset($_SERVER['REQUEST_URI'])) {
70 1
                return false;
71
            }
72
73 1
            $uri = (string) $_SERVER['REQUEST_URI'];
74
        }
75
76 1
        $uriOrig = $uri;
77
78
        //
79
        // Ensures the URL is well formed UTF-8
80
        //
81
82 1
        if (UTF8::is_utf8(\rawurldecode($uri)) === true) {
83 1
            return $uri;
84
        }
85
86
        //
87
        // When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL
88
        //
89
90 1
        $uri = (string) \preg_replace_callback(
91 1
            '/[\x80-\xFF]+/',
92 1
            static function ($m) {
93 1
                return \rawurlencode($m[0]);
94 1
            },
95 1
            $uri
96
        );
97
98 1
        $uri = (string) \preg_replace_callback(
99 1
            '/(?:%[89A-F][0-9A-F])+/i',
100 1
            static function ($m) {
101 1
                return \rawurlencode(UTF8::rawurldecode($m[0]));
102 1
            },
103 1
            $uri
104
        );
105
106
        if (
107 1
            $uri !== $uriOrig
108
            &&
109 1
            $exit === true
110
            &&
111 1
            \headers_sent() === false
112
        ) {
113
            // Use ob_start() to buffer content and avoid problem of headers already sent...
114
            $severProtocol = ($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
115
            \header($severProtocol . ' 301 Moved Permanently');
116
            \header('Location: ' . $uri);
117
            exit();
118
        }
119
120 1
        return $uri;
121
    }
122
123
    /**
124
     * Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.
125
     *
126
     * @param mixed  $input
127
     * @param int    $normalization_form
128
     * @param string $leading_combining
129
     *
130
     * @return mixed
131
     */
132 1
    public static function filterString($input, int $normalization_form = 4 /* n::NFC */, string $leading_combining = '◌')
133
    {
134 1
        return UTF8::filter($input, $normalization_form, $leading_combining);
135
    }
136
137
    /**
138
     * Get random bytes via "random_bytes()"
139
     *
140
     * @param int $length <p>output length</p>
141
     *
142
     * @throws \Exception if it was not possible to gather sufficient entropy
143
     *
144
     * @return false|string
145
     *                      <strong>false</strong> on error
146
     */
147 1
    public static function get_random_bytes($length)
148
    {
149 1
        if (!$length) {
150 1
            return false;
151
        }
152
153 1
        $length = (int) $length;
154
155 1
        if ($length <= 0) {
156 1
            return false;
157
        }
158
159 1
        return \random_bytes($length);
160
    }
161
162
    /**
163
     * bootstrap
164
     */
165 1
    public static function initAll()
166
    {
167 1
        \ini_set('default_charset', 'UTF-8');
168
169
        // everything is init via composer, so we are done here ...
170 1
    }
171
172
    /**
173
     * Determines if the current version of PHP is equal to or greater than the supplied value.
174
     *
175
     * @param string $version <p>e.g. "7.1"<p>
176
     *
177
     * @return bool
178
     *              Return <strong>true</strong> if the current version is $version or higher
179
     */
180 11
    public static function is_php($version): bool
181
    {
182 11
        static $_IS_PHP;
183
184 11
        $version = (string) $version;
185
186 11
        if (!isset($_IS_PHP[$version])) {
187 3
            $_IS_PHP[$version] = \version_compare(\PHP_VERSION, $version, '>=');
188
        }
189
190 11
        return $_IS_PHP[$version];
191
    }
192
}
193