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