3 namespace Friendica\Module;
6 use Friendica\BaseModule;
8 use Friendica\Core\Config\Cache;
9 use Friendica\Core\Renderer;
11 use Friendica\Network\HTTPException;
12 use Friendica\Util\BasePath;
13 use Friendica\Util\Strings;
14 use Friendica\Util\Temporal;
16 class Install extends BaseModule
19 * Step one - System check
21 const SYSTEM_CHECK = 1;
23 * Step two - Base information
25 const BASE_CONFIG = 2;
27 * Step three - Database configuration
29 const DATABASE_CONFIG = 3;
31 * Step four - Adapt site settings
33 const SITE_SETTINGS = 4;
35 * Step five - All steps finished
40 * @var int The current step of the wizard
42 private static $currentWizardStep;
45 * @var Core\Installer The installer
47 private static $installer;
49 public static function init(array $parameters = [])
53 if (!DI::mode()->isInstall()) {
54 throw new HTTPException\ForbiddenException();
57 // route: install/testrwrite
58 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
59 if (DI::args()->get(1, '') == 'testrewrite') {
60 // Status Code 204 means that it worked without content
61 throw new HTTPException\NoContentException();
64 self::$installer = new Core\Installer();
66 // get basic installation information and save them to the config cache
67 $configCache = $a->getConfigCache();
68 $basePath = new BasePath($a->getBasePath());
69 self::$installer->setUpCache($configCache, $basePath->getPath());
71 // We overwrite current theme css, because during install we may not have a working mod_rewrite
72 // so we may not have a css at all. Here we set a static css file for the install procedure pages
73 Renderer::$theme['stylesheet'] = DI::baseUrl()->get() . '/view/install/style.css';
75 self::$currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
78 public static function post(array $parameters = [])
81 $configCache = $a->getConfigCache();
83 switch (self::$currentWizardStep) {
84 case self::SYSTEM_CHECK:
85 case self::BASE_CONFIG:
86 self::checkSetting($configCache, $_POST, 'config', 'php_path');
89 case self::DATABASE_CONFIG:
90 self::checkSetting($configCache, $_POST, 'config', 'php_path');
92 self::checkSetting($configCache, $_POST, 'config', 'hostname');
93 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
94 self::checkSetting($configCache, $_POST, 'system', 'basepath');
95 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
98 case self::SITE_SETTINGS:
99 self::checkSetting($configCache, $_POST, 'config', 'php_path');
101 self::checkSetting($configCache, $_POST, 'config', 'hostname');
102 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
103 self::checkSetting($configCache, $_POST, 'system', 'basepath');
104 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
106 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
107 self::checkSetting($configCache, $_POST, 'database', 'username', '');
108 self::checkSetting($configCache, $_POST, 'database', 'password', '');
109 self::checkSetting($configCache, $_POST, 'database', 'database', '');
111 // If we cannot connect to the database, return to the previous step
112 if (!self::$installer->checkDB(DI::dba())) {
113 self::$currentWizardStep = self::DATABASE_CONFIG;
119 self::checkSetting($configCache, $_POST, 'config', 'php_path');
121 self::checkSetting($configCache, $_POST, 'config', 'hostname');
122 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
123 self::checkSetting($configCache, $_POST, 'system', 'basepath');
124 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
126 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
127 self::checkSetting($configCache, $_POST, 'database', 'username', '');
128 self::checkSetting($configCache, $_POST, 'database', 'password', '');
129 self::checkSetting($configCache, $_POST, 'database', 'database', '');
131 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
132 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
133 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
135 // If we cannot connect to the database, return to the Database config wizard
136 if (!self::$installer->checkDB(DI::dba())) {
137 self::$currentWizardStep = self::DATABASE_CONFIG;
141 if (!self::$installer->createConfig($configCache)) {
145 self::$installer->installDatabase($configCache->get('system', 'basepath'));
151 public static function content(array $parameters = [])
154 $configCache = $a->getConfigCache();
158 $install_title = DI::l10n()->t('Friendica Communications Server - Setup');
160 switch (self::$currentWizardStep) {
161 case self::SYSTEM_CHECK:
162 $php_path = $configCache->get('config', 'php_path');
164 $status = self::$installer->checkEnvironment(DI::baseUrl()->get(), $php_path);
166 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
167 $output .= Renderer::replaceMacros($tpl, [
168 '$title' => $install_title,
169 '$pass' => DI::l10n()->t('System check'),
170 '$checks' => self::$installer->getChecks(),
171 '$passed' => $status,
172 '$see_install' => DI::l10n()->t('Please see the file "INSTALL.txt".'),
173 '$next' => DI::l10n()->t('Next'),
174 '$reload' => DI::l10n()->t('Check again'),
175 '$php_path' => $php_path,
179 case self::BASE_CONFIG:
181 App\BaseURL::SSL_POLICY_NONE => DI::l10n()->t("No SSL policy, links will track page SSL state"),
182 App\BaseURL::SSL_POLICY_FULL => DI::l10n()->t("Force all links to use SSL"),
183 App\BaseURL::SSL_POLICY_SELFSIGN => DI::l10n()->t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
186 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
187 $output .= Renderer::replaceMacros($tpl, [
188 '$title' => $install_title,
189 '$pass' => DI::l10n()->t('Base settings'),
190 '$ssl_policy' => ['system-ssl_policy',
191 DI::l10n()->t("SSL link policy"),
192 $configCache->get('system', 'ssl_policy'),
193 DI::l10n()->t("Determines whether generated links should be forced to use SSL"),
195 '$hostname' => ['config-hostname',
196 DI::l10n()->t('Host name'),
197 $configCache->get('config', 'hostname'),
198 DI::l10n()->t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
200 '$basepath' => ['system-basepath',
201 DI::l10n()->t("Base path to installation"),
202 $configCache->get('system', 'basepath'),
203 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."),
205 '$urlpath' => ['system-urlpath',
206 DI::l10n()->t('Sub path of the URL'),
207 $configCache->get('system', 'urlpath'),
208 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.'),
210 '$php_path' => $configCache->get('config', 'php_path'),
211 '$submit' => DI::l10n()->t('Submit'),
215 case self::DATABASE_CONFIG:
216 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
217 $output .= Renderer::replaceMacros($tpl, [
218 '$title' => $install_title,
219 '$pass' => DI::l10n()->t('Database connection'),
220 '$info_01' => DI::l10n()->t('In order to install Friendica we need to know how to connect to your database.'),
221 '$info_02' => DI::l10n()->t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
222 '$info_03' => DI::l10n()->t('The database you specify below should already exist. If it does not, please create it before continuing.'),
223 'checks' => self::$installer->getChecks(),
224 '$hostname' => $configCache->get('config', 'hostname'),
225 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
226 '$basepath' => $configCache->get('system', 'basepath'),
227 '$urlpath' => $configCache->get('system', 'urlpath'),
228 '$dbhost' => ['database-hostname',
229 DI::l10n()->t('Database Server Name'),
230 $configCache->get('database', 'hostname'),
233 '$dbuser' => ['database-username',
234 DI::l10n()->t('Database Login Name'),
235 $configCache->get('database', 'username'),
239 '$dbpass' => ['database-password',
240 DI::l10n()->t('Database Login Password'),
241 $configCache->get('database', 'password'),
242 DI::l10n()->t("For security reasons the password must not be empty"),
244 '$dbdata' => ['database-database',
245 DI::l10n()->t('Database Name'),
246 $configCache->get('database', 'database'),
249 '$lbl_10' => DI::l10n()->t('Please select a default timezone for your website'),
250 '$php_path' => $configCache->get('config', 'php_path'),
251 '$submit' => DI::l10n()->t('Submit')
255 case self::SITE_SETTINGS:
256 /* Installed langs */
257 $lang_choices = DI::l10n()->getAvailableLanguages();
259 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
260 $output .= Renderer::replaceMacros($tpl, [
261 '$title' => $install_title,
262 '$checks' => self::$installer->getChecks(),
263 '$pass' => DI::l10n()->t('Site settings'),
264 '$hostname' => $configCache->get('config', 'hostname'),
265 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
266 '$basepath' => $configCache->get('system', 'basepath'),
267 '$urlpath' => $configCache->get('system', 'urlpath'),
268 '$dbhost' => $configCache->get('database', 'hostname'),
269 '$dbuser' => $configCache->get('database', 'username'),
270 '$dbpass' => $configCache->get('database', 'password'),
271 '$dbdata' => $configCache->get('database', 'database'),
272 '$adminmail' => ['config-admin_email',
273 DI::l10n()->t('Site administrator email address'),
274 $configCache->get('config', 'admin_email'),
275 DI::l10n()->t('Your account email address must match this in order to use the web admin panel.'),
276 'required', 'autofocus', 'email'],
277 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
278 DI::l10n()->t('Please select a default timezone for your website'),
279 $configCache->get('system', 'default_timezone'),
281 '$language' => ['system-language',
282 DI::l10n()->t('System Language:'),
283 $configCache->get('system', 'language'),
284 DI::l10n()->t('Set the default language for your Friendica installation interface and to send emails.'),
286 '$php_path' => $configCache->get('config', 'php_path'),
287 '$submit' => DI::l10n()->t('Submit')
292 $db_return_text = "";
294 if (count(self::$installer->getChecks()) == 0) {
295 $txt = '<p style="font-size: 130%;">';
296 $txt .= DI::l10n()->t('Your Friendica site database has been installed.') . EOL;
297 $db_return_text .= $txt;
300 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
301 $output .= Renderer::replaceMacros($tpl, [
302 '$title' => $install_title,
303 '$checks' => self::$installer->getChecks(),
304 '$pass' => DI::l10n()->t('Installation finished'),
305 '$text' => $db_return_text . self::whatNext(),
315 * Creates the text for the next steps
317 * @return string The text for the next steps
318 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
320 private static function whatNext()
322 $baseurl = DI::baseUrl()->get();
324 DI::l10n()->t('<h1>What next</h1>')
325 . "<p>" . DI::l10n()->t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
326 . DI::l10n()->t('Please see the file "INSTALL.txt".')
328 . 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)
333 * Checks the $_POST settings and updates the config Cache for it
335 * @param Cache $configCache The current config cache
336 * @param array $post The $_POST data
337 * @param string $cat The category of the setting
338 * @param string $key The key of the setting
339 * @param null|string $default The default value
341 private static function checkSetting(Cache $configCache, array $post, $cat, $key, $default = null)
343 $configCache->set($cat, $key,
345 trim(($post[sprintf('%s-%s', $cat, $key)] ?? '') ?:
346 ($default ?? $configCache->get($cat, $key))