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