xref: /webtrees/app/Services/ServerCheckService.php (revision b5c8fd7e66957665381ee23f19cf39bda22bc768)
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;
23497c5612SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
24b7059dccSGreg Roachuse Illuminate\Support\Collection;
25b7059dccSGreg Roachuse Illuminate\Support\Str;
26b7059dccSGreg Roachuse SQLite3;
27497c5612SGreg Roachuse stdClass;
287bf2ba3bSGreg Roachuse Throwable;
293976b470SGreg Roach
30b7059dccSGreg Roachuse function array_map;
31b33d97e2SGreg Roachuse function class_exists;
32b33d97e2SGreg Roachuse function date;
33b33d97e2SGreg Roachuse function e;
34b7059dccSGreg Roachuse function explode;
35b7059dccSGreg Roachuse function extension_loaded;
36b33d97e2SGreg Roachuse function function_exists;
37b7059dccSGreg Roachuse function in_array;
38b33d97e2SGreg Roachuse function preg_replace;
39b33d97e2SGreg Roachuse function strpos;
407bb10f9aSGreg Roachuse function strtolower;
41b7059dccSGreg Roachuse function sys_get_temp_dir;
42b7059dccSGreg Roachuse function trim;
43b7059dccSGreg Roachuse function version_compare;
443976b470SGreg Roach
45b7059dccSGreg Roachuse const PATH_SEPARATOR;
46b7059dccSGreg Roachuse const PHP_MAJOR_VERSION;
47b7059dccSGreg Roachuse const PHP_MINOR_VERSION;
48b33d97e2SGreg Roachuse const PHP_VERSION;
49b7059dccSGreg Roach
50b7059dccSGreg Roach/**
51b7059dccSGreg Roach * Check if the server meets the minimum requirements for webtrees.
52b7059dccSGreg Roach */
53b7059dccSGreg Roachclass ServerCheckService
54b7059dccSGreg Roach{
55bb5a472eSGreg Roach    private const PHP_SUPPORT_URL   = 'https://secure.php.net/supported-versions.php';
56bb5a472eSGreg Roach    private const PHP_MINOR_VERSION = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
57bb5a472eSGreg Roach    private const PHP_SUPPORT_DATES = [
58b7059dccSGreg Roach        '7.1' => '2019-12-01',
59b7059dccSGreg Roach        '7.2' => '2020-11-30',
60b7059dccSGreg Roach        '7.3' => '2021-12-06',
61b7059dccSGreg Roach    ];
62b7059dccSGreg Roach
63b7059dccSGreg Roach    // As required by illuminate/database 5.8
64b7059dccSGreg Roach    private const MINIMUM_SQLITE_VERSION = '3.7.11';
65b7059dccSGreg Roach
66b7059dccSGreg Roach    /**
67b7059dccSGreg Roach     * Things that may cause webtrees to break.
68b7059dccSGreg Roach     *
69b7059dccSGreg Roach     * @param string $driver
70b7059dccSGreg Roach     *
71*b5c8fd7eSGreg Roach     * @return Collection<string>
72b7059dccSGreg Roach     */
73b7059dccSGreg Roach    public function serverErrors($driver = ''): Collection
74b7059dccSGreg Roach    {
75b7059dccSGreg Roach        $errors = Collection::make([
76b7059dccSGreg Roach            $this->databaseDriverErrors($driver),
77b7059dccSGreg Roach            $this->checkPhpExtension('mbstring'),
78b7059dccSGreg Roach            $this->checkPhpExtension('iconv'),
79b7059dccSGreg Roach            $this->checkPhpExtension('pcre'),
80b7059dccSGreg Roach            $this->checkPhpExtension('session'),
81b7059dccSGreg Roach            $this->checkPhpExtension('xml'),
82b7059dccSGreg Roach            $this->checkPhpFunction('parse_ini_file'),
83b7059dccSGreg Roach        ]);
84b7059dccSGreg Roach
85b7059dccSGreg Roach        return $errors
86b7059dccSGreg Roach            ->flatten()
87b7059dccSGreg Roach            ->filter();
88b7059dccSGreg Roach    }
89b7059dccSGreg Roach
90b7059dccSGreg Roach    /**
91b7059dccSGreg Roach     * Things that should be fixed, but which won't stop completely webtrees from running.
92b7059dccSGreg Roach     *
93b7059dccSGreg Roach     * @param string $driver
94b7059dccSGreg Roach     *
95*b5c8fd7eSGreg Roach     * @return Collection<string>
96b7059dccSGreg Roach     */
97b7059dccSGreg Roach    public function serverWarnings($driver = ''): Collection
98b7059dccSGreg Roach    {
99b7059dccSGreg Roach        $warnings = Collection::make([
100b7059dccSGreg Roach            $this->databaseDriverWarnings($driver),
101497c5612SGreg Roach            $this->databaseEngineWarnings(),
102b7059dccSGreg Roach            $this->checkPhpExtension('curl'),
103c95eb19bSGreg Roach            $this->checkPhpExtension('fileinfo'),
104b7059dccSGreg Roach            $this->checkPhpExtension('gd'),
10523c3b21dSGreg Roach            $this->checkPhpExtension('zip'),
106b7059dccSGreg Roach            $this->checkPhpExtension('simplexml'),
107b7059dccSGreg Roach            $this->checkPhpIni('file_uploads', true),
108b7059dccSGreg Roach            $this->checkSystemTemporaryFolder(),
109b7059dccSGreg Roach            $this->checkPhpVersion(),
110b7059dccSGreg Roach        ]);
111b7059dccSGreg Roach
112b7059dccSGreg Roach        return $warnings
113b7059dccSGreg Roach            ->flatten()
114b7059dccSGreg Roach            ->filter();
115b7059dccSGreg Roach    }
116b7059dccSGreg Roach
117b7059dccSGreg Roach    /**
118b7059dccSGreg Roach     * Check if a PHP extension is loaded.
119b7059dccSGreg Roach     *
120b7059dccSGreg Roach     * @param string $extension
121b7059dccSGreg Roach     *
122b7059dccSGreg Roach     * @return string
123b7059dccSGreg Roach     */
124b7059dccSGreg Roach    private function checkPhpExtension(string $extension): string
125b7059dccSGreg Roach    {
126b7059dccSGreg Roach        if (!extension_loaded($extension)) {
127b7059dccSGreg Roach            return I18N::translate('The PHP extension “%s” is not installed.', $extension);
128b7059dccSGreg Roach        }
129b7059dccSGreg Roach
130b7059dccSGreg Roach        return '';
131b7059dccSGreg Roach    }
132b7059dccSGreg Roach
133b7059dccSGreg Roach    /**
134b7059dccSGreg Roach     * Check if a PHP setting is correct.
135b7059dccSGreg Roach     *
136b7059dccSGreg Roach     * @param string $varname
137b7059dccSGreg Roach     * @param bool   $expected
138b7059dccSGreg Roach     *
139b7059dccSGreg Roach     * @return string
140b7059dccSGreg Roach     */
141b7059dccSGreg Roach    private function checkPhpIni(string $varname, bool $expected): string
142b7059dccSGreg Roach    {
143b7059dccSGreg Roach        $ini_get = (bool) ini_get($varname);
144b7059dccSGreg Roach
145b7059dccSGreg Roach        if ($expected && $ini_get !== $expected) {
146b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is disabled.', $varname);
147b7059dccSGreg Roach        }
148b7059dccSGreg Roach
149b7059dccSGreg Roach        if (!$expected && $ini_get !== $expected) {
150b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is enabled.', $varname);
151b7059dccSGreg Roach        }
152b7059dccSGreg Roach
153b7059dccSGreg Roach        return '';
154b7059dccSGreg Roach    }
155b7059dccSGreg Roach
156b7059dccSGreg Roach    /**
1577bb10f9aSGreg Roach     * Check if a PHP function is in the list of disabled functions.
1587bb10f9aSGreg Roach     *
1597bb10f9aSGreg Roach     * @param string $function
1607bb10f9aSGreg Roach     *
1617d99559cSGreg Roach     * @return bool
1627bb10f9aSGreg Roach     */
1637bb10f9aSGreg Roach    public function isFunctionDisabled(string $function): bool
1647bb10f9aSGreg Roach    {
1657bb10f9aSGreg Roach        $disable_functions = explode(',', ini_get('disable_functions'));
1660b5fd0a6SGreg Roach        $disable_functions = array_map(static function (string $func): string {
167e364afe4SGreg Roach            return strtolower(trim($func));
1687bb10f9aSGreg Roach        }, $disable_functions);
1697bb10f9aSGreg Roach
1707bb10f9aSGreg Roach        $function = strtolower($function);
1717bb10f9aSGreg Roach
1727bb10f9aSGreg Roach        return in_array($function, $disable_functions, true) || !function_exists($function);
1737bb10f9aSGreg Roach    }
1747bb10f9aSGreg Roach
1757bb10f9aSGreg Roach    /**
1767bb10f9aSGreg Roach     * Create a warning message for a disabled function.
177b7059dccSGreg Roach     *
178b7059dccSGreg Roach     * @param string $function
179b7059dccSGreg Roach     *
180b7059dccSGreg Roach     * @return string
181b7059dccSGreg Roach     */
182b7059dccSGreg Roach    private function checkPhpFunction(string $function): string
183b7059dccSGreg Roach    {
1847bb10f9aSGreg Roach        if ($this->isFunctionDisabled($function)) {
185acf70b2aSGreg Roach            return I18N::translate('The PHP function “%1$s” is disabled.', $function . '()');
186b7059dccSGreg Roach        }
187b7059dccSGreg Roach
188b7059dccSGreg Roach        return '';
189b7059dccSGreg Roach    }
190b7059dccSGreg Roach
191b7059dccSGreg Roach    /**
192b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
193b7059dccSGreg Roach     */
194b7059dccSGreg Roach    private function checkPhpVersion(): string
195b7059dccSGreg Roach    {
196b7059dccSGreg Roach        $today = date('Y-m-d');
197b7059dccSGreg Roach
198b7059dccSGreg Roach        foreach (self::PHP_SUPPORT_DATES as $version => $end_date) {
199497c5612SGreg Roach            if ($today > $end_date && version_compare(self::PHP_MINOR_VERSION, $version) <= 0) {
200b7059dccSGreg 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>';
201b7059dccSGreg Roach            }
202b7059dccSGreg Roach        }
203b7059dccSGreg Roach
204b7059dccSGreg Roach        return '';
205b7059dccSGreg Roach    }
206b7059dccSGreg Roach
207b7059dccSGreg Roach    /**
208b7059dccSGreg Roach     * Check the
209b7059dccSGreg Roach     *
210b7059dccSGreg Roach     * @return string
211b7059dccSGreg Roach     */
212b7059dccSGreg Roach    private function checkSqliteVersion(): string
213b7059dccSGreg Roach    {
214b7059dccSGreg Roach        if (class_exists(SQLite3::class)) {
215b7059dccSGreg Roach            $sqlite_version = SQLite3::version()['versionString'];
216b7059dccSGreg Roach
217b7059dccSGreg Roach            if (version_compare($sqlite_version, self::MINIMUM_SQLITE_VERSION) < 0) {
218b7059dccSGreg Roach                return I18N::translate('SQLite version %s is installed. SQLite version %s or later is required.', $sqlite_version, self::MINIMUM_SQLITE_VERSION);
219b7059dccSGreg Roach            }
220b7059dccSGreg Roach        }
221b7059dccSGreg Roach
222b7059dccSGreg Roach        return '';
223b7059dccSGreg Roach    }
224b7059dccSGreg Roach
225b7059dccSGreg Roach    /**
226b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
227b7059dccSGreg Roach     */
228b7059dccSGreg Roach    private function checkSystemTemporaryFolder(): string
229b7059dccSGreg Roach    {
230b7059dccSGreg Roach        $open_basedir = ini_get('open_basedir');
231b7059dccSGreg Roach
232b33d97e2SGreg Roach        if ($open_basedir === '') {
233b33d97e2SGreg Roach            // open_basedir not used.
234b7059dccSGreg Roach            return '';
235b7059dccSGreg Roach        }
236b7059dccSGreg Roach
237b33d97e2SGreg Roach        $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
238b33d97e2SGreg Roach
239b33d97e2SGreg Roach        $sys_temp_dir = sys_get_temp_dir();
240b33d97e2SGreg Roach        $sys_temp_dir = $this->normalizeFolder($sys_temp_dir);
241b33d97e2SGreg Roach
242b33d97e2SGreg Roach        foreach ($open_basedirs as $dir) {
243b33d97e2SGreg Roach            $dir = $this->normalizeFolder($dir);
244b33d97e2SGreg Roach
245b33d97e2SGreg Roach            if (strpos($sys_temp_dir, $dir) === 0) {
246b33d97e2SGreg Roach                return '';
247b33d97e2SGreg Roach            }
248b33d97e2SGreg Roach        }
249b33d97e2SGreg Roach
250b7059dccSGreg Roach        $message = I18N::translate('The server’s temporary folder cannot be accessed.');
251b7059dccSGreg Roach        $message .= '<br>sys_get_temp_dir() = "' . e($sys_temp_dir) . '"';
252b7059dccSGreg Roach        $message .= '<br>ini_get("open_basedir") = "' . e($open_basedir) . '"';
253b7059dccSGreg Roach
254b7059dccSGreg Roach        return $message;
255b7059dccSGreg Roach    }
256b7059dccSGreg Roach
257b7059dccSGreg Roach    /**
258b33d97e2SGreg Roach     * Convert a folder name to a canonical form:
259b33d97e2SGreg Roach     * - forward slashes.
260b33d97e2SGreg Roach     * - trailing slash.
261b33d97e2SGreg Roach     * We can't use realpath() as this can trigger open_basedir restrictions,
262b33d97e2SGreg Roach     * and we are using this code to find out whether open_basedir will affect us.
263b33d97e2SGreg Roach     *
264b33d97e2SGreg Roach     * @param string $path
265b33d97e2SGreg Roach     *
266b33d97e2SGreg Roach     * @return string
267b33d97e2SGreg Roach     */
268b33d97e2SGreg Roach    private function normalizeFolder(string $path): string
269b33d97e2SGreg Roach    {
270b33d97e2SGreg Roach        $path = preg_replace('/[\\/]+/', '/', $path);
271b33d97e2SGreg Roach        $path = Str::finish($path, '/');
272b33d97e2SGreg Roach
273b33d97e2SGreg Roach        return $path;
274b33d97e2SGreg Roach    }
275b33d97e2SGreg Roach
276b33d97e2SGreg Roach    /**
277b7059dccSGreg Roach     * @param string $driver
278b7059dccSGreg Roach     *
279*b5c8fd7eSGreg Roach     * @return Collection<string>
280b7059dccSGreg Roach     */
281b7059dccSGreg Roach    private function databaseDriverErrors(string $driver): Collection
282b7059dccSGreg Roach    {
283b7059dccSGreg Roach        switch ($driver) {
284b7059dccSGreg Roach            case 'mysql':
285b7059dccSGreg Roach                return Collection::make([
286b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
287b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_mysql'),
288b7059dccSGreg Roach                ]);
289b7059dccSGreg Roach
290b7059dccSGreg Roach            case 'sqlite':
291b7059dccSGreg Roach                return Collection::make([
292b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
293b7059dccSGreg Roach                    $this->checkPhpExtension('sqlite3'),
294b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_sqlite'),
295b7059dccSGreg Roach                    $this->checkSqliteVersion(),
296b7059dccSGreg Roach                ]);
297b7059dccSGreg Roach
298b7059dccSGreg Roach            case 'pgsql':
299b7059dccSGreg Roach                return Collection::make([
300b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
301b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_pgsql'),
302b7059dccSGreg Roach                ]);
303b7059dccSGreg Roach
304b7059dccSGreg Roach            case 'sqlsvr':
305b7059dccSGreg Roach                return Collection::make([
306b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
307b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_odbc'),
308b7059dccSGreg Roach                ]);
309b7059dccSGreg Roach
310b7059dccSGreg Roach            default:
311b7059dccSGreg Roach                return new Collection();
312b7059dccSGreg Roach        }
313b7059dccSGreg Roach    }
314b7059dccSGreg Roach
315b7059dccSGreg Roach    /**
316b7059dccSGreg Roach     * @param string $driver
317b7059dccSGreg Roach     *
318*b5c8fd7eSGreg Roach     * @return Collection<string>
319b7059dccSGreg Roach     */
320b7059dccSGreg Roach    private function databaseDriverWarnings(string $driver): Collection
321b7059dccSGreg Roach    {
322b7059dccSGreg Roach        switch ($driver) {
323b7059dccSGreg Roach            case 'sqlite':
324b7059dccSGreg Roach                return new Collection([
325b7059dccSGreg Roach                    I18N::translate('SQLite is only suitable for small sites, testing and evaluation.'),
326b7059dccSGreg Roach                ]);
327b7059dccSGreg Roach
328b7059dccSGreg Roach            case 'pgsql':
329b7059dccSGreg Roach                return new Collection([
330b7059dccSGreg Roach                    I18N::translate('Support for PostgreSQL is experimental.'),
331b7059dccSGreg Roach                ]);
332b7059dccSGreg Roach
333b7059dccSGreg Roach            case 'sqlsvr':
334b7059dccSGreg Roach                return new Collection([
335b7059dccSGreg Roach                    I18N::translate('Support for SQL Server is experimental.'),
336b7059dccSGreg Roach                ]);
337b7059dccSGreg Roach
338b7059dccSGreg Roach            default:
339b7059dccSGreg Roach                return new Collection();
340b7059dccSGreg Roach        }
341b7059dccSGreg Roach    }
342497c5612SGreg Roach
343497c5612SGreg Roach    /**
344*b5c8fd7eSGreg Roach     * @return Collection<string>
345497c5612SGreg Roach     */
346497c5612SGreg Roach    private function databaseEngineWarnings(): Collection
347497c5612SGreg Roach    {
348497c5612SGreg Roach        $warnings = new Collection();
349497c5612SGreg Roach
350497c5612SGreg Roach        try {
351497c5612SGreg Roach            $connection = DB::connection();
3527bf2ba3bSGreg Roach        } catch (Throwable $ex) {
353497c5612SGreg Roach            // During setup, there won't be a connection.
354497c5612SGreg Roach            return new Collection();
355497c5612SGreg Roach        }
356497c5612SGreg Roach
357497c5612SGreg Roach        if ($connection->getDriverName() === 'mysql') {
3584a9ddbdcSGreg Roach            $sql = "SELECT table_name FROM information_schema.tables JOIN information_schema.engines USING (engine) WHERE table_schema = ? AND LEFT(table_name, ?) = ? AND transactions <> 'YES'";
3594a9ddbdcSGreg Roach
3604a9ddbdcSGreg Roach            $bindings = [
361497c5612SGreg Roach                $connection->getDatabaseName(),
362497c5612SGreg Roach                mb_strlen($connection->getTablePrefix()),
363497c5612SGreg Roach                $connection->getTablePrefix(),
3644a9ddbdcSGreg Roach            ];
3654a9ddbdcSGreg Roach
3664a9ddbdcSGreg Roach            $rows = DB::connection()->select($sql, $bindings);
367497c5612SGreg Roach
368497c5612SGreg Roach            $rows = new Collection($rows);
369497c5612SGreg Roach
370497c5612SGreg Roach            $rows = $rows->map(static function (stdClass $row): string {
3711a6f7674SGreg Roach                $table = $row->TABLE_NAME ?? $row->table_name;
3721a6f7674SGreg Roach                return '<code>ALTER TABLE `' . $table . '` ENGINE=InnoDB;</code>';
373497c5612SGreg Roach            });
374497c5612SGreg Roach
375497c5612SGreg Roach            if ($rows->isNotEmpty()) {
376497c5612SGreg Roach                $warning =
377497c5612SGreg Roach                    'The database uses non-transactional tables.' .
378497c5612SGreg Roach                    ' ' .
379497c5612SGreg Roach                    'You may get errors if more than one user updates data at the same time.' .
380497c5612SGreg Roach                    ' ' .
381497c5612SGreg Roach                    'To fix this, run the following SQL commands.' .
382497c5612SGreg Roach                    '<br>' .
383497c5612SGreg Roach                    $rows->implode('<br>');
384497c5612SGreg Roach
385497c5612SGreg Roach                $warnings->push($warning);
386497c5612SGreg Roach            }
387497c5612SGreg Roach        }
388497c5612SGreg Roach
389497c5612SGreg Roach        return $warnings;
390497c5612SGreg Roach    }
391b7059dccSGreg Roach}
392