3 * @file src/Core/Install.php
5 namespace Friendica\Core;
9 use Friendica\Core\Renderer;
10 use Friendica\Database\DBA;
11 use Friendica\Database\DBStructure;
12 use Friendica\Object\Image;
13 use Friendica\Util\Network;
14 use Friendica\Util\Strings;
17 * Contains methods for installation purpose of Friendica
21 // Default values for the install page
22 const DEFAULT_LANG = 'en';
23 const DEFAULT_TZ = 'America/Los_Angeles';
24 const DEFAULT_HOST = 'localhost';
27 * @var array the check outcomes
32 * @var string The path to the PHP binary
34 private $phppath = null;
37 * Returns all checks made
39 * @return array the checks
41 public function getChecks()
47 * Returns the PHP path
49 * @return string the PHP Path
50 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
52 public function getPHPPath()
54 // if not set, determine the PHP path
55 if (!isset($this->phppath)) {
60 return $this->phppath;
66 public function resetChecks()
72 * Install constructor.
75 public function __construct()
81 * Checks the current installation environment. There are optional and mandatory checks.
83 * @param string $baseurl The baseurl of Friendica
84 * @param string $phpath Optional path to the PHP binary
86 * @return bool if the check succeed
87 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
89 public function checkEnvironment($baseurl, $phpath = null)
94 if (!$this->checkPHP($phpath)) {
99 if (!$this->checkFunctions()) {
103 if (!$this->checkImagick()) {
107 if (!$this->checkLocalIni()) {
111 if (!$this->checkSmarty3()) {
115 if (!$this->checkKeys()) {
119 if (!$this->checkHtAccess($baseurl)) {
127 * Executes the installation of Friendica in the given environment.
128 * - Creates `config/local.config.php`
129 * - Installs Database Structure
131 * @param string $phppath Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
132 * @param string $urlpath Path based on the URL of Friendica (e.g. '/friendica')
133 * @param string $dbhost Hostname/IP of the Friendica Database
134 * @param string $dbuser Username of the Database connection credentials
135 * @param string $dbpass Password of the Database connection credentials
136 * @param string $dbdata Name of the Database
137 * @param string $timezone Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
138 * @param string $language 2-letter ISO 639-1 code (eg. 'en')
139 * @param string $adminmail Mail-Adress of the administrator
140 * @param string $basepath The basepath of Friendica
142 * @return bool true if the config was created, otherwise false
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
147 $tpl = Renderer::getMarkupTemplate('local.config.tpl');
148 $txt = Renderer::replaceMacros($tpl, [
149 '$phpath' => $phppath,
150 '$dbhost' => $dbhost,
151 '$dbuser' => $dbuser,
152 '$dbpass' => $dbpass,
153 '$dbdata' => $dbdata,
154 '$timezone' => $timezone,
155 '$language' => $language,
156 '$urlpath' => $urlpath,
157 '$adminmail' => $adminmail,
160 $result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
163 $this->addCheck(L10n::t('The database configuration file "config/local.config.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.'), false, false, htmlentities($txt, ENT_COMPAT, 'UTF-8'));
170 * Installs the DB-Scheme for Friendica
172 * @return bool true if the installation was successful, otherwise false
175 public function installDatabase()
177 $result = DBStructure::update(false, true, true);
180 $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
181 $txt .= L10n::t('Please see the file "INSTALL.txt".');
183 $this->addCheck($txt, false, true, htmlentities($result, ENT_COMPAT, 'UTF-8'));
192 * Adds new checks to the array $checks
194 * @param string $title The title of the current check
195 * @param bool $status 1 = check passed, 0 = check not passed
196 * @param bool $required 1 = check is mandatory, 0 = check is optional
197 * @param string $help A help-string for the current check
198 * @param string $error_msg Optional. A error message, if the current check failed
200 private function addCheck($title, $status, $required, $help, $error_msg = "")
202 array_push($this->checks, [
205 'required' => $required,
207 'error_msg' => $error_msg,
214 * Checks the PHP environment.
216 * - Checks if a PHP binary is available
217 * - Checks if it is the CLI version
218 * - Checks if "register_argc_argv" is enabled
220 * @param string $phppath Optional. The Path to the PHP-Binary
221 * @param bool $required Optional. If set to true, the PHP-Binary has to exist (Default false)
223 * @return bool false if something required failed
224 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
226 public function checkPHP($phppath = null, $required = false)
232 if (!isset($phppath)) {
236 $passed = file_exists($phppath);
238 $phppath = trim(shell_exec('which ' . $phppath));
239 $passed = strlen($phppath);
244 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
245 $help .= L10n::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;
247 $tpl = Renderer::getMarkupTemplate('field_input.tpl');
248 $help .= Renderer::replaceMacros($tpl, [
249 '$field' => ['phpath', L10n::t('PHP executable path'), $phppath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
254 $this->addCheck(L10n::t('Command line PHP') . ($passed ? " (<tt>$phppath</tt>)" : ""), $passed, false, $help);
257 $cmd = "$phppath -v";
258 $result = trim(shell_exec($cmd));
259 $passed2 = (strpos($result, "(cli)") !== false);
260 list($result) = explode("\n", $result);
263 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
264 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
266 $this->addCheck(L10n::t('PHP cli binary'), $passed2, true, $help);
268 // return if it was required
273 $str = Strings::getRandomName(8);
274 $cmd = "$phppath bin/testargs.php $str";
275 $result = trim(shell_exec($cmd));
276 $passed3 = $result == $str;
279 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
280 $help .= L10n::t('This is required for message delivery to work.');
282 $this->phppath = $phppath;
285 $this->addCheck(L10n::t('PHP register_argc_argv'), $passed3, true, $help);
288 // passed2 & passed3 are required if first check passed
289 return $passed2 && $passed3;
295 * Checks the OpenSSL Environment
297 * - Checks, if the command "openssl_pkey_new" is available
299 * @return bool false if something required failed
301 public function checkKeys()
307 if (function_exists('openssl_pkey_new')) {
308 $res = openssl_pkey_new([
309 'digest_alg' => 'sha1',
310 'private_key_bits' => 4096,
311 'encrypt_key' => false
317 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
318 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
321 $this->addCheck(L10n::t('Generate encryption keys'), $res, true, $help);
327 * PHP basic function check
329 * @param string $name The name of the function
330 * @param string $title The (localized) title of the function
331 * @param string $help The (localized) help of the function
332 * @param boolean $required If true, this check is required
334 * @return bool false, if the check failed
336 private function checkFunction($name, $title, $help, $required)
340 if (!function_exists($name)) {
344 $this->addCheck($title, $status, $required, $currHelp);
346 return $status || (!$status && !$required);
350 * PHP functions Check
352 * Checks the following PHP functions
362 * @return bool false if something required failed
364 public function checkFunctions()
370 if (function_exists('apache_get_modules')) {
371 if (!in_array('mod_rewrite', apache_get_modules())) {
372 $help = L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.');
377 $this->addCheck(L10n::t('Apache mod_rewrite module'), $status, true, $help);
381 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
383 $help = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
386 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
388 $help = L10n::t('Error: The MySQL driver for PDO is not installed.');
392 $this->addCheck(L10n::t('PDO or MySQLi PHP module'), $status, true, $help);
394 // check for XML DOM Documents being able to be generated
398 $xml = new DOMDocument();
399 } catch (Exception $e) {
400 $help = L10n::t('Error, XML PHP module required but not installed.');
404 $this->addCheck(L10n::t('XML PHP module'), $status, true, $help);
406 $status = $this->checkFunction('curl_init',
407 L10n::t('libCurl PHP module'),
408 L10n::t('Error: libCURL PHP module required but not installed.'),
411 $returnVal = $returnVal ? $status : false;
413 $status = $this->checkFunction('imagecreatefromjpeg',
414 L10n::t('GD graphics PHP module'),
415 L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
418 $returnVal = $returnVal ? $status : false;
420 $status = $this->checkFunction('openssl_public_encrypt',
421 L10n::t('OpenSSL PHP module'),
422 L10n::t('Error: openssl PHP module required but not installed.'),
425 $returnVal = $returnVal ? $status : false;
427 $status = $this->checkFunction('mb_strlen',
428 L10n::t('mb_string PHP module'),
429 L10n::t('Error: mb_string PHP module required but not installed.'),
432 $returnVal = $returnVal ? $status : false;
434 $status = $this->checkFunction('iconv_strlen',
435 L10n::t('iconv PHP module'),
436 L10n::t('Error: iconv PHP module required but not installed.'),
439 $returnVal = $returnVal ? $status : false;
441 $status = $this->checkFunction('posix_kill',
442 L10n::t('POSIX PHP module'),
443 L10n::t('Error: POSIX PHP module required but not installed.'),
446 $returnVal = $returnVal ? $status : false;
448 $status = $this->checkFunction('json_encode',
449 L10n::t('JSON PHP module'),
450 L10n::t('Error: JSON PHP module required but not installed.'),
453 $returnVal = $returnVal ? $status : false;
459 * "config/local.config.php" - Check
461 * Checks if it's possible to create the "config/local.config.php"
463 * @return bool false if something required failed
465 public function checkLocalIni()
469 if ((file_exists('config/local.config.php') && !is_writable('config/local.config.php')) ||
470 (!file_exists('config/local.config.php') && !is_writable('.'))) {
473 $help = L10n::t('The web installer needs to be able to create a file called "local.config.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
474 $help .= L10n::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;
475 $help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.config.php in your Friendica "config" folder.') . EOL;
476 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
479 $this->addCheck(L10n::t('config/local.config.php is writable'), $status, false, $help);
481 // Local INI File is not required
486 * Smarty3 Template Check
488 * Checks, if the directory of Smarty3 is writable
490 * @return bool false if something required failed
492 public function checkSmarty3()
496 if (!is_writable('view/smarty3')) {
499 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
500 $help .= L10n::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;
501 $help .= L10n::t("Please ensure that the user that your web server runs as \x28e.g. www-data\x29 has write access to this folder.") . EOL;
502 $help .= L10n::t("Note: as a security measure, you should give the web server write access to view/smarty3/ only--not the template files \x28.tpl\x29 that it contains.") . EOL;
505 $this->addCheck(L10n::t('view/smarty3 is writable'), $status, true, $help);
511 * ".htaccess" - Check
513 * Checks, if "url_rewrite" is enabled in the ".htaccess" file
515 * @param string $baseurl The baseurl of the app
516 * @return bool false if something required failed
517 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
519 public function checkHtAccess($baseurl)
524 if (function_exists('curl_init')) {
525 $fetchResult = Network::fetchUrlFull($baseurl . "/install/testrewrite");
527 $url = Strings::normaliseLink($baseurl . "/install/testrewrite");
528 if ($fetchResult->getReturnCode() != 204) {
529 $fetchResult = Network::fetchUrlFull($url);
532 if ($fetchResult->getReturnCode() != 204) {
534 $help = L10n::t('Url rewrite in .htaccess is not working. Make sure you copied .htaccess-dist to .htaccess.');
536 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
537 $error_msg['url'] = $fetchResult->getRedirectUrl();
538 $error_msg['msg'] = $fetchResult->getError();
541 $this->addCheck(L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
543 // cannot check modrewrite if libcurl is not installed
544 /// @TODO Maybe issue warning here?
553 * Checks, if the imagick module is available
555 * @return bool false if something required failed
557 public function checkImagick()
562 if (class_exists('Imagick')) {
564 $supported = Image::supportedTypes();
565 if (array_key_exists('image/gif', $supported)) {
570 $this->addCheck(L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
572 $this->addCheck(L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
574 $this->addCheck(L10n::t('ImageMagick supports GIF'), $gif, false, "");
578 // Imagick is not required
583 * Checking the Database connection and if it is available for the current installation
585 * @param string $dbhost Hostname/IP of the Friendica Database
586 * @param string $dbuser Username of the Database connection credentials
587 * @param string $dbpass Password of the Database connection credentials
588 * @param string $dbdata Name of the Database
590 * @return bool true if the check was successful, otherwise false
593 public function checkDB($dbhost, $dbuser, $dbpass, $dbdata)
595 if (!DBA::connect($dbhost, $dbuser, $dbpass, $dbdata)) {
596 $this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
601 if (DBA::connected()) {
602 if (DBStructure::existsTable('user')) {
603 $this->addCheck(L10n::t('Database already in use.'), false, true, '');