GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Input::getCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Kernel\Cli;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Spl\DataStructures\SplArrayObject;
19
20
/**
21
 * Class Input
22
 *
23
 * Http Kernel Input data with optional filter functionality, all data as it has arrived to the
24
 * Kernel Input from the CGI and/or PHP on command line interface environment, including:
25
 *
26
 * - The values represented in $_SERVER, $_ENV, and $_REQUEST.
27
 * - The values from argv.
28
 *
29
 * @package O2System\Kernel\Http
30
 */
31
class Input
32
{
33
    /**
34
     * Input::get
35
     *
36
     * Fetch input from GET data.
37
     *
38
     * @param string|null $offset The offset of $_GET variable to fetch.
39
     *                            When set null will returns filtered $_GET variable.
40
     * @param int         $filter The ID of the filter to apply.
41
     *                            The Types of filters manual page lists the available filters.
42
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
43
     *                            This will result in no filtering taking place by default.
44
     *
45
     * @return mixed
46
     */
47
    final public function get($offset = null, $filter = null)
48
    {
49
        return $this->filter(INPUT_GET, $offset, $filter);
50
    }
51
52
    // ------------------------------------------------------------------------
53
54
    /**
55
     * Input::filter
56
     *
57
     * Gets a specific external variable by name and optionally filters it.
58
     *
59
     * @see http://php.net/manual/en/function.filter-input.php
60
     *
61
     * @param int   $type   One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
62
     * @param mixed $offset The offset key of input variable.
63
     * @param int   $filter The ID of the filter to apply.
64
     *                      The Types of filters manual page lists the available filters.
65
     *                      If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
66
     *                      This will result in no filtering taking place by default.
67
     *
68
     * @return mixed|\O2System\Spl\DataStructures\SplArrayObject
69
     */
70
    protected function filter($type, $offset = null, $filter = FILTER_DEFAULT)
71
    {
72
        // If $offset is null, it means that the whole input type array is requested
73
        if (is_null($offset)) {
74
            $loopThrough = [];
75
76
            switch ($type) {
77
                case INPUT_GET    :
78
                    $loopThrough = $_GET;
79
                    break;
80
                case INPUT_POST   :
81
                    $loopThrough = $_POST;
82
                    break;
83
                case INPUT_SERVER :
84
                    $loopThrough = $_SERVER;
85
                    break;
86
                case INPUT_ENV    :
87
                    $loopThrough = $_ENV;
88
                    break;
89
                case INPUT_REQUEST    :
90
                    $loopThrough = $_REQUEST;
91
                    break;
92
                case INPUT_SESSION    :
93
                    $loopThrough = $_ENV;
94
                    break;
95
            }
96
97
            $loopThrough = $this->filterRecursive($loopThrough, $filter);
98
99
            if (empty($loopThrough)) {
100
                return false;
101
            }
102
103
            return new SplArrayObject($loopThrough);
104
        } // allow fetching multiple keys at once
105
        elseif (is_array($offset)) {
106
            $loopThrough = [];
107
108
            foreach ($offset as $key) {
109
                $loopThrough[ $key ] = $this->filter($type, $key, $filter);
110
            }
111
112
            if (empty($loopThrough)) {
113
                return false;
114
            }
115
116
            return new SplArrayObject($loopThrough);
117
        } elseif (isset($offset)) {
118
            // Due to issues with FastCGI and testing,
119
            // we need to do these all manually instead
120
            // of the simpler filter_input();
121
            switch ($type) {
122
                case INPUT_GET:
123
                    $value = isset($_GET[ $offset ])
124
                        ? $_GET[ $offset ]
125
                        : null;
126
                    break;
127
                case INPUT_POST:
128
                    $value = isset($_POST[ $offset ])
129
                        ? $_POST[ $offset ]
130
                        : null;
131
                    break;
132
                case INPUT_SERVER:
133
                    $value = isset($_SERVER[ $offset ])
134
                        ? $_SERVER[ $offset ]
135
                        : null;
136
                    break;
137
                case INPUT_ENV:
138
                    $value = isset($_ENV[ $offset ])
139
                        ? $_ENV[ $offset ]
140
                        : null;
141
                    break;
142
                case INPUT_COOKIE:
143
                    $value = isset($_COOKIE[ $offset ])
144
                        ? $_COOKIE[ $offset ]
145
                        : null;
146
                    break;
147
                case INPUT_REQUEST:
148
                    $value = isset($_REQUEST[ $offset ])
149
                        ? $_REQUEST[ $offset ]
150
                        : null;
151
                    break;
152
                case INPUT_SESSION:
153
                    $value = isset($_SESSION[ $offset ])
154
                        ? $_SESSION[ $offset ]
155
                        : null;
156
                    break;
157
                default:
158
                    $value = '';
159
            }
160
161
            if (is_array($value)) {
162
                $value = $this->filterRecursive($value, $filter);
163
164
                if (is_string(key($value))) {
165
                    return new SplArrayObject($value);
166
                } else {
167
                    return $value;
168
                }
169
            } elseif (is_object($value)) {
170
                return $value;
171
            }
172
173
            if (isset($filter)) {
174
                return filter_var($value, $filter);
175
            }
176
177
            return $value;
178
        }
179
180
        return null;
181
    }
182
183
    // ------------------------------------------------------------------------
184
185
    /**
186
     * Input::filterRecursive
187
     *
188
     * Gets multiple variables and optionally filters them.
189
     *
190
     * @see http://php.net/manual/en/function.filter-var.php
191
     * @see http://php.net/manual/en/function.filter-var-array.php
192
     *
193
     *
194
     * @param array     $data   An array with string keys containing the data to filter.
195
     * @param int|mixed $filter The ID of the filter to apply.
196
     *                          The Types of filters manual page lists the available filters.
197
     *                          If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
198
     *                          This will result in no filtering taking place by default.
199
     *                          Its also can be An array defining the arguments.
200
     *                          A valid key is a string containing a variable name and a valid value is either
201
     *                          a filter type, or an array optionally specifying the filter, flags and options.
202
     *                          If the value is an array, valid keys are filter which specifies the filter type,
203
     *                          flags which specifies any flags that apply to the filter, and options which
204
     *                          specifies any options that apply to the filter. See the example below for
205
     *                          a better understanding.
206
     *
207
     * @return mixed
208
     */
209
    protected function filterRecursive(array $data, $filter = FILTER_DEFAULT)
210
    {
211
        foreach ($data as $key => $value) {
212
            if (is_array($value) AND is_array($filter)) {
213
                $data[ $key ] = filter_var_array($value, $filter);
214
            } elseif (is_array($value)) {
215
                $data[ $key ] = $this->filterRecursive($value, $filter);
216
            } elseif (isset($filter)) {
217
                $data[ $key ] = filter_var($value, $filter);
218
            } else {
219
                $data[ $key ] = $value;
220
            }
221
        }
222
223
        return $data;
224
    }
225
226
    // ------------------------------------------------------------------------
227
228
    /**
229
     * Input::post
230
     *
231
     * Fetch input from POST data.
232
     *
233
     * @param string|null $offset The offset of $_POST variable to fetch.
234
     *                            When set null will returns filtered $_POST variable.
235
     * @param int         $filter The ID of the filter to apply.
236
     *                            The Types of filters manual page lists the available filters.
237
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
238
     *                            This will result in no filtering taking place by default.
239
     *
240
     * @return mixed
241
     */
242
    final public function post($offset = null, $filter = null)
243
    {
244
        return $this->filter(INPUT_POST, $offset, $filter);
245
    }
246
247
    // ------------------------------------------------------------------------
248
249
    /**
250
     * Input::argv
251
     *
252
     * Fetch input from GET data.
253
     *
254
     * @param string|null $offset The offset of $_GET variable to fetch.
255
     *                            When set null will returns filtered $_GET variable.
256
     * @param int         $filter The ID of the filter to apply.
257
     *                            The Types of filters manual page lists the available filters.
258
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
259
     *                            This will result in no filtering taking place by default.
260
     *
261
     * @return mixed
262
     */
263
    final public function argv($offset = null, $filter = null)
264
    {
265
        $arguments = $_SERVER[ 'argv' ];
266
        $numArguments = $_SERVER[ 'argc' ];
267
268
        $argv = [];
269
        for ($i = 1; $i < $numArguments; $i++) {
270
            $optionCommand = trim($arguments[ $i ]);
271
            $optionValue = true;
272
273
            if (empty($optionCommand)) {
274
                continue;
275
            }
276
277
            if (strpos($optionCommand, '=') !== false) {
278
                $xOptionCommand = explode('=', $optionCommand);
279
                $xOptionCommand = array_map('trim', $xOptionCommand);
280
281
                $optionCommand = str_replace(['-', '--'], '', $xOptionCommand[ 0 ]);
282
                $optionValue = $xOptionCommand[ 1 ];
283
284
                $argv[ $optionCommand ] = $optionValue;
285
                continue;
286
            }
287
288
            if (strpos($optionCommand, '--') !== false
289
                || strpos($optionCommand, '-') !== false
290
            ) {
291
                $optionCommand = str_replace(['-', '--'], '', $optionCommand);
292
293
                if (isset($arguments[ $i + 1 ])) {
294
                    $nextOptionCommand = $arguments[ $i + 1 ];
295
296
                    if (strpos($nextOptionCommand, '--') === false
297
                        || strpos($nextOptionCommand, '-') === false
298
                    ) {
299
                        $optionValue = $nextOptionCommand;
300
                        $arguments[ $i + 1 ] = null;
301
                    }
302
                }
303
            }
304
305
            if (isset($filter)) {
306
                $optionValue = filter_var($optionValue, $filter);
307
            } else {
308
                $optionValue = filter_var($optionValue, FILTER_DEFAULT);
309
            }
310
311
            $argv[ $optionCommand ] = $optionValue;
312
        }
313
314
        if (empty($offset)) {
315
            return $argv;
316
        } elseif (isset($argv[ $offset ])) {
317
            return $argv[ $offset ];
318
        }
319
    }
320
321
    // ------------------------------------------------------------------------
322
323
    /**
324
     * Input::standard
325
     *
326
     * @return string
327
     */
328
    public function standard()
329
    {
330
        return trim(fgets(STDIN));
331
    }
332
333
    // ------------------------------------------------------------------------
334
335
    /**
336
     * Input::getApp
337
     *
338
     * @return bool
339
     */
340
    public function getApp()
341
    {
342
        return isset($_SERVER[ 'argv' ][ 0 ])
343
            ? $_SERVER[ 'argv' ][ 0 ]
344
            : false;
345
    }
346
347
    // ------------------------------------------------------------------------
348
349
    /**
350
     * Input::getCommand
351
     *
352
     * @return bool
353
     */
354
    public function getCommand()
355
    {
356
        return isset($_SERVER[ 'argv' ][ 1 ])
357
            ? $_SERVER[ 'argv' ][ 1 ]
358
            : false;
359
    }
360
361
    // ------------------------------------------------------------------------
362
363
    /**
364
     * Input::getOptions
365
     *
366
     * @param string|null $offset
367
     * @param mixed $filter
368
     *
369
     * @return array|mixed
370
     */
371
    public function getOptions($offset = null, $filter = null)
372
    {
373
        $arguments = $_SERVER[ 'argv' ];
374
        $numArguments = $_SERVER[ 'argc' ];
375
376
        $argv = [];
377
378
        for ($i = 2; $i < $numArguments; $i++) {
379
            $optionCommand = trim($arguments[ $i ]);
380
            $optionValue = true;
381
382
            if (empty($optionCommand)) {
383
                continue;
384
            }
385
386
            if (strpos($optionCommand, '=') !== false) {
387
                $xOptionCommand = explode('=', $optionCommand);
388
                $xOptionCommand = array_map('trim', $xOptionCommand);
389
390
                $optionCommand = str_replace(['-', '--'], '', $xOptionCommand[ 0 ]);
391
                $optionValue = $xOptionCommand[ 1 ];
392
393
                $argv[ $optionCommand ] = $optionValue;
394
                continue;
395
            }
396
397
            if (strpos($optionCommand, '--') !== false
398
                || strpos($optionCommand, '-') !== false
399
            ) {
400
                $optionCommand = str_replace(['-', '--'], '', $optionCommand);
401
402
                if (isset($arguments[ $i + 1 ])) {
403
                    $nextOptionCommand = $arguments[ $i + 1 ];
404
405
                    if (strpos($nextOptionCommand, '--') === false
406
                        || strpos($nextOptionCommand, '-') === false
407
                    ) {
408
                        $optionValue = $nextOptionCommand;
409
                        $arguments[ $i + 1 ] = null;
410
                    }
411
                }
412
            }
413
414
            if (isset($filter)) {
415
                $optionValue = filter_var($optionValue, $filter);
416
            } else {
417
                $optionValue = filter_var($optionValue, FILTER_DEFAULT);
418
            }
419
420
            $argv[ $optionCommand ] = $optionValue;
421
        }
422
423
        if (empty($offset)) {
424
            return $argv;
425
        } elseif (isset($argv[ $offset ])) {
426
            return $argv[ $offset ];
427
        }
428
    }
429
430
    // ------------------------------------------------------------------------
431
432
    /**
433
     * Input::env
434
     *
435
     * Fetch input from ENV data.
436
     *
437
     * @param string|null $offset The offset of $_ENV variable to fetch.
438
     *                            When set null will returns filtered $_ENV variable.
439
     * @param int         $filter The ID of the filter to apply.
440
     *                            The Types of filters manual page lists the available filters.
441
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
442
     *                            This will result in no filtering taking place by default.
443
     *
444
     * @return mixed
445
     */
446
    final public function env($offset = null, $filter = null)
447
    {
448
        return $this->filter(INPUT_ENV, $offset, $filter);
449
    }
450
451
    //--------------------------------------------------------------------
452
453
    /**
454
     * Input::cookie
455
     *
456
     * Fetch input from COOKIE data.
457
     *
458
     * @param string|null $offset The offset of $_COOKIE variable to fetch.
459
     *                            When set null will returns filtered $_COOKIE variable.
460
     * @param int         $filter The ID of the filter to apply.
461
     *                            The Types of filters manual page lists the available filters.
462
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
463
     *                            This will result in no filtering taking place by default.
464
     *
465
     * @return mixed
466
     */
467
    final public function cookie($offset = null, $filter = null)
468
    {
469
        return $this->filter(INPUT_COOKIE, $offset, $filter);
470
    }
471
472
    //--------------------------------------------------------------------
473
474
    /**
475
     * Input::server
476
     *
477
     * Fetch input from SERVER data.
478
     *
479
     * @param string|null $offset The offset of $_SERVER variable to fetch.
480
     *                            When set null will returns filtered $_SERVER variable.
481
     * @param int         $filter The ID of the filter to apply.
482
     *                            The Types of filters manual page lists the available filters.
483
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
484
     *                            This will result in no filtering taking place by default.
485
     *
486
     * @return mixed
487
     */
488
    final public function server($offset = null, $filter = null)
489
    {
490
        return $this->filter(INPUT_SERVER, $offset, $filter);
491
    }
492
493
    //--------------------------------------------------------------------
494
495
    /**
496
     * Input::request
497
     *
498
     * Fetch input from REQUEST data.
499
     *
500
     * @param string|null $offset The offset of $_REQUEST variable to fetch.
501
     *                            When set null will returns filtered $_REQUEST variable.
502
     * @param int         $filter The ID of the filter to apply.
503
     *                            The Types of filters manual page lists the available filters.
504
     *                            If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW.
505
     *                            This will result in no filtering taking place by default.
506
     *
507
     * @return mixed
508
     */
509
    final public function request($offset = null, $filter = null)
510
    {
511
        return $this->filter(INPUT_REQUEST, $offset, $filter);
512
    }
513
}