Passed
Pull Request — main (#32)
by Dimitri
13:44
created

CheckPhpIni::checkIni()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 40
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 31
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 40
ccs 0
cts 6
cp 0
crap 20
rs 9.424
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Security;
13
14
use Ahc\Cli\Output\Color;
15
16
/**
17
 * Checks php.ini settings
18
 *
19
 * @used-by \BlitzPHP\Cli\Commands\Utilities\PhpIniCheck
20
 */
21
class CheckPhpIni
22
{
23
    /**
24
     * @param bool $isCli Set false if you run via Web
25
     *
26
     * @return array|string HTML string or array in CLI
27
     */
28
    public static function run(bool $isCli = true)
29
    {
30
        $output = static::checkIni();
31
32
        $thead = ['Directive', 'Globale', 'Actuelle', 'Recommandation', 'Remarque'];
33
        $tbody = [];
34
35
        // CLI
36
        if ($isCli) {
37
            return self::outputForCli($output, $thead, $tbody);
38
        }
39
40
        // Web
41
        return self::outputForWeb($output, $thead, $tbody);
42
    }
43
44
    private static function outputForCli(array $output, array $thead, array $tbody): array
45
    {
46
		$color = new Color;
47
48
        foreach ($output as $directive => $values) {
49
            $current        = $values['current'] ?? '';
50
            $notRecommended = false;
51
52
            if ($values['recommended'] !== '') {
53
                if ($values['recommended'] !== $current) {
54
                    $notRecommended = true;
55
                }
56
57
                $current = $notRecommended
58
                    ? $color->error($current === '' ? 'n/a' : $current)
59
                    : $current;
60
            }
61
62
            $directive = $notRecommended ? $color->error($directive) : $directive;
63
            $tbody[]   = [
64
                $directive, $values['global'], $current, $values['recommended'], $values['remark'],
65
            ];
66
        }
67
68
        $table = [];
69
        foreach ($tbody as $body) {
70
            $table[] = array_combine($thead, $body);
71
        }
72
73
        return $table;
74
    }
75
76
    private static function outputForWeb(array $output, array $thead, array $tbody): string
77
    {
78
        foreach ($output as $directive => $values) {
79
            $current        = $values['current'];
80
            $notRecommended = false;
81
82
            if ($values['recommended'] !== '') {
83
                if ($values['recommended'] !== $values['current']) {
84
                    $notRecommended = true;
85
                }
86
87
                if ($values['current'] === '') {
88
                    $current = 'n/a';
89
                }
90
91
                $current = $notRecommended
92
                    ? '<span style="color: red">' . $current . '</span>'
93
                    : $current;
94
            }
95
96
            $directive = $notRecommended
97
                ? '<span style="color: red">' . $directive . '</span>'
98
                : $directive;
99
            $tbody[] = [
100
                $directive, $values['global'], $current, $values['recommended'], $values['remark'],
101
            ];
102
        }
103
104
        $table    = new Table();
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Security\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
        $template = [
106
            'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
107
        ];
108
        $table->setTemplate($template);
109
110
        $table->setHeading($thead);
111
112
        return '<pre>' . $table->generate($tbody) . '</pre>';
113
    }
114
115
    /**
116
     * @internal Used for testing purposes only.
117
     */
118
    public static function checkIni(): array
119
    {
120
        $items = [
121
            'error_reporting'         => ['recommended' => '5111'],
122
            'display_errors'          => ['recommended' => '0'],
123
            'display_startup_errors'  => ['recommended' => '0'],
124
            'log_errors'              => [],
125
            'error_log'               => [],
126
            'default_charset'         => ['recommended' => 'UTF-8'],
127
            'max_execution_time'      => ['remark' => 'The default is 30.'],
128
            'memory_limit'            => ['remark' => '> post_max_size'],
129
            'post_max_size'           => ['remark' => '> upload_max_filesize'],
130
            'upload_max_filesize'     => ['remark' => '< post_max_size'],
131
            'max_input_vars'          => ['remark' => 'The default is 1000.'],
132
            'request_order'           => ['recommended' => 'GP'],
133
            'variables_order'         => ['recommended' => 'GPCS'],
134
            'date.timezone'           => ['recommended' => 'UTC'],
135
            'mbstring.language'       => ['recommended' => 'neutral'],
136
            'opcache.enable'          => ['recommended' => '1'],
137
            'opcache.enable_cli'      => [],
138
            'opcache.jit'             => [],
139
            'opcache.jit_buffer_size' => [],
140
            'zend.assertions'         => ['recommended' => '-1'],
141
        ];
142
143
        $output = [];
144
        $ini    = ini_get_all();
145
146
        foreach ($items as $key => $values) {
147
            $hasKeyInIni  = array_key_exists($key, $ini);
148
            $output[$key] = [
149
                'global'      => $hasKeyInIni ? $ini[$key]['global_value'] : 'disabled',
150
                'current'     => $hasKeyInIni ? $ini[$key]['local_value'] : 'disabled',
151
                'recommended' => $values['recommended'] ?? '',
152
                'remark'      => $values['remark'] ?? '',
153
            ];
154
        }
155
156
        // [directive => [current_value, recommended_value]]
157
        return $output;
158
    }
159
}
160