3 * @file src/Core/Install.php
5 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Database\DBStructure;
11 use Friendica\Object\Image;
12 use Friendica\Util\Network;
15 * Contains methods for installation purpose of Friendica
17 class Install extends BaseObject
20 * Checks the current installation environment. There are optional and mandatory checks.
22 * @param string $phpath Optional path to the PHP binary (Default is 'php')
24 * @return array First element is a list of all checks and their results,
25 * the second element is a list of passed checks
27 public static function check($phpath = 'php')
31 self::checkFunctions($checks);
33 self::checkImagick($checks);
35 self::checkLocalIni($checks);
37 self::checkSmarty3($checks);
39 self::checkKeys($checks);
41 self::checkPHP($phpath, $checks);
43 self::checkHtAccess($checks);
45 $checkspassed = array_reduce($checks,
47 if (!empty($c['require'])) {
48 $v = $v && $c['status'];
54 return array($checks, $checkspassed);
58 * Executes the installation of Friendica in the given environment.
59 * - Creates `config/local.ini.php`
60 * - Installs Database Structure
62 * @param string $urlpath Path based on the URL of Friendica (e.g. '/friendica')
63 * @param string $dbhost Hostname/IP of the Friendica Database
64 * @param string $dbuser Username of the Database connection credentials
65 * @param string $dbpass Password of the Database connection credentials
66 * @param string $dbdata Name of the Database
67 * @param string $phpath Path to the PHP-Binary (e.g. 'php' or '/usr/bin/php')
68 * @param string $timezone Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
69 * @param string $language 2-letter ISO 639-1 code (eg. 'en')
70 * @param string $adminmail Mail-Adress of the administrator
72 public static function createConfig($urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $phpath, $timezone, $language, $adminmail)
74 $tpl = get_markup_template('local.ini.tpl');
75 $txt = replace_macros($tpl,[
80 '$timezone' => $timezone,
81 '$language' => $language,
82 '$urlpath' => $urlpath,
84 '$adminmail' => $adminmail,
87 $result = file_put_contents('config/local.ini.php', $txt);
89 self::getApp()->data['txt'] = $txt;
95 * Adds new checks to the array $checks
97 * @param array $checks The list of all checks (by-ref parameter!)
98 * @param string $title The title of the current check
99 * @param bool $status 1 = check passed, 0 = check not passed
100 * @param bool $required 1 = check is mandatory, 0 = check is optional
101 * @param string $help A help-string for the current check
102 * @param string $error_msg Optional. A error message, if the current check failed
104 private static function addCheck(&$checks, $title, $status, $required, $help, $error_msg = "")
109 'required' => $required,
111 'error_msg' => $error_msg,
118 * Checks the PHP environment.
120 * - Checks if a PHP binary is available
121 * - Checks if it is the CLI version
122 * - Checks if "register_argc_argv" is enabled
124 * @param string $phpath Optional. The Path to the PHP-Binary
125 * @param array $checks The list of all checks (by-ref parameter!)
127 public static function checkPHP($phpath, &$checks)
129 $passed = $passed2 = $passed3 = false;
130 if (strlen($phpath)) {
131 $passed = file_exists($phpath);
133 $phpath = trim(shell_exec('which php'));
134 $passed = strlen($phpath);
138 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
139 $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;
141 $tpl = get_markup_template('field_input.tpl');
142 $help .= replace_macros($tpl, [
143 '$field' => ['phpath', L10n::t('PHP executable path'), $phpath, L10n::t('Enter full path to php executable. You can leave this blank to continue the installation.')],
148 self::addCheck($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
152 $result = trim(shell_exec($cmd));
153 $passed2 = (strpos($result, "(cli)") !== false);
154 list($result) = explode("\n", $result);
157 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
158 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
160 self::addCheck($checks, L10n::t('PHP cli binary'), $passed2, true, $help);
165 $cmd = "$phpath testargs.php $str";
166 $result = trim(shell_exec($cmd));
167 $passed3 = $result == $str;
170 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
171 $help .= L10n::t('This is required for message delivery to work.');
173 self::addCheck($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);
180 * Checks the OpenSSL Environment
182 * - Checks, if the command "openssl_pkey_new" is available
184 * @param array $checks The list of all checks (by-ref parameter!)
186 public static function checkKeys(&$checks)
191 if (function_exists('openssl_pkey_new')) {
192 $res = openssl_pkey_new([
193 'digest_alg' => 'sha1',
194 'private_key_bits' => 4096,
195 'encrypt_key' => false
201 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
202 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
204 self::addCheck($checks, L10n::t('Generate encryption keys'), $res, true, $help);
208 * PHP functions Check
210 * Checks the following PHP functions
220 * @param array $checks The list of all checks (by-ref parameter!)
222 public static function checkFunctions(&$checks)
225 self::addCheck($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");
226 self::addCheck($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");
227 self::addCheck($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");
228 self::addCheck($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");
229 self::addCheck($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");
230 self::addCheck($ck_funcs, L10n::t('XML PHP module'), true, true, "");
231 self::addCheck($ck_funcs, L10n::t('iconv PHP module'), true, true, "");
232 self::addCheck($ck_funcs, L10n::t('POSIX PHP module'), true, true, "");
234 if (function_exists('apache_get_modules')) {
235 if (! in_array('mod_rewrite',apache_get_modules())) {
236 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), false, true, L10n::t('Error: Apache webserver mod-rewrite module is required but not installed.'));
238 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");
242 if (!function_exists('curl_init')) {
243 $ck_funcs[0]['status'] = false;
244 $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');
246 if (!function_exists('imagecreatefromjpeg')) {
247 $ck_funcs[1]['status'] = false;
248 $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');
250 if (!function_exists('openssl_public_encrypt')) {
251 $ck_funcs[2]['status'] = false;
252 $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');
254 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
255 $ck_funcs[3]['status'] = false;
256 $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
258 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
259 $ck_funcs[3]['status'] = false;
260 $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');
262 if (!function_exists('mb_strlen')) {
263 $ck_funcs[4]['status'] = false;
264 $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');
266 if (!function_exists('iconv_strlen')) {
267 $ck_funcs[6]['status'] = false;
268 $ck_funcs[6]['help'] = L10n::t('Error: iconv PHP module required but not installed.');
270 if (!function_exists('posix_kill')) {
271 $ck_funcs[7]['status'] = false;
272 $ck_funcs[7]['help'] = L10n::t('Error: POSIX PHP module required but not installed.');
275 $checks = array_merge($checks, $ck_funcs);
277 // check for XML DOM Documents being able to be generated
279 $xml = new DOMDocument();
280 } catch (Exception $e) {
281 $ck_funcs[5]['status'] = false;
282 $ck_funcs[5]['help'] = L10n::t('Error, XML PHP module required but not installed.');
287 * "config/local.ini.php" - Check
289 * Checks if it's possible to create the "config/local.ini.php"
291 * @param array $checks The list of all checks (by-ref parameter!)
293 public static function checkLocalIni(&$checks)
297 if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||
298 (!file_exists('config/local.ini.php') && !is_writable('.'))) {
301 $help = L10n::t('The web installer needs to be able to create a file called "local.ini.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
302 $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;
303 $help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.ini.php in your Friendica "config" folder.') . EOL;
304 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
307 self::addCheck($checks, L10n::t('config/local.ini.php is writable'), $status, false, $help);
312 * Smarty3 Template Check
314 * Checks, if the directory of Smarty3 is writable
316 * @param array $checks The list of all checks (by-ref parameter!)
318 public static function checkSmarty3(&$checks)
322 if (!is_writable('view/smarty3')) {
325 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
326 $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;
327 $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;
328 $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;
331 self::addCheck($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);
335 * ".htaccess" - Check
337 * Checks, if "url_rewrite" is enabled in the ".htaccess" file
339 * @param array $checks The list of all checks (by-ref parameter!)
341 public static function checkHtAccess(&$checks)
346 if (function_exists('curl_init')) {
347 $test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");
349 $url = normalise_link(System::baseUrl() . "/install/testrewrite");
350 if ($test['body'] != "ok") {
351 $test = Network::fetchUrlFull($url);
354 if ($test['body'] != "ok") {
356 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
358 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
359 $error_msg['url'] = $test['redirect_url'];
360 $error_msg['msg'] = defaults($test, 'error', '');
362 self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
364 // cannot check modrewrite if libcurl is not installed
365 /// @TODO Maybe issue warning here?
372 * Checks, if the imagick module is available
374 * @param array $checks The list of all checks (by-ref parameter!)
376 public static function checkImagick(&$checks)
381 if (class_exists('Imagick')) {
383 $supported = Image::supportedTypes();
384 if (array_key_exists('image/gif', $supported)) {
388 if ($imagick == false) {
389 self::addCheck($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
391 self::addCheck($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
393 self::addCheck($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");
399 * Installs the Database structure
401 * @return string A possible error
403 public static function installDatabaseStructure()
405 $errors = DBStructure::update(false, true, true);