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 $app = self::getApp();
89 $result = file_put_contents($app->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php', $txt);
91 $app->data['txt'] = $txt;
96 * Adds new checks to the array $checks
98 * @param array $checks The list of all checks (by-ref parameter!)
99 * @param string $title The title of the current check
100 * @param bool $status 1 = check passed, 0 = check not passed
101 * @param bool $required 1 = check is mandatory, 0 = check is optional
102 * @param string $help A help-string for the current check
103 * @param string $error_msg Optional. A error message, if the current check failed
105 private static function addCheck(&$checks, $title, $status, $required, $help, $error_msg = "")
110 'required' => $required,
112 'error_msg' => $error_msg,
119 * Checks the PHP environment.
121 * - Checks if a PHP binary is available
122 * - Checks if it is the CLI version
123 * - Checks if "register_argc_argv" is enabled
125 * @param string $phpath Optional. The Path to the PHP-Binary
126 * @param array $checks The list of all checks (by-ref parameter!)
128 public static function checkPHP($phpath, &$checks)
130 $passed = $passed2 = $passed3 = false;
131 if (strlen($phpath)) {
132 $passed = file_exists($phpath);
134 $phpath = trim(shell_exec('which php'));
135 $passed = strlen($phpath);
139 $help .= L10n::t('Could not find a command line version of PHP in the web server PATH.') . EOL;
140 $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;
142 $tpl = get_markup_template('field_input.tpl');
143 $help .= replace_macros($tpl, [
144 '$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.')],
149 self::addCheck($checks, L10n::t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
153 $result = trim(shell_exec($cmd));
154 $passed2 = (strpos($result, "(cli)") !== false);
155 list($result) = explode("\n", $result);
158 $help .= L10n::t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
159 $help .= L10n::t('Found PHP version: ') . "<tt>$result</tt>";
161 self::addCheck($checks, L10n::t('PHP cli binary'), $passed2, true, $help);
166 $cmd = "$phpath testargs.php $str";
167 $result = trim(shell_exec($cmd));
168 $passed3 = $result == $str;
171 $help .= L10n::t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
172 $help .= L10n::t('This is required for message delivery to work.');
174 self::addCheck($checks, L10n::t('PHP register_argc_argv'), $passed3, true, $help);
181 * Checks the OpenSSL Environment
183 * - Checks, if the command "openssl_pkey_new" is available
185 * @param array $checks The list of all checks (by-ref parameter!)
187 public static function checkKeys(&$checks)
192 if (function_exists('openssl_pkey_new')) {
193 $res = openssl_pkey_new([
194 'digest_alg' => 'sha1',
195 'private_key_bits' => 4096,
196 'encrypt_key' => false
202 $help .= L10n::t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
203 $help .= L10n::t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
205 self::addCheck($checks, L10n::t('Generate encryption keys'), $res, true, $help);
209 * PHP functions Check
211 * Checks the following PHP functions
221 * @param array $checks The list of all checks (by-ref parameter!)
223 public static function checkFunctions(&$checks)
226 self::addCheck($ck_funcs, L10n::t('libCurl PHP module'), true, true, "");
227 self::addCheck($ck_funcs, L10n::t('GD graphics PHP module'), true, true, "");
228 self::addCheck($ck_funcs, L10n::t('OpenSSL PHP module'), true, true, "");
229 self::addCheck($ck_funcs, L10n::t('PDO or MySQLi PHP module'), true, true, "");
230 self::addCheck($ck_funcs, L10n::t('mb_string PHP module'), true, true, "");
231 self::addCheck($ck_funcs, L10n::t('XML PHP module'), true, true, "");
232 self::addCheck($ck_funcs, L10n::t('iconv PHP module'), true, true, "");
233 self::addCheck($ck_funcs, L10n::t('POSIX PHP module'), true, true, "");
235 if (function_exists('apache_get_modules')) {
236 if (! in_array('mod_rewrite',apache_get_modules())) {
237 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.'));
239 self::addCheck($ck_funcs, L10n::t('Apache mod_rewrite module'), true, true, "");
243 if (!function_exists('curl_init')) {
244 $ck_funcs[0]['status'] = false;
245 $ck_funcs[0]['help'] = L10n::t('Error: libCURL PHP module required but not installed.');
247 if (!function_exists('imagecreatefromjpeg')) {
248 $ck_funcs[1]['status'] = false;
249 $ck_funcs[1]['help'] = L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.');
251 if (!function_exists('openssl_public_encrypt')) {
252 $ck_funcs[2]['status'] = false;
253 $ck_funcs[2]['help'] = L10n::t('Error: openssl PHP module required but not installed.');
255 if (!function_exists('mysqli_connect') && !class_exists('pdo')) {
256 $ck_funcs[3]['status'] = false;
257 $ck_funcs[3]['help'] = L10n::t('Error: PDO or MySQLi PHP module required but not installed.');
259 if (!function_exists('mysqli_connect') && class_exists('pdo') && !in_array('mysql', \PDO::getAvailableDrivers())) {
260 $ck_funcs[3]['status'] = false;
261 $ck_funcs[3]['help'] = L10n::t('Error: The MySQL driver for PDO is not installed.');
263 if (!function_exists('mb_strlen')) {
264 $ck_funcs[4]['status'] = false;
265 $ck_funcs[4]['help'] = L10n::t('Error: mb_string PHP module required but not installed.');
267 if (!function_exists('iconv_strlen')) {
268 $ck_funcs[6]['status'] = false;
269 $ck_funcs[6]['help'] = L10n::t('Error: iconv PHP module required but not installed.');
271 if (!function_exists('posix_kill')) {
272 $ck_funcs[7]['status'] = false;
273 $ck_funcs[7]['help'] = L10n::t('Error: POSIX PHP module required but not installed.');
276 $checks = array_merge($checks, $ck_funcs);
278 // check for XML DOM Documents being able to be generated
280 $xml = new DOMDocument();
281 } catch (Exception $e) {
282 $ck_funcs[5]['status'] = false;
283 $ck_funcs[5]['help'] = L10n::t('Error, XML PHP module required but not installed.');
288 * "config/local.ini.php" - Check
290 * Checks if it's possible to create the "config/local.ini.php"
292 * @param array $checks The list of all checks (by-ref parameter!)
294 public static function checkLocalIni(&$checks)
298 if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||
299 (!file_exists('config/local.ini.php') && !is_writable('.'))) {
302 $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;
303 $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;
304 $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;
305 $help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
308 self::addCheck($checks, L10n::t('config/local.ini.php is writable'), $status, false, $help);
313 * Smarty3 Template Check
315 * Checks, if the directory of Smarty3 is writable
317 * @param array $checks The list of all checks (by-ref parameter!)
319 public static function checkSmarty3(&$checks)
323 if (!is_writable('view/smarty3')) {
326 $help = L10n::t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') . EOL;
327 $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;
328 $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;
329 $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;
332 self::addCheck($checks, L10n::t('view/smarty3 is writable'), $status, true, $help);
336 * ".htaccess" - Check
338 * Checks, if "url_rewrite" is enabled in the ".htaccess" file
340 * @param array $checks The list of all checks (by-ref parameter!)
342 public static function checkHtAccess(&$checks)
347 if (function_exists('curl_init')) {
348 $test = Network::fetchUrlFull(System::baseUrl() . "/install/testrewrite");
350 $url = normalise_link(System::baseUrl() . "/install/testrewrite");
351 if ($test['body'] != "ok") {
352 $test = Network::fetchUrlFull($url);
355 if ($test['body'] != "ok") {
357 $help = L10n::t('Url rewrite in .htaccess is not working. Check your server configuration.');
359 $error_msg['head'] = L10n::t('Error message from Curl when fetching');
360 $error_msg['url'] = $test['redirect_url'];
361 $error_msg['msg'] = defaults($test, 'error', '');
363 self::addCheck($checks, L10n::t('Url rewrite is working'), $status, true, $help, $error_msg);
365 // cannot check modrewrite if libcurl is not installed
366 /// @TODO Maybe issue warning here?
373 * Checks, if the imagick module is available
375 * @param array $checks The list of all checks (by-ref parameter!)
377 public static function checkImagick(&$checks)
382 if (class_exists('Imagick')) {
384 $supported = Image::supportedTypes();
385 if (array_key_exists('image/gif', $supported)) {
389 if ($imagick == false) {
390 self::addCheck($checks, L10n::t('ImageMagick PHP extension is not installed'), $imagick, false, "");
392 self::addCheck($checks, L10n::t('ImageMagick PHP extension is installed'), $imagick, false, "");
394 self::addCheck($checks, L10n::t('ImageMagick supports GIF'), $gif, false, "");
400 * Installs the Database structure
402 * @return string A possible error
404 public static function installDatabaseStructure()
406 $errors = DBStructure::update(false, true, true);