xref: /webtrees/app/Services/ServerCheckService.php (revision 1a6f76743f393620d32bb7880f7420121bc4b5e1)
1b7059dccSGreg Roach<?php
2b7059dccSGreg Roach/**
3b7059dccSGreg Roach * webtrees: online genealogy
4b7059dccSGreg Roach * Copyright (C) 2019 webtrees development team
5b7059dccSGreg Roach * This program is free software: you can redistribute it and/or modify
6b7059dccSGreg Roach * it under the terms of the GNU General Public License as published by
7b7059dccSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8b7059dccSGreg Roach * (at your option) any later version.
9b7059dccSGreg Roach * This program is distributed in the hope that it will be useful,
10b7059dccSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11b7059dccSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12b7059dccSGreg Roach * GNU General Public License for more details.
13b7059dccSGreg Roach * You should have received a copy of the GNU General Public License
14b7059dccSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15b7059dccSGreg Roach */
16b7059dccSGreg Roachdeclare(strict_types=1);
17b7059dccSGreg Roach
18b7059dccSGreg Roachnamespace Fisharebest\Webtrees\Services;
19b7059dccSGreg Roach
20b7059dccSGreg Roachuse Fisharebest\Webtrees\I18N;
21497c5612SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
22497c5612SGreg Roachuse Illuminate\Database\Query\Expression;
23b7059dccSGreg Roachuse Illuminate\Support\Collection;
24b7059dccSGreg Roachuse Illuminate\Support\Str;
25b7059dccSGreg Roachuse SQLite3;
26497c5612SGreg Roachuse stdClass;
277bf2ba3bSGreg Roachuse Throwable;
28b7059dccSGreg Roachuse function array_map;
29b33d97e2SGreg Roachuse function class_exists;
30b33d97e2SGreg Roachuse function date;
31b33d97e2SGreg Roachuse function e;
32b7059dccSGreg Roachuse function explode;
33b7059dccSGreg Roachuse function extension_loaded;
34b33d97e2SGreg Roachuse function function_exists;
35b7059dccSGreg Roachuse function in_array;
36b33d97e2SGreg Roachuse function preg_replace;
37b33d97e2SGreg Roachuse function strpos;
387bb10f9aSGreg Roachuse function strtolower;
39b7059dccSGreg Roachuse function sys_get_temp_dir;
40b7059dccSGreg Roachuse function trim;
41b7059dccSGreg Roachuse function version_compare;
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{
52bb5a472eSGreg Roach    private const PHP_SUPPORT_URL   = 'https://secure.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',
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     *
6854c7f8dfSGreg Roach     * @return Collection
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     *
9254c7f8dfSGreg Roach     * @return Collection
93b7059dccSGreg Roach     */
94b7059dccSGreg Roach    public function serverWarnings($driver = ''): Collection
95b7059dccSGreg Roach    {
96b7059dccSGreg Roach        $warnings = Collection::make([
97b7059dccSGreg Roach            $this->databaseDriverWarnings($driver),
98497c5612SGreg Roach            $this->databaseEngineWarnings(),
99b7059dccSGreg Roach            $this->checkPhpExtension('curl'),
100b7059dccSGreg Roach            $this->checkPhpExtension('gd'),
10123c3b21dSGreg Roach            $this->checkPhpExtension('zip'),
102b7059dccSGreg Roach            $this->checkPhpExtension('simplexml'),
103b7059dccSGreg Roach            $this->checkPhpIni('file_uploads', true),
104b7059dccSGreg Roach            $this->checkSystemTemporaryFolder(),
105b7059dccSGreg Roach            $this->checkPhpVersion(),
106b7059dccSGreg Roach        ]);
107b7059dccSGreg Roach
108b7059dccSGreg Roach        return $warnings
109b7059dccSGreg Roach            ->flatten()
110b7059dccSGreg Roach            ->filter();
111b7059dccSGreg Roach    }
112b7059dccSGreg Roach
113b7059dccSGreg Roach    /**
114b7059dccSGreg Roach     * Check if a PHP extension is loaded.
115b7059dccSGreg Roach     *
116b7059dccSGreg Roach     * @param string $extension
117b7059dccSGreg Roach     *
118b7059dccSGreg Roach     * @return string
119b7059dccSGreg Roach     */
120b7059dccSGreg Roach    private function checkPhpExtension(string $extension): string
121b7059dccSGreg Roach    {
122b7059dccSGreg Roach        if (!extension_loaded($extension)) {
123b7059dccSGreg Roach            return I18N::translate('The PHP extension “%s” is not installed.', $extension);
124b7059dccSGreg Roach        }
125b7059dccSGreg Roach
126b7059dccSGreg Roach        return '';
127b7059dccSGreg Roach    }
128b7059dccSGreg Roach
129b7059dccSGreg Roach    /**
130b7059dccSGreg Roach     * Check if a PHP setting is correct.
131b7059dccSGreg Roach     *
132b7059dccSGreg Roach     * @param string $varname
133b7059dccSGreg Roach     * @param bool   $expected
134b7059dccSGreg Roach     *
135b7059dccSGreg Roach     * @return string
136b7059dccSGreg Roach     */
137b7059dccSGreg Roach    private function checkPhpIni(string $varname, bool $expected): string
138b7059dccSGreg Roach    {
139b7059dccSGreg Roach        $ini_get = (bool) ini_get($varname);
140b7059dccSGreg Roach
141b7059dccSGreg Roach        if ($expected && $ini_get !== $expected) {
142b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is disabled.', $varname);
143b7059dccSGreg Roach        }
144b7059dccSGreg Roach
145b7059dccSGreg Roach        if (!$expected && $ini_get !== $expected) {
146b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is enabled.', $varname);
147b7059dccSGreg Roach        }
148b7059dccSGreg Roach
149b7059dccSGreg Roach        return '';
150b7059dccSGreg Roach    }
151b7059dccSGreg Roach
152b7059dccSGreg Roach    /**
1537bb10f9aSGreg Roach     * Check if a PHP function is in the list of disabled functions.
1547bb10f9aSGreg Roach     *
1557bb10f9aSGreg Roach     * @param string $function
1567bb10f9aSGreg Roach     *
1577d99559cSGreg Roach     * @return bool
1587bb10f9aSGreg Roach     */
1597bb10f9aSGreg Roach    public function isFunctionDisabled(string $function): bool
1607bb10f9aSGreg Roach    {
1617bb10f9aSGreg Roach        $disable_functions = explode(',', ini_get('disable_functions'));
1620b5fd0a6SGreg Roach        $disable_functions = array_map(static function (string $func): string {
163e364afe4SGreg Roach            return strtolower(trim($func));
1647bb10f9aSGreg Roach        }, $disable_functions);
1657bb10f9aSGreg Roach
1667bb10f9aSGreg Roach        $function = strtolower($function);
1677bb10f9aSGreg Roach
1687bb10f9aSGreg Roach        return in_array($function, $disable_functions, true) || !function_exists($function);
1697bb10f9aSGreg Roach    }
1707bb10f9aSGreg Roach
1717bb10f9aSGreg Roach    /**
1727bb10f9aSGreg Roach     * Create a warning message for a disabled function.
173b7059dccSGreg Roach     *
174b7059dccSGreg Roach     * @param string $function
175b7059dccSGreg Roach     *
176b7059dccSGreg Roach     * @return string
177b7059dccSGreg Roach     */
178b7059dccSGreg Roach    private function checkPhpFunction(string $function): string
179b7059dccSGreg Roach    {
1807bb10f9aSGreg Roach        if ($this->isFunctionDisabled($function)) {
181acf70b2aSGreg Roach            return I18N::translate('The PHP function “%1$s” is disabled.', $function . '()');
182b7059dccSGreg Roach        }
183b7059dccSGreg Roach
184b7059dccSGreg Roach        return '';
185b7059dccSGreg Roach    }
186b7059dccSGreg Roach
187b7059dccSGreg Roach    /**
188b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
189b7059dccSGreg Roach     */
190b7059dccSGreg Roach    private function checkPhpVersion(): string
191b7059dccSGreg Roach    {
192b7059dccSGreg Roach        $today = date('Y-m-d');
193b7059dccSGreg Roach
194b7059dccSGreg Roach        foreach (self::PHP_SUPPORT_DATES as $version => $end_date) {
195497c5612SGreg Roach            if ($today > $end_date && version_compare(self::PHP_MINOR_VERSION, $version) <= 0) {
196b7059dccSGreg 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>';
197b7059dccSGreg Roach            }
198b7059dccSGreg Roach        }
199b7059dccSGreg Roach
200b7059dccSGreg Roach        return '';
201b7059dccSGreg Roach    }
202b7059dccSGreg Roach
203b7059dccSGreg Roach    /**
204b7059dccSGreg Roach     * Check the
205b7059dccSGreg Roach     *
206b7059dccSGreg Roach     * @return string
207b7059dccSGreg Roach     */
208b7059dccSGreg Roach    private function checkSqliteVersion(): string
209b7059dccSGreg Roach    {
210b7059dccSGreg Roach        if (class_exists(SQLite3::class)) {
211b7059dccSGreg Roach            $sqlite_version = SQLite3::version()['versionString'];
212b7059dccSGreg Roach
213b7059dccSGreg Roach            if (version_compare($sqlite_version, self::MINIMUM_SQLITE_VERSION) < 0) {
214b7059dccSGreg Roach                return I18N::translate('SQLite version %s is installed. SQLite version %s or later is required.', $sqlite_version, self::MINIMUM_SQLITE_VERSION);
215b7059dccSGreg Roach            }
216b7059dccSGreg Roach        }
217b7059dccSGreg Roach
218b7059dccSGreg Roach        return '';
219b7059dccSGreg Roach    }
220b7059dccSGreg Roach
221b7059dccSGreg Roach    /**
222b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
223b7059dccSGreg Roach     */
224b7059dccSGreg Roach    private function checkSystemTemporaryFolder(): string
225b7059dccSGreg Roach    {
226b7059dccSGreg Roach        $open_basedir = ini_get('open_basedir');
227b7059dccSGreg Roach
228b33d97e2SGreg Roach        if ($open_basedir === '') {
229b33d97e2SGreg Roach            // open_basedir not used.
230b7059dccSGreg Roach            return '';
231b7059dccSGreg Roach        }
232b7059dccSGreg Roach
233b33d97e2SGreg Roach        $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
234b33d97e2SGreg Roach
235b33d97e2SGreg Roach        $sys_temp_dir = sys_get_temp_dir();
236b33d97e2SGreg Roach        $sys_temp_dir = $this->normalizeFolder($sys_temp_dir);
237b33d97e2SGreg Roach
238b33d97e2SGreg Roach        foreach ($open_basedirs as $dir) {
239b33d97e2SGreg Roach            $dir = $this->normalizeFolder($dir);
240b33d97e2SGreg Roach
241b33d97e2SGreg Roach            if (strpos($sys_temp_dir, $dir) === 0) {
242b33d97e2SGreg Roach                return '';
243b33d97e2SGreg Roach            }
244b33d97e2SGreg Roach        }
245b33d97e2SGreg Roach
246b7059dccSGreg Roach        $message = I18N::translate('The server’s temporary folder cannot be accessed.');
247b7059dccSGreg Roach        $message .= '<br>sys_get_temp_dir() = "' . e($sys_temp_dir) . '"';
248b7059dccSGreg Roach        $message .= '<br>ini_get("open_basedir") = "' . e($open_basedir) . '"';
249b7059dccSGreg Roach
250b7059dccSGreg Roach        return $message;
251b7059dccSGreg Roach    }
252b7059dccSGreg Roach
253b7059dccSGreg Roach    /**
254b33d97e2SGreg Roach     * Convert a folder name to a canonical form:
255b33d97e2SGreg Roach     * - forward slashes.
256b33d97e2SGreg Roach     * - trailing slash.
257b33d97e2SGreg Roach     * We can't use realpath() as this can trigger open_basedir restrictions,
258b33d97e2SGreg Roach     * and we are using this code to find out whether open_basedir will affect us.
259b33d97e2SGreg Roach     *
260b33d97e2SGreg Roach     * @param string $path
261b33d97e2SGreg Roach     *
262b33d97e2SGreg Roach     * @return string
263b33d97e2SGreg Roach     */
264b33d97e2SGreg Roach    private function normalizeFolder(string $path): string
265b33d97e2SGreg Roach    {
266b33d97e2SGreg Roach        $path = preg_replace('/[\\/]+/', '/', $path);
267b33d97e2SGreg Roach        $path = Str::finish($path, '/');
268b33d97e2SGreg Roach
269b33d97e2SGreg Roach        return $path;
270b33d97e2SGreg Roach    }
271b33d97e2SGreg Roach
272b33d97e2SGreg Roach    /**
273b7059dccSGreg Roach     * @param string $driver
274b7059dccSGreg Roach     *
275b7059dccSGreg Roach     * @return Collection
276b7059dccSGreg Roach     */
277b7059dccSGreg Roach    private function databaseDriverErrors(string $driver): Collection
278b7059dccSGreg Roach    {
279b7059dccSGreg Roach        switch ($driver) {
280b7059dccSGreg Roach            case 'mysql':
281b7059dccSGreg Roach                return Collection::make([
282b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
283b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_mysql'),
284b7059dccSGreg Roach                ]);
285b7059dccSGreg Roach
286b7059dccSGreg Roach            case 'sqlite':
287b7059dccSGreg Roach                return Collection::make([
288b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
289b7059dccSGreg Roach                    $this->checkPhpExtension('sqlite3'),
290b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_sqlite'),
291b7059dccSGreg Roach                    $this->checkSqliteVersion(),
292b7059dccSGreg Roach                ]);
293b7059dccSGreg Roach
294b7059dccSGreg Roach            case 'pgsql':
295b7059dccSGreg Roach                return Collection::make([
296b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
297b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_pgsql'),
298b7059dccSGreg Roach                ]);
299b7059dccSGreg Roach
300b7059dccSGreg Roach            case 'sqlsvr':
301b7059dccSGreg Roach                return Collection::make([
302b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
303b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_odbc'),
304b7059dccSGreg Roach                ]);
305b7059dccSGreg Roach
306b7059dccSGreg Roach            default:
307b7059dccSGreg Roach                return new Collection();
308b7059dccSGreg Roach        }
309b7059dccSGreg Roach    }
310b7059dccSGreg Roach
311b7059dccSGreg Roach    /**
312b7059dccSGreg Roach     * @param string $driver
313b7059dccSGreg Roach     *
314b7059dccSGreg Roach     * @return Collection
315b7059dccSGreg Roach     */
316b7059dccSGreg Roach    private function databaseDriverWarnings(string $driver): Collection
317b7059dccSGreg Roach    {
318b7059dccSGreg Roach        switch ($driver) {
319b7059dccSGreg Roach            case 'sqlite':
320b7059dccSGreg Roach                return new Collection([
321b7059dccSGreg Roach                    I18N::translate('SQLite is only suitable for small sites, testing and evaluation.'),
322b7059dccSGreg Roach                ]);
323b7059dccSGreg Roach
324b7059dccSGreg Roach            case 'pgsql':
325b7059dccSGreg Roach                return new Collection([
326b7059dccSGreg Roach                    I18N::translate('Support for PostgreSQL is experimental.'),
327b7059dccSGreg Roach                ]);
328b7059dccSGreg Roach
329b7059dccSGreg Roach            case 'sqlsvr':
330b7059dccSGreg Roach                return new Collection([
331b7059dccSGreg Roach                    I18N::translate('Support for SQL Server is experimental.'),
332b7059dccSGreg Roach                ]);
333b7059dccSGreg Roach
334b7059dccSGreg Roach            default:
335b7059dccSGreg Roach                return new Collection();
336b7059dccSGreg Roach        }
337b7059dccSGreg Roach    }
338497c5612SGreg Roach
339497c5612SGreg Roach    /**
340497c5612SGreg Roach     * @param string $driver
341497c5612SGreg Roach     *
342497c5612SGreg Roach     * @return Collection
343497c5612SGreg Roach     */
344497c5612SGreg Roach    private function databaseEngineWarnings(): Collection
345497c5612SGreg Roach    {
346497c5612SGreg Roach        $warnings = new Collection();
347497c5612SGreg Roach
348497c5612SGreg Roach        try {
349497c5612SGreg Roach            $connection = DB::connection();
3507bf2ba3bSGreg Roach        } catch (Throwable $ex) {
351497c5612SGreg Roach            // During setup, there won't be a connection.
352497c5612SGreg Roach            return new Collection();
353497c5612SGreg Roach        }
354497c5612SGreg Roach
355497c5612SGreg Roach        if ($connection->getDriverName() === 'mysql') {
356497c5612SGreg Roach            $rows = DB::select(
357*1a6f7674SGreg Roach                "SELECT table_name FROM information_schema.tables JOIN information_schema.engines USING (engine) WHERE table_schema = ? AND LEFT(table_name, ?) = ? AND transactions <> 'YES'",[
358497c5612SGreg Roach                    $connection->getDatabaseName(),
359497c5612SGreg Roach                    mb_strlen($connection->getTablePrefix()),
360497c5612SGreg Roach                    $connection->getTablePrefix(),
361497c5612SGreg Roach                ]);
362497c5612SGreg Roach
363497c5612SGreg Roach            $rows = new Collection($rows);
364497c5612SGreg Roach
365497c5612SGreg Roach            $rows = $rows->map(static function (stdClass $row): string {
366*1a6f7674SGreg Roach                $table = $row->TABLE_NAME ?? $row->table_name;
367*1a6f7674SGreg Roach                return '<code>ALTER TABLE `' . $table . '` ENGINE=InnoDB;</code>';
368497c5612SGreg Roach            });
369497c5612SGreg Roach
370497c5612SGreg Roach            if ($rows->isNotEmpty()) {
371497c5612SGreg Roach                $warning =
372497c5612SGreg Roach                    'The database uses non-transactional tables.' .
373497c5612SGreg Roach                    ' ' .
374497c5612SGreg Roach                    'You may get errors if more than one user updates data at the same time.' .
375497c5612SGreg Roach                    ' ' .
376497c5612SGreg Roach                    'To fix this, run the following SQL commands.' .
377497c5612SGreg Roach                    '<br>' .
378497c5612SGreg Roach                    $rows->implode('<br>');
379497c5612SGreg Roach
380497c5612SGreg Roach                $warnings->push($warning);
381497c5612SGreg Roach            }
382497c5612SGreg Roach        }
383497c5612SGreg Roach
384497c5612SGreg Roach        return $warnings;
385497c5612SGreg Roach    }
386b7059dccSGreg Roach}
387