]> git.mxchange.org Git - friendica.git/blob - src/Core/Install.php
Type hinting & call bugfixing
[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\Object\Image;
10 use Friendica\Util\Network;
11
12 /**
13  * Contains methods for installation purpose of Friendica
14  */
15 class Install
16 {
17         /**
18          * @var array the check outcomes
19          */
20         private $checks;
21
22         /**
23          * Returns all checks made
24          *
25          * @return array the checks
26          */
27         public function getChecks()
28         {
29                 return $this->checks;
30         }
31
32         /**
33          * Resets all checks
34          */
35         public function resetChecks()
36         {
37                 $this->checks = [];
38         }
39
40         /**
41          * Install constructor.
42          *
43          */
44         public function __construct()
45         {
46                 $this->checks = [];
47         }
48
49         /**
50          * Checks the current installation environment. There are optional and mandatory checks.
51          *
52          * @param string $basepath    The basepath of Friendica
53          * @param string $baseurl     The baseurl of Friendica
54          * @param string $phpath      Optional path to the PHP binary
55          *
56          * @return bool if the check succeed
57          */
58         public function checkAll($basepath, $baseurl, $phpath = null)
59         {
60                 $returnVal = true;
61
62                 if (isset($phpath)) {
63                         if (!$this->checkPHP($phpath)) {
64                                 $returnVal = false;
65                         }
66                 }
67
68                 if (!$this->checkFunctions()) {
69                         $returnVal = false;
70                 }
71
72                 if (!$this->checkImagick()) {
73                         $returnVal = false;
74                 }
75
76                 if (!$this->checkLocalIni()) {
77                         $returnVal = false;
78                 }
79
80                 if (!$this->checkSmarty3()) {
81                         $returnVal = false;
82                 }
83
84                 if (!$this->checkKeys()) {
85                         $returnVal = false;
86                 }
87
88                 if (!$this->checkHtAccess($basepath, $baseurl)) {
89                         $returnVal = false;
90                 }
91
92                 return $returnVal;
93         }
94
95         /**
96          * Executes the installation of Friendica in the given environment.
97          * - Creates `config/local.ini.php`
98          * - Installs Database Structure
99          *
100          * @param string        $urlpath        Path based on the URL of Friendica (e.g. '/friendica')
101          * @param string        $dbhost         Hostname/IP of the Friendica Database
102          * @param string        $dbuser         Username of the Database connection credentials
103          * @param string        $dbpass         Password of the Database connection credentials
104          * @param string        $dbdata         Name of the Database
105          * @param string        $timezone       Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
106          * @param string        $language       2-letter ISO 639-1 code (eg. 'en')
107          * @param string        $adminmail      Mail-Adress of the administrator
108          * @param string        $basepath   The basepath of Friendica
109          * @param string        $phpath         Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
110          *
111          * @return bool|string true if the config was created, the text if something went wrong
112          */
113         public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
114         {
115                 $tpl = get_markup_template('local.ini.tpl');
116                 $txt = replace_macros($tpl,[
117                         '$phpath' => $phppath,
118                         '$dbhost' => $dbhost,
119                         '$dbuser' => $dbuser,
120                         '$dbpass' => $dbpass,
121                         '$dbdata' => $dbdata,
122                         '$timezone' => $timezone,
123                         '$language' => $language,
124                         '$urlpath' => $urlpath,
125                         '$adminmail' => $adminmail,
126                 ]);
127
128                 $result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php', $txt);
129
130                 if (!$result) {
131                         return $txt;
132                 } else {
133                         return true;
134                 }
135         }
136
137         /**
138          * Adds new checks to the array $checks
139          *
140          * @param string $title The title of the current check
141          * @param bool $status 1 = check passed, 0 = check not passed
142          * @param bool $required 1 = check is mandatory, 0 = check is optional
143          * @param string $help A help-string for the current check
144          * @param string $error_msg Optional. A error message, if the current check failed
145          */
146         private function addCheck($title, $status, $required, $help, $error_msg = "")
147         {
148                 array_push($this->checks, [
149                         'title' => $title,
150                         'status' => $status,
151                         'required' => $required,
152                         'help' => $help,
153                         'error_msg' => $error_msg,
154                 ]);
155         }
156
157         /**
158          * PHP Check
159          *
160          * Checks the PHP environment.
161          *
162          * - Checks if a PHP binary is available
163          * - Checks if it is the CLI version
164          * - Checks if "register_argc_argv" is enabled
165          *
166          * @param string $phppath Optional. The Path to the PHP-Binary
167          * @param bool   $required Optional. If set to true, the PHP-Binary has to exist (Default false)
168          *
169          * @return bool false if something required failed
170          */
171         public function checkPHP($phppath = null, $required = false)
172         {
173                 $passed = $passed2 = $passed3 = false;
174                 if (isset($phppath)) {
175                         $passed = file_exists($phppath);
176                 } else {
177                         $phppath = trim(shell_exec('which php'));
178                         $passed = strlen($phppath);
179                 }
180
181                 $help = "";
182                 if (!$passed) {
183                         $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
184                         $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;
185                         $help .= EOL . EOL;
186                         $tpl = get_markup_template('field_input.tpl');
187                         $help .= replace_macros($tpl, [
188                                 '$field' => ['phpath', L10n::t('PHP executable path'), $phppath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
189                         ]);
190                         $phppath = "";
191                 }
192
193                 $this->addCheck(L10n::t('Command line PHP') . ($passed ? " (<tt>$phppath</tt>)" : ""), $passed, false, $help);
194
195                 if ($passed) {
196                         $cmd = "$phppath -v";
197                         $result = trim(shell_exec($cmd));
198                         $passed2 = (strpos($result, "(cli)") !== false);
199                         list($result) = explode("\n", $result);
200                         $help = "";
201                         if (!$passed2) {
202                                 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
203                                 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
204                         }
205                         $this->addCheck(L10n::t('PHP cli binary'), $passed2, true, $help);
206                 } else {
207                         // return if it was required
208                         return $required;
209                 }
210
211                 if ($passed2) {
212                         $str = autoname(8);
213                         $cmd = "$phppath testargs.php $str";
214                         $result = trim(shell_exec($cmd));
215                         $passed3 = $result == $str;
216                         $help = "";
217                         if (!$passed3) {
218                                 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
219                                 $help .= L10n::t('This is required for message delivery to work.');
220                         } else {
221                                 $this->phppath = $phppath;
222                         }
223
224                         $this->addCheck(L10n::t('PHP register_argc_argv'), $passed3, true, $help);
225                 }
226
227                 // passed2 & passed3 are required if first check passed
228                 return $passed2 && $passed3;
229         }
230
231         /**
232          * OpenSSL Check
233          *
234          * Checks the OpenSSL Environment
235          *
236          * - Checks, if the command "openssl_pkey_new" is available
237          *
238          * @return bool false if something required failed
239          */
240         public function checkKeys()
241         {
242                 $help = '';
243                 $res = false;
244                 $status = true;
245
246                 if (function_exists('openssl_pkey_new')) {
247                         $res = openssl_pkey_new([
248                                 'digest_alg' => 'sha1',
249                                 'private_key_bits' => 4096,
250                                 'encrypt_key' => false
251                         ]);
252                 }
253
254                 // Get private key
255                 if (!$res) {
256                         $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
257                         $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
258                         $status = false;
259                 }
260                 $this->addCheck(L10n::t('Generate encryption keys'), $res, true, $help);
261
262                 return $status;
263         }
264
265         /**
266          * PHP basic function check
267          *
268          * @param string $name The name of the function
269          * @param string $title The (localized) title of the function
270          * @param string $help The (localized) help of the function
271          * @param boolean $required If true, this check is required
272          *
273          * @return bool false, if the check failed
274          */
275         private function checkFunction($name, $title, $help, $required)
276         {
277                 $currHelp = '';
278                 $status = true;
279                 if (!function_exists($name)) {
280                         $currHelp = $help;
281                         $status = false;
282                 }
283                 $this->addCheck($title, $status, $required, $currHelp);
284
285                 return $status || (!$status && !$required);
286         }
287
288         /**
289          * PHP functions Check
290          *
291          * Checks the following PHP functions
292          * - libCurl
293          * - GD Graphics
294          * - OpenSSL
295          * - PDO or MySQLi
296          * - mb_string
297          * - XML
298          * - iconv
299          * - POSIX
300          *
301          * @return bool false if something required failed
302          */
303         public function checkFunctions()
304         {
305                 $returnVal = true;
306
307                 $help = '';
308                 $status = true;
309                 if (function_exists('apache_get_modules')) {
310                         if (!in_array('mod_rewrite', apache_get_modules())) {
311                                 $help = L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.');
312                                 $status = false;
313                                 $returnVal = false;
314                         }
315                 }
316                 $this->addCheck(L10n::t('Apache mod_rewrite module'), $status, true, $help);
317
318                 $help = '';
319                 $status = true;
320                 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
321                         $status = false;
322                         $help = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
323                         $returnVal = false;
324                 } else {
325                         if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
326                                 $status = false;
327                                 $help = L10n::t('Error: The MySQL driver for PDO is not installed.');
328                                 $returnVal = false;
329                         }
330                 }
331                 $this->addCheck(L10n::t('PDO or MySQLi PHP module'), $status, true, $help);
332
333                 // check for XML DOM Documents being able to be generated
334                 $help = '';
335                 $status = true;
336                 try {
337                         $xml = new DOMDocument();
338                 } catch (Exception $e) {
339                         $help = L10n::t('Error, XML PHP module required but not installed.');
340                         $status = false;
341                         $returnVal = false;
342                 }
343                 $this->addCheck(L10n::t('XML PHP module'), $status, true, $help);
344
345                 $status = $this->checkFunction('curl_init',
346                         L10n::t('libCurl PHP module'),
347                         L10n::t('Error: libCURL PHP module required but not installed.'),
348                         true
349                 );
350                 $returnVal = $returnVal ? $status : false;
351
352                 $status = $this->checkFunction('imagecreatefromjpeg',
353                         L10n::t('GD graphics PHP module'),
354                         L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
355                         true
356                 );
357                 $returnVal = $returnVal ? $status : false;
358
359                 $status = $this->checkFunction('openssl_public_encrypt',
360                         L10n::t('OpenSSL PHP module'),
361                         L10n::t('Error: openssl PHP module required but not installed.'),
362                         true
363                 );
364                 $returnVal = $returnVal ? $status : false;
365
366                 $status = $this->checkFunction('mb_strlen',
367                         L10n::t('mb_string PHP module'),
368                         L10n::t('Error: mb_string PHP module required but not installed.'),
369                         true
370                 );
371                 $returnVal = $returnVal ? $status : false;
372
373                 $status = $this->checkFunction('iconv_strlen',
374                         L10n::t('iconv PHP module'),
375                         L10n::t('Error: iconv PHP module required but not installed.'),
376                         true
377                 );
378                 $returnVal = $returnVal ? $status : false;
379
380                 $status = $this->checkFunction('posix_kill',
381                         L10n::t('POSIX PHP module'),
382                         L10n::t('Error: POSIX PHP module required but not installed.'),
383                         true
384                 );
385                 $returnVal = $returnVal ? $status : false;
386
387                 return $returnVal;
388         }
389
390         /**
391          * "config/local.ini.php" - Check
392          *
393          * Checks if it's possible to create the "config/local.ini.php"
394          *
395          * @return bool false if something required failed
396          */
397         public function checkLocalIni()
398         {
399                 $status = true;
400                 $help = "";
401                 if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||
402                         (!file_exists('config/local.ini.php') && !is_writable('.'))) {
403
404                         $status = false;
405                         $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;
406                         $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;
407                         $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;
408                         $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
409                 }
410
411                 $this->addCheck(L10n::t('config/local.ini.php is writable'), $status, false, $help);
412
413                 // Local INI File is not required
414                 return true;
415         }
416
417         /**
418          * Smarty3 Template Check
419          *
420          * Checks, if the directory of Smarty3 is writable
421          *
422          * @return bool false if something required failed
423          */
424         public function checkSmarty3()
425         {
426                 $status = true;
427                 $help = "";
428                 if (!is_writable('view/smarty3')) {
429
430                         $status = false;
431                         $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
432                         $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;
433                         $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;
434                         $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;
435                 }
436
437                 $this->addCheck(L10n::t('view/smarty3 is writable'), $status, true, $help);
438
439                 return $status;
440         }
441
442         /**
443          * ".htaccess" - Check
444          *
445          * Checks, if "url_rewrite" is enabled in the ".htaccess" file
446          *
447          * @param string $basepath   The basepath of the app
448          * @param string $baseurl    The baseurl of the app
449          * @return bool false if something required failed
450          */
451         public function checkHtAccess($basepath, $baseurl)
452         {
453                 $status = true;
454                 $help = "";
455                 $error_msg = "";
456                 if (function_exists('curl_init')) {
457                         $fetchResult = Network::fetchUrlFull($basepath . "/install/testrewrite");
458
459                         $url = normalise_link($baseurl . "/install/testrewrite");
460                         if ($fetchResult->getBody() != "ok") {
461                                 $fetchResult = Network::fetchUrlFull($url);
462                         }
463
464                         if ($fetchResult->getBody() != "ok") {
465                                 $status = false;
466                                 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
467                                 $error_msg = [];
468                                 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
469                                 $error_msg['url'] = $fetchResult->getRedirectUrl();
470                                 $error_msg['msg'] = $fetchResult->getError();
471                         }
472
473                         $this->addCheck(L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
474                 } else {
475                         // cannot check modrewrite if libcurl is not installed
476                         /// @TODO Maybe issue warning here?
477                 }
478
479                 return $status;
480         }
481
482         /**
483          * Imagick Check
484          *
485          * Checks, if the imagick module is available
486          *
487          * @return bool false if something required failed
488          */
489         public function checkImagick()
490         {
491                 $imagick = false;
492                 $gif = false;
493
494                 if (class_exists('Imagick')) {
495                         $imagick = true;
496                         $supported = Image::supportedTypes();
497                         if (array_key_exists('image/gif', $supported)) {
498                                 $gif = true;
499                         }
500                 }
501                 if (!$imagick) {
502                         $this->addCheck(L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
503                 } else {
504                         $this->addCheck(L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
505                         if ($imagick) {
506                                 $this->addCheck(L10n::t('ImageMagick supports GIF'), $gif, false, "");
507                         }
508                 }
509
510                 // Imagick is not required
511                 return true;
512         }
513 }