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