4 use Friendica\Core\System;
6 require_once "include/Photo.php";
8 $install_wizard_pass = 1;
10 function install_init(App $a) {
12 // $baseurl/install/testrwrite to test if rewite in .htaccess is working
13 if ($a->argc == 2 && $a->argv[1] == "testrewrite") {
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";
23 global $install_wizard_pass;
24 if (x($_POST, 'pass')) {
25 $install_wizard_pass = intval($_POST['pass']);
30 function install_post(App $a) {
31 global $install_wizard_pass;
33 switch($install_wizard_pass) {
37 break; // just in case return don't return :)
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']));
46 require_once("include/dba.php");
47 if (!dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true)) {
48 $a->data['db_conn_failed'] = true;
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']));
66 dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true);
68 $tpl = get_markup_template('htconfig.tpl');
69 $txt = replace_macros($tpl,array(
74 '$timezone' => $timezone,
75 '$language' => $language,
76 '$urlpath' => $urlpath,
78 '$adminmail' => $adminmail,
83 $result = file_put_contents('.htconfig.php', $txt);
85 $a->data['txt'] = $txt;
88 $errors = load_database();
92 $a->data['db_failed'] = $errors;
94 $a->data['db_installed'] = true;
102 function install_content(App $a) {
104 global $install_wizard_pass;
107 $install_title = t('Friendica Communications Server - Setup');
111 if (x($a->data, 'db_conn_failed')) {
112 $install_wizard_pass = 2;
113 $wizard_status = t('Could not connect to database.');
115 if (x($a->data, 'db_create_failed')) {
116 $install_wizard_pass = 2;
117 $wizard_status = t('Could not create table.');
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;
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;
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,
141 '$status' => t('Database already in use.'),
147 if (x($a->data, 'txt') && strlen($a->data['txt'])) {
148 $db_return_text .= manual_config($a);
151 if ($db_return_text != "") {
152 $tpl = get_markup_template('install.tpl');
153 return replace_macros($tpl, array(
154 '$title' => $install_title,
156 '$text' => $db_return_text . what_next(),
160 switch ($install_wizard_pass) {
161 case 1: { // System check
166 check_funcs($checks);
168 check_imagik($checks);
170 check_htconfig($checks);
172 check_smarty3($checks);
176 if (x($_POST, 'phpath')) {
177 $phpath = notags(trim($_POST['phpath']));
180 check_php($phpath, $checks);
182 check_htaccess($checks);
184 /// @TODO Maybe move this out?
185 function check_passed($v, $c) {
186 if ($c['required']) {
187 $v = $v && $c['status'];
191 $checkspassed = array_reduce($checks, "check_passed", true);
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(),
210 case 2: { // Database config
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']));
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.'),
227 '$status' => $wizard_status,
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'),
237 '$lbl_10' => t('Please select a default timezone for your website'),
239 '$baseurl' => System::baseUrl(),
241 '$phpath' => $phpath,
243 '$submit' => t('Submit'),
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']));
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();
261 $tpl = get_markup_template('install_settings.tpl');
262 $o .= replace_macros($tpl, array(
263 '$title' => $install_title,
264 '$pass' => t('Site settings'),
266 '$status' => $wizard_status,
268 '$dbhost' => $dbhost,
269 '$dbuser' => $dbuser,
270 '$dbpass' => $dbpass,
271 '$dbdata' => $dbdata,
272 '$phpath' => $phpath,
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'),
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(),
283 '$submit' => t('Submit'),
293 * checks : array passed to template
297 * help : string optional
299 function check_add(&$checks, $title, $status, $required, $help) {
303 'required' => $required,
308 function check_php(&$phpath, &$checks) {
309 $passed = $passed2 = $passed3 = false;
310 if (strlen($phpath)) {
311 $passed = file_exists($phpath);
313 $phpath = trim(shell_exec('which php'));
314 $passed = strlen($phpath);
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;
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.')),
328 check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
332 $result = trim(shell_exec($cmd));
333 $passed2 = ( strpos($result, "(cli)") !== false);
334 list($result) = explode("\n", $result);
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>";
340 check_add($checks, t('PHP cli binary'), $passed2, true, $help);
346 $cmd = "$phpath testargs.php $str";
347 $result = trim(shell_exec($cmd));
348 $passed3 = $result == $str;
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.');
354 check_add($checks, t('PHP register_argc_argv'), $passed3, true, $help);
360 function check_keys(&$checks) {
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
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".');
380 check_add($checks, t('Generate encryption keys'), $res, true, $help);
385 function check_funcs(&$checks) {
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, "");
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.'));
399 check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
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.');
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.');
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.');
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.');
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.');
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.');
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.');
432 $checks = array_merge($checks, $ck_funcs);
434 // check for XML DOM Documents being able to be generated
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.');
444 function check_htconfig(&$checks) {
447 if ((file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
448 (!file_exists('.htconfig.php') && !is_writable('.'))) {
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;
457 check_add($checks, t('.htconfig.php is writable'), $status, false, $help);
461 function check_smarty3(&$checks) {
464 if (!is_writable('view/smarty3')) {
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;
473 check_add($checks, t('view/smarty3 is writable'), $status, true, $help);
477 function check_htaccess(&$checks) {
480 if (function_exists('curl_init')) {
481 $test = fetch_url(System::baseUrl()."/install/testrewrite");
484 $test = fetch_url(normalise_link(System::baseUrl()."/install/testrewrite"));
489 $help = t('Url rewrite in .htaccess is not working. Check your server configuration.');
491 check_add($checks, t('Url rewrite is working'), $status, true, $help);
493 // cannot check modrewrite if libcurl is not installed
494 /// @TODO Maybe issue warning here?
498 function check_imagik(&$checks) {
502 if (class_exists('Imagick')) {
504 $supported = Photo::supportedTypes();
505 if (array_key_exists('image/gif', $supported)) {
509 if ($imagick == false) {
510 check_add($checks, t('ImageMagick PHP extension is not installed'), $imagick, false, "");
512 check_add($checks, t('ImageMagick PHP extension is installed'), $imagick, false, "");
514 check_add($checks, t('ImageMagick supports GIF'), $gif, false, "");
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>";
526 function load_database_rem($v, $i) {
528 if (strlen($l)>1 && ($l[0] == "-" || ($l[0] == "/" && $l[1] == "*"))) {
535 function load_database() {
537 require_once("include/dbstructure.php");
538 $errors = update_structure(false, true);
543 function what_next() {
544 $baseurl = System::baseUrl();
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".')
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.")