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