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