Passed
Push — main ( 268f21...f6aab6 )
by Dimitri
22:04 queued 18:12
created

CheckPhpIni::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 0
cts 5
cp 0
crap 6
rs 10
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
70
        foreach ($tbody as $body) {
71
            $table[] = array_combine($thead, $body);
72
        }
73
74
        return $table;
75
    }
76
77
    private static function outputForWeb(array $output, array $thead, array $tbody): string
78
    {
79
        foreach ($output as $directive => $values) {
80
            $current        = $values['current'];
81
            $notRecommended = false;
82
83
            if ($values['recommended'] !== '') {
84
                if ($values['recommended'] !== $values['current']) {
85
                    $notRecommended = true;
86
                }
87
88
                if ($values['current'] === '') {
89
                    $current = 'n/a';
90
                }
91
92
                $current = $notRecommended
93
                    ? '<span style="color: red">' . $current . '</span>'
94
                    : $current;
95
            }
96
97
            $directive = $notRecommended
98
                ? '<span style="color: red">' . $directive . '</span>'
99
                : $directive;
100
            $tbody[] = [
101
                $directive, $values['global'], $current, $values['recommended'], $values['remark'],
102
            ];
103
        }
104
105
        $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...
106
        $template = [
107
            'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
108
        ];
109
        $table->setTemplate($template);
110
111
        $table->setHeading($thead);
112
113
        return '<pre>' . $table->generate($tbody) . '</pre>';
114
    }
115
116
    /**
117
     * @internal Used for testing purposes only.
118
     */
119
    public static function checkIni(): array
120
    {
121
        $items = [
122
            'error_reporting'         => ['recommended' => '5111'],
123
            'display_errors'          => ['recommended' => '0'],
124
            'display_startup_errors'  => ['recommended' => '0'],
125
            'log_errors'              => [],
126
            'error_log'               => [],
127
            'default_charset'         => ['recommended' => 'UTF-8'],
128
            'max_execution_time'      => ['remark' => 'The default is 30.'],
129
            'memory_limit'            => ['remark' => '> post_max_size'],
130
            'post_max_size'           => ['remark' => '> upload_max_filesize'],
131
            'upload_max_filesize'     => ['remark' => '< post_max_size'],
132
            'max_input_vars'          => ['remark' => 'The default is 1000.'],
133
            'request_order'           => ['recommended' => 'GP'],
134
            'variables_order'         => ['recommended' => 'GPCS'],
135
            'date.timezone'           => ['recommended' => 'UTC'],
136
            'mbstring.language'       => ['recommended' => 'neutral'],
137
            'opcache.enable'          => ['recommended' => '1'],
138
            'opcache.enable_cli'      => [],
139
            'opcache.jit'             => [],
140
            'opcache.jit_buffer_size' => [],
141
            'zend.assertions'         => ['recommended' => '-1'],
142
        ];
143
144
        $output = [];
145
        $ini    = ini_get_all();
146
147
        foreach ($items as $key => $values) {
148
            $hasKeyInIni  = array_key_exists($key, $ini);
149
            $output[$key] = [
150
                'global'      => $hasKeyInIni ? $ini[$key]['global_value'] : 'disabled',
151
                'current'     => $hasKeyInIni ? $ini[$key]['local_value'] : 'disabled',
152
                'recommended' => $values['recommended'] ?? '',
153
                'remark'      => $values['remark'] ?? '',
154
            ];
155
        }
156
157
        // [directive => [current_value, recommended_value]]
158
        return $output;
159
    }
160
}
161