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