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