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