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\Profiler;
35 use Friendica\Util\Temporal;
36 use Psr\Log\LoggerInterface;
38 class Install extends BaseModule
41 * Step one - System check
43 const SYSTEM_CHECK = 1;
45 * Step two - Base information
47 const BASE_CONFIG = 2;
49 * Step three - Database configuration
51 const DATABASE_CONFIG = 3;
53 * Step four - Adapt site settings
55 const SITE_SETTINGS = 4;
57 * Step five - All steps finished
62 * @var int The current step of the wizard
64 private $currentWizardStep;
67 * @var Core\Installer The installer
76 public function __construct(App $app, App\Mode $mode, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Core\Installer $installer, array $server, array $parameters = [])
78 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
82 $this->installer = $installer;
84 if (!$this->mode->isInstall()) {
85 throw new HTTPException\ForbiddenException();
88 // route: install/testrwrite
89 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
90 if ($args->get(1, '') == 'testrewrite') {
91 // Status Code 204 means that it worked without content
92 throw new HTTPException\NoContentException();
95 // get basic installation information and save them to the config cache
96 $configCache = $this->app->getConfigCache();
97 $basePath = new BasePath($this->app->getBasePath());
98 $this->installer->setUpCache($configCache, $basePath->getPath());
100 // We overwrite current theme css, because during install we may not have a working mod_rewrite
101 // so we may not have a css at all. Here we set a static css file for the install procedure pages
102 Renderer::$theme['stylesheet'] = $this->baseUrl->get() . '/view/install/style.css';
104 $this->currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
107 protected function post(array $request = [], array $post = [])
109 $configCache = $this->app->getConfigCache();
111 switch ($this->currentWizardStep) {
112 case self::SYSTEM_CHECK:
113 case self::BASE_CONFIG:
114 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
117 case self::DATABASE_CONFIG:
118 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
120 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
121 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
122 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
123 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
126 case self::SITE_SETTINGS:
127 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
129 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
130 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
131 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
132 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
134 $this->checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
135 $this->checkSetting($configCache, $_POST, 'database', 'username', '');
136 $this->checkSetting($configCache, $_POST, 'database', 'password', '');
137 $this->checkSetting($configCache, $_POST, 'database', 'database', '');
139 // If we cannot connect to the database, return to the previous step
140 if (!$this->installer->checkDB(DI::dba())) {
141 $this->currentWizardStep = self::DATABASE_CONFIG;
147 $this->checkSetting($configCache, $_POST, 'config', 'php_path');
149 $this->checkSetting($configCache, $_POST, 'config', 'hostname');
150 $this->checkSetting($configCache, $_POST, 'system', 'ssl_policy');
151 $this->checkSetting($configCache, $_POST, 'system', 'basepath');
152 $this->checkSetting($configCache, $_POST, 'system', 'urlpath');
154 $this->checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
155 $this->checkSetting($configCache, $_POST, 'database', 'username', '');
156 $this->checkSetting($configCache, $_POST, 'database', 'password', '');
157 $this->checkSetting($configCache, $_POST, 'database', 'database', '');
159 $this->checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
160 $this->checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
161 $this->checkSetting($configCache, $_POST, 'config', 'admin_email', '');
163 // If we cannot connect to the database, return to the Database config wizard
164 if (!$this->installer->checkDB(DI::dba())) {
165 $this->currentWizardStep = self::DATABASE_CONFIG;
169 if (!$this->installer->createConfig($configCache)) {
173 $this->installer->installDatabase($configCache->get('system', 'basepath'));
175 // install allowed themes to register theme hooks
176 // this is same as "Reload active theme" in /admin/themes
177 $allowed_themes = Theme::getAllowedList();
178 $allowed_themes = array_unique($allowed_themes);
179 foreach ($allowed_themes as $theme) {
180 Theme::uninstall($theme);
181 Theme::install($theme);
183 Theme::setAllowedList($allowed_themes);
189 protected function content(array $request = []): string
191 $configCache = $this->app->getConfigCache();
195 $install_title = $this->t('Friendica Communications Server - Setup');
197 switch ($this->currentWizardStep) {
198 case self::SYSTEM_CHECK:
199 $php_path = $configCache->get('config', 'php_path');
201 $status = $this->installer->checkEnvironment($this->baseUrl->get(), $php_path);
203 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
204 $output .= Renderer::replaceMacros($tpl, [
205 '$title' => $install_title,
206 '$pass' => $this->t('System check'),
207 '$required' => $this->t('Required'),
208 '$requirement_not_satisfied' => $this->t('Requirement not satisfied'),
209 '$optional_requirement_not_satisfied' => $this->t('Optional requirement not satisfied'),
210 '$ok' => $this->t('OK'),
211 '$checks' => $this->installer->getChecks(),
212 '$passed' => $status,
213 '$see_install' => $this->t('Please see the file "doc/INSTALL.md".'),
214 '$next' => $this->t('Next'),
215 '$reload' => $this->t('Check again'),
216 '$php_path' => $php_path,
220 case self::BASE_CONFIG:
222 App\BaseURL::SSL_POLICY_NONE => $this->t("No SSL policy, links will track page SSL state"),
223 App\BaseURL::SSL_POLICY_FULL => $this->t("Force all links to use SSL"),
224 App\BaseURL::SSL_POLICY_SELFSIGN => $this->t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
227 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
228 $output .= Renderer::replaceMacros($tpl, [
229 '$title' => $install_title,
230 '$pass' => $this->t('Base settings'),
231 '$ssl_policy' => ['system-ssl_policy',
232 $this->t("SSL link policy"),
233 $configCache->get('system', 'ssl_policy'),
234 $this->t("Determines whether generated links should be forced to use SSL"),
236 '$hostname' => ['config-hostname',
237 $this->t('Host name'),
238 $configCache->get('config', 'hostname'),
239 $this->t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
240 $this->t('Required')],
241 '$basepath' => ['system-basepath',
242 $this->t("Base path to installation"),
243 $configCache->get('system', 'basepath'),
244 $this->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."),
245 $this->t('Required')],
246 '$urlpath' => ['system-urlpath',
247 $this->t('Sub path of the URL'),
248 $configCache->get('system', 'urlpath'),
249 $this->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.'),
251 '$php_path' => $configCache->get('config', 'php_path'),
252 '$submit' => $this->t('Submit'),
256 case self::DATABASE_CONFIG:
257 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
258 $output .= Renderer::replaceMacros($tpl, [
259 '$title' => $install_title,
260 '$pass' => $this->t('Database connection'),
261 '$info_01' => $this->t('In order to install Friendica we need to know how to connect to your database.'),
262 '$info_02' => $this->t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
263 '$info_03' => $this->t('The database you specify below should already exist. If it does not, please create it before continuing.'),
264 '$required' => $this->t('Required'),
265 '$requirement_not_satisfied' => $this->t('Requirement not satisfied'),
266 '$checks' => $this->installer->getChecks(),
267 '$hostname' => $configCache->get('config', 'hostname'),
268 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
269 '$basepath' => $configCache->get('system', 'basepath'),
270 '$urlpath' => $configCache->get('system', 'urlpath'),
271 '$dbhost' => ['database-hostname',
272 $this->t('Database Server Name'),
273 $configCache->get('database', 'hostname'),
275 $this->t('Required')],
276 '$dbuser' => ['database-username',
277 $this->t('Database Login Name'),
278 $configCache->get('database', 'username'),
280 $this->t('Required'),
282 '$dbpass' => ['database-password',
283 $this->t('Database Login Password'),
284 $configCache->get('database', 'password'),
285 $this->t("For security reasons the password must not be empty"),
286 $this->t('Required')],
287 '$dbdata' => ['database-database',
288 $this->t('Database Name'),
289 $configCache->get('database', 'database'),
291 $this->t('Required')],
292 '$lbl_10' => $this->t('Please select a default timezone for your website'),
293 '$php_path' => $configCache->get('config', 'php_path'),
294 '$submit' => $this->t('Submit')
298 case self::SITE_SETTINGS:
299 /* Installed langs */
300 $lang_choices = $this->l10n->getAvailableLanguages();
302 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
303 $output .= Renderer::replaceMacros($tpl, [
304 '$title' => $install_title,
305 '$required' => $this->t('Required'),
306 '$checks' => $this->installer->getChecks(),
307 '$pass' => $this->t('Site settings'),
308 '$hostname' => $configCache->get('config', 'hostname'),
309 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
310 '$basepath' => $configCache->get('system', 'basepath'),
311 '$urlpath' => $configCache->get('system', 'urlpath'),
312 '$dbhost' => $configCache->get('database', 'hostname'),
313 '$dbuser' => $configCache->get('database', 'username'),
314 '$dbpass' => $configCache->get('database', 'password'),
315 '$dbdata' => $configCache->get('database', 'database'),
316 '$adminmail' => ['config-admin_email',
317 $this->t('Site administrator email address'),
318 $configCache->get('config', 'admin_email'),
319 $this->t('Your account email address must match this in order to use the web admin panel.'),
320 $this->t('Required'), 'autofocus', 'email'],
321 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
322 $this->t('Please select a default timezone for your website'),
323 $configCache->get('system', 'default_timezone'),
325 '$language' => ['system-language',
326 $this->t('System Language:'),
327 $configCache->get('system', 'language'),
328 $this->t('Set the default language for your Friendica installation interface and to send emails.'),
330 '$php_path' => $configCache->get('config', 'php_path'),
331 '$submit' => $this->t('Submit')
336 $db_return_text = "";
338 if (count($this->installer->getChecks()) == 0) {
339 $txt = '<p style="font-size: 130%;">';
340 $txt .= $this->t('Your Friendica site database has been installed.') . EOL;
341 $db_return_text .= $txt;
344 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
345 $output .= Renderer::replaceMacros($tpl, [
346 '$title' => $install_title,
347 '$required' => $this->t('Required'),
348 '$requirement_not_satisfied' => $this->t('Requirement not satisfied'),
349 '$checks' => $this->installer->getChecks(),
350 '$pass' => $this->t('Installation finished'),
351 '$text' => $db_return_text . $this->whatNext(),
361 * Creates the text for the next steps
363 * @return string The text for the next steps
364 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
366 private function whatNext()
368 $baseurl = $this->baseUrl->get();
370 $this->t('<h1>What next</h1>')
371 . "<p>" . $this->t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
372 . $this->t('Please see the file "doc/INSTALL.md".')
374 . $this->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)
379 * Checks the $_POST settings and updates the config Cache for it
381 * @param \Friendica\Core\Config\ValueObject\Cache $configCache The current config cache
382 * @param array $post The $_POST data
383 * @param string $cat The category of the setting
384 * @param string $key The key of the setting
385 * @param null|string $default The default value
387 private function checkSetting(Cache $configCache, array $post, string $cat, string $key, ?string $default = null)
391 if (isset($post[sprintf('%s-%s', $cat, $key)])) {
392 $value = trim($post[sprintf('%s-%s', $cat, $key)]);
396 $configCache->set($cat, $key, $value, Cache::SOURCE_ENV);
400 if (isset($default)) {
401 $configCache->set($cat, $key, $default, Cache::SOURCE_ENV);