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