xref: /webtrees/app/Services/ServerCheckService.php (revision bb5a472e1a5e38c642047dde59b04f1118bcd991)
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;
21b7059dccSGreg Roachuse Illuminate\Support\Collection;
22b7059dccSGreg Roachuse Illuminate\Support\Str;
23b7059dccSGreg Roachuse SQLite3;
24b7059dccSGreg Roachuse function array_map;
25b7059dccSGreg Roachuse function explode;
26b7059dccSGreg Roachuse function extension_loaded;
27b7059dccSGreg Roachuse function in_array;
28b7059dccSGreg Roachuse function ini_get;
29b7059dccSGreg Roachuse function sys_get_temp_dir;
30b7059dccSGreg Roachuse function trim;
31b7059dccSGreg Roachuse function version_compare;
32b7059dccSGreg Roachuse const PATH_SEPARATOR;
33b7059dccSGreg Roachuse const PHP_MAJOR_VERSION;
34b7059dccSGreg Roachuse const PHP_MINOR_VERSION;
35b7059dccSGreg Roach
36b7059dccSGreg Roach/**
37b7059dccSGreg Roach * Check if the server meets the minimum requirements for webtrees.
38b7059dccSGreg Roach */
39b7059dccSGreg Roachclass ServerCheckService
40b7059dccSGreg Roach{
41*bb5a472eSGreg Roach    private const PHP_SUPPORT_URL   = 'https://secure.php.net/supported-versions.php';
42*bb5a472eSGreg Roach    private const PHP_MINOR_VERSION = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
43*bb5a472eSGreg Roach    private const PHP_SUPPORT_DATES = [
44b7059dccSGreg Roach        '7.1' => '2019-12-01',
45b7059dccSGreg Roach        '7.2' => '2020-11-30',
46b7059dccSGreg Roach        '7.3' => '2021-12-06',
47b7059dccSGreg Roach    ];
48b7059dccSGreg Roach
49b7059dccSGreg Roach    // As required by illuminate/database 5.8
50b7059dccSGreg Roach    private const MINIMUM_SQLITE_VERSION = '3.7.11';
51b7059dccSGreg Roach
52b7059dccSGreg Roach    /**
53b7059dccSGreg Roach     * Things that may cause webtrees to break.
54b7059dccSGreg Roach     *
55b7059dccSGreg Roach     * @param string $driver
56b7059dccSGreg Roach     *
5754c7f8dfSGreg Roach     * @return Collection
5854c7f8dfSGreg Roach     * @return string[]
59b7059dccSGreg Roach     */
60b7059dccSGreg Roach    public function serverErrors($driver = ''): Collection
61b7059dccSGreg Roach    {
62b7059dccSGreg Roach        $errors = Collection::make([
63b7059dccSGreg Roach            $this->databaseDriverErrors($driver),
64b7059dccSGreg Roach            $this->checkPhpExtension('mbstring'),
65b7059dccSGreg Roach            $this->checkPhpExtension('iconv'),
66b7059dccSGreg Roach            $this->checkPhpExtension('pcre'),
67b7059dccSGreg Roach            $this->checkPhpExtension('session'),
68b7059dccSGreg Roach            $this->checkPhpExtension('xml'),
69b7059dccSGreg Roach            $this->checkPhpFunction('parse_ini_file'),
70b7059dccSGreg Roach        ]);
71b7059dccSGreg Roach
72b7059dccSGreg Roach        return $errors
73b7059dccSGreg Roach            ->flatten()
74b7059dccSGreg Roach            ->filter();
75b7059dccSGreg Roach    }
76b7059dccSGreg Roach
77b7059dccSGreg Roach    /**
78b7059dccSGreg Roach     * Things that should be fixed, but which won't stop completely webtrees from running.
79b7059dccSGreg Roach     *
80b7059dccSGreg Roach     * @param string $driver
81b7059dccSGreg Roach     *
8254c7f8dfSGreg Roach     * @return Collection
8354c7f8dfSGreg Roach     * @return string[]
84b7059dccSGreg Roach     */
85b7059dccSGreg Roach    public function serverWarnings($driver = ''): Collection
86b7059dccSGreg Roach    {
87b7059dccSGreg Roach        $warnings = Collection::make([
88b7059dccSGreg Roach            $this->databaseDriverWarnings($driver),
89b7059dccSGreg Roach            $this->checkPhpExtension('curl'),
90b7059dccSGreg Roach            $this->checkPhpExtension('gd'),
91b7059dccSGreg Roach            $this->checkPhpExtension('simplexml'),
92b7059dccSGreg Roach            $this->checkPhpIni('file_uploads', true),
93b7059dccSGreg Roach            $this->checkSystemTemporaryFolder(),
94b7059dccSGreg Roach            $this->checkPhpVersion(),
95b7059dccSGreg Roach        ]);
96b7059dccSGreg Roach
97b7059dccSGreg Roach        return $warnings
98b7059dccSGreg Roach            ->flatten()
99b7059dccSGreg Roach            ->filter();
100b7059dccSGreg Roach    }
101b7059dccSGreg Roach
102b7059dccSGreg Roach    /**
103b7059dccSGreg Roach     * Check if a PHP extension is loaded.
104b7059dccSGreg Roach     *
105b7059dccSGreg Roach     * @param string $extension
106b7059dccSGreg Roach     *
107b7059dccSGreg Roach     * @return string
108b7059dccSGreg Roach     */
109b7059dccSGreg Roach    private function checkPhpExtension(string $extension): string
110b7059dccSGreg Roach    {
111b7059dccSGreg Roach        if (!extension_loaded($extension)) {
112b7059dccSGreg Roach            return I18N::translate('The PHP extension “%s” is not installed.', $extension);
113b7059dccSGreg Roach        }
114b7059dccSGreg Roach
115b7059dccSGreg Roach        return '';
116b7059dccSGreg Roach    }
117b7059dccSGreg Roach
118b7059dccSGreg Roach    /**
119b7059dccSGreg Roach     * Check if a PHP setting is correct.
120b7059dccSGreg Roach     *
121b7059dccSGreg Roach     * @param string $varname
122b7059dccSGreg Roach     * @param bool   $expected
123b7059dccSGreg Roach     *
124b7059dccSGreg Roach     * @return string
125b7059dccSGreg Roach     */
126b7059dccSGreg Roach    private function checkPhpIni(string $varname, bool $expected): string
127b7059dccSGreg Roach    {
128b7059dccSGreg Roach        $ini_get = (bool) ini_get($varname);
129b7059dccSGreg Roach
130b7059dccSGreg Roach        if ($expected && $ini_get !== $expected) {
131b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is disabled.', $varname);
132b7059dccSGreg Roach        }
133b7059dccSGreg Roach
134b7059dccSGreg Roach        if (!$expected && $ini_get !== $expected) {
135b7059dccSGreg Roach            return I18N::translate('The PHP.INI setting “%1$s” is enabled.', $varname);
136b7059dccSGreg Roach        }
137b7059dccSGreg Roach
138b7059dccSGreg Roach        return '';
139b7059dccSGreg Roach    }
140b7059dccSGreg Roach
141b7059dccSGreg Roach    /**
142b7059dccSGreg Roach     * Check if a PHP extension is loaded.
143b7059dccSGreg Roach     *
144b7059dccSGreg Roach     * @param string $function
145b7059dccSGreg Roach     *
146b7059dccSGreg Roach     * @return string
147b7059dccSGreg Roach     */
148b7059dccSGreg Roach    private function checkPhpFunction(string $function): string
149b7059dccSGreg Roach    {
150b7059dccSGreg Roach        $disable_functions = explode(',', ini_get('disable_functions'));
151b7059dccSGreg Roach        $disable_functions = array_map(function (string $func): string {
152b7059dccSGreg Roach            return trim($func);
153b7059dccSGreg Roach        }, $disable_functions);
154b7059dccSGreg Roach
155b7059dccSGreg Roach        if (in_array($function, $disable_functions)) {
156acf70b2aSGreg Roach            return I18N::translate('The PHP function “%1$s” is disabled.', $function . '()');
157b7059dccSGreg Roach        }
158b7059dccSGreg Roach
159b7059dccSGreg Roach        return '';
160b7059dccSGreg Roach    }
161b7059dccSGreg Roach
162b7059dccSGreg Roach    /**
163b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
164b7059dccSGreg Roach     */
165b7059dccSGreg Roach    private function checkPhpVersion(): string
166b7059dccSGreg Roach    {
167b7059dccSGreg Roach        $today = date('Y-m-d');
168b7059dccSGreg Roach
169b7059dccSGreg Roach        foreach (self::PHP_SUPPORT_DATES as $version => $end_date) {
170b7059dccSGreg Roach            if (version_compare(self::PHP_MINOR_VERSION, $version) <= 0 && $today > $end_date) {
171b7059dccSGreg 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>';
172b7059dccSGreg Roach            }
173b7059dccSGreg Roach        }
174b7059dccSGreg Roach
175b7059dccSGreg Roach        return '';
176b7059dccSGreg Roach    }
177b7059dccSGreg Roach
178b7059dccSGreg Roach    /**
179b7059dccSGreg Roach     * Check the
180b7059dccSGreg Roach     *
181b7059dccSGreg Roach     * @return string
182b7059dccSGreg Roach     */
183b7059dccSGreg Roach    private function checkSqliteVersion(): string
184b7059dccSGreg Roach    {
185b7059dccSGreg Roach        if (class_exists(SQLite3::class)) {
186b7059dccSGreg Roach            $sqlite_version = SQLite3::version()['versionString'];
187b7059dccSGreg Roach
188b7059dccSGreg Roach            if (version_compare($sqlite_version, self::MINIMUM_SQLITE_VERSION) < 0) {
189b7059dccSGreg Roach                return I18N::translate('SQLite version %s is installed. SQLite version %s or later is required.', $sqlite_version, self::MINIMUM_SQLITE_VERSION);
190b7059dccSGreg Roach            }
191b7059dccSGreg Roach        }
192b7059dccSGreg Roach
193b7059dccSGreg Roach        return '';
194b7059dccSGreg Roach    }
195b7059dccSGreg Roach
196b7059dccSGreg Roach    /**
197b7059dccSGreg Roach     * Some servers configure their temporary folder in an unaccessible place.
198b7059dccSGreg Roach     */
199b7059dccSGreg Roach    private function checkSystemTemporaryFolder(): string
200b7059dccSGreg Roach    {
201b7059dccSGreg Roach        $open_basedir  = ini_get('open_basedir');
202b7059dccSGreg Roach        $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
203b7059dccSGreg Roach        $sys_temp_dir  = sys_get_temp_dir();
204b7059dccSGreg Roach
205b7059dccSGreg Roach        if ($open_basedir === '' || Str::startsWith($sys_temp_dir, $open_basedirs)) {
206b7059dccSGreg Roach            return '';
207b7059dccSGreg Roach        }
208b7059dccSGreg Roach
209b7059dccSGreg Roach        $message = I18N::translate('The server’s temporary folder cannot be accessed.');
210b7059dccSGreg Roach        $message .= '<br>sys_get_temp_dir() = "' . e($sys_temp_dir) . '"';
211b7059dccSGreg Roach        $message .= '<br>ini_get("open_basedir") = "' . e($open_basedir) . '"';
212b7059dccSGreg Roach
213b7059dccSGreg Roach        return $message;
214b7059dccSGreg Roach    }
215b7059dccSGreg Roach
216b7059dccSGreg Roach    /**
217b7059dccSGreg Roach     * @param string $driver
218b7059dccSGreg Roach     *
219b7059dccSGreg Roach     * @return Collection
220b7059dccSGreg Roach     */
221b7059dccSGreg Roach    private function databaseDriverErrors(string $driver): Collection
222b7059dccSGreg Roach    {
223b7059dccSGreg Roach        switch ($driver) {
224b7059dccSGreg Roach            case 'mysql':
225b7059dccSGreg Roach                return Collection::make([
226b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
227b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_mysql'),
228b7059dccSGreg Roach                ]);
229b7059dccSGreg Roach
230b7059dccSGreg Roach            case 'sqlite':
231b7059dccSGreg Roach                return Collection::make([
232b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
233b7059dccSGreg Roach                    $this->checkPhpExtension('sqlite3'),
234b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_sqlite'),
235b7059dccSGreg Roach                    $this->checkSqliteVersion(),
236b7059dccSGreg Roach                ]);
237b7059dccSGreg Roach
238b7059dccSGreg Roach            case 'pgsql':
239b7059dccSGreg Roach                return Collection::make([
240b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
241b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_pgsql'),
242b7059dccSGreg Roach                ]);
243b7059dccSGreg Roach
244b7059dccSGreg Roach            case 'sqlsvr':
245b7059dccSGreg Roach                return Collection::make([
246b7059dccSGreg Roach                    $this->checkPhpExtension('pdo'),
247b7059dccSGreg Roach                    $this->checkPhpExtension('pdo_odbc'),
248b7059dccSGreg Roach                ]);
249b7059dccSGreg Roach
250b7059dccSGreg Roach            default:
251b7059dccSGreg Roach                return new Collection();
252b7059dccSGreg Roach        }
253b7059dccSGreg Roach    }
254b7059dccSGreg Roach
255b7059dccSGreg Roach    /**
256b7059dccSGreg Roach     * @param string $driver
257b7059dccSGreg Roach     *
258b7059dccSGreg Roach     * @return Collection
259b7059dccSGreg Roach     */
260b7059dccSGreg Roach    private function databaseDriverWarnings(string $driver): Collection
261b7059dccSGreg Roach    {
262b7059dccSGreg Roach        switch ($driver) {
263b7059dccSGreg Roach            case 'sqlite':
264b7059dccSGreg Roach                return new Collection([
265b7059dccSGreg Roach                    I18N::translate('SQLite is only suitable for small sites, testing and evaluation.'),
266b7059dccSGreg Roach                ]);
267b7059dccSGreg Roach
268b7059dccSGreg Roach            case 'pgsql':
269b7059dccSGreg Roach                return new Collection([
270b7059dccSGreg Roach                    I18N::translate('Support for PostgreSQL is experimental.'),
271b7059dccSGreg Roach                ]);
272b7059dccSGreg Roach
273b7059dccSGreg Roach            case 'sqlsvr':
274b7059dccSGreg Roach                return new Collection([
275b7059dccSGreg Roach                    I18N::translate('Support for SQL Server is experimental.'),
276b7059dccSGreg Roach                ]);
277b7059dccSGreg Roach
278b7059dccSGreg Roach            default:
279b7059dccSGreg Roach                return new Collection();
280b7059dccSGreg Roach        }
281b7059dccSGreg Roach    }
282b7059dccSGreg Roach}
283