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