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