xref: /webtrees/app/Services/ServerCheckService.php (revision 4c8fd9a21a2f83b2d6f6ff708f3d81a9938d02f3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\I18N;
23use Illuminate\Support\Collection;
24use SQLite3;
25
26use function array_map;
27use function class_exists;
28use function date;
29use function e;
30use function explode;
31use function extension_loaded;
32use function function_exists;
33use function in_array;
34use function str_ends_with;
35use function str_starts_with;
36use function strtolower;
37use function sys_get_temp_dir;
38use function trim;
39use function version_compare;
40
41use const PATH_SEPARATOR;
42use const PHP_MAJOR_VERSION;
43use const PHP_MINOR_VERSION;
44use const PHP_VERSION;
45
46/**
47 * Check if the server meets the minimum requirements for webtrees.
48 */
49class ServerCheckService
50{
51    private const PHP_SUPPORT_URL   = 'https://www.php.net/supported-versions.php';
52    private const PHP_MINOR_VERSION = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
53    private const PHP_SUPPORT_DATES = [
54        '8.1' => '2024-11-25',
55        '8.2' => '2025-12-08',
56    ];
57
58    // As required by illuminate/database 8.x
59    private const MINIMUM_SQLITE_VERSION = '3.8.8';
60
61    /**
62     * Things that may cause webtrees to break.
63     *
64     * @param string $driver
65     *
66     * @return Collection<int,string>
67     */
68    public function serverErrors(string $driver = ''): Collection
69    {
70        $errors = Collection::make([
71            $this->databaseDriverErrors($driver),
72            $this->checkPhpExtension('mbstring'),
73            $this->checkPhpExtension('iconv'),
74            $this->checkPhpExtension('pcre'),
75            $this->checkPhpExtension('session'),
76            $this->checkPhpExtension('xml'),
77            $this->checkPhpFunction('parse_ini_file'),
78        ]);
79
80        return $errors
81            ->flatten()
82            ->filter();
83    }
84
85    /**
86     * Things that should be fixed, but which won't stop completely webtrees from running.
87     *
88     * @param string $driver
89     *
90     * @return Collection<int,string>
91     */
92    public function serverWarnings(string $driver = ''): Collection
93    {
94        $warnings = Collection::make([
95            $this->databaseDriverWarnings($driver),
96            $this->checkPhpExtension('curl'),
97            $this->checkPhpExtension('fileinfo'),
98            $this->checkPhpExtension('gd'),
99            $this->checkPhpExtension('intl'),
100            $this->checkPhpExtension('zip'),
101            $this->checkPhpIni('file_uploads', true),
102            $this->checkSystemTemporaryFolder(),
103            $this->checkPhpVersion(),
104        ]);
105
106        return $warnings
107            ->flatten()
108            ->filter();
109    }
110
111    /**
112     * Check if a PHP extension is loaded.
113     *
114     * @param string $extension
115     *
116     * @return string
117     */
118    private function checkPhpExtension(string $extension): string
119    {
120        if (!extension_loaded($extension)) {
121            return I18N::translate('The PHP extension “%s” is not installed.', $extension);
122        }
123
124        return '';
125    }
126
127    /**
128     * Check if a PHP setting is correct.
129     *
130     * @param string $varname
131     * @param bool   $expected
132     *
133     * @return string
134     */
135    private function checkPhpIni(string $varname, bool $expected): string
136    {
137        $actual = (bool) ini_get($varname);
138
139        if ($expected && !$actual) {
140            return I18N::translate('The PHP.INI setting “%1$s” is disabled.', $varname);
141        }
142
143        if (!$expected && $actual) {
144            return I18N::translate('The PHP.INI setting “%1$s” is enabled.', $varname);
145        }
146
147        return '';
148    }
149
150    /**
151     * Check if a PHP function is in the list of disabled functions.
152     *
153     * @param string $function
154     *
155     * @return bool
156     */
157    public function isFunctionDisabled(string $function): bool
158    {
159        $function = strtolower($function);
160
161        $disable_functions = explode(',', (string) ini_get('disable_functions'));
162        $disable_functions = array_map(static fn (string $func): string => strtolower(trim($func)), $disable_functions);
163
164        return in_array($function, $disable_functions, true) || !function_exists($function);
165    }
166
167    /**
168     * Create a warning message for a disabled function.
169     *
170     * @param string $function
171     *
172     * @return string
173     */
174    private function checkPhpFunction(string $function): string
175    {
176        if ($this->isFunctionDisabled($function)) {
177            return I18N::translate('The PHP function “%1$s” is disabled.', $function . '()');
178        }
179
180        return '';
181    }
182
183    /**
184     * Some servers configure their temporary folder in an inaccessible place.
185     */
186    private function checkPhpVersion(): string
187    {
188        $today = date('Y-m-d');
189
190        foreach (self::PHP_SUPPORT_DATES as $version => $end_date) {
191            if ($today > $end_date && version_compare(self::PHP_MINOR_VERSION, $version) <= 0) {
192                return I18N::translate('Your web server is using PHP version %s, which is no longer receiving security updates. You should upgrade to a later version as soon as possible.', PHP_VERSION) . ' <a href="' . e(self::PHP_SUPPORT_URL) . '">' . e(self::PHP_SUPPORT_URL) . '</a>';
193            }
194        }
195
196        return '';
197    }
198
199    /**
200     * Check the
201     *
202     * @return string
203     */
204    private function checkSqliteVersion(): string
205    {
206        if (class_exists(SQLite3::class)) {
207            $sqlite_version = SQLite3::version()['versionString'];
208
209            if (version_compare($sqlite_version, self::MINIMUM_SQLITE_VERSION) < 0) {
210                return I18N::translate('SQLite version %s is installed. SQLite version %s or later is required.', $sqlite_version, self::MINIMUM_SQLITE_VERSION);
211            }
212        }
213
214        return '';
215    }
216
217    /**
218     * Some servers configure their temporary folder in an inaccessible place.
219     */
220    private function checkSystemTemporaryFolder(): string
221    {
222        $open_basedir = ini_get('open_basedir');
223
224        if ($open_basedir === '') {
225            // open_basedir not used.
226            return '';
227        }
228
229        $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
230
231        $sys_temp_dir = sys_get_temp_dir();
232        $sys_temp_dir = $this->normalizeFolder($sys_temp_dir);
233
234        foreach ($open_basedirs as $dir) {
235            $dir = $this->normalizeFolder($dir);
236
237            if (str_starts_with($sys_temp_dir, $dir)) {
238                return '';
239            }
240        }
241
242        $message = I18N::translate('The server’s temporary folder cannot be accessed.');
243        $message .= '<br>sys_get_temp_dir() = "' . e($sys_temp_dir) . '"';
244        $message .= '<br>ini_get("open_basedir") = "' . e($open_basedir) . '"';
245
246        return $message;
247    }
248
249    /**
250     * Convert a folder name to a canonical form:
251     * - forward slashes.
252     * - trailing slash.
253     * We can't use realpath() as this can trigger open_basedir restrictions,
254     * and we are using this code to find out whether open_basedir will affect us.
255     *
256     * @param string $path
257     *
258     * @return string
259     */
260    private function normalizeFolder(string $path): string
261    {
262        $path = strtr($path, ['\\' => '/']);
263
264        if (str_ends_with($path, '/')) {
265            return $path;
266        }
267
268        return $path . '/';
269    }
270
271    /**
272     * @param string $driver
273     *
274     * @return Collection<int,string>
275     */
276    private function databaseDriverErrors(string $driver): Collection
277    {
278        switch ($driver) {
279            case 'mysql':
280                return Collection::make([
281                    $this->checkPhpExtension('pdo'),
282                    $this->checkPhpExtension('pdo_mysql'),
283                ]);
284
285            case 'sqlite':
286                return Collection::make([
287                    $this->checkPhpExtension('pdo'),
288                    $this->checkPhpExtension('sqlite3'),
289                    $this->checkPhpExtension('pdo_sqlite'),
290                    $this->checkSqliteVersion(),
291                ]);
292
293            case 'pgsql':
294                return Collection::make([
295                    $this->checkPhpExtension('pdo'),
296                    $this->checkPhpExtension('pdo_pgsql'),
297                ]);
298
299            case 'sqlsrv':
300                return Collection::make([
301                    $this->checkPhpExtension('pdo'),
302                    $this->checkPhpExtension('pdo_odbc'),
303                ]);
304
305            default:
306                return new Collection();
307        }
308    }
309
310    /**
311     * @param string $driver
312     *
313     * @return Collection<int,string>
314     */
315    private function databaseDriverWarnings(string $driver): Collection
316    {
317        switch ($driver) {
318            case 'sqlite':
319                return new Collection([
320                    I18N::translate('SQLite is only suitable for small sites, testing and evaluation.'),
321                ]);
322
323            case 'pgsql':
324                return new Collection([
325                    I18N::translate('Support for PostgreSQL is experimental.'),
326                ]);
327
328            case 'sqlsrv':
329                return new Collection([
330                    I18N::translate('Support for SQL Server is experimental.'),
331                ]);
332
333            default:
334                return new Collection();
335        }
336    }
337}
338