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