3 * @file src/Core/Install.php
5 namespace Friendica\Core;
9 use Friendica\Core\Config\ConfigCache;
10 use Friendica\Database\DBA;
11 use Friendica\Database\DBStructure;
12 use Friendica\Object\Image;
13 use Friendica\Util\Network;
14 use Friendica\Util\Profiler;
15 use Friendica\Util\Strings;
18 * Contains methods for installation purpose of Friendica
22 // Default values for the install page
23 const DEFAULT_LANG = 'en';
24 const DEFAULT_TZ = 'America/Los_Angeles';
25 const DEFAULT_HOST = 'localhost';
28 * @var array the check outcomes
33 * @var string The path to the PHP binary
35 private $phppath = null;
38 * Returns all checks made
40 * @return array the checks
42 public function getChecks()
48 * Returns the PHP path
50 * @return string the PHP Path
51 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
53 public function getPHPPath()
55 // if not set, determine the PHP path
56 if (!isset($this->phppath)) {
61 return $this->phppath;
67 public function resetChecks()
73 * Install constructor.
76 public function __construct()
82 * Checks the current installation environment. There are optional and mandatory checks.
84 * @param string $baseurl The baseurl of Friendica
85 * @param string $phpath Optional path to the PHP binary
87 * @return bool if the check succeed
88 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90 public function checkEnvironment($baseurl, $phpath = null)
95 if (!$this->checkPHP($phpath)) {
100 if (!$this->checkFunctions()) {
104 if (!$this->checkImagick()) {
108 if (!$this->checkLocalIni()) {
112 if (!$this->checkSmarty3()) {
116 if (!$this->checkKeys()) {
120 if (!$this->checkHtAccess($baseurl)) {
128 * Executes the installation of Friendica in the given environment.
129 * - Creates `config/local.config.php`
130 * - Installs Database Structure
132 * @param string $phppath Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
133 * @param string $urlpath Path based on the URL of Friendica (e.g. '/friendica')
134 * @param string $dbhost Hostname/IP of the Friendica Database
135 * @param string $dbuser Username of the Database connection credentials
136 * @param string $dbpass Password of the Database connection credentials
137 * @param string $dbdata Name of the Database
138 * @param string $timezone Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
139 * @param string $language 2-letter ISO 639-1 code (eg. 'en')
140 * @param string $adminmail Mail-Adress of the administrator
141 * @param string $basepath The basepath of Friendica
143 * @return bool true if the config was created, otherwise false
144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146 public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
148 $tpl = Renderer::getMarkupTemplate('local.config.tpl');
149 $txt = Renderer::replaceMacros($tpl, [
150 '$phpath' => $phppath,
151 '$dbhost' => $dbhost,
152 '$dbuser' => $dbuser,
153 '$dbpass' => $dbpass,
154 '$dbdata' => $dbdata,
155 '$timezone' => $timezone,
156 '$language' => $language,
157 '$urlpath' => $urlpath,
158 '$adminmail' => $adminmail,
161 $result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
164 $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'));
171 * Installs the DB-Scheme for Friendica
173 * @param string $basePath The base path of this application
175 * @return bool true if the installation was successful, otherwise false
178 public function installDatabase($basePath)
180 $result = DBStructure::update($basePath, false, true, true);
183 $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
184 $txt .= L10n::t('Please see the file "INSTALL.txt".');
186 $this->addCheck($txt, false, true, htmlentities($result, ENT_COMPAT, 'UTF-8'));
195 * Adds new checks to the array $checks
197 * @param string $title The title of the current check
198 * @param bool $status 1 = check passed, 0 = check not passed
199 * @param bool $required 1 = check is mandatory, 0 = check is optional
200 * @param string $help A help-string for the current check
201 * @param string $error_msg Optional. A error message, if the current check failed
203 private function addCheck($title, $status, $required, $help, $error_msg = "")
205 array_push($this->checks, [
208 'required' => $required,
210 'error_msg' => $error_msg,
217 * Checks the PHP environment.
219 * - Checks if a PHP binary is available
220 * - Checks if it is the CLI version
221 * - Checks if "register_argc_argv" is enabled
223 * @param string $phppath Optional. The Path to the PHP-Binary
224 * @param bool $required Optional. If set to true, the PHP-Binary has to exist (Default false)
226 * @return bool false if something required failed
227 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
229 public function checkPHP($phppath = null, $required = false)
233 if (!isset($phppath)) {
237 $passed = file_exists($phppath);
239 $phppath = trim(shell_exec('which ' . $phppath));
240 $passed = strlen($phppath);
245 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
246 $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;
248 $tpl = Renderer::getMarkupTemplate('field_input.tpl');
249 $help .= Renderer::replaceMacros($tpl, [
250 '$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.')],
255 $this->addCheck(L10n::t('Command line PHP') . ($passed ? " (<tt>$phppath</tt>)" : ""), $passed, false, $help);
258 $cmd = "$phppath -v";
259 $result = trim(shell_exec($cmd));
260 $passed2 = (strpos($result, "(cli)") !== false);
261 list($result) = explode("\n", $result);
264 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
265 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
267 $this->addCheck(L10n::t('PHP cli binary'), $passed2, true, $help);
269 // return if it was required
274 $str = Strings::getRandomName(8);
275 $cmd = "$phppath bin/testargs.php $str";
276 $result = trim(shell_exec($cmd));
277 $passed3 = $result == $str;
280 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
281 $help .= L10n::t('This is required for message delivery to work.');
283 $this->phppath = $phppath;
286 $this->addCheck(L10n::t('PHP register_argc_argv'), $passed3, true, $help);
289 // passed2 & passed3 are required if first check passed
290 return $passed2 && $passed3;
296 * Checks the OpenSSL Environment
298 * - Checks, if the command "openssl_pkey_new" is available
300 * @return bool false if something required failed
302 public function checkKeys()
308 if (function_exists('openssl_pkey_new')) {
309 $res = openssl_pkey_new([
310 'digest_alg' => 'sha1',
311 'private_key_bits' => 4096,
312 'encrypt_key' => false
318 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
319 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
322 $this->addCheck(L10n::t('Generate encryption keys'), $res, true, $help);
328 * PHP basic function check
330 * @param string $name The name of the function
331 * @param string $title The (localized) title of the function
332 * @param string $help The (localized) help of the function
333 * @param boolean $required If true, this check is required
335 * @return bool false, if the check failed
337 private function checkFunction($name, $title, $help, $required)
341 if (!function_exists($name)) {
345 $this->addCheck($title, $status, $required, $currHelp);
347 return $status || (!$status && !$required);
351 * PHP functions Check
353 * Checks the following PHP functions
363 * @return bool false if something required failed
365 public function checkFunctions()
371 if (function_exists('apache_get_modules')) {
372 if (!in_array('mod_rewrite', apache_get_modules())) {
373 $help = L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.');
378 $this->addCheck(L10n::t('Apache mod_rewrite module'), $status, true, $help);
382 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
384 $help = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
387 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
389 $help = L10n::t('Error: The MySQL driver for PDO is not installed.');
393 $this->addCheck(L10n::t('PDO or MySQLi PHP module'), $status, true, $help);
395 // check for XML DOM Documents being able to be generated
400 } catch (Exception $e) {
401 $help = L10n::t('Error, XML PHP module required but not installed.');
405 $this->addCheck(L10n::t('XML PHP module'), $status, true, $help);
407 $status = $this->checkFunction('curl_init',
408 L10n::t('libCurl PHP module'),
409 L10n::t('Error: libCURL PHP module required but not installed.'),
412 $returnVal = $returnVal ? $status : false;
414 $status = $this->checkFunction('imagecreatefromjpeg',
415 L10n::t('GD graphics PHP module'),
416 L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
419 $returnVal = $returnVal ? $status : false;
421 $status = $this->checkFunction('openssl_public_encrypt',
422 L10n::t('OpenSSL PHP module'),
423 L10n::t('Error: openssl PHP module required but not installed.'),
426 $returnVal = $returnVal ? $status : false;
428 $status = $this->checkFunction('mb_strlen',
429 L10n::t('mb_string PHP module'),
430 L10n::t('Error: mb_string PHP module required but not installed.'),
433 $returnVal = $returnVal ? $status : false;
435 $status = $this->checkFunction('iconv_strlen',
436 L10n::t('iconv PHP module'),
437 L10n::t('Error: iconv PHP module required but not installed.'),
440 $returnVal = $returnVal ? $status : false;
442 $status = $this->checkFunction('posix_kill',
443 L10n::t('POSIX PHP module'),
444 L10n::t('Error: POSIX PHP module required but not installed.'),
447 $returnVal = $returnVal ? $status : false;
449 $status = $this->checkFunction('json_encode',
450 L10n::t('JSON PHP module'),
451 L10n::t('Error: JSON PHP module required but not installed.'),
454 $returnVal = $returnVal ? $status : false;
460 * "config/local.config.php" - Check
462 * Checks if it's possible to create the "config/local.config.php"
464 * @return bool false if something required failed
466 public function checkLocalIni()
470 if ((file_exists('config/local.config.php') && !is_writable('config/local.config.php')) ||
471 (!file_exists('config/local.config.php') && !is_writable('.'))) {
474 $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;
475 $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;
476 $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;
477 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
480 $this->addCheck(L10n::t('config/local.config.php is writable'), $status, false, $help);
482 // Local INI File is not required
487 * Smarty3 Template Check
489 * Checks, if the directory of Smarty3 is writable
491 * @return bool false if something required failed
493 public function checkSmarty3()
497 if (!is_writable('view/smarty3')) {
500 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
501 $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;
502 $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;
503 $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;
506 $this->addCheck(L10n::t('view/smarty3 is writable'), $status, true, $help);
512 * ".htaccess" - Check
514 * Checks, if "url_rewrite" is enabled in the ".htaccess" file
516 * @param string $baseurl The baseurl of the app
517 * @return bool false if something required failed
518 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
520 public function checkHtAccess($baseurl)
525 if (function_exists('curl_init')) {
526 $fetchResult = Network::fetchUrlFull($baseurl . "/install/testrewrite");
528 $url = Strings::normaliseLink($baseurl . "/install/testrewrite");
529 if ($fetchResult->getReturnCode() != 204) {
530 $fetchResult = Network::fetchUrlFull($url);
533 if ($fetchResult->getReturnCode() != 204) {
535 $help = L10n::t('Url rewrite in .htaccess is not working. Make sure you copied .htaccess-dist to .htaccess.');
537 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
538 $error_msg['url'] = $fetchResult->getRedirectUrl();
539 $error_msg['msg'] = $fetchResult->getError();
542 $this->addCheck(L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
544 // cannot check modrewrite if libcurl is not installed
545 /// @TODO Maybe issue warning here?
554 * Checks, if the imagick module is available
556 * @return bool false if something required failed
558 public function checkImagick()
563 if (class_exists('Imagick')) {
565 $supported = Image::supportedTypes();
566 if (array_key_exists('image/gif', $supported)) {
571 $this->addCheck(L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
573 $this->addCheck(L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
575 $this->addCheck(L10n::t('ImageMagick supports GIF'), $gif, false, "");
579 // Imagick is not required
584 * Checking the Database connection and if it is available for the current installation
586 * @param ConfigCache $configCache The configuration cache
587 * @param Profiler $profiler The profiler of this app
588 * @param string $dbhost Hostname/IP of the Friendica Database
589 * @param string $dbuser Username of the Database connection credentials
590 * @param string $dbpass Password of the Database connection credentials
591 * @param string $dbdata Name of the Database
593 * @return bool true if the check was successful, otherwise false
596 public function checkDB(ConfigCache $configCache, Profiler $profiler, $dbhost, $dbuser, $dbpass, $dbdata)
598 if (!DBA::connect($configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) {
599 $this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
604 if (DBA::connected()) {
605 if (DBStructure::existsTable('user')) {
606 $this->addCheck(L10n::t('Database already in use.'), false, true, '');