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