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