3 * @file mod/install.php
6 use Friendica\Core\System;
7 use Friendica\Database\DBM;
8 use Friendica\Object\Image;
10 $install_wizard_pass = 1;
12 function install_init(App $a) {
14 // $baseurl/install/testrwrite to test if rewite in .htaccess is working
15 if ($a->argc == 2 && $a->argv[1] == "testrewrite") {
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";
25 global $install_wizard_pass;
26 if (x($_POST, 'pass')) {
27 $install_wizard_pass = intval($_POST['pass']);
32 function install_post(App $a) {
33 global $install_wizard_pass;
35 switch($install_wizard_pass) {
39 break; // just in case return don't return :)
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']));
48 require_once("include/dba.php");
49 if (!dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true)) {
50 $a->data['db_conn_failed'] = true;
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']));
68 dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true);
70 $tpl = get_markup_template('htconfig.tpl');
71 $txt = replace_macros($tpl,array(
76 '$timezone' => $timezone,
77 '$language' => $language,
78 '$urlpath' => $urlpath,
80 '$adminmail' => $adminmail,
85 $result = file_put_contents('.htconfig.php', $txt);
87 $a->data['txt'] = $txt;
90 $errors = load_database();
94 $a->data['db_failed'] = $errors;
96 $a->data['db_installed'] = true;
104 function install_content(App $a) {
106 global $install_wizard_pass;
109 $install_title = t('Friendica Communications Server - Setup');
113 if (x($a->data, 'db_conn_failed')) {
114 $install_wizard_pass = 2;
115 $wizard_status = t('Could not connect to database.');
117 if (x($a->data, 'db_create_failed')) {
118 $install_wizard_pass = 2;
119 $wizard_status = t('Could not create table.');
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;
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;
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,
143 '$status' => t('Database already in use.'),
149 if (x($a->data, 'txt') && strlen($a->data['txt'])) {
150 $db_return_text .= manual_config($a);
153 if ($db_return_text != "") {
154 $tpl = get_markup_template('install.tpl');
155 return replace_macros($tpl, array(
156 '$title' => $install_title,
158 '$text' => $db_return_text . what_next(),
162 switch ($install_wizard_pass) {
163 case 1: { // System check
168 check_funcs($checks);
170 check_imagik($checks);
172 check_htconfig($checks);
174 check_smarty3($checks);
178 if (x($_POST, 'phpath')) {
179 $phpath = notags(trim($_POST['phpath']));
182 check_php($phpath, $checks);
184 check_htaccess($checks);
186 /// @TODO Maybe move this out?
187 function check_passed($v, $c) {
188 if ($c['required']) {
189 $v = $v && $c['status'];
193 $checkspassed = array_reduce($checks, "check_passed", true);
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(),
212 case 2: { // Database config
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']));
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.'),
229 '$status' => $wizard_status,
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'),
239 '$lbl_10' => t('Please select a default timezone for your website'),
241 '$baseurl' => System::baseUrl(),
243 '$phpath' => $phpath,
245 '$submit' => t('Submit'),
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']));
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();
263 $tpl = get_markup_template('install_settings.tpl');
264 $o .= replace_macros($tpl, array(
265 '$title' => $install_title,
266 '$pass' => t('Site settings'),
268 '$status' => $wizard_status,
270 '$dbhost' => $dbhost,
271 '$dbuser' => $dbuser,
272 '$dbpass' => $dbpass,
273 '$dbdata' => $dbdata,
274 '$phpath' => $phpath,
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'),
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(),
285 '$submit' => t('Submit'),
295 * checks : array passed to template
299 * help : string optional
301 function check_add(&$checks, $title, $status, $required, $help) {
305 'required' => $required,
310 function check_php(&$phpath, &$checks) {
311 $passed = $passed2 = $passed3 = false;
312 if (strlen($phpath)) {
313 $passed = file_exists($phpath);
315 $phpath = trim(shell_exec('which php'));
316 $passed = strlen($phpath);
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;
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.')),
330 check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
334 $result = trim(shell_exec($cmd));
335 $passed2 = ( strpos($result, "(cli)") !== false);
336 list($result) = explode("\n", $result);
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>";
342 check_add($checks, t('PHP cli binary'), $passed2, true, $help);
348 $cmd = "$phpath testargs.php $str";
349 $result = trim(shell_exec($cmd));
350 $passed3 = $result == $str;
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.');
356 check_add($checks, t('PHP register_argc_argv'), $passed3, true, $help);
362 function check_keys(&$checks) {
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
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".');
382 check_add($checks, t('Generate encryption keys'), $res, true, $help);
387 function check_funcs(&$checks) {
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, "");
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.'));
401 check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
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.');
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.');
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.');
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.');
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.');
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.');
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.');
434 $checks = array_merge($checks, $ck_funcs);
436 // check for XML DOM Documents being able to be generated
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.');
446 function check_htconfig(&$checks) {
449 if ((file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
450 (!file_exists('.htconfig.php') && !is_writable('.'))) {
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;
459 check_add($checks, t('.htconfig.php is writable'), $status, false, $help);
463 function check_smarty3(&$checks) {
466 if (!is_writable('view/smarty3')) {
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;
475 check_add($checks, t('view/smarty3 is writable'), $status, true, $help);
479 function check_htaccess(&$checks) {
482 if (function_exists('curl_init')) {
483 $test = fetch_url(System::baseUrl()."/install/testrewrite");
486 $test = fetch_url(normalise_link(System::baseUrl()."/install/testrewrite"));
491 $help = t('Url rewrite in .htaccess is not working. Check your server configuration.');
493 check_add($checks, t('Url rewrite is working'), $status, true, $help);
495 // cannot check modrewrite if libcurl is not installed
496 /// @TODO Maybe issue warning here?
500 function check_imagik(&$checks) {
504 if (class_exists('Imagick')) {
506 $supported = Image::supportedTypes();
507 if (array_key_exists('image/gif', $supported)) {
511 if ($imagick == false) {
512 check_add($checks, t('ImageMagick PHP extension is not installed'), $imagick, false, "");
514 check_add($checks, t('ImageMagick PHP extension is installed'), $imagick, false, "");
516 check_add($checks, t('ImageMagick supports GIF'), $gif, false, "");
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>";
528 function load_database_rem($v, $i) {
530 if (strlen($l)>1 && ($l[0] == "-" || ($l[0] == "/" && $l[1] == "*"))) {
537 function load_database() {
539 require_once("include/dbstructure.php");
540 $errors = update_structure(false, true);
545 function what_next() {
546 $baseurl = System::baseUrl();
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".')
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.")