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