3 * @file mod/install.php
6 use Friendica\Core\System;
7 use Friendica\Database\DBM;
8 use Friendica\Database\DBStructure;
9 use Friendica\Object\Image;
11 $install_wizard_pass = 1;
13 function install_init(App $a) {
15 // $baseurl/install/testrwrite to test if rewite in .htaccess is working
16 if ($a->argc == 2 && $a->argv[1] == "testrewrite") {
21 // We overwrite current theme css, because during install we could not have a working mod_rewrite
22 // so we could not have a css at all. Here we set a static css file for the install procedure pages
23 $a->config['system']['theme'] = "../install";
24 $a->theme['stylesheet'] = System::baseUrl()."/view/install/style.css";
26 global $install_wizard_pass;
27 if (x($_POST, 'pass')) {
28 $install_wizard_pass = intval($_POST['pass']);
33 function install_post(App $a) {
34 global $install_wizard_pass;
36 switch($install_wizard_pass) {
40 break; // just in case return don't return :)
42 $urlpath = $a->get_path();
43 $dbhost = notags(trim($_POST['dbhost']));
44 $dbuser = notags(trim($_POST['dbuser']));
45 $dbpass = notags(trim($_POST['dbpass']));
46 $dbdata = notags(trim($_POST['dbdata']));
47 $phpath = notags(trim($_POST['phpath']));
49 require_once("include/dba.php");
50 if (!dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true)) {
51 $a->data['db_conn_failed'] = true;
57 $urlpath = $a->get_path();
58 $dbhost = notags(trim($_POST['dbhost']));
59 $dbuser = notags(trim($_POST['dbuser']));
60 $dbpass = notags(trim($_POST['dbpass']));
61 $dbdata = notags(trim($_POST['dbdata']));
62 $phpath = notags(trim($_POST['phpath']));
63 $timezone = notags(trim($_POST['timezone']));
64 $language = notags(trim($_POST['language']));
65 $adminmail = notags(trim($_POST['adminmail']));
69 dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true);
71 $tpl = get_markup_template('htconfig.tpl');
72 $txt = replace_macros($tpl,array(
77 '$timezone' => $timezone,
78 '$language' => $language,
79 '$urlpath' => $urlpath,
81 '$adminmail' => $adminmail,
86 $result = file_put_contents('.htconfig.php', $txt);
88 $a->data['txt'] = $txt;
91 $errors = load_database();
95 $a->data['db_failed'] = $errors;
97 $a->data['db_installed'] = true;
105 function install_content(App $a) {
107 global $install_wizard_pass;
110 $install_title = t('Friendica Communications Server - Setup');
114 if (x($a->data, 'db_conn_failed')) {
115 $install_wizard_pass = 2;
116 $wizard_status = t('Could not connect to database.');
118 if (x($a->data, 'db_create_failed')) {
119 $install_wizard_pass = 2;
120 $wizard_status = t('Could not create table.');
123 $db_return_text = "";
124 if (x($a->data, 'db_installed')) {
125 $txt = '<p style="font-size: 130%;">';
126 $txt .= t('Your Friendica site database has been installed.') . EOL;
127 $db_return_text .= $txt;
130 if (x($a->data, 'db_failed')) {
131 $txt = t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
132 $txt .= t('Please see the file "INSTALL.txt".') . EOL ."<hr>";
133 $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL;
134 $db_return_text .= $txt;
137 if (dba::$connected) {
138 $r = q("SELECT COUNT(*) as `total` FROM `user`");
139 if (DBM::is_result($r) && $r[0]['total']) {
140 $tpl = get_markup_template('install.tpl');
141 return replace_macros($tpl, array(
142 '$title' => $install_title,
144 '$status' => t('Database already in use.'),
150 if (x($a->data, 'txt') && strlen($a->data['txt'])) {
151 $db_return_text .= manual_config($a);
154 if ($db_return_text != "") {
155 $tpl = get_markup_template('install.tpl');
156 return replace_macros($tpl, array(
157 '$title' => $install_title,
159 '$text' => $db_return_text . what_next(),
163 switch ($install_wizard_pass) {
164 case 1: { // System check
169 check_funcs($checks);
171 check_imagik($checks);
173 check_htconfig($checks);
175 check_smarty3($checks);
179 if (x($_POST, 'phpath')) {
180 $phpath = notags(trim($_POST['phpath']));
183 check_php($phpath, $checks);
185 check_htaccess($checks);
187 /// @TODO Maybe move this out?
188 function check_passed($v, $c) {
189 if ($c['required']) {
190 $v = $v && $c['status'];
194 $checkspassed = array_reduce($checks, "check_passed", true);
198 $tpl = get_markup_template('install_checks.tpl');
199 $o .= replace_macros($tpl, array(
200 '$title' => $install_title,
201 '$pass' => t('System check'),
202 '$checks' => $checks,
203 '$passed' => $checkspassed,
204 '$see_install' => t('Please see the file "INSTALL.txt".'),
205 '$next' => t('Next'),
206 '$reload' => t('Check again'),
207 '$phpath' => $phpath,
208 '$baseurl' => System::baseUrl(),
213 case 2: { // Database config
215 $dbhost = ((x($_POST, 'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
216 $dbuser = notags(trim($_POST['dbuser']));
217 $dbpass = notags(trim($_POST['dbpass']));
218 $dbdata = notags(trim($_POST['dbdata']));
219 $phpath = notags(trim($_POST['phpath']));
222 $tpl = get_markup_template('install_db.tpl');
223 $o .= replace_macros($tpl, array(
224 '$title' => $install_title,
225 '$pass' => t('Database connection'),
226 '$info_01' => t('In order to install Friendica we need to know how to connect to your database.'),
227 '$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
228 '$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
230 '$status' => $wizard_status,
232 '$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, '', 'required'),
233 '$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, '', 'required', 'autofocus'),
234 '$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, t("For security reasons the password must not be empty"), 'required'),
235 '$dbdata' => array('dbdata', t('Database Name'), $dbdata, '', 'required'),
236 '$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'),
240 '$lbl_10' => t('Please select a default timezone for your website'),
242 '$baseurl' => System::baseUrl(),
244 '$phpath' => $phpath,
246 '$submit' => t('Submit'),
251 case 3: { // Site settings
252 require_once('include/datetime.php');
253 $dbhost = ((x($_POST, 'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
254 $dbuser = notags(trim($_POST['dbuser']));
255 $dbpass = notags(trim($_POST['dbpass']));
256 $dbdata = notags(trim($_POST['dbdata']));
257 $phpath = notags(trim($_POST['phpath']));
259 $adminmail = notags(trim($_POST['adminmail']));
260 $timezone = ((x($_POST, 'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
261 /* Installed langs */
262 $lang_choices = get_available_languages();
264 $tpl = get_markup_template('install_settings.tpl');
265 $o .= replace_macros($tpl, array(
266 '$title' => $install_title,
267 '$pass' => t('Site settings'),
269 '$status' => $wizard_status,
271 '$dbhost' => $dbhost,
272 '$dbuser' => $dbuser,
273 '$dbpass' => $dbpass,
274 '$dbdata' => $dbdata,
275 '$phpath' => $phpath,
277 '$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'),
280 '$timezone' => field_timezone('timezone', t('Please select a default timezone for your website'), $timezone, ''),
281 '$language' => array('language', t('System Language:'), 'en', t('Set the default language for your Friendica installation interface and to send emails.'), $lang_choices),
282 '$baseurl' => System::baseUrl(),
286 '$submit' => t('Submit'),
296 * checks : array passed to template
300 * help : string optional
302 function check_add(&$checks, $title, $status, $required, $help) {
306 'required' => $required,
311 function check_php(&$phpath, &$checks) {
312 $passed = $passed2 = $passed3 = false;
313 if (strlen($phpath)) {
314 $passed = file_exists($phpath);
316 $phpath = trim(shell_exec('which php'));
317 $passed = strlen($phpath);
321 $help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
322 $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;
324 $tpl = get_markup_template('field_input.tpl');
325 $help .= replace_macros($tpl, array(
326 '$field' => array('phpath', t('PHP executable path'), $phpath, t('Enter full path to php executable. You can leave this blank to continue the installation.')),
331 check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
335 $result = trim(shell_exec($cmd));
336 $passed2 = ( strpos($result, "(cli)") !== false);
337 list($result) = explode("\n", $result);
340 $help .= t('PHP executable is not the php cli binary (could be cgi-fgci version)'). EOL;
341 $help .= t('Found PHP version: ')."<tt>$result</tt>";
343 check_add($checks, t('PHP cli binary'), $passed2, true, $help);
349 $cmd = "$phpath testargs.php $str";
350 $result = trim(shell_exec($cmd));
351 $passed3 = $result == $str;
354 $help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
355 $help .= t('This is required for message delivery to work.');
357 check_add($checks, t('PHP register_argc_argv'), $passed3, true, $help);
363 function check_keys(&$checks) {
369 if (function_exists('openssl_pkey_new')) {
370 $res = openssl_pkey_new(array(
371 'digest_alg' => 'sha1',
372 'private_key_bits' => 4096,
373 'encrypt_key' => false
380 $help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
381 $help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
383 check_add($checks, t('Generate encryption keys'), $res, true, $help);
388 function check_funcs(&$checks) {
390 check_add($ck_funcs, t('libCurl PHP module'), true, true, "");
391 check_add($ck_funcs, t('GD graphics PHP module'), true, true, "");
392 check_add($ck_funcs, t('OpenSSL PHP module'), true, true, "");
393 check_add($ck_funcs, t('PDO or MySQLi PHP module'), true, true, "");
394 check_add($ck_funcs, t('mb_string PHP module'), true, true, "");
395 check_add($ck_funcs, t('XML PHP module'), true, true, "");
396 check_add($ck_funcs, t('iconv module'), true, true, "");
398 if (function_exists('apache_get_modules')) {
399 if (! in_array('mod_rewrite',apache_get_modules())) {
400 check_add($ck_funcs, t('Apache mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
402 check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
406 if (! function_exists('curl_init')) {
407 $ck_funcs[0]['status'] = false;
408 $ck_funcs[0]['help'] = t('Error: libCURL PHP module required but not installed.');
410 if (! function_exists('imagecreatefromjpeg')) {
411 $ck_funcs[1]['status'] = false;
412 $ck_funcs[1]['help'] = t('Error: GD graphics PHP module with JPEG support required but not installed.');
414 if (! function_exists('openssl_public_encrypt')) {
415 $ck_funcs[2]['status'] = false;
416 $ck_funcs[2]['help'] = t('Error: openssl PHP module required but not installed.');
418 if (! function_exists('mysqli_connect') && !class_exists('pdo')) {
419 $ck_funcs[3]['status'] = false;
420 $ck_funcs[3]['help'] = t('Error: PDO or MySQLi PHP module required but not installed.');
422 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', PDO::getAvailableDrivers())) {
423 $ck_funcs[3]['status'] = false;
424 $ck_funcs[3]['help'] = t('Error: The MySQL driver for PDO is not installed.');
426 if (! function_exists('mb_strlen')) {
427 $ck_funcs[4]['status'] = false;
428 $ck_funcs[4]['help'] = t('Error: mb_string PHP module required but not installed.');
430 if (! function_exists('iconv_strlen')) {
431 $ck_funcs[7]['status'] = false;
432 $ck_funcs[7]['help'] = t('Error: iconv PHP module required but not installed.');
435 $checks = array_merge($checks, $ck_funcs);
437 // check for XML DOM Documents being able to be generated
439 $xml = new DOMDocument();
440 } catch (Exception $e) {
441 $ck_funcs[6]['status'] = false;
442 $ck_funcs[6]['help'] = t('Error, XML PHP module required but not installed.');
447 function check_htconfig(&$checks) {
450 if ((file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
451 (!file_exists('.htconfig.php') && !is_writable('.'))) {
454 $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;
455 $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;
456 $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;
457 $help .= t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.').EOL;
460 check_add($checks, t('.htconfig.php is writable'), $status, false, $help);
464 function check_smarty3(&$checks) {
467 if (!is_writable('view/smarty3')) {
470 $help = t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') .EOL;
471 $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;
472 $help .= t('Please ensure that the user that your web server runs as (e.g. www-data) has write access to this folder.').EOL;
473 $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;
476 check_add($checks, t('view/smarty3 is writable'), $status, true, $help);
480 function check_htaccess(&$checks) {
483 if (function_exists('curl_init')) {
484 $test = fetch_url(System::baseUrl()."/install/testrewrite");
487 $test = fetch_url(normalise_link(System::baseUrl()."/install/testrewrite"));
492 $help = t('Url rewrite in .htaccess is not working. Check your server configuration.');
494 check_add($checks, t('Url rewrite is working'), $status, true, $help);
496 // cannot check modrewrite if libcurl is not installed
497 /// @TODO Maybe issue warning here?
501 function check_imagik(&$checks) {
505 if (class_exists('Imagick')) {
507 $supported = Image::supportedTypes();
508 if (array_key_exists('image/gif', $supported)) {
512 if ($imagick == false) {
513 check_add($checks, t('ImageMagick PHP extension is not installed'), $imagick, false, "");
515 check_add($checks, t('ImageMagick PHP extension is installed'), $imagick, false, "");
517 check_add($checks, t('ImageMagick supports GIF'), $gif, false, "");
522 function manual_config(App $a) {
523 $data = htmlentities($a->data['txt'],ENT_COMPAT, 'UTF-8');
524 $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.');
525 $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
529 function load_database_rem($v, $i) {
531 if (strlen($l)>1 && ($l[0] == "-" || ($l[0] == "/" && $l[1] == "*"))) {
538 function load_database() {
539 $errors = DBStructure::update(false, true);
544 function what_next() {
545 $baseurl = System::baseUrl();
547 t('<h1>What next</h1>')
548 ."<p>".t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
549 .t('Please see the file "INSTALL.txt".')
551 .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.")