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