3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
25 use Friendica\BaseModule;
27 use Friendica\Core\Config\Cache;
28 use Friendica\Core\Renderer;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\BasePath;
32 use Friendica\Util\Strings;
33 use Friendica\Util\Temporal;
35 class Install extends BaseModule
38 * Step one - System check
40 const SYSTEM_CHECK = 1;
42 * Step two - Base information
44 const BASE_CONFIG = 2;
46 * Step three - Database configuration
48 const DATABASE_CONFIG = 3;
50 * Step four - Adapt site settings
52 const SITE_SETTINGS = 4;
54 * Step five - All steps finished
59 * @var int The current step of the wizard
61 private static $currentWizardStep;
64 * @var Core\Installer The installer
66 private static $installer;
68 public static function init(array $parameters = [])
72 if (!DI::mode()->isInstall()) {
73 throw new HTTPException\ForbiddenException();
76 // route: install/testrwrite
77 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
78 if (DI::args()->get(1, '') == 'testrewrite') {
79 // Status Code 204 means that it worked without content
80 throw new HTTPException\NoContentException();
83 self::$installer = new Core\Installer();
85 // get basic installation information and save them to the config cache
86 $configCache = $a->getConfigCache();
87 $basePath = new BasePath($a->getBasePath());
88 self::$installer->setUpCache($configCache, $basePath->getPath());
90 // We overwrite current theme css, because during install we may not have a working mod_rewrite
91 // so we may not have a css at all. Here we set a static css file for the install procedure pages
92 Renderer::$theme['stylesheet'] = DI::baseUrl()->get() . '/view/install/style.css';
94 self::$currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
97 public static function post(array $parameters = [])
100 $configCache = $a->getConfigCache();
102 switch (self::$currentWizardStep) {
103 case self::SYSTEM_CHECK:
104 case self::BASE_CONFIG:
105 self::checkSetting($configCache, $_POST, 'config', 'php_path');
108 case self::DATABASE_CONFIG:
109 self::checkSetting($configCache, $_POST, 'config', 'php_path');
111 self::checkSetting($configCache, $_POST, 'config', 'hostname');
112 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
113 self::checkSetting($configCache, $_POST, 'system', 'basepath');
114 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
117 case self::SITE_SETTINGS:
118 self::checkSetting($configCache, $_POST, 'config', 'php_path');
120 self::checkSetting($configCache, $_POST, 'config', 'hostname');
121 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
122 self::checkSetting($configCache, $_POST, 'system', 'basepath');
123 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
125 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
126 self::checkSetting($configCache, $_POST, 'database', 'username', '');
127 self::checkSetting($configCache, $_POST, 'database', 'password', '');
128 self::checkSetting($configCache, $_POST, 'database', 'database', '');
130 // If we cannot connect to the database, return to the previous step
131 if (!self::$installer->checkDB(DI::dba())) {
132 self::$currentWizardStep = self::DATABASE_CONFIG;
138 self::checkSetting($configCache, $_POST, 'config', 'php_path');
140 self::checkSetting($configCache, $_POST, 'config', 'hostname');
141 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
142 self::checkSetting($configCache, $_POST, 'system', 'basepath');
143 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
145 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
146 self::checkSetting($configCache, $_POST, 'database', 'username', '');
147 self::checkSetting($configCache, $_POST, 'database', 'password', '');
148 self::checkSetting($configCache, $_POST, 'database', 'database', '');
150 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
151 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
152 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
154 // If we cannot connect to the database, return to the Database config wizard
155 if (!self::$installer->checkDB(DI::dba())) {
156 self::$currentWizardStep = self::DATABASE_CONFIG;
160 if (!self::$installer->createConfig($configCache)) {
164 self::$installer->installDatabase($configCache->get('system', 'basepath'));
170 public static function content(array $parameters = [])
173 $configCache = $a->getConfigCache();
177 $install_title = DI::l10n()->t('Friendica Communications Server - Setup');
179 switch (self::$currentWizardStep) {
180 case self::SYSTEM_CHECK:
181 $php_path = $configCache->get('config', 'php_path');
183 $status = self::$installer->checkEnvironment(DI::baseUrl()->get(), $php_path);
185 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
186 $output .= Renderer::replaceMacros($tpl, [
187 '$title' => $install_title,
188 '$pass' => DI::l10n()->t('System check'),
189 '$required' => DI::l10n()->t('Required'),
190 '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
191 '$optional_requirement_not_satisfied' => DI::l10n()->t('Optional requirement not satisfied'),
192 '$ok' => DI::l10n()->t('OK'),
193 '$checks' => self::$installer->getChecks(),
194 '$passed' => $status,
195 '$see_install' => DI::l10n()->t('Please see the file "doc/INSTALL.md".'),
196 '$next' => DI::l10n()->t('Next'),
197 '$reload' => DI::l10n()->t('Check again'),
198 '$php_path' => $php_path,
202 case self::BASE_CONFIG:
204 App\BaseURL::SSL_POLICY_NONE => DI::l10n()->t("No SSL policy, links will track page SSL state"),
205 App\BaseURL::SSL_POLICY_FULL => DI::l10n()->t("Force all links to use SSL"),
206 App\BaseURL::SSL_POLICY_SELFSIGN => DI::l10n()->t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
209 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
210 $output .= Renderer::replaceMacros($tpl, [
211 '$title' => $install_title,
212 '$pass' => DI::l10n()->t('Base settings'),
213 '$ssl_policy' => ['system-ssl_policy',
214 DI::l10n()->t("SSL link policy"),
215 $configCache->get('system', 'ssl_policy'),
216 DI::l10n()->t("Determines whether generated links should be forced to use SSL"),
218 '$hostname' => ['config-hostname',
219 DI::l10n()->t('Host name'),
220 $configCache->get('config', 'hostname'),
221 DI::l10n()->t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
222 DI::l10n()->t('Required')],
223 '$basepath' => ['system-basepath',
224 DI::l10n()->t("Base path to installation"),
225 $configCache->get('system', 'basepath'),
226 DI::l10n()->t("If the system cannot detect the correct path to your installation, enter the correct path here. This setting should only be set if you are using a restricted system and symbolic links to your webroot."),
227 DI::l10n()->t('Required')],
228 '$urlpath' => ['system-urlpath',
229 DI::l10n()->t('Sub path of the URL'),
230 $configCache->get('system', 'urlpath'),
231 DI::l10n()->t('Overwrite this field in case the sub path determination isn\'t right, otherwise leave it as is. Leaving this field blank means the installation is at the base URL without sub path.'),
233 '$php_path' => $configCache->get('config', 'php_path'),
234 '$submit' => DI::l10n()->t('Submit'),
238 case self::DATABASE_CONFIG:
239 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
240 $output .= Renderer::replaceMacros($tpl, [
241 '$title' => $install_title,
242 '$pass' => DI::l10n()->t('Database connection'),
243 '$info_01' => DI::l10n()->t('In order to install Friendica we need to know how to connect to your database.'),
244 '$info_02' => DI::l10n()->t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
245 '$info_03' => DI::l10n()->t('The database you specify below should already exist. If it does not, please create it before continuing.'),
246 '$required' => DI::l10n()->t('Required'),
247 '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
248 '$checks' => self::$installer->getChecks(),
249 '$hostname' => $configCache->get('config', 'hostname'),
250 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
251 '$basepath' => $configCache->get('system', 'basepath'),
252 '$urlpath' => $configCache->get('system', 'urlpath'),
253 '$dbhost' => ['database-hostname',
254 DI::l10n()->t('Database Server Name'),
255 $configCache->get('database', 'hostname'),
257 DI::l10n()->t('Required')],
258 '$dbuser' => ['database-username',
259 DI::l10n()->t('Database Login Name'),
260 $configCache->get('database', 'username'),
262 DI::l10n()->t('Required'),
264 '$dbpass' => ['database-password',
265 DI::l10n()->t('Database Login Password'),
266 $configCache->get('database', 'password'),
267 DI::l10n()->t("For security reasons the password must not be empty"),
268 DI::l10n()->t('Required')],
269 '$dbdata' => ['database-database',
270 DI::l10n()->t('Database Name'),
271 $configCache->get('database', 'database'),
273 DI::l10n()->t('Required')],
274 '$lbl_10' => DI::l10n()->t('Please select a default timezone for your website'),
275 '$php_path' => $configCache->get('config', 'php_path'),
276 '$submit' => DI::l10n()->t('Submit')
280 case self::SITE_SETTINGS:
281 /* Installed langs */
282 $lang_choices = DI::l10n()->getAvailableLanguages();
284 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
285 $output .= Renderer::replaceMacros($tpl, [
286 '$title' => $install_title,
287 '$required' => DI::l10n()->t('Required'),
288 '$checks' => self::$installer->getChecks(),
289 '$pass' => DI::l10n()->t('Site settings'),
290 '$hostname' => $configCache->get('config', 'hostname'),
291 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
292 '$basepath' => $configCache->get('system', 'basepath'),
293 '$urlpath' => $configCache->get('system', 'urlpath'),
294 '$dbhost' => $configCache->get('database', 'hostname'),
295 '$dbuser' => $configCache->get('database', 'username'),
296 '$dbpass' => $configCache->get('database', 'password'),
297 '$dbdata' => $configCache->get('database', 'database'),
298 '$adminmail' => ['config-admin_email',
299 DI::l10n()->t('Site administrator email address'),
300 $configCache->get('config', 'admin_email'),
301 DI::l10n()->t('Your account email address must match this in order to use the web admin panel.'),
302 DI::l10n()->t('Required'), 'autofocus', 'email'],
303 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
304 DI::l10n()->t('Please select a default timezone for your website'),
305 $configCache->get('system', 'default_timezone'),
307 '$language' => ['system-language',
308 DI::l10n()->t('System Language:'),
309 $configCache->get('system', 'language'),
310 DI::l10n()->t('Set the default language for your Friendica installation interface and to send emails.'),
312 '$php_path' => $configCache->get('config', 'php_path'),
313 '$submit' => DI::l10n()->t('Submit')
318 $db_return_text = "";
320 if (count(self::$installer->getChecks()) == 0) {
321 $txt = '<p style="font-size: 130%;">';
322 $txt .= DI::l10n()->t('Your Friendica site database has been installed.') . EOL;
323 $db_return_text .= $txt;
326 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
327 $output .= Renderer::replaceMacros($tpl, [
328 '$title' => $install_title,
329 '$required' => DI::l10n()->t('Required'),
330 '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
331 '$checks' => self::$installer->getChecks(),
332 '$pass' => DI::l10n()->t('Installation finished'),
333 '$text' => $db_return_text . self::whatNext(),
343 * Creates the text for the next steps
345 * @return string The text for the next steps
346 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
348 private static function whatNext()
350 $baseurl = DI::baseUrl()->get();
352 DI::l10n()->t('<h1>What next</h1>')
353 . "<p>" . DI::l10n()->t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
354 . DI::l10n()->t('Please see the file "doc/INSTALL.md".')
356 . DI::l10n()->t('Go to your new Friendica node <a href="%s/register">registration page</a> and register as new user. Remember to use the same email you have entered as administrator email. This will allow you to enter the site admin panel.', $baseurl)
361 * Checks the $_POST settings and updates the config Cache for it
363 * @param Cache $configCache The current config cache
364 * @param array $post The $_POST data
365 * @param string $cat The category of the setting
366 * @param string $key The key of the setting
367 * @param null|string $default The default value
369 private static function checkSetting(Cache $configCache, array $post, $cat, $key, $default = null)
371 $configCache->set($cat, $key,
373 trim(($post[sprintf('%s-%s', $cat, $key)] ?? '') ?:
374 ($default ?? $configCache->get($cat, $key))