]> git.mxchange.org Git - friendica.git/blob - mod/install.php
Remove unused global cache constants
[friendica.git] / mod / install.php
1 <?php
2 /**
3  * @file mod/install.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Database\DBStructure;
11 use Friendica\Object\Image;
12 use Friendica\Util\Network;
13 use Friendica\Util\Temporal;
14
15 $install_wizard_pass = 1;
16
17 function install_init(App $a) {
18
19         // $baseurl/install/testrwrite to test if rewite in .htaccess is working
20         if ($a->argc == 2 && $a->argv[1] == "testrewrite") {
21                 echo "ok";
22                 killme();
23         }
24
25         // We overwrite current theme css, because during install we could not have a working mod_rewrite
26         // so we could not have a css at all. Here we set a static css file for the install procedure pages
27         $a->config['system']['theme'] = "../install";
28         $a->theme['stylesheet'] = System::baseUrl()."/view/install/style.css";
29
30         global $install_wizard_pass;
31         if (x($_POST, 'pass')) {
32                 $install_wizard_pass = intval($_POST['pass']);
33         }
34
35 }
36
37 function install_post(App $a) {
38         global $install_wizard_pass;
39
40         switch($install_wizard_pass) {
41                 case 1:
42                 case 2:
43                         return;
44                         break; // just in case return don't return :)
45                 case 3:
46                         $urlpath = $a->get_path();
47                         $dbhost = notags(trim($_POST['dbhost']));
48                         $dbuser = notags(trim($_POST['dbuser']));
49                         $dbpass = notags(trim($_POST['dbpass']));
50                         $dbdata = notags(trim($_POST['dbdata']));
51                         $phpath = notags(trim($_POST['phpath']));
52
53                         require_once("include/dba.php");
54                         if (!dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true)) {
55                                 $a->data['db_conn_failed'] = true;
56                         }
57
58                         return;
59                         break;
60                 case 4:
61                         $urlpath = $a->get_path();
62                         $dbhost = notags(trim($_POST['dbhost']));
63                         $dbuser = notags(trim($_POST['dbuser']));
64                         $dbpass = notags(trim($_POST['dbpass']));
65                         $dbdata = notags(trim($_POST['dbdata']));
66                         $phpath = notags(trim($_POST['phpath']));
67                         $timezone = notags(trim($_POST['timezone']));
68                         $language = notags(trim($_POST['language']));
69                         $adminmail = notags(trim($_POST['adminmail']));
70                         $rino = 1;
71
72                         // connect to db
73                         dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true);
74
75                         $tpl = get_markup_template('htconfig.tpl');
76                         $txt = replace_macros($tpl,[
77                                 '$dbhost' => $dbhost,
78                                 '$dbuser' => $dbuser,
79                                 '$dbpass' => $dbpass,
80                                 '$dbdata' => $dbdata,
81                                 '$timezone' => $timezone,
82                                 '$language' => $language,
83                                 '$urlpath' => $urlpath,
84                                 '$phpath' => $phpath,
85                                 '$adminmail' => $adminmail,
86                                 '$rino' => $rino
87                         ]);
88
89
90                         $result = file_put_contents('.htconfig.php', $txt);
91                         if (! $result) {
92                                 $a->data['txt'] = $txt;
93                         }
94
95                         $errors = load_database();
96
97
98                         if ($errors) {
99                                 $a->data['db_failed'] = $errors;
100                         } else {
101                                 $a->data['db_installed'] = true;
102                         }
103
104                         return;
105                 break;
106         }
107 }
108
109 function install_content(App $a) {
110
111         global $install_wizard_pass;
112         $o = '';
113         $wizard_status = "";
114         $install_title = L10n::t('Friendica Communications Server - Setup');
115
116
117
118         if (x($a->data, 'db_conn_failed')) {
119                 $install_wizard_pass = 2;
120                 $wizard_status = L10n::t('Could not connect to database.');
121         }
122         if (x($a->data, 'db_create_failed')) {
123                 $install_wizard_pass = 2;
124                 $wizard_status = L10n::t('Could not create table.');
125         }
126
127         $db_return_text = "";
128         if (x($a->data, 'db_installed')) {
129                 $txt = '<p style="font-size: 130%;">';
130                 $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
131                 $db_return_text .= $txt;
132         }
133
134         if (x($a->data, 'db_failed')) {
135                 $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
136                 $txt .= L10n::t('Please see the file "INSTALL.txt".') . EOL ."<hr>";
137                 $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL;
138                 $db_return_text .= $txt;
139         }
140
141         if (dba::$connected) {
142                 $r = q("SELECT COUNT(*) as `total` FROM `user`");
143                 if (DBM::is_result($r) && $r[0]['total']) {
144                         $tpl = get_markup_template('install.tpl');
145                         return replace_macros($tpl, [
146                                 '$title' => $install_title,
147                                 '$pass' => '',
148                                 '$status' => L10n::t('Database already in use.'),
149                                 '$text' => '',
150                         ]);
151                 }
152         }
153
154         if (x($a->data, 'txt') && strlen($a->data['txt'])) {
155                 $db_return_text .= manual_config($a);
156         }
157
158         if ($db_return_text != "") {
159                 $tpl = get_markup_template('install.tpl');
160                 return replace_macros($tpl, [
161                         '$title' => $install_title,
162                         '$pass' => "",
163                         '$text' => $db_return_text . what_next(),
164                 ]);
165         }
166
167         switch ($install_wizard_pass) {
168                 case 1: { // System check
169
170
171                         $checks = [];
172
173                         check_funcs($checks);
174
175                         check_imagik($checks);
176
177                         check_htconfig($checks);
178
179                         check_smarty3($checks);
180
181                         check_keys($checks);
182
183                         if (x($_POST, 'phpath')) {
184                                 $phpath = notags(trim($_POST['phpath']));
185                         }
186
187                         check_php($phpath, $checks);
188
189                         check_htaccess($checks);
190
191                         /// @TODO Maybe move this out?
192                         function check_passed($v, $c) {
193                                 if ($c['required']) {
194                                         $v = $v && $c['status'];
195                                 }
196                                 return $v;
197                         }
198                         $checkspassed = array_reduce($checks, "check_passed", true);
199
200
201
202                         $tpl = get_markup_template('install_checks.tpl');
203                         $o .= replace_macros($tpl, [
204                                 '$title' => $install_title,
205                                 '$pass' => L10n::t('System check'),
206                                 '$checks' => $checks,
207                                 '$passed' => $checkspassed,
208                                 '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
209                                 '$next' => L10n::t('Next'),
210                                 '$reload' => L10n::t('Check again'),
211                                 '$phpath' => $phpath,
212                                 '$baseurl' => System::baseUrl(),
213                         ]);
214                         return $o;
215                 }; break;
216
217                 case 2: { // Database config
218
219                         $dbhost = ((x($_POST, 'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
220                         $dbuser = notags(trim($_POST['dbuser']));
221                         $dbpass = notags(trim($_POST['dbpass']));
222                         $dbdata = notags(trim($_POST['dbdata']));
223                         $phpath = notags(trim($_POST['phpath']));
224
225                         $adminmail = notags(trim($_POST['adminmail']));
226
227                         $tpl = get_markup_template('install_db.tpl');
228                         $o .= replace_macros($tpl, [
229                                 '$title' => $install_title,
230                                 '$pass' => L10n::t('Database connection'),
231                                 '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
232                                 '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
233                                 '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
234
235                                 '$status' => $wizard_status,
236
237                                 '$dbhost' => ['dbhost', L10n::t('Database Server Name'), $dbhost, '', 'required'],
238                                 '$dbuser' => ['dbuser', L10n::t('Database Login Name'), $dbuser, '', 'required', 'autofocus'],
239                                 '$dbpass' => ['dbpass', L10n::t('Database Login Password'), $dbpass, L10n::t("For security reasons the password must not be empty"), 'required'],
240                                 '$dbdata' => ['dbdata', L10n::t('Database Name'), $dbdata, '', 'required'],
241                                 '$adminmail' => ['adminmail', L10n::t('Site administrator email address'), $adminmail, L10n::t('Your account email address must match this in order to use the web admin panel.'), 'required', 'autofocus', 'email'],
242
243
244
245                                 '$lbl_10' => L10n::t('Please select a default timezone for your website'),
246
247                                 '$baseurl' => System::baseUrl(),
248
249                                 '$phpath' => $phpath,
250
251                                 '$submit' => L10n::t('Submit'),
252
253                         ]);
254                         return $o;
255                 }; break;
256                 case 3: { // Site settings
257                         $dbhost = ((x($_POST, 'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
258                         $dbuser = notags(trim($_POST['dbuser']));
259                         $dbpass = notags(trim($_POST['dbpass']));
260                         $dbdata = notags(trim($_POST['dbdata']));
261                         $phpath = notags(trim($_POST['phpath']));
262
263                         $adminmail = notags(trim($_POST['adminmail']));
264                         $timezone = ((x($_POST, 'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
265                         /* Installed langs */
266                         $lang_choices = L10n::getAvailableLanguages();
267
268                         $tpl = get_markup_template('install_settings.tpl');
269                         $o .= replace_macros($tpl, [
270                                 '$title' => $install_title,
271                                 '$pass' => L10n::t('Site settings'),
272
273                                 '$status' => $wizard_status,
274
275                                 '$dbhost' => $dbhost,
276                                 '$dbuser' => $dbuser,
277                                 '$dbpass' => $dbpass,
278                                 '$dbdata' => $dbdata,
279                                 '$phpath' => $phpath,
280
281                                 '$adminmail' => ['adminmail', L10n::t('Site administrator email address'), $adminmail, L10n::t('Your account email address must match this in order to use the web admin panel.'), 'required', 'autofocus', 'email'],
282
283
284                                 '$timezone' => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
285                                 '$language' => ['language', L10n::t('System Language:'), 'en', L10n::t('Set the default language for your Friendica installation interface and to send emails.'), $lang_choices],
286                                 '$baseurl' => System::baseUrl(),
287
288
289
290                                 '$submit' => L10n::t('Submit'),
291
292                         ]);
293                         return $o;
294                 }; break;
295
296         }
297 }
298
299 /**
300  * checks   : array passed to template
301  * title    : string
302  * status   : boolean
303  * required : boolean
304  * help         : string optional
305  */
306 function check_add(&$checks, $title, $status, $required, $help) {
307         $checks[] = [
308                 'title' => $title,
309                 'status' => $status,
310                 'required' => $required,
311                 'help'  => $help,
312         ];
313 }
314
315 function check_php(&$phpath, &$checks) {
316         $passed = $passed2 = $passed3 = false;
317         if (strlen($phpath)) {
318                 $passed = file_exists($phpath);
319         } else {
320                 $phpath = trim(shell_exec('which php'));
321                 $passed = strlen($phpath);
322         }
323         $help = "";
324         if (!$passed) {
325                 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.'). EOL;
326                 $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;
327                 $help .= EOL . EOL;
328                 $tpl = get_markup_template('field_input.tpl');
329                 $help .= replace_macros($tpl, [
330                         '$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.')],
331                 ]);
332                 $phpath = "";
333         }
334
335         check_add($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
336
337         if ($passed) {
338                 $cmd = "$phpath -v";
339                 $result = trim(shell_exec($cmd));
340                 $passed2 = ( strpos($result, "(cli)") !== false);
341                 list($result) = explode("\n", $result);
342                 $help = "";
343                 if (!$passed2) {
344                         $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29"). EOL;
345                         $help .= L10n::t('Found PHP version: ')."<tt>$result</tt>";
346                 }
347                 check_add($checks, L10n::t('PHP cli binary'), $passed2, true, $help);
348         }
349
350
351         if ($passed2) {
352                 $str = autoname(8);
353                 $cmd = "$phpath testargs.php $str";
354                 $result = trim(shell_exec($cmd));
355                 $passed3 = $result == $str;
356                 $help = "";
357                 if (!$passed3) {
358                         $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
359                         $help .= L10n::t('This is required for message delivery to work.');
360                 }
361                 check_add($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);
362         }
363
364
365 }
366
367 function check_keys(&$checks) {
368
369         $help = '';
370
371         $res = false;
372
373         if (function_exists('openssl_pkey_new')) {
374                 $res = openssl_pkey_new([
375                         'digest_alg'       => 'sha1',
376                         'private_key_bits' => 4096,
377                         'encrypt_key'      => false
378                 ]);
379         }
380
381         // Get private key
382
383         if (! $res) {
384                 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
385                 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
386         }
387         check_add($checks, L10n::t('Generate encryption keys'), $res, true, $help);
388
389 }
390
391
392 function check_funcs(&$checks) {
393         $ck_funcs = [];
394         check_add($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");
395         check_add($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");
396         check_add($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");
397         check_add($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");
398         check_add($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");
399         check_add($ck_funcs, L10n::t('XML PHP module'), true, true, "");
400         check_add($ck_funcs, L10n::t('iconv module'), true, true, "");
401
402         if (function_exists('apache_get_modules')) {
403                 if (! in_array('mod_rewrite',apache_get_modules())) {
404                         check_add($ck_funcs, L10n::t('Apache mod_rewrite module'), false, true, L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.'));
405                 } else {
406                         check_add($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");
407                 }
408         }
409
410         if (! function_exists('curl_init')) {
411                 $ck_funcs[0]['status'] = false;
412                 $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');
413         }
414         if (! function_exists('imagecreatefromjpeg')) {
415                 $ck_funcs[1]['status'] = false;
416                 $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');
417         }
418         if (! function_exists('openssl_public_encrypt')) {
419                 $ck_funcs[2]['status'] = false;
420                 $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');
421         }
422         if (! function_exists('mysqli_connect') && !class_exists('pdo')) {
423                 $ck_funcs[3]['status'] = false;
424                 $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
425         }
426         if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', PDO::getAvailableDrivers())) {
427                 $ck_funcs[3]['status'] = false;
428                 $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');
429         }
430         if (! function_exists('mb_strlen')) {
431                 $ck_funcs[4]['status'] = false;
432                 $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');
433         }
434         if (! function_exists('iconv_strlen')) {
435                 $ck_funcs[7]['status'] = false;
436                 $ck_funcs[7]['help'] = L10n::t('Error: iconv PHP module required but not installed.');
437         }
438
439         $checks = array_merge($checks, $ck_funcs);
440
441         // check for XML DOM Documents being able to be generated
442         try {
443                 $xml = new DOMDocument();
444         } catch (Exception $e) {
445                 $ck_funcs[6]['status'] = false;
446                 $ck_funcs[6]['help'] = L10n::t('Error, XML PHP module required but not installed.');
447         }
448 }
449
450
451 function check_htconfig(&$checks) {
452         $status = true;
453         $help = "";
454         if ((file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
455                 (!file_exists('.htconfig.php') && !is_writable('.'))) {
456
457                 $status = false;
458                 $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;
459                 $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;
460                 $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;
461                 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.').EOL;
462         }
463
464         check_add($checks, L10n::t('.htconfig.php is writable'), $status, false, $help);
465
466 }
467
468 function check_smarty3(&$checks) {
469         $status = true;
470         $help = "";
471         if (!is_writable('view/smarty3')) {
472
473                 $status = false;
474                 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') .EOL;
475                 $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;
476                 $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;
477                 $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;
478         }
479
480         check_add($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);
481
482 }
483
484 function check_htaccess(&$checks) {
485         $status = true;
486         $help = "";
487         if (function_exists('curl_init')) {
488                 $test = Network::fetchUrl(System::baseUrl()."/install/testrewrite");
489
490                 if ($test != "ok") {
491                         $test = Network::fetchUrl(normalise_link(System::baseUrl()."/install/testrewrite"));
492                 }
493
494                 if ($test != "ok") {
495                         $status = false;
496                         $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
497                 }
498                 check_add($checks, L10n::t('Url rewrite is working'), $status, true, $help);
499         } else {
500                 // cannot check modrewrite if libcurl is not installed
501                 /// @TODO Maybe issue warning here?
502         }
503 }
504
505 function check_imagik(&$checks) {
506         $imagick = false;
507         $gif = false;
508
509         if (class_exists('Imagick')) {
510                 $imagick = true;
511                 $supported = Image::supportedTypes();
512                 if (array_key_exists('image/gif', $supported)) {
513                         $gif = true;
514                 }
515         }
516         if ($imagick == false) {
517                 check_add($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
518         } else {
519                 check_add($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
520                 if ($imagick) {
521                         check_add($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");
522                 }
523         }
524 }
525
526 function manual_config(App $a) {
527         $data = htmlentities($a->data['txt'],ENT_COMPAT, 'UTF-8');
528         $o = L10n::t('The database configuration file ".htconfig.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.');
529         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
530         return $o;
531 }
532
533 function load_database_rem($v, $i) {
534         $l = trim($i);
535         if (strlen($l)>1 && ($l[0] == "-" || ($l[0] == "/" && $l[1] == "*"))) {
536                 return $v;
537         } else  {
538                 return $v."\n".$i;
539         }
540 }
541
542 function load_database() {
543         $errors = DBStructure::update(false, true);
544
545         return $errors;
546 }
547
548 function what_next() {
549         $baseurl = System::baseUrl();
550         return
551                 L10n::t('<h1>What next</h1>')
552                 ."<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
553                 .L10n::t('Please see the file "INSTALL.txt".')
554                 ."</p><p>"
555                 .L10n::t('Go to your new Friendica node <a href="%s/register">registration page</a> and register as new user. Remember to use the same email you have entered as administrator email. This will allow you to enter the site admin panel.', $baseurl)
556                 ."</p>";
557 }