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