]> git.mxchange.org Git - friendica.git/blob - src/Core/Install.php
Update use statement lists with new Friendica\Database\dba class
[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::checkLocalIni($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 (!empty($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 `config/local.ini.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)\r
84         {\r
85                 $tpl = get_markup_template('local.ini.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                 ]);\r
97 \r
98                 $result = file_put_contents('config/local.ini.php', $txt);\r
99                 if (! $result) {\r
100                         self::getApp()->data['txt'] = $txt;\r
101                 }\r
102 \r
103                 $errors = self::installDatabaseStructure();\r
104 \r
105                 if ($errors) {\r
106                         self::getApp()->data['db_failed'] = $errors;\r
107                 } else {\r
108                         self::getApp()->data['db_installed'] = true;\r
109                 }\r
110         }\r
111 \r
112         /**\r
113          * Adds new checks to the array $checks\r
114          *\r
115          * @param array $checks The list of all checks (by-ref parameter!)\r
116          * @param string $title The title of the current check\r
117          * @param bool $status 1 = check passed, 0 = check not passed\r
118          * @param bool $required 1 = check is mandatory, 0 = check is optional\r
119          * @param string $help A help-string for the current check\r
120          * @param string $error_msg Optional. A error message, if the current check failed\r
121          */\r
122         private static function addCheck(&$checks, $title, $status, $required, $help, $error_msg = "")\r
123         {\r
124                 $checks[] = [\r
125                         'title' => $title,\r
126                         'status' => $status,\r
127                         'required' => $required,\r
128                         'help' => $help,\r
129                         'error_msg' => $error_msg,\r
130                 ];\r
131         }\r
132 \r
133         /**\r
134          * PHP Check\r
135          *\r
136          * Checks the PHP environment.\r
137          *\r
138          * - Checks if a PHP binary is available\r
139          * - Checks if it is the CLI version\r
140          * - Checks if "register_argc_argv" is enabled\r
141          *\r
142          * @param string $phpath Optional. The Path to the PHP-Binary\r
143          * @param array $checks The list of all checks (by-ref parameter!)\r
144          */\r
145         public static function checkPHP(&$phpath, &$checks)\r
146         {\r
147                 $passed = $passed2 = $passed3 = false;\r
148                 if (strlen($phpath)) {\r
149                         $passed = file_exists($phpath);\r
150                 } else {\r
151                         $phpath = trim(shell_exec('which php'));\r
152                         $passed = strlen($phpath);\r
153                 }\r
154                 $help = "";\r
155                 if (!$passed) {\r
156                         $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;\r
157                         $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
158                         $help .= EOL . EOL;\r
159                         $tpl = get_markup_template('field_input.tpl');\r
160                         $help .= replace_macros($tpl, [\r
161                                 '$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
162                         ]);\r
163                         $phpath = "";\r
164                 }\r
165 \r
166                 self::addCheck($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);\r
167 \r
168                 if ($passed) {\r
169                         $cmd = "$phpath -v";\r
170                         $result = trim(shell_exec($cmd));\r
171                         $passed2 = (strpos($result, "(cli)") !== false);\r
172                         list($result) = explode("\n", $result);\r
173                         $help = "";\r
174                         if (!$passed2) {\r
175                                 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;\r
176                                 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";\r
177                         }\r
178                         self::addCheck($checks, L10n::t('PHP cli binary'), $passed2, true, $help);\r
179                 }\r
180 \r
181                 if ($passed2) {\r
182                         $str = autoname(8);\r
183                         $cmd = "$phpath testargs.php $str";\r
184                         $result = trim(shell_exec($cmd));\r
185                         $passed3 = $result == $str;\r
186                         $help = "";\r
187                         if (!$passed3) {\r
188                                 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;\r
189                                 $help .= L10n::t('This is required for message delivery to work.');\r
190                         }\r
191                         self::addCheck($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);\r
192                 }\r
193         }\r
194 \r
195         /**\r
196          * OpenSSL Check\r
197          *\r
198          * Checks the OpenSSL Environment\r
199          *\r
200          * - Checks, if the command "openssl_pkey_new" is available\r
201          *\r
202          * @param array $checks The list of all checks (by-ref parameter!)\r
203          */\r
204         public static function checkKeys(&$checks)\r
205         {\r
206                 $help = '';\r
207                 $res = false;\r
208 \r
209                 if (function_exists('openssl_pkey_new')) {\r
210                         $res = openssl_pkey_new([\r
211                                 'digest_alg' => 'sha1',\r
212                                 'private_key_bits' => 4096,\r
213                                 'encrypt_key' => false\r
214                         ]);\r
215                 }\r
216 \r
217                 // Get private key\r
218                 if (!$res) {\r
219                         $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;\r
220                         $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');\r
221                 }\r
222                 self::addCheck($checks, L10n::t('Generate encryption keys'), $res, true, $help);\r
223         }\r
224 \r
225         /**\r
226          * PHP functions Check\r
227          *\r
228          * Checks the following PHP functions\r
229          * - libCurl\r
230          * - GD Graphics\r
231          * - OpenSSL\r
232          * - PDO or MySQLi\r
233          * - mb_string\r
234          * - XML\r
235          * - iconv\r
236          * - POSIX\r
237          *\r
238          * @param array $checks The list of all checks (by-ref parameter!)\r
239          */\r
240         public static function checkFunctions(&$checks)\r
241         {\r
242                 $ck_funcs = [];\r
243                 self::addCheck($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");\r
244                 self::addCheck($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");\r
245                 self::addCheck($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");\r
246                 self::addCheck($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");\r
247                 self::addCheck($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");\r
248                 self::addCheck($ck_funcs, L10n::t('XML PHP module'), true, true, "");\r
249                 self::addCheck($ck_funcs, L10n::t('iconv PHP module'), true, true, "");\r
250                 self::addCheck($ck_funcs, L10n::t('POSIX PHP module'), true, true, "");\r
251 \r
252                 if (function_exists('apache_get_modules')) {\r
253                         if (! in_array('mod_rewrite',apache_get_modules())) {\r
254                                 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
255                         } else {\r
256                                 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");\r
257                         }\r
258                 }\r
259 \r
260                 if (!function_exists('curl_init')) {\r
261                         $ck_funcs[0]['status'] = false;\r
262                         $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');\r
263                 }\r
264                 if (!function_exists('imagecreatefromjpeg')) {\r
265                         $ck_funcs[1]['status'] = false;\r
266                         $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');\r
267                 }\r
268                 if (!function_exists('openssl_public_encrypt')) {\r
269                         $ck_funcs[2]['status'] = false;\r
270                         $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');\r
271                 }\r
272                 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {\r
273                         $ck_funcs[3]['status'] = false;\r
274                         $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');\r
275                 }\r
276                 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {\r
277                         $ck_funcs[3]['status'] = false;\r
278                         $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');\r
279                 }\r
280                 if (!function_exists('mb_strlen')) {\r
281                         $ck_funcs[4]['status'] = false;\r
282                         $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');\r
283                 }\r
284                 if (!function_exists('iconv_strlen')) {\r
285                         $ck_funcs[6]['status'] = false;\r
286                         $ck_funcs[6]['help'] = L10n::t('Error: iconv PHP module required but not installed.');\r
287                 }\r
288                 if (!function_exists('posix_kill')) {\r
289                         $ck_funcs[7]['status'] = false;\r
290                         $ck_funcs[7]['help'] = L10n::t('Error: POSIX PHP module required but not installed.');\r
291                 }\r
292 \r
293                 $checks = array_merge($checks, $ck_funcs);\r
294 \r
295                 // check for XML DOM Documents being able to be generated\r
296                 try {\r
297                         $xml = new DOMDocument();\r
298                 } catch (Exception $e) {\r
299                         $ck_funcs[5]['status'] = false;\r
300                         $ck_funcs[5]['help'] = L10n::t('Error, XML PHP module required but not installed.');\r
301                 }\r
302         }\r
303 \r
304         /**\r
305          * "config/local.ini.php" - Check\r
306          *\r
307          * Checks if it's possible to create the "config/local.ini.php"\r
308          *\r
309          * @param array $checks The list of all checks (by-ref parameter!)\r
310          */\r
311         public static function checkLocalIni(&$checks)\r
312         {\r
313                 $status = true;\r
314                 $help = "";\r
315                 if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||\r
316                         (!file_exists('config/local.ini.php') && !is_writable('.'))) {\r
317 \r
318                         $status = false;\r
319                         $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;\r
320                         $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
321                         $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;\r
322                         $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;\r
323                 }\r
324 \r
325                 self::addCheck($checks, L10n::t('config/local.ini.php is writable'), $status, false, $help);\r
326 \r
327         }\r
328 \r
329         /**\r
330          * Smarty3 Template Check\r
331          *\r
332          * Checks, if the directory of Smarty3 is writable\r
333          *\r
334          * @param array $checks The list of all checks (by-ref parameter!)\r
335          */\r
336         public static function checkSmarty3(&$checks)\r
337         {\r
338                 $status = true;\r
339                 $help = "";\r
340                 if (!is_writable('view/smarty3')) {\r
341 \r
342                         $status = false;\r
343                         $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
344                         $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
345                         $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
346                         $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
347                 }\r
348 \r
349                 self::addCheck($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);\r
350         }\r
351 \r
352         /**\r
353          * ".htaccess" - Check\r
354          *\r
355          * Checks, if "url_rewrite" is enabled in the ".htaccess" file\r
356          *\r
357          * @param array $checks The list of all checks (by-ref parameter!)\r
358          */\r
359         public static function checkHtAccess(&$checks)\r
360         {\r
361                 $status = true;\r
362                 $help = "";\r
363                 $error_msg = "";\r
364                 if (function_exists('curl_init')) {\r
365                         $test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");\r
366 \r
367                         $url = normalise_link(System::baseUrl() . "/install/testrewrite");\r
368                         if ($test['body'] != "ok") {\r
369                                 $test = Network::fetchUrlFull($url);\r
370                         }\r
371 \r
372                         if ($test['body'] != "ok") {\r
373                                 $status = false;\r
374                                 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');\r
375                                 $error_msg = [];\r
376                                 $error_msg['head'] = L10n::t('Error message from Curl when fetching');\r
377                                 $error_msg['url'] = $test['redirect_url'];\r
378                                 $error_msg['msg'] = defaults($test, 'error', '');\r
379                         }\r
380                         self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);\r
381                 } else {\r
382                         // cannot check modrewrite if libcurl is not installed\r
383                         /// @TODO Maybe issue warning here?\r
384                 }\r
385         }\r
386 \r
387         /**\r
388          * Imagick Check\r
389          *\r
390          * Checks, if the imagick module is available\r
391          *\r
392          * @param array $checks The list of all checks (by-ref parameter!)\r
393          */\r
394         public static function checkImagick(&$checks)\r
395         {\r
396                 $imagick = false;\r
397                 $gif = false;\r
398 \r
399                 if (class_exists('Imagick')) {\r
400                         $imagick = true;\r
401                         $supported = Image::supportedTypes();\r
402                         if (array_key_exists('image/gif', $supported)) {\r
403                                 $gif = true;\r
404                         }\r
405                 }\r
406                 if ($imagick == false) {\r
407                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");\r
408                 } else {\r
409                         self::addCheck($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");\r
410                         if ($imagick) {\r
411                                 self::addCheck($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");\r
412                         }\r
413                 }\r
414         }\r
415 \r
416         /**\r
417          * Installs the Database structure\r
418          *\r
419          * @return string A possible error\r
420          */\r
421         public static function installDatabaseStructure()\r
422         {\r
423                 $errors = DBStructure::update(false, true, true);\r
424 \r
425                 return $errors;\r
426         }\r
427 }\r