3 namespace Friendica\Module;
6 use Friendica\BaseModule;
8 use Friendica\Core\Config\Cache\ConfigCache;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Network\HTTPException;
12 use Friendica\Util\BasePath;
13 use Friendica\Util\BaseURL;
14 use Friendica\Util\Strings;
15 use Friendica\Util\Temporal;
17 class Install extends BaseModule
20 * Step one - System check
22 const SYSTEM_CHECK = 1;
24 * Step two - Base information
26 const BASE_CONFIG = 2;
28 * Step three - Database configuration
30 const DATABASE_CONFIG = 3;
32 * Step four - Adapt site settings
34 const SITE_SETTINGS = 4;
36 * Step five - All steps finished
41 * @var int The current step of the wizard
43 private static $currentWizardStep;
46 * @var Core\Installer The installer
48 private static $installer;
50 public static function init()
54 if (!$a->getMode()->isInstall()) {
55 throw new HTTPException\ForbiddenException();
58 // route: install/testrwrite
59 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
60 // @TODO: Replace with parameter from router
61 if ($a->getArgumentValue(1, '') == 'testrewrite') {
62 // Status Code 204 means that it worked without content
63 throw new HTTPException\NoContentException();
66 self::$installer = new Core\Installer();
68 // get basic installation information and save them to the config cache
69 $configCache = $a->getConfigCache();
70 $basePath = new BasePath($a->getBasePath());
71 self::$installer->setUpCache($configCache, $basePath->getPath());
73 // We overwrite current theme css, because during install we may not have a working mod_rewrite
74 // so we may not have a css at all. Here we set a static css file for the install procedure pages
75 Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
77 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
80 public static function post()
83 $configCache = $a->getConfigCache();
85 switch (self::$currentWizardStep) {
86 case self::SYSTEM_CHECK:
87 case self::BASE_CONFIG:
88 self::checkSetting($configCache, $_POST, 'config', 'php_path');
91 case self::DATABASE_CONFIG:
92 self::checkSetting($configCache, $_POST, 'config', 'php_path');
94 self::checkSetting($configCache, $_POST, 'config', 'hostname');
95 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
96 self::checkSetting($configCache, $_POST, 'system', 'basepath');
97 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
100 case self::SITE_SETTINGS:
101 self::checkSetting($configCache, $_POST, 'config', 'php_path');
103 self::checkSetting($configCache, $_POST, 'config', 'hostname');
104 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
105 self::checkSetting($configCache, $_POST, 'system', 'basepath');
106 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
108 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
109 self::checkSetting($configCache, $_POST, 'database', 'username', '');
110 self::checkSetting($configCache, $_POST, 'database', 'password', '');
111 self::checkSetting($configCache, $_POST, 'database', 'database', '');
113 // If we cannot connect to the database, return to the previous step
114 if (!self::$installer->checkDB($a->getDBA())) {
115 self::$currentWizardStep = self::DATABASE_CONFIG;
121 self::checkSetting($configCache, $_POST, 'config', 'php_path');
123 self::checkSetting($configCache, $_POST, 'config', 'hostname');
124 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
125 self::checkSetting($configCache, $_POST, 'system', 'basepath');
126 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
128 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
129 self::checkSetting($configCache, $_POST, 'database', 'username', '');
130 self::checkSetting($configCache, $_POST, 'database', 'password', '');
131 self::checkSetting($configCache, $_POST, 'database', 'database', '');
133 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
134 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
135 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
137 // If we cannot connect to the database, return to the Database config wizard
138 if (!self::$installer->checkDB($a->getDBA())) {
139 self::$currentWizardStep = self::DATABASE_CONFIG;
143 if (!self::$installer->createConfig($configCache)) {
147 self::$installer->installDatabase($configCache->get('system', 'basepath'));
153 public static function content()
156 $configCache = $a->getConfigCache();
160 $install_title = L10n::t('Friendica Communications Server - Setup');
162 switch (self::$currentWizardStep) {
163 case self::SYSTEM_CHECK:
164 $php_path = $configCache->get('config', 'php_path');
166 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
168 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
169 $output .= Renderer::replaceMacros($tpl, [
170 '$title' => $install_title,
171 '$pass' => L10n::t('System check'),
172 '$checks' => self::$installer->getChecks(),
173 '$passed' => $status,
174 '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
175 '$next' => L10n::t('Next'),
176 '$reload' => L10n::t('Check again'),
177 '$php_path' => $php_path,
181 case self::BASE_CONFIG:
183 BaseURL::SSL_POLICY_NONE => L10n::t("No SSL policy, links will track page SSL state"),
184 BaseURL::SSL_POLICY_FULL => L10n::t("Force all links to use SSL"),
185 BaseURL::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
188 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
189 $output .= Renderer::replaceMacros($tpl, [
190 '$title' => $install_title,
191 '$pass' => L10n::t('Base settings'),
192 '$ssl_policy' => ['system-ssl_policy',
193 L10n::t("SSL link policy"),
194 $configCache->get('system', 'ssl_policy'),
195 L10n::t("Determines whether generated links should be forced to use SSL"),
197 '$hostname' => ['config-hostname',
198 L10n::t('Host name'),
199 $configCache->get('config', 'hostname'),
200 L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
202 '$basepath' => ['system-basepath',
203 L10n::t("Base path to installation"),
204 $configCache->get('system', 'basepath'),
205 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."),
207 '$urlpath' => ['system-urlpath',
208 L10n::t('Sub path of the URL'),
209 $configCache->get('system', 'urlpath'),
210 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.'),
212 '$php_path' => $configCache->get('config', 'php_path'),
213 '$submit' => L10n::t('Submit'),
217 case self::DATABASE_CONFIG:
218 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
219 $output .= Renderer::replaceMacros($tpl, [
220 '$title' => $install_title,
221 '$pass' => L10n::t('Database connection'),
222 '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
223 '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
224 '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
225 'checks' => self::$installer->getChecks(),
226 '$hostname' => $configCache->get('config', 'hostname'),
227 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
228 '$basepath' => $configCache->get('system', 'basepath'),
229 '$urlpath' => $configCache->get('system', 'urlpath'),
230 '$dbhost' => ['database-hostname',
231 L10n::t('Database Server Name'),
232 $configCache->get('database', 'hostname'),
235 '$dbuser' => ['database-username',
236 L10n::t('Database Login Name'),
237 $configCache->get('database', 'username'),
241 '$dbpass' => ['database-password',
242 L10n::t('Database Login Password'),
243 $configCache->get('database', 'password'),
244 L10n::t("For security reasons the password must not be empty"),
246 '$dbdata' => ['database-database',
247 L10n::t('Database Name'),
248 $configCache->get('database', 'database'),
251 '$lbl_10' => L10n::t('Please select a default timezone for your website'),
252 '$php_path' => $configCache->get('config', 'php_path'),
253 '$submit' => L10n::t('Submit')
257 case self::SITE_SETTINGS:
258 /* Installed langs */
259 $lang_choices = L10n::getAvailableLanguages();
261 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
262 $output .= Renderer::replaceMacros($tpl, [
263 '$title' => $install_title,
264 '$checks' => self::$installer->getChecks(),
265 '$pass' => L10n::t('Site settings'),
266 '$hostname' => $configCache->get('config', 'hostname'),
267 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
268 '$basepath' => $configCache->get('system', 'basepath'),
269 '$urlpath' => $configCache->get('system', 'urlpath'),
270 '$dbhost' => $configCache->get('database', 'hostname'),
271 '$dbuser' => $configCache->get('database', 'username'),
272 '$dbpass' => $configCache->get('database', 'password'),
273 '$dbdata' => $configCache->get('database', 'database'),
274 '$adminmail' => ['config-admin_email',
275 L10n::t('Site administrator email address'),
276 $configCache->get('config', 'admin_email'),
277 L10n::t('Your account email address must match this in order to use the web admin panel.'),
278 'required', 'autofocus', 'email'],
279 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
280 L10n::t('Please select a default timezone for your website'),
281 $configCache->get('system', 'default_timezone'),
283 '$language' => ['system-language',
284 L10n::t('System Language:'),
285 $configCache->get('system', 'language'),
286 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
288 '$php_path' => $configCache->get('config', 'php_path'),
289 '$submit' => L10n::t('Submit')
294 $db_return_text = "";
296 if (count(self::$installer->getChecks()) == 0) {
297 $txt = '<p style="font-size: 130%;">';
298 $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
299 $db_return_text .= $txt;
302 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
303 $output .= Renderer::replaceMacros($tpl, [
304 '$title' => $install_title,
305 '$checks' => self::$installer->getChecks(),
306 '$pass' => L10n::t('Installation finished'),
307 '$text' => $db_return_text . self::whatNext($a),
317 * Creates the text for the next steps
319 * @param App $a The global App
321 * @return string The text for the next steps
322 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
324 private static function whatNext($a)
326 $baseurl = $a->getBaseUrl();
328 L10n::t('<h1>What next</h1>')
329 . "<p>" . L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
330 . L10n::t('Please see the file "INSTALL.txt".')
332 . 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)
337 * Checks the $_POST settings and updates the config Cache for it
339 * @param ConfigCache $configCache The current config cache
340 * @param array $post The $_POST data
341 * @param string $cat The category of the setting
342 * @param string $key The key of the setting
343 * @param null|string $default The default value
345 private static function checkSetting(ConfigCache $configCache, array $post, $cat, $key, $default = null)
347 $configCache->set($cat, $key,
349 trim(defaults($post, sprintf('%s-%s', $cat, $key),
350 (!isset($default) ? $configCache->get($cat, $key) : $default))