3 * @file src/Core/Install.php
5 namespace Friendica\Core;
9 use Friendica\Core\Config\Cache\IConfigCache;
10 use Friendica\Database\DBA;
11 use Friendica\Database\DBStructure;
12 use Friendica\Object\Image;
13 use Friendica\Util\Logger\VoidLogger;
14 use Friendica\Util\BasePath;
15 use Friendica\Util\Network;
16 use Friendica\Util\Profiler;
17 use Friendica\Util\Strings;
20 * Contains methods for installation purpose of Friendica
24 // Default values for the install page
25 const DEFAULT_LANG = 'en';
26 const DEFAULT_TZ = 'America/Los_Angeles';
27 const DEFAULT_HOST = 'localhost';
30 * @var array the check outcomes
35 * @var string The path to the PHP binary
37 private $phppath = null;
40 * Returns all checks made
42 * @return array the checks
44 public function getChecks()
50 * Returns the PHP path
52 * @return string the PHP Path
53 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
55 public function getPHPPath()
57 // if not set, determine the PHP path
58 if (!isset($this->phppath)) {
63 return $this->phppath;
69 public function resetChecks()
75 * Install constructor.
78 public function __construct()
84 * Checks the current installation environment. There are optional and mandatory checks.
86 * @param string $baseurl The baseurl of Friendica
87 * @param string $phpath Optional path to the PHP binary
89 * @return bool if the check succeed
90 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
92 public function checkEnvironment($baseurl, $phpath = null)
97 if (!$this->checkPHP($phpath)) {
102 if (!$this->checkFunctions()) {
106 if (!$this->checkImagick()) {
110 if (!$this->checkLocalIni()) {
114 if (!$this->checkSmarty3()) {
118 if (!$this->checkKeys()) {
122 if (!$this->checkHtAccess($baseurl)) {
130 * Executes the installation of Friendica in the given environment.
131 * - Creates `config/local.config.php`
132 * - Installs Database Structure
134 * @param IConfigCache $configCache The config cache with all config relevant information
136 * @return bool true if the config was created, otherwise false
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 public function createConfig(IConfigCache $configCache)
141 $basepath = $configCache->get('system', 'basepath');
143 $url = $configCache->get('system', 'url');
145 $tpl = Renderer::getMarkupTemplate('local.config.tpl');
146 $txt = Renderer::replaceMacros($tpl, [
147 '$dbhost' => $configCache->get('database', 'hostname'),
148 '$dbuser' => $configCache->get('database', 'username'),
149 '$dbpass' => $configCache->get('database', 'password'),
150 '$dbdata' => $configCache->get('database', 'database'),
152 '$phpath' => $configCache->get('config', 'php_path'),
153 '$adminmail' => $configCache->get('config', 'admin_email'),
154 '$hostname' => $configCache->get('config', 'hostname'),
156 '$urlpath' => $configCache->get('system', 'urlpath'),
158 '$sslpolicy' => $configCache->get('system', 'ssl_policy'),
159 '$basepath' => $basepath,
160 '$timezone' => $configCache->get('system', 'default_timezone'),
161 '$language' => $configCache->get('system', 'language'),
164 $result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
167 $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'));
174 * Installs the DB-Scheme for Friendica
176 * @param string $basePath The base path of this application
178 * @return bool true if the installation was successful, otherwise false
181 public function installDatabase($basePath)
183 $result = DBStructure::update($basePath, false, true, true);
186 $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
187 $txt .= L10n::t('Please see the file "INSTALL.txt".');
189 $this->addCheck($txt, false, true, htmlentities($result, ENT_COMPAT, 'UTF-8'));
198 * Adds new checks to the array $checks
200 * @param string $title The title of the current check
201 * @param bool $status 1 = check passed, 0 = check not passed
202 * @param bool $required 1 = check is mandatory, 0 = check is optional
203 * @param string $help A help-string for the current check
204 * @param string $error_msg Optional. A error message, if the current check failed
206 private function addCheck($title, $status, $required, $help, $error_msg = "")
208 array_push($this->checks, [
211 'required' => $required,
213 'error_msg' => $error_msg,
220 * Checks the PHP environment.
222 * - Checks if a PHP binary is available
223 * - Checks if it is the CLI version
224 * - Checks if "register_argc_argv" is enabled
226 * @param string $phppath Optional. The Path to the PHP-Binary
227 * @param bool $required Optional. If set to true, the PHP-Binary has to exist (Default false)
229 * @return bool false if something required failed
230 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
232 public function checkPHP($phppath = null, $required = false)
236 if (!isset($phppath)) {
240 $passed = file_exists($phppath);
242 $phppath = trim(shell_exec('which ' . $phppath));
243 $passed = strlen($phppath);
248 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
249 $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;
251 $tpl = Renderer::getMarkupTemplate('field_input.tpl');
252 /// @todo Separate backend Installer class and presentation layer/view
253 $help .= Renderer::replaceMacros($tpl, [
254 '$field' => ['config-php_path', L10n::t('PHP executable path'), $phppath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
259 $this->addCheck(L10n::t('Command line PHP') . ($passed ? " (<tt>$phppath</tt>)" : ""), $passed, false, $help);
262 $cmd = "$phppath -v";
263 $result = trim(shell_exec($cmd));
264 $passed2 = (strpos($result, "(cli)") !== false);
265 list($result) = explode("\n", $result);
268 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
269 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
271 $this->addCheck(L10n::t('PHP cli binary'), $passed2, true, $help);
273 // return if it was required
278 $str = Strings::getRandomName(8);
279 $cmd = "$phppath bin/testargs.php $str";
280 $result = trim(shell_exec($cmd));
281 $passed3 = $result == $str;
284 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
285 $help .= L10n::t('This is required for message delivery to work.');
287 $this->phppath = $phppath;
290 $this->addCheck(L10n::t('PHP register_argc_argv'), $passed3, true, $help);
293 // passed2 & passed3 are required if first check passed
294 return $passed2 && $passed3;
300 * Checks the OpenSSL Environment
302 * - Checks, if the command "openssl_pkey_new" is available
304 * @return bool false if something required failed
306 public function checkKeys()
312 if (function_exists('openssl_pkey_new')) {
313 $res = openssl_pkey_new([
314 'digest_alg' => 'sha1',
315 'private_key_bits' => 4096,
316 'encrypt_key' => false
322 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
323 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
326 $this->addCheck(L10n::t('Generate encryption keys'), $res, true, $help);
332 * PHP basic function check
334 * @param string $name The name of the function
335 * @param string $title The (localized) title of the function
336 * @param string $help The (localized) help of the function
337 * @param boolean $required If true, this check is required
339 * @return bool false, if the check failed
341 private function checkFunction($name, $title, $help, $required)
345 if (!function_exists($name)) {
349 $this->addCheck($title, $status, $required, $currHelp);
351 return $status || (!$status && !$required);
355 * PHP functions Check
357 * Checks the following PHP functions
368 * @return bool false if something required failed
370 public function checkFunctions()
376 if (function_exists('apache_get_modules')) {
377 if (!in_array('mod_rewrite', apache_get_modules())) {
378 $help = L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.');
383 $this->addCheck(L10n::t('Apache mod_rewrite module'), $status, true, $help);
387 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
389 $help = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
392 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
394 $help = L10n::t('Error: The MySQL driver for PDO is not installed.');
398 $this->addCheck(L10n::t('PDO or MySQLi PHP module'), $status, true, $help);
400 // check for XML DOM Documents being able to be generated
405 } catch (Exception $e) {
406 $help = L10n::t('Error, XML PHP module required but not installed.');
410 $this->addCheck(L10n::t('XML PHP module'), $status, true, $help);
412 $status = $this->checkFunction('curl_init',
413 L10n::t('libCurl PHP module'),
414 L10n::t('Error: libCURL PHP module required but not installed.'),
417 $returnVal = $returnVal ? $status : false;
419 $status = $this->checkFunction('imagecreatefromjpeg',
420 L10n::t('GD graphics PHP module'),
421 L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
424 $returnVal = $returnVal ? $status : false;
426 $status = $this->checkFunction('openssl_public_encrypt',
427 L10n::t('OpenSSL PHP module'),
428 L10n::t('Error: openssl PHP module required but not installed.'),
431 $returnVal = $returnVal ? $status : false;
433 $status = $this->checkFunction('mb_strlen',
434 L10n::t('mb_string PHP module'),
435 L10n::t('Error: mb_string PHP module required but not installed.'),
438 $returnVal = $returnVal ? $status : false;
440 $status = $this->checkFunction('iconv_strlen',
441 L10n::t('iconv PHP module'),
442 L10n::t('Error: iconv PHP module required but not installed.'),
445 $returnVal = $returnVal ? $status : false;
447 $status = $this->checkFunction('posix_kill',
448 L10n::t('POSIX PHP module'),
449 L10n::t('Error: POSIX PHP module required but not installed.'),
452 $returnVal = $returnVal ? $status : false;
454 $status = $this->checkFunction('json_encode',
455 L10n::t('JSON PHP module'),
456 L10n::t('Error: JSON PHP module required but not installed.'),
459 $returnVal = $returnVal ? $status : false;
461 $status = $this->checkFunction('finfo_open',
462 L10n::t('File Information PHP module'),
463 L10n::t('Error: File Information PHP module required but not installed.'),
466 $returnVal = $returnVal ? $status : false;
472 * "config/local.config.php" - Check
474 * Checks if it's possible to create the "config/local.config.php"
476 * @return bool false if something required failed
478 public function checkLocalIni()
482 if ((file_exists('config/local.config.php') && !is_writable('config/local.config.php')) ||
483 (!file_exists('config/local.config.php') && !is_writable('.'))) {
486 $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;
487 $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;
488 $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;
489 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
492 $this->addCheck(L10n::t('config/local.config.php is writable'), $status, false, $help);
494 // Local INI File is not required
499 * Smarty3 Template Check
501 * Checks, if the directory of Smarty3 is writable
503 * @return bool false if something required failed
505 public function checkSmarty3()
509 if (!is_writable('view/smarty3')) {
512 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
513 $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;
514 $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;
515 $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;
518 $this->addCheck(L10n::t('view/smarty3 is writable'), $status, true, $help);
524 * ".htaccess" - Check
526 * Checks, if "url_rewrite" is enabled in the ".htaccess" file
528 * @param string $baseurl The baseurl of the app
529 * @return bool false if something required failed
530 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
532 public function checkHtAccess($baseurl)
537 if (function_exists('curl_init')) {
538 $fetchResult = Network::fetchUrlFull($baseurl . "/install/testrewrite");
540 $url = Strings::normaliseLink($baseurl . "/install/testrewrite");
541 if ($fetchResult->getReturnCode() != 204) {
542 $fetchResult = Network::fetchUrlFull($url);
545 if ($fetchResult->getReturnCode() != 204) {
547 $help = L10n::t('Url rewrite in .htaccess is not working. Make sure you copied .htaccess-dist to .htaccess.');
549 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
550 $error_msg['url'] = $fetchResult->getRedirectUrl();
551 $error_msg['msg'] = $fetchResult->getError();
554 $this->addCheck(L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
556 // cannot check modrewrite if libcurl is not installed
557 /// @TODO Maybe issue warning here?
566 * Checks, if the imagick module is available
568 * @return bool false if something required failed
570 public function checkImagick()
575 if (class_exists('Imagick')) {
577 $supported = Image::supportedTypes();
578 if (array_key_exists('image/gif', $supported)) {
583 $this->addCheck(L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
585 $this->addCheck(L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
587 $this->addCheck(L10n::t('ImageMagick supports GIF'), $gif, false, "");
591 // Imagick is not required
596 * Checking the Database connection and if it is available for the current installation
598 * @param IConfigCache $configCache The configuration cache
599 * @param Profiler $profiler The profiler of this app
601 * @return bool true if the check was successful, otherwise false
604 public function checkDB(IConfigCache $configCache, Profiler $profiler)
606 $dbhost = $configCache->get('database', 'hostname');
607 $dbuser = $configCache->get('database', 'username');
608 $dbpass = $configCache->get('database', 'password');
609 $dbdata = $configCache->get('database', 'database');
611 if (!DBA::connect($configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata)) {
612 $this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
617 if (DBA::connected()) {
618 if (DBStructure::existsTable('user')) {
619 $this->addCheck(L10n::t('Database already in use.'), false, true, '');
629 * Setup the default cache for a new installation
631 * @param IConfigCache $configCache The configuration cache
632 * @param string $basePath The determined basepath
634 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
636 public function setUpCache(IConfigCache $configCache, $basePath)
638 $configCache->set('config', 'php_path' , $this->getPHPPath());
639 $configCache->set('system', 'basepath' , $basePath);