3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\ValueObject\Cache;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Theme;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\BasePath;
34 use Friendica\Util\Temporal;
36 class Install extends BaseModule
39 * Step one - System check
41 const SYSTEM_CHECK = 1;
43 * Step two - Base information
45 const BASE_CONFIG = 2;
47 * Step three - Database configuration
49 const DATABASE_CONFIG = 3;
51 * Step four - Adapt site settings
53 const SITE_SETTINGS = 4;
55 * Step five - All steps finished
60 * @var int The current step of the wizard
62 private $currentWizardStep;
65 * @var Core\Installer The installer
73 /** @var App\BaseURL */
76 public function __construct(App $app, App\Mode $mode, App\BaseURL $baseUrl, App\Arguments $args, Core\Installer $installer, L10n $l10n, array $parameters = [])
78 parent::__construct($l10n, $parameters);
82 $this->baseUrl = $baseUrl;
83 $this->installer = $installer;
85 if (!$this->mode->isInstall()) {
86 throw new HTTPException\ForbiddenException();
89 // route: install/testrwrite
90 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
91 if ($args->get(1, '') == 'testrewrite') {
92 // Status Code 204 means that it worked without content
93 throw new HTTPException\NoContentException();
96 // get basic installation information and save them to the config cache
97 $configCache = $this->app->getConfigCache();
98 $basePath = new BasePath($this->app->getBasePath());
99 $this->installer->setUpCache($configCache, $basePath->getPath());
101 // We overwrite current theme css, because during install we may not have a working mod_rewrite
102 // so we may not have a css at all. Here we set a static css file for the install procedure pages
103 Renderer::$theme['stylesheet'] = $this->baseUrl->get() . '/view/install/style.css';
105 $this->currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
108 public function post()
110 $configCache = $this->app->getConfigCache();
112 switch ($this->currentWizardStep) {
113 case self::SYSTEM_CHECK:
114 case self::BASE_CONFIG:
115 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
118 case self::DATABASE_CONFIG:
119 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
121 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
122 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
123 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
124 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
127 case self::SITE_SETTINGS:
128 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
130 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
131 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
132 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
133 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
135 $this->checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
136 $this->checkSetting($configCache, $_POST, 'database', 'username', '');
137 $this->checkSetting($configCache, $_POST, 'database', 'password', '');
138 $this->checkSetting($configCache, $_POST, 'database', 'database', '');
140 // If we cannot connect to the database, return to the previous step
141 if (!$this->installer->checkDB(DI::dba())) {
142 $this->currentWizardStep = self::DATABASE_CONFIG;
148 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
150 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
151 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
152 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
153 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
155 $this->checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
156 $this->checkSetting($configCache, $_POST, 'database', 'username', '');
157 $this->checkSetting($configCache, $_POST, 'database', 'password', '');
158 $this->checkSetting($configCache, $_POST, 'database', 'database', '');
160 $this->checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
161 $this->checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
162 $this->checkSetting($configCache, $_POST, 'config', 'admin_email', '');
164 // If we cannot connect to the database, return to the Database config wizard
165 if (!$this->installer->checkDB(DI::dba())) {
166 $this->currentWizardStep = self::DATABASE_CONFIG;
170 if (!$this->installer->createConfig($configCache)) {
174 $this->installer->installDatabase($configCache->get('system', 'basepath'));
176 // install allowed themes to register theme hooks
177 // this is same as "Reload active theme" in /admin/themes
178 $allowed_themes = Theme::getAllowedList();
179 $allowed_themes = array_unique($allowed_themes);
180 foreach ($allowed_themes as $theme) {
181 Theme::uninstall($theme);
182 Theme::install($theme);
184 Theme::setAllowedList($allowed_themes);
190 public function content(): string
192 $configCache = $this->app->getConfigCache();
196 $install_title = $this->l10n->t('Friendica Communications Server - Setup');
198 switch ($this->currentWizardStep) {
199 case self::SYSTEM_CHECK:
200 $php_path = $configCache->get('config', 'php_path');
202 $status = $this->installer->checkEnvironment($this->baseUrl->get(), $php_path);
204 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
205 $output .= Renderer::replaceMacros($tpl, [
206 '$title' => $install_title,
207 '$pass' => $this->l10n->t('System check'),
208 '$required' => $this->l10n->t('Required'),
209 '$requirement_not_satisfied' => $this->l10n->t('Requirement not satisfied'),
210 '$optional_requirement_not_satisfied' => $this->l10n->t('Optional requirement not satisfied'),
211 '$ok' => $this->l10n->t('OK'),
212 '$checks' => $this->installer->getChecks(),
213 '$passed' => $status,
214 '$see_install' => $this->l10n->t('Please see the file "doc/INSTALL.md".'),
215 '$next' => $this->l10n->t('Next'),
216 '$reload' => $this->l10n->t('Check again'),
217 '$php_path' => $php_path,
221 case self::BASE_CONFIG:
223 App\BaseURL::SSL_POLICY_NONE => $this->l10n->t("No SSL policy, links will track page SSL state"),
224 App\BaseURL::SSL_POLICY_FULL => $this->l10n->t("Force all links to use SSL"),
225 App\BaseURL::SSL_POLICY_SELFSIGN => $this->l10n->t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
228 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
229 $output .= Renderer::replaceMacros($tpl, [
230 '$title' => $install_title,
231 '$pass' => $this->l10n->t('Base settings'),
232 '$ssl_policy' => ['system-ssl_policy',
233 $this->l10n->t("SSL link policy"),
234 $configCache->get('system', 'ssl_policy'),
235 $this->l10n->t("Determines whether generated links should be forced to use SSL"),
237 '$hostname' => ['config-hostname',
238 $this->l10n->t('Host name'),
239 $configCache->get('config', 'hostname'),
240 $this->l10n->t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
241 $this->l10n->t('Required')],
242 '$basepath' => ['system-basepath',
243 $this->l10n->t("Base path to installation"),
244 $configCache->get('system', 'basepath'),
245 $this->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."),
246 $this->l10n->t('Required')],
247 '$urlpath' => ['system-urlpath',
248 $this->l10n->t('Sub path of the URL'),
249 $configCache->get('system', 'urlpath'),
250 $this->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.'),
252 '$php_path' => $configCache->get('config', 'php_path'),
253 '$submit' => $this->l10n->t('Submit'),
257 case self::DATABASE_CONFIG:
258 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
259 $output .= Renderer::replaceMacros($tpl, [
260 '$title' => $install_title,
261 '$pass' => $this->l10n->t('Database connection'),
262 '$info_01' => $this->l10n->t('In order to install Friendica we need to know how to connect to your database.'),
263 '$info_02' => $this->l10n->t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
264 '$info_03' => $this->l10n->t('The database you specify below should already exist. If it does not, please create it before continuing.'),
265 '$required' => $this->l10n->t('Required'),
266 '$requirement_not_satisfied' => $this->l10n->t('Requirement not satisfied'),
267 '$checks' => $this->installer->getChecks(),
268 '$hostname' => $configCache->get('config', 'hostname'),
269 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
270 '$basepath' => $configCache->get('system', 'basepath'),
271 '$urlpath' => $configCache->get('system', 'urlpath'),
272 '$dbhost' => ['database-hostname',
273 $this->l10n->t('Database Server Name'),
274 $configCache->get('database', 'hostname'),
276 $this->l10n->t('Required')],
277 '$dbuser' => ['database-username',
278 $this->l10n->t('Database Login Name'),
279 $configCache->get('database', 'username'),
281 $this->l10n->t('Required'),
283 '$dbpass' => ['database-password',
284 $this->l10n->t('Database Login Password'),
285 $configCache->get('database', 'password'),
286 $this->l10n->t("For security reasons the password must not be empty"),
287 $this->l10n->t('Required')],
288 '$dbdata' => ['database-database',
289 $this->l10n->t('Database Name'),
290 $configCache->get('database', 'database'),
292 $this->l10n->t('Required')],
293 '$lbl_10' => $this->l10n->t('Please select a default timezone for your website'),
294 '$php_path' => $configCache->get('config', 'php_path'),
295 '$submit' => $this->l10n->t('Submit')
299 case self::SITE_SETTINGS:
300 /* Installed langs */
301 $lang_choices = $this->l10n->getAvailableLanguages();
303 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
304 $output .= Renderer::replaceMacros($tpl, [
305 '$title' => $install_title,
306 '$required' => $this->l10n->t('Required'),
307 '$checks' => $this->installer->getChecks(),
308 '$pass' => $this->l10n->t('Site settings'),
309 '$hostname' => $configCache->get('config', 'hostname'),
310 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
311 '$basepath' => $configCache->get('system', 'basepath'),
312 '$urlpath' => $configCache->get('system', 'urlpath'),
313 '$dbhost' => $configCache->get('database', 'hostname'),
314 '$dbuser' => $configCache->get('database', 'username'),
315 '$dbpass' => $configCache->get('database', 'password'),
316 '$dbdata' => $configCache->get('database', 'database'),
317 '$adminmail' => ['config-admin_email',
318 $this->l10n->t('Site administrator email address'),
319 $configCache->get('config', 'admin_email'),
320 $this->l10n->t('Your account email address must match this in order to use the web admin panel.'),
321 $this->l10n->t('Required'), 'autofocus', 'email'],
322 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
323 $this->l10n->t('Please select a default timezone for your website'),
324 $configCache->get('system', 'default_timezone'),
326 '$language' => ['system-language',
327 $this->l10n->t('System Language:'),
328 $configCache->get('system', 'language'),
329 $this->l10n->t('Set the default language for your Friendica installation interface and to send emails.'),
331 '$php_path' => $configCache->get('config', 'php_path'),
332 '$submit' => $this->l10n->t('Submit')
337 $db_return_text = "";
339 if (count($this->installer->getChecks()) == 0) {
340 $txt = '<p style="font-size: 130%;">';
341 $txt .= $this->l10n->t('Your Friendica site database has been installed.') . EOL;
342 $db_return_text .= $txt;
345 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
346 $output .= Renderer::replaceMacros($tpl, [
347 '$title' => $install_title,
348 '$required' => $this->l10n->t('Required'),
349 '$requirement_not_satisfied' => $this->l10n->t('Requirement not satisfied'),
350 '$checks' => $this->installer->getChecks(),
351 '$pass' => $this->l10n->t('Installation finished'),
352 '$text' => $db_return_text . $this->whatNext(),
362 * Creates the text for the next steps
364 * @return string The text for the next steps
365 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
367 private function whatNext()
369 $baseurl = $this->baseUrl->get();
371 $this->l10n->t('<h1>What next</h1>')
372 . "<p>" . $this->l10n->t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
373 . $this->l10n->t('Please see the file "doc/INSTALL.md".')
375 . $this->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)
380 * Checks the $_POST settings and updates the config Cache for it
382 * @param \Friendica\Core\Config\ValueObject\Cache $configCache The current config cache
383 * @param array $post The $_POST data
384 * @param string $cat The category of the setting
385 * @param string $key The key of the setting
386 * @param null|string $default The default value
388 private function checkSetting(Cache $configCache, array $post, $cat, $key, $default = null)
390 $configCache->set($cat, $key,
391 trim(($post[sprintf('%s-%s', $cat, $key)] ?? '') ?:
392 ($default ?? $configCache->get($cat, $key))