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