|
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 Defini a `false` s'il est exécuté via le Web |
|
25
|
|
|
* |
|
26
|
|
|
* @return array|string chaine HTML sur le web ou tableau en CLI |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function run(bool $isCli = true, ?string $argument = null) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$output = static::checkIni($argument); |
|
31
|
|
|
|
|
32
|
4 |
|
$thead = ['Directive', 'Globale', 'Actuelle', 'Recommandation', 'Remarque']; |
|
33
|
4 |
|
$tbody = []; |
|
34
|
|
|
|
|
35
|
|
|
// CLI |
|
36
|
|
|
if ($isCli) { |
|
37
|
2 |
|
return self::outputForCli($output, $thead, $tbody); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Web |
|
41
|
2 |
|
return self::outputForWeb($output, $thead, $tbody); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private static function outputForCli(array $output, array $thead, array $tbody): array |
|
45
|
|
|
{ |
|
46
|
2 |
|
$color = new Color(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($output as $directive => $values) { |
|
49
|
2 |
|
$current = $values['current'] ?? ''; |
|
50
|
2 |
|
$notRecommended = false; |
|
51
|
|
|
|
|
52
|
|
|
if ($values['recommended'] !== '') { |
|
53
|
|
|
if ($values['recommended'] !== $current) { |
|
54
|
2 |
|
$notRecommended = true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$current = $notRecommended |
|
58
|
|
|
? $color->error($current === '' ? 'n/a' : $current) |
|
59
|
2 |
|
: $current; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
$directive = $notRecommended ? $color->error($directive) : $directive; |
|
63
|
|
|
$tbody[] = [ |
|
64
|
|
|
$directive, $values['global'], $current, $values['recommended'], $values['remark'], |
|
65
|
2 |
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$table = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($tbody as $body) { |
|
71
|
2 |
|
$table[] = array_combine($thead, $body); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $table; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private static function outputForWeb(array $output, array $thead, array $tbody): string |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($output as $directive => $values) { |
|
80
|
2 |
|
$current = $values['current']; |
|
81
|
2 |
|
$notRecommended = false; |
|
82
|
|
|
|
|
83
|
|
|
if ($values['recommended'] !== '') { |
|
84
|
|
|
if ($values['recommended'] !== $values['current']) { |
|
85
|
2 |
|
$notRecommended = true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($values['current'] === '') { |
|
89
|
2 |
|
$current = 'n/a'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$current = $notRecommended |
|
93
|
|
|
? '<span style="color: red">' . $current . '</span>' |
|
94
|
2 |
|
: $current; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$directive = $notRecommended |
|
98
|
|
|
? '<span style="color: red">' . $directive . '</span>' |
|
99
|
2 |
|
: $directive; |
|
100
|
|
|
$tbody[] = [ |
|
101
|
|
|
$directive, $values['global'], $current, $values['recommended'], $values['remark'], |
|
102
|
2 |
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* $table = new Table(); |
|
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
|
2 |
|
return ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @internal Utilisé uniquement à des fins de test. |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function checkIni(?string $argument = null): array |
|
122
|
|
|
{ |
|
123
|
|
|
$items = [ |
|
124
|
|
|
'error_reporting' => ['recommended' => '5111'], |
|
125
|
|
|
'display_errors' => ['recommended' => '0'], |
|
126
|
|
|
'display_startup_errors' => ['recommended' => '0'], |
|
127
|
|
|
'log_errors' => [], |
|
128
|
|
|
'error_log' => [], |
|
129
|
|
|
'default_charset' => ['recommended' => 'UTF-8'], |
|
130
|
|
|
'max_execution_time' => ['remark' => 'The default is 30.'], |
|
131
|
|
|
'memory_limit' => ['remark' => '> post_max_size'], |
|
132
|
|
|
'post_max_size' => ['remark' => '> upload_max_filesize'], |
|
133
|
|
|
'upload_max_filesize' => ['remark' => '< post_max_size'], |
|
134
|
|
|
'max_input_vars' => ['remark' => 'La valeur par défaut est 1000.'], |
|
135
|
|
|
'request_order' => ['recommended' => 'GP'], |
|
136
|
|
|
'variables_order' => ['recommended' => 'GPCS'], |
|
137
|
|
|
'date.timezone' => ['recommended' => 'UTC'], |
|
138
|
|
|
'mbstring.language' => ['recommended' => 'neutral'], |
|
139
|
|
|
'opcache.enable' => ['recommended' => '1'], |
|
140
|
|
|
'opcache.enable_cli' => ['recommended' => '0', 'remark' => 'Activer lorsque vous utilisez des files d\'attente ou que vous exécutez des tâches CLI répétitives'], |
|
141
|
|
|
'opcache.jit' => ['recommended' => 'tracing'], |
|
142
|
|
|
'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Ajustez avec votre espace mémoire libre'], |
|
143
|
|
|
'zend.assertions' => ['recommended' => '-1'], |
|
144
|
4 |
|
]; |
|
145
|
|
|
|
|
146
|
|
|
if ($argument === 'opcache') { |
|
147
|
|
|
$items = [ |
|
148
|
|
|
'opcache.enable' => ['recommended' => '1'], |
|
149
|
|
|
'opcache.enable_cli' => ['recommended' => '0', 'remark' => 'Activer lorsque vous utilisez des files d\'attente ou que vous exécutez des tâches CLI répétitives'], |
|
150
|
|
|
'opcache.jit' => ['recommended' => 'tracing', 'remark' => 'Désactiver lorsque vous utilisez des extensions tierces'], |
|
151
|
|
|
'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Ajustez avec votre espace mémoire libre'], |
|
152
|
|
|
'opcache.memory_consumption' => ['recommended' => '128', 'remark' => 'Ajustez avec votre espace mémoire libre'], |
|
153
|
|
|
'opcache.interned_strings_buffer' => ['recommended' => '16'], |
|
154
|
|
|
'opcache.max_accelerated_files' => ['remark' => 'Ajuster en fonction du nombre de fichiers PHP dans votre projet (par exemple : find your_project/ -iname \'*.php\'|wc -l)'], |
|
155
|
|
|
'opcache.max_wasted_percentage' => ['recommended' => '10'], |
|
156
|
|
|
'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'Lorsque vous le désactivez, opcache conserve votre code dans la mémoire partagée. Le redémarrage du serveur web est nécessaire'], |
|
157
|
|
|
'opcache.revalidate_freq' => [], |
|
158
|
|
|
'opcache.file_cache' => ['remark' => 'Mise en cache du fichier de localisation, ce qui devrait améliorer les performances lorsque la mémoire du SHM est pleine.'], |
|
159
|
|
|
'opcache.file_cache_only' => ['remark' => 'Mise en cache du code optique dans la mémoire partagée, désactivée lorsque vous utilisez Windows'], |
|
160
|
|
|
'opcache.file_cache_fallback' => ['remark' => 'Activer lorsque vous utilisez Windows'], |
|
161
|
|
|
'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Activé lorsque vous utilisez l\'annotation docblock `package require`'], |
|
162
|
4 |
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
4 |
|
$output = []; |
|
166
|
4 |
|
$ini = ini_get_all(); |
|
167
|
|
|
|
|
168
|
|
|
foreach ($items as $key => $values) { |
|
169
|
4 |
|
$hasKeyInIni = array_key_exists($key, $ini); |
|
170
|
|
|
$output[$key] = [ |
|
171
|
|
|
'global' => $hasKeyInIni ? $ini[$key]['global_value'] : 'disabled', |
|
172
|
|
|
'current' => $hasKeyInIni ? $ini[$key]['local_value'] : 'disabled', |
|
173
|
|
|
'recommended' => $values['recommended'] ?? '', |
|
174
|
|
|
'remark' => $values['remark'] ?? '', |
|
175
|
4 |
|
]; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// [directive => [current_value, recommended_value]] |
|
179
|
4 |
|
return $output; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.