Passed
Push — master ( c5aae7...acc240 )
by Lars
09:56 queued 03:00
created

Bootup::initAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * @return bool
20
     *
21
     * @deprecated <p>This method will be removed in future releases, so please don't use it anymore.</p>
22
     */
23 1
    public static function filterRequestInputs(
24
        int $normalization_form = \Normalizer::NFC,
25
        string $leading_combining = '◌'
26
    ): bool {
27
        $a = [
28 1
            &$_FILES,
29 1
            &$_ENV,
30 1
            &$_GET,
31 1
            &$_POST,
32 1
            &$_COOKIE,
33 1
            &$_SERVER,
34 1
            &$_REQUEST,
35
        ];
36
37
        /** @noinspection ReferenceMismatchInspection */
38
        /** @noinspection ForeachSourceInspection */
39 1
        foreach ($a[0] as &$r) {
40 1
            $a[] = [
41 1
                &$r['name'],
42 1
                &$r['type'],
43
            ];
44
        }
45 1
        unset($r, $a[0]);
46
47 1
        $len = \count($a) + 1;
48 1
        for ($i = 1; $i < $len; ++$i) {
49
            /** @noinspection ReferenceMismatchInspection */
50
            /** @noinspection ForeachSourceInspection */
51 1
            foreach ($a[$i] as &$r) {
52
                /** @noinspection ReferenceMismatchInspection */
53 1
                $s = $r; // $r is a reference, $s a copy
54 1
                if (\is_array($s)) {
55 1
                    $a[$len++] = &$r;
56
                } else {
57 1
                    $r = self::filterString($s, $normalization_form, $leading_combining);
58
                }
59
            }
60 1
            unset($r, $a[$i]);
61
        }
62
63 1
        return $len > 1;
64
    }
65
66
    /**
67
     * Filter current REQUEST_URI .
68
     *
69
     * @param string|null $uri  <p>If null is set, then the server REQUEST_URI will be used.</p>
70
     * @param bool        $exit
71
     *
72
     * @return mixed
73
     *
74
     * @deprecated <p>This method will be removed in future releases, so please don't use it anymore.</p>
75
     */
76 1
    public static function filterRequestUri($uri = null, bool $exit = true)
77
    {
78 1
        if ($uri === null) {
79 1
            if (!isset($_SERVER['REQUEST_URI'])) {
80 1
                return false;
81
            }
82
83 1
            $uri = (string) $_SERVER['REQUEST_URI'];
84
        }
85
86 1
        $uriOrig = $uri;
87
88
        //
89
        // Ensures the URL is well formed UTF-8
90
        //
91
92 1
        if (UTF8::is_utf8(\rawurldecode($uri)) === true) {
93 1
            return $uri;
94
        }
95
96
        //
97
        // When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL
98
        //
99
100 1
        $uri = (string) \preg_replace_callback(
101 1
            '/[\x80-\xFF]+/',
102
            /**
103
             * @param array $m
104
             *
105
             * @return string
106
             */
107 1
            static function (array $m): string {
108 1
                return \rawurlencode($m[0]);
109 1
            },
110 1
            $uri
111
        );
112
113 1
        $uri = (string) \preg_replace_callback(
114 1
            '/(?:%[89A-F][0-9A-F])+/i',
115
            /**
116
             * @param array $m
117
             *
118
             * @return string
119
             */
120 1
            static function (array $m): string {
121 1
                return \rawurlencode(UTF8::rawurldecode($m[0]));
122 1
            },
123 1
            $uri
124
        );
125
126
        if (
127 1
            $uri !== $uriOrig
128
            &&
129 1
            $exit === true
130
            &&
131 1
            \headers_sent() === false
132
        ) {
133
            $severProtocol = ($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
134
            \header($severProtocol . ' 301 Moved Permanently');
135
136
            if (\strncmp($uri, '/', 1) === 0) {
137
                \header('Location: /' . \ltrim($uri, '/'));
138
            } else {
139
                \header('Location: ' . $uri);
140
            }
141
142
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
143
        }
144
145 1
        if (\strncmp($uri, '/', 1) === 0) {
146 1
            $uri = '/' . \ltrim($uri, '/');
147
        }
148
149 1
        return $uri;
150
    }
151
152
    /**
153
     * Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.
154
     *
155
     * @param mixed  $input
156
     * @param int    $normalization_form
157
     * @param string $leading_combining
158
     *
159
     * @return mixed
160
     */
161 1
    public static function filterString(
162
        $input,
163
        int $normalization_form = \Normalizer::NFC,
164
        string $leading_combining = '◌'
165
    ) {
166 1
        return UTF8::filter(
167 1
            $input,
168 1
            $normalization_form,
169 1
            $leading_combining
170
        );
171
    }
172
173
    /**
174
     * Get random bytes via "random_bytes()"
175
     *
176
     * @param int $length <p>output length</p>
177
     *
178
     * @throws \Exception if it was not possible to gather sufficient entropy
179
     *
180
     * @return false|string
181
     *                      <strong>false</strong> on error
182
     */
183 1
    public static function get_random_bytes($length)
184
    {
185 1
        if (!$length) {
186 1
            return false;
187
        }
188
189 1
        $length = (int) $length;
190
191 1
        if ($length <= 0) {
192 1
            return false;
193
        }
194
195 1
        return \random_bytes($length);
196
    }
197
198
    /**
199
     * @return bool
200
     */
201 1
    public static function initAll(): bool
202
    {
203 1
        $result = \ini_set('default_charset', 'UTF-8');
204
205
        // everything else is init via composer, so we are done here ...
206
207 1
        return $result !== false;
208
    }
209
210
    /**
211
     * Determines if the current version of PHP is equal to or greater than the supplied value.
212
     *
213
     * @param string $version <p>e.g. "7.1"<p>
214
     *
215
     * @return bool
216
     *              Return <strong>true</strong> if the current version is $version or higher
217
     */
218 93
    public static function is_php($version): bool
219
    {
220 93
        static $_IS_PHP;
221
222 93
        $version = (string) $version;
223
224 93
        if (!isset($_IS_PHP[$version])) {
225 4
            $_IS_PHP[$version] = \version_compare(\PHP_VERSION, $version, '>=');
226
        }
227
228 93
        return $_IS_PHP[$version];
229
    }
230
}
231