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