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