]> git.mxchange.org Git - friendica.git/blob - mod/install.php
Bump DB version
[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(App $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'] = App::get_baseurl()."/view/install/style.css";
19
20         global $install_wizard_pass;
21         if (x($_POST,'pass')) {
22                 $install_wizard_pass = intval($_POST['pass']);
23         }
24
25 }
26
27 function install_post(App $a) {
28         global $install_wizard_pass, $db;
29
30         switch($install_wizard_pass) {
31                 case 1:
32                 case 2:
33                         return;
34                         break; // just in case return don't return :)
35                 case 3:
36                         $urlpath = $a->get_path();
37                         $dbhost = notags(trim($_POST['dbhost']));
38                         $dbuser = notags(trim($_POST['dbuser']));
39                         $dbpass = notags(trim($_POST['dbpass']));
40                         $dbdata = notags(trim($_POST['dbdata']));
41                         $phpath = notags(trim($_POST['phpath']));
42
43                         require_once("include/dba.php");
44                         unset($db);
45                         $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
46                         /*if(get_db_errno()) {
47                                 unset($db);
48                                 $db = new dba($dbhost, $dbuser, $dbpass, '', true);
49
50                                 if(! get_db_errno()) {
51                                         $r = q("CREATE DATABASE '%s'",
52                                                         dbesc($dbdata)
53                                         );
54                                         if ($r) {
55                                                 unset($db);
56                                                 $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
57                                         } else {
58                                                 $a->data['db_create_failed']=true;
59                                         }
60                                 } else {
61                                         $a->data['db_conn_failed']=true;
62                                         return;
63                                 }
64                         }*/
65                         if (get_db_errno()) {
66                                 $a->data['db_conn_failed']=true;
67                         }
68
69                         return;
70                         break;
71                 case 4:
72                         $urlpath = $a->get_path();
73                         $dbhost = notags(trim($_POST['dbhost']));
74                         $dbuser = notags(trim($_POST['dbuser']));
75                         $dbpass = notags(trim($_POST['dbpass']));
76                         $dbdata = notags(trim($_POST['dbdata']));
77                         $phpath = notags(trim($_POST['phpath']));
78                         $timezone = notags(trim($_POST['timezone']));
79                         $language = notags(trim($_POST['language']));
80                         $adminmail = notags(trim($_POST['adminmail']));
81                         $rino = 2;
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                                 '$rino' => $rino
98                         ));
99
100
101                         $result = file_put_contents('.htconfig.php', $txt);
102                         if (! $result) {
103                                 $a->data['txt'] = $txt;
104                         }
105
106                         $errors = load_database($db);
107
108
109                         if ($errors) {
110                                 $a->data['db_failed'] = $errors;
111                         } else {
112                                 $a->data['db_installed'] = true;
113                         }
114
115                         return;
116                 break;
117         }
118 }
119
120 function get_db_errno() {
121         if (class_exists('mysqli')) {
122                 return mysqli_connect_errno();
123         } else {
124                 return mysql_errno();
125         }
126 }
127
128 function install_content(App $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 (dbm::is_result($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
206                         check_php($phpath, $checks);
207
208                         check_htaccess($checks);
209
210                         /// @TODO Maybe move this out?
211                         function check_passed($v, $c){
212                                 if ($c['required']) {
213                                         $v = $v && $c['status'];
214                                 }
215                                 return $v;
216                         }
217                         $checkspassed = array_reduce($checks, "check_passed", true);
218
219
220
221                         $tpl = get_markup_template('install_checks.tpl');
222                         $o .= replace_macros($tpl, array(
223                                 '$title' => $install_title,
224                                 '$pass' => t('System check'),
225                                 '$checks' => $checks,
226                                 '$passed' => $checkspassed,
227                                 '$see_install' => t('Please see the file "INSTALL.txt".'),
228                                 '$next' => t('Next'),
229                                 '$reload' => t('Check again'),
230                                 '$phpath' => $phpath,
231                                 '$baseurl' => App::get_baseurl(),
232                         ));
233                         return $o;
234                 }; break;
235
236                 case 2: { // Database config
237
238                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
239                         $dbuser = notags(trim($_POST['dbuser']));
240                         $dbpass = notags(trim($_POST['dbpass']));
241                         $dbdata = notags(trim($_POST['dbdata']));
242                         $phpath = notags(trim($_POST['phpath']));
243
244
245                         $tpl = get_markup_template('install_db.tpl');
246                         $o .= replace_macros($tpl, array(
247                                 '$title' => $install_title,
248                                 '$pass' => t('Database connection'),
249                                 '$info_01' => t('In order to install Friendica we need to know how to connect to your database.'),
250                                 '$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
251                                 '$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
252
253                                 '$status' => $wizard_status,
254
255                                 '$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, '', 'required'),
256                                 '$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, '', 'required', 'autofocus'),
257                                 '$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, t("For security reasons the password must not be empty"), 'required'),
258                                 '$dbdata' => array('dbdata', t('Database Name'), $dbdata, '', 'required'),
259                                 '$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'),
260
261
262
263                                 '$lbl_10' => t('Please select a default timezone for your website'),
264
265                                 '$baseurl' => App::get_baseurl(),
266
267                                 '$phpath' => $phpath,
268
269                                 '$submit' => t('Submit'),
270
271                         ));
272                         return $o;
273                 }; break;
274                 case 3: { // Site settings
275                         require_once('include/datetime.php');
276                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
277                         $dbuser = notags(trim($_POST['dbuser']));
278                         $dbpass = notags(trim($_POST['dbpass']));
279                         $dbdata = notags(trim($_POST['dbdata']));
280                         $phpath = notags(trim($_POST['phpath']));
281
282                         $adminmail = notags(trim($_POST['adminmail']));
283                         $timezone = ((x($_POST,'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
284                         /* Installed langs */
285                         $lang_choices = get_available_languages();
286
287                         $tpl = get_markup_template('install_settings.tpl');
288                         $o .= replace_macros($tpl, array(
289                                 '$title' => $install_title,
290                                 '$pass' => t('Site settings'),
291
292                                 '$status' => $wizard_status,
293
294                                 '$dbhost' => $dbhost,
295                                 '$dbuser' => $dbuser,
296                                 '$dbpass' => $dbpass,
297                                 '$dbdata' => $dbdata,
298                                 '$phpath' => $phpath,
299
300                                 '$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'),
301
302
303                                 '$timezone' => field_timezone('timezone', t('Please select a default timezone for your website'), $timezone, ''),
304                                 '$language' => array('language', t('System Language:'), 'en', t('Set the default language for your Friendica installation interface and to send emails.'), $lang_choices),
305                                 '$baseurl' => App::get_baseurl(),
306
307
308
309                                 '$submit' => t('Submit'),
310
311                         ));
312                         return $o;
313                 }; break;
314
315         }
316 }
317
318 /**
319  * checks   : array passed to template
320  * title    : string
321  * status   : boolean
322  * required : boolean
323  * help         : string optional
324  */
325 function check_add(&$checks, $title, $status, $required, $help){
326         $checks[] = array(
327                 'title' => $title,
328                 'status' => $status,
329                 'required' => $required,
330                 'help'  => $help,
331         );
332 }
333
334 function check_php(&$phpath, &$checks) {
335         $passed = $passed2 = $passed3 = false;
336         if (strlen($phpath)){
337                 $passed = file_exists($phpath);
338         } else {
339                 $phpath = trim(shell_exec('which php'));
340                 $passed = strlen($phpath);
341         }
342         $help = "";
343         if (!$passed) {
344                 $help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
345                 $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 ;
346                 $help .= EOL . EOL ;
347                 $tpl = get_markup_template('field_input.tpl');
348                 $help .= replace_macros($tpl, array(
349                         '$field' => array('phpath', t('PHP executable path'), $phpath, t('Enter full path to php executable. You can leave this blank to continue the installation.')),
350                 ));
351                 $phpath="";
352         }
353
354         check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
355
356         if($passed) {
357                 $cmd = "$phpath -v";
358                 $result = trim(shell_exec($cmd));
359                 $passed2 = ( strpos($result, "(cli)") !== false );
360                 list($result) = explode("\n", $result);
361                 $help = "";
362                 if(!$passed2) {
363                         $help .= t('PHP executable is not the php cli binary (could be cgi-fgci version)'). EOL;
364                         $help .= t('Found PHP version: ')."<tt>$result</tt>";
365                 }
366                 check_add($checks, t('PHP cli binary'), $passed2, true, $help);
367         }
368
369
370         if ($passed2) {
371                 $str = autoname(8);
372                 $cmd = "$phpath testargs.php $str";
373                 $result = trim(shell_exec($cmd));
374                 $passed3 = $result == $str;
375                 $help = "";
376                 if(!$passed3) {
377                         $help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
378                         $help .= t('This is required for message delivery to work.');
379                 }
380                 check_add($checks, t('PHP register_argc_argv'), $passed3, true, $help);
381         }
382
383
384 }
385
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         }
399
400         // Get private key
401
402         if (! $res) {
403                 $help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
404                 $help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
405         }
406         check_add($checks, t('Generate encryption keys'), $res, true, $help);
407
408 }
409
410
411 function check_funcs(&$checks) {
412         $ck_funcs = array();
413         check_add($ck_funcs, t('libCurl PHP module'), true, true, "");
414         check_add($ck_funcs, t('GD graphics PHP module'), true, true, "");
415         check_add($ck_funcs, t('OpenSSL PHP module'), true, true, "");
416         check_add($ck_funcs, t('mysqli PHP module'), true, true, "");
417         check_add($ck_funcs, t('mb_string PHP module'), true, true, "");
418         check_add($ck_funcs, t('XML PHP module'), true, true, "");
419         check_add($ck_funcs, t('iconv module'), true, true, "");
420
421         if (function_exists('apache_get_modules')){
422                 if (! in_array('mod_rewrite',apache_get_modules())) {
423                         check_add($ck_funcs, t('Apache mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
424                 } else {
425                         check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
426                 }
427         }
428
429         if (! function_exists('curl_init')){
430                 $ck_funcs[0]['status']= false;
431                 $ck_funcs[0]['help']= t('Error: libCURL PHP module required but not installed.');
432         }
433         if (! function_exists('imagecreatefromjpeg')){
434                 $ck_funcs[1]['status']= false;
435                 $ck_funcs[1]['help']= t('Error: GD graphics PHP module with JPEG support required but not installed.');
436         }
437         if (! function_exists('openssl_public_encrypt')) {
438                 $ck_funcs[2]['status']= false;
439                 $ck_funcs[2]['help']= t('Error: openssl PHP module required but not installed.');
440         }
441         if (! function_exists('mysqli_connect')){
442                 $ck_funcs[3]['status']= false;
443                 $ck_funcs[3]['help']= t('Error: mysqli PHP module required but not installed.');
444         }
445         if (! function_exists('mb_strlen')){
446                 $ck_funcs[4]['status']= false;
447                 $ck_funcs[4]['help']= t('Error: mb_string PHP module required but not installed.');
448         }
449         if (! function_exists('iconv_strlen')){
450                 $ck_funcs[7]['status']= false;
451                 $ck_funcs[7]['help']= t('Error: iconv PHP module required but not installed.');
452         }
453
454         $checks = array_merge($checks, $ck_funcs);
455
456         // check for XML DOM Documents being able to be generated
457         try {
458                 $xml = new DOMDocument();
459         } catch (Exception $e) {
460                 $ck_funcs[6]['status'] = false;
461                 $ck_funcs[6]['help'] = t('Error, XML PHP module required but not installed.');
462         }
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         $status = true;
501         $help = "";
502         if (function_exists('curl_init')) {
503                 $test = fetch_url(App::get_baseurl()."/install/testrewrite");
504
505                 if ($test!="ok") {
506                         $test = fetch_url(normalise_link(App::get_baseurl()."/install/testrewrite"));
507                 }
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                 /// @TODO Maybe issue warning here?
517         }
518 }
519
520 function check_imagik(&$checks) {
521         $imagick = false;
522         $gif = false;
523
524         if (class_exists('Imagick')) {
525                 $imagick = true;
526                 $supported = Photo::supportedTypes();
527                 if (array_key_exists('image/gif', $supported)) {
528                         $gif = true;
529                 }
530         }
531         if ($imagick == false) {
532                 check_add($checks, t('ImageMagick PHP extension is not installed'), $imagick, false, "");
533         } else {
534                 check_add($checks, t('ImageMagick PHP extension is installed'), $imagick, false, "");
535                 if ($imagick) {
536                         check_add($checks, t('ImageMagick supports GIF'), $gif, false, "");
537                 }
538         }
539 }
540
541 function manual_config(App $a) {
542         $data = htmlentities($a->data['txt'],ENT_COMPAT,'UTF-8');
543         $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.');
544         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
545         return $o;
546 }
547
548 function load_database_rem($v, $i){
549         $l = trim($i);
550         if (strlen($l)>1 && ($l[0]=="-" || ($l[0]=="/" && $l[1]=="*"))){
551                 return $v;
552         } else  {
553                 return $v."\n".$i;
554         }
555 }
556
557 function load_database($db) {
558
559         require_once("include/dbstructure.php");
560         $errors = update_structure(false, true);
561
562 /*      $str = file_get_contents('database.sql');
563         $arr = explode(';',$str);
564         $errors = false;
565         foreach($arr as $a) {
566                 if(strlen(trim($a))) {
567                         $r = @$db->q(trim($a));
568                         if(false === $r) {
569                                 $errors .=  t('Errors encountered creating database tables.') . $a . EOL;
570                         }
571                 }
572         }*/
573
574         return $errors;
575 }
576
577 function what_next() {
578         $baseurl = App::get_baseurl();
579         return
580                 t('<h1>What next</h1>')
581                 ."<p>".t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.')
582                 .t('Please see the file "INSTALL.txt".')
583                 ."</p><p>"
584                 .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.")
585                 ."</p>";
586 }
587
588