Skip to content

Commit aea4135

Browse files
committed
feat: add CheckPhpIni class
1 parent f40b371 commit aea4135

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

system/Security/CheckPhpIni.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Security;
15+
16+
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\View\Table;
18+
19+
/**
20+
* Checks php.ini settings
21+
*
22+
* @used-by \CodeIgniter\Commands\Utilities\PhpIniCheck
23+
* @see \CodeIgniter\Security\CheckPhpIniTest
24+
*/
25+
class CheckPhpIni
26+
{
27+
/**
28+
* @param bool $isCli Set false if you run via Web
29+
*
30+
* @return string|void HTML string or void in CLI
31+
*/
32+
public static function run(bool $isCli = true)
33+
{
34+
$output = static::checkIni();
35+
36+
$thead = ['Directive', 'Global', 'Current', 'Recommended', 'Remark'];
37+
$tbody = [];
38+
39+
// CLI
40+
if ($isCli) {
41+
self::outputForCli($output, $thead, $tbody);
42+
43+
return;
44+
}
45+
46+
// Web
47+
return self::outputForWeb($output, $thead, $tbody);
48+
}
49+
50+
private static function outputForCli(array $output, array $thead, array $tbody): void
51+
{
52+
foreach ($output as $directive => $values) {
53+
$current = $values['current'];
54+
$notRecommended = false;
55+
56+
if ($values['recommended'] !== '') {
57+
if ($values['recommended'] !== $values['current']) {
58+
$notRecommended = true;
59+
}
60+
61+
$current = $notRecommended
62+
? CLI::color($values['current'] === '' ? 'n/a' : $values['current'], 'red')
63+
: $values['current'];
64+
}
65+
66+
$directive = $notRecommended ? CLI::color($directive, 'red') : $directive;
67+
$tbody[] = [
68+
$directive, $values['global'], $current, $values['recommended'], $values['remark'],
69+
];
70+
}
71+
72+
CLI::table($tbody, $thead);
73+
}
74+
75+
private static function outputForWeb(array $output, array $thead, array $tbody): string
76+
{
77+
foreach ($output as $directive => $values) {
78+
$current = $values['current'];
79+
$notRecommended = false;
80+
81+
if ($values['recommended'] !== '') {
82+
if ($values['recommended'] !== $values['current']) {
83+
$notRecommended = true;
84+
}
85+
86+
if ($values['current'] === '') {
87+
$current = 'n/a';
88+
}
89+
90+
$current = $notRecommended
91+
? '<span style="color: red">' . $current . '</span>'
92+
: $current;
93+
}
94+
95+
$directive = $notRecommended
96+
? '<span style="color: red">' . $directive . '</span>'
97+
: $directive;
98+
$tbody[] = [
99+
$directive, $values['global'], $current, $values['recommended'], $values['remark'],
100+
];
101+
}
102+
103+
$table = new Table();
104+
$template = [
105+
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
106+
];
107+
$table->setTemplate($template);
108+
109+
$table->setHeading($thead);
110+
111+
return '<pre>' . $table->generate($tbody) . '</pre>';
112+
}
113+
114+
/**
115+
* @internal Used for testing purposes only.
116+
* @testTag
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+
'memory_limit' => ['remark' => '> post_max_size'],
128+
'post_max_size' => ['remark' => '> upload_max_filesize'],
129+
'upload_max_filesize' => ['remark' => '< post_max_size'],
130+
'request_order' => ['recommended' => 'GP'],
131+
'variables_order' => ['recommended' => 'GPCS'],
132+
'date.timezone' => ['recommended' => 'UTC'],
133+
'mbstring.language' => ['recommended' => 'neutral'],
134+
];
135+
136+
$output = [];
137+
$ini = ini_get_all();
138+
139+
foreach ($items as $key => $values) {
140+
$output[$key] = [
141+
'global' => $ini[$key]['global_value'],
142+
'current' => $ini[$key]['local_value'],
143+
'recommended' => $values['recommended'] ?? '',
144+
'remark' => $values['remark'] ?? '',
145+
];
146+
}
147+
148+
// [directive => [current_value, recommended_value]]
149+
return $output;
150+
}
151+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[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 CodeIgniter\Security;
13+
14+
use CodeIgniter\CLI\CLI;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use CodeIgniter\Test\Mock\MockInputOutput;
17+
18+
/**
19+
* @internal
20+
*
21+
* @group Others
22+
*/
23+
final class CheckPhpIniTest extends CIUnitTestCase
24+
{
25+
public function testCheckIni()
26+
{
27+
$output = CheckPhpIni::checkIni();
28+
29+
$expected = [
30+
'global' => '',
31+
'current' => '1',
32+
'recommended' => '0',
33+
'remark' => '',
34+
];
35+
$this->assertSame($expected, $output['display_errors']);
36+
}
37+
38+
public function testRunCli()
39+
{
40+
// Set MockInputOutput to CLI.
41+
$io = new MockInputOutput();
42+
CLI::setInputOutput($io);
43+
44+
CheckPhpIni::run(true);
45+
46+
// Get the whole output string.
47+
$output = $io->getOutput();
48+
49+
$this->assertStringContainsString('display_errors', $output);
50+
51+
// Remove MockInputOutput.
52+
CLI::resetInputOutput();
53+
}
54+
55+
public function testRunWeb()
56+
{
57+
$output = CheckPhpIni::run(false);
58+
59+
$this->assertStringContainsString('display_errors', $output);
60+
}
61+
}

0 commit comments

Comments
 (0)