]> git.mxchange.org Git - friendica.git/blob - src/Core/Install.php
Automatic Installation fix (#5565)
[friendica.git] / src / Core / Install.php
1 <?php
2 /**
3  * @file src/Core/Install.php
4  */
5 namespace Friendica\Core;
6
7 use DOMDocument;
8 use Exception;
9 use Friendica\BaseObject;
10 use Friendica\Database\DBStructure;
11 use Friendica\Object\Image;
12 use Friendica\Util\Network;
13
14 /**
15  * Contains methods for installation purpose of Friendica
16  */
17 class Install extends BaseObject
18 {
19         /**
20          * Checks the current installation environment. There are optional and mandatory checks.
21          *
22          * @param string $phpath Optional path to the PHP binary (Default is 'php')
23          *
24          * @return array First element is a list of all checks and their results,
25          *               the second element is a list of passed checks
26          */
27         public static function check($phpath = 'php')
28         {
29                 $checks = [];
30
31                 self::checkFunctions($checks);
32
33                 self::checkImagick($checks);
34
35                 self::checkLocalIni($checks);
36
37                 self::checkSmarty3($checks);
38
39                 self::checkKeys($checks);
40
41                 self::checkPHP($phpath, $checks);
42
43                 self::checkHtAccess($checks);
44
45                 $checkspassed = array_reduce($checks,
46                         function ($v, $c) {
47                                 if (!empty($c['require'])) {
48                                         $v = $v && $c['status'];
49                                 }
50                                 return $v;
51                         },
52                         true);
53
54                 return array($checks, $checkspassed);
55         }
56
57         /**
58          * Executes the installation of Friendica in the given environment.
59          * - Creates `config/local.ini.php`
60          * - Installs Database Structure
61          *
62          * @param string        $urlpath        Path based on the URL of Friendica (e.g. '/friendica')
63          * @param string        $dbhost         Hostname/IP of the Friendica Database
64          * @param string        $dbuser         Username of the Database connection credentials
65          * @param string        $dbpass         Password of the Database connection credentials
66          * @param string        $dbdata         Name of the Database
67          * @param string        $phpath         Path to the PHP-Binary (e.g. 'php' or '/usr/bin/php')
68          * @param string        $timezone       Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
69          * @param string        $language       2-letter ISO 639-1 code (eg. 'en')
70          * @param string        $adminmail      Mail-Adress of the administrator
71          */
72         public static function createConfig($urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $phpath, $timezone, $language, $adminmail)
73         {
74                 $tpl = get_markup_template('local.ini.tpl');
75                 $txt = replace_macros($tpl,[
76                         '$dbhost' => $dbhost,
77                         '$dbuser' => $dbuser,
78                         '$dbpass' => $dbpass,
79                         '$dbdata' => $dbdata,
80                         '$timezone' => $timezone,
81                         '$language' => $language,
82                         '$urlpath' => $urlpath,
83                         '$phpath' => $phpath,
84                         '$adminmail' => $adminmail,
85                 ]);
86
87                 $result = file_put_contents('config/local.ini.php', $txt);
88                 if (!$result) {
89                         self::getApp()->data['txt'] = $txt;
90                 }
91
92         }
93
94         /**
95          * Adds new checks to the array $checks
96          *
97          * @param array $checks The list of all checks (by-ref parameter!)
98          * @param string $title The title of the current check
99          * @param bool $status 1 = check passed, 0 = check not passed
100          * @param bool $required 1 = check is mandatory, 0 = check is optional
101          * @param string $help A help-string for the current check
102          * @param string $error_msg Optional. A error message, if the current check failed
103          */
104         private static function addCheck(&$checks, $title, $status, $required, $help, $error_msg = "")
105         {
106                 $checks[] = [
107                         'title' => $title,
108                         'status' => $status,
109                         'required' => $required,
110                         'help' => $help,
111                         'error_msg' => $error_msg,
112                 ];
113         }
114
115         /**
116          * PHP Check
117          *
118          * Checks the PHP environment.
119          *
120          * - Checks if a PHP binary is available
121          * - Checks if it is the CLI version
122          * - Checks if "register_argc_argv" is enabled
123          *
124          * @param string $phpath Optional. The Path to the PHP-Binary
125          * @param array $checks The list of all checks (by-ref parameter!)
126          */
127         public static function checkPHP($phpath, &$checks)
128         {
129                 $passed = $passed2 = $passed3 = false;
130                 if (strlen($phpath)) {
131                         $passed = file_exists($phpath);
132                 } else {
133                         $phpath = trim(shell_exec('which php'));
134                         $passed = strlen($phpath);
135                 }
136                 $help = "";
137                 if (!$passed) {
138                         $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
139                         $help .= L10n::t("If you don't have a command line version of PHP installed on your server, you will not be able to run the background processing. See <a href='https://github.com/friendica/friendica/blob/master/doc/Install.md#set-up-the-worker'>'Setup the worker'</a>") . EOL;
140                         $help .= EOL . EOL;
141                         $tpl = get_markup_template('field_input.tpl');
142                         $help .= replace_macros($tpl, [
143                                 '$field' => ['phpath', L10n::t('PHP executable path'), $phpath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
144                         ]);
145                         $phpath = "";
146                 }
147
148                 self::addCheck($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
149
150                 if ($passed) {
151                         $cmd = "$phpath -v";
152                         $result = trim(shell_exec($cmd));
153                         $passed2 = (strpos($result, "(cli)") !== false);
154                         list($result) = explode("\n", $result);
155                         $help = "";
156                         if (!$passed2) {
157                                 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
158                                 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
159                         }
160                         self::addCheck($checks, L10n::t('PHP cli binary'), $passed2, true, $help);
161                 }
162
163                 if ($passed2) {
164                         $str = autoname(8);
165                         $cmd = "$phpath testargs.php $str";
166                         $result = trim(shell_exec($cmd));
167                         $passed3 = $result == $str;
168                         $help = "";
169                         if (!$passed3) {
170                                 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
171                                 $help .= L10n::t('This is required for message delivery to work.');
172                         }
173                         self::addCheck($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);
174                 }
175         }
176
177         /**
178          * OpenSSL Check
179          *
180          * Checks the OpenSSL Environment
181          *
182          * - Checks, if the command "openssl_pkey_new" is available
183          *
184          * @param array $checks The list of all checks (by-ref parameter!)
185          */
186         public static function checkKeys(&$checks)
187         {
188                 $help = '';
189                 $res = false;
190
191                 if (function_exists('openssl_pkey_new')) {
192                         $res = openssl_pkey_new([
193                                 'digest_alg' => 'sha1',
194                                 'private_key_bits' => 4096,
195                                 'encrypt_key' => false
196                         ]);
197                 }
198
199                 // Get private key
200                 if (!$res) {
201                         $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
202                         $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
203                 }
204                 self::addCheck($checks, L10n::t('Generate encryption keys'), $res, true, $help);
205         }
206
207         /**
208          * PHP functions Check
209          *
210          * Checks the following PHP functions
211          * - libCurl
212          * - GD Graphics
213          * - OpenSSL
214          * - PDO or MySQLi
215          * - mb_string
216          * - XML
217          * - iconv
218          * - POSIX
219          *
220          * @param array $checks The list of all checks (by-ref parameter!)
221          */
222         public static function checkFunctions(&$checks)
223         {
224                 $ck_funcs = [];
225                 self::addCheck($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");
226                 self::addCheck($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");
227                 self::addCheck($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");
228                 self::addCheck($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");
229                 self::addCheck($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");
230                 self::addCheck($ck_funcs, L10n::t('XML PHP module'), true, true, "");
231                 self::addCheck($ck_funcs, L10n::t('iconv PHP module'), true, true, "");
232                 self::addCheck($ck_funcs, L10n::t('POSIX PHP module'), true, true, "");
233
234                 if (function_exists('apache_get_modules')) {
235                         if (! in_array('mod_rewrite',apache_get_modules())) {
236                                 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), false, true, L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.'));
237                         } else {
238                                 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");
239                         }
240                 }
241
242                 if (!function_exists('curl_init')) {
243                         $ck_funcs[0]['status'] = false;
244                         $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');
245                 }
246                 if (!function_exists('imagecreatefromjpeg')) {
247                         $ck_funcs[1]['status'] = false;
248                         $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');
249                 }
250                 if (!function_exists('openssl_public_encrypt')) {
251                         $ck_funcs[2]['status'] = false;
252                         $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');
253                 }
254                 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
255                         $ck_funcs[3]['status'] = false;
256                         $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
257                 }
258                 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
259                         $ck_funcs[3]['status'] = false;
260                         $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');
261                 }
262                 if (!function_exists('mb_strlen')) {
263                         $ck_funcs[4]['status'] = false;
264                         $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');
265                 }
266                 if (!function_exists('iconv_strlen')) {
267                         $ck_funcs[6]['status'] = false;
268                         $ck_funcs[6]['help'] = L10n::t('Error: iconv PHP module required but not installed.');
269                 }
270                 if (!function_exists('posix_kill')) {
271                         $ck_funcs[7]['status'] = false;
272                         $ck_funcs[7]['help'] = L10n::t('Error: POSIX PHP module required but not installed.');
273                 }
274
275                 $checks = array_merge($checks, $ck_funcs);
276
277                 // check for XML DOM Documents being able to be generated
278                 try {
279                         $xml = new DOMDocument();
280                 } catch (Exception $e) {
281                         $ck_funcs[5]['status'] = false;
282                         $ck_funcs[5]['help'] = L10n::t('Error, XML PHP module required but not installed.');
283                 }
284         }
285
286         /**
287          * "config/local.ini.php" - Check
288          *
289          * Checks if it's possible to create the "config/local.ini.php"
290          *
291          * @param array $checks The list of all checks (by-ref parameter!)
292          */
293         public static function checkLocalIni(&$checks)
294         {
295                 $status = true;
296                 $help = "";
297                 if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||
298                         (!file_exists('config/local.ini.php') && !is_writable('.'))) {
299
300                         $status = false;
301                         $help = L10n::t('The web installer needs to be able to create a file called "local.ini.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
302                         $help .= L10n::t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.') . EOL;
303                         $help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.ini.php in your Friendica "config" folder.') . EOL;
304                         $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
305                 }
306
307                 self::addCheck($checks, L10n::t('config/local.ini.php is writable'), $status, false, $help);
308
309         }
310
311         /**
312          * Smarty3 Template Check
313          *
314          * Checks, if the directory of Smarty3 is writable
315          *
316          * @param array $checks The list of all checks (by-ref parameter!)
317          */
318         public static function checkSmarty3(&$checks)
319         {
320                 $status = true;
321                 $help = "";
322                 if (!is_writable('view/smarty3')) {
323
324                         $status = false;
325                         $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
326                         $help .= L10n::t('In order to store these compiled templates, the web server needs to have write access to the directory view/smarty3/ under the Friendica top level folder.') . EOL;
327                         $help .= L10n::t("Please ensure that the user that your web server runs as \x28e.g. www-data\x29 has write access to this folder.") . EOL;
328                         $help .= L10n::t("Note: as a security measure, you should give the web server write access to view/smarty3/ only--not the template files \x28.tpl\x29 that it contains.") . EOL;
329                 }
330
331                 self::addCheck($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);
332         }
333
334         /**
335          * ".htaccess" - Check
336          *
337          * Checks, if "url_rewrite" is enabled in the ".htaccess" file
338          *
339          * @param array $checks The list of all checks (by-ref parameter!)
340          */
341         public static function checkHtAccess(&$checks)
342         {
343                 $status = true;
344                 $help = "";
345                 $error_msg = "";
346                 if (function_exists('curl_init')) {
347                         $test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");
348
349                         $url = normalise_link(System::baseUrl() . "/install/testrewrite");
350                         if ($test['body'] != "ok") {
351                                 $test = Network::fetchUrlFull($url);
352                         }
353
354                         if ($test['body'] != "ok") {
355                                 $status = false;
356                                 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
357                                 $error_msg = [];
358                                 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
359                                 $error_msg['url'] = $test['redirect_url'];
360                                 $error_msg['msg'] = defaults($test, 'error', '');
361                         }
362                         self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
363                 } else {
364                         // cannot check modrewrite if libcurl is not installed
365                         /// @TODO Maybe issue warning here?
366                 }
367         }
368
369         /**
370          * Imagick Check
371          *
372          * Checks, if the imagick module is available
373          *
374          * @param array $checks The list of all checks (by-ref parameter!)
375          */
376         public static function checkImagick(&$checks)
377         {
378                 $imagick = false;
379                 $gif = false;
380
381                 if (class_exists('Imagick')) {
382                         $imagick = true;
383                         $supported = Image::supportedTypes();
384                         if (array_key_exists('image/gif', $supported)) {
385                                 $gif = true;
386                         }
387                 }
388                 if ($imagick == false) {
389                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
390                 } else {
391                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
392                         if ($imagick) {
393                                 self::addCheck($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");
394                         }
395                 }
396         }
397
398         /**
399          * Installs the Database structure
400          *
401          * @return string A possible error
402          */
403         public static function installDatabaseStructure()
404         {
405                 $errors = DBStructure::update(false, true, true);
406
407                 return $errors;
408         }
409 }