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