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