3 namespace Friendica\Module;
6 use Friendica\BaseModule;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Util\BasePath;
11 use Friendica\Util\BaseURL;
12 use Friendica\Util\Strings;
13 use Friendica\Util\Temporal;
15 class Install extends BaseModule
18 * Step one - System check
20 const SYSTEM_CHECK = 1;
22 * Step two - Base information
24 const BASE_CONFIG = 2;
26 * Step three - Database configuration
28 const DATABASE_CONFIG = 3;
30 * Step four - Adapt site settings
32 const SITE_SETTINGS = 4;
34 * Step five - All steps finished
39 * @var int The current step of the wizard
41 private static $currentWizardStep;
44 * @var Core\Installer The installer
46 private static $installer;
48 public static function init()
52 if (!$a->getMode()->isInstall()) {
53 throw new \Friendica\Network\HTTPException\ForbiddenException();
56 // route: install/testrwrite
57 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
58 // @TODO: Replace with parameter from router
59 if ($a->getArgumentValue(1, '') == 'testrewrite') {
60 // Status Code 204 means that it worked without content
61 throw new \Friendica\Network\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 self::$installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
70 // We overwrite current theme css, because during install we may not have a working mod_rewrite
71 // so we may not have a css at all. Here we set a static css file for the install procedure pages
72 Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
74 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
77 public static function post()
80 $configCache = $a->getConfigCache();
82 switch (self::$currentWizardStep) {
83 case self::SYSTEM_CHECK:
84 case self::BASE_CONFIG:
85 self::checkSetting($configCache, $_POST, 'config', 'php_path');
88 case self::DATABASE_CONFIG:
89 self::checkSetting($configCache, $_POST, 'config', 'php_path');
91 self::checkSetting($configCache, $_POST, 'config', 'hostname');
92 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
93 self::checkSetting($configCache, $_POST, 'system', 'basepath');
94 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
97 case self::SITE_SETTINGS:
98 self::checkSetting($configCache, $_POST, 'config', 'php_path');
100 self::checkSetting($configCache, $_POST, 'config', 'hostname');
101 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
102 self::checkSetting($configCache, $_POST, 'system', 'basepath');
103 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
105 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
106 self::checkSetting($configCache, $_POST, 'database', 'username', '');
107 self::checkSetting($configCache, $_POST, 'database', 'password', '');
108 self::checkSetting($configCache, $_POST, 'database', 'database', '');
110 // If we cannot connect to the database, return to the previous step
111 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
112 self::$currentWizardStep = self::DATABASE_CONFIG;
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 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
131 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
132 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
134 // If we cannot connect to the database, return to the Database config wizard
135 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
136 self::$currentWizardStep = self::DATABASE_CONFIG;
140 if (!self::$installer->createConfig($configCache)) {
144 self::$installer->installDatabase($configCache->get('system', 'basepath'));
150 public static function content()
153 $configCache = $a->getConfigCache();
157 $install_title = L10n::t('Friendica Communications Server - Setup');
159 switch (self::$currentWizardStep) {
160 case self::SYSTEM_CHECK:
161 $php_path = $configCache->get('config', 'php_path');
163 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
165 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
166 $output .= Renderer::replaceMacros($tpl, [
167 '$title' => $install_title,
168 '$pass' => L10n::t('System check'),
169 '$checks' => self::$installer->getChecks(),
170 '$passed' => $status,
171 '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
172 '$next' => L10n::t('Next'),
173 '$reload' => L10n::t('Check again'),
174 '$php_path' => $php_path,
178 case self::BASE_CONFIG:
180 BaseURL::SSL_POLICY_NONE => L10n::t("No SSL policy, links will track page SSL state"),
181 BaseURL::SSL_POLICY_FULL => L10n::t("Force all links to use SSL"),
182 BaseURL::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
185 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
186 $output .= Renderer::replaceMacros($tpl, [
187 '$title' => $install_title,
188 '$pass' => L10n::t('Base settings'),
189 '$ssl_policy' => ['system-ssl_policy',
190 L10n::t("SSL link policy"),
191 $configCache->get('system', 'ssl_policy'),
192 L10n::t("Determines whether generated links should be forced to use SSL"),
194 '$hostname' => ['config-hostname',
195 L10n::t('Host name'),
196 $configCache->get('config', 'hostname'),
197 L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
199 '$basepath' => ['system-basepath',
200 L10n::t("Base path to installation"),
201 $configCache->get('system', 'basepath'),
202 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."),
204 '$urlpath' => ['system-urlpath',
205 L10n::t('Sub path of the URL'),
206 $configCache->get('system', 'urlpath'),
207 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.'),
209 '$php_path' => $configCache->get('config', 'php_path'),
210 '$submit' => L10n::t('Submit'),
214 case self::DATABASE_CONFIG:
215 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
216 $output .= Renderer::replaceMacros($tpl, [
217 '$title' => $install_title,
218 '$pass' => L10n::t('Database connection'),
219 '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
220 '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
221 '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
222 'checks' => self::$installer->getChecks(),
223 '$hostname' => $configCache->get('config', 'hostname'),
224 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
225 '$basepath' => $configCache->get('system', 'basepath'),
226 '$urlpath' => $configCache->get('system', 'urlpath'),
227 '$dbhost' => ['database-hostname',
228 L10n::t('Database Server Name'),
229 $configCache->get('database', 'hostname'),
232 '$dbuser' => ['database-username',
233 L10n::t('Database Login Name'),
234 $configCache->get('database', 'username'),
238 '$dbpass' => ['database-password',
239 L10n::t('Database Login Password'),
240 $configCache->get('database', 'password'),
241 L10n::t("For security reasons the password must not be empty"),
243 '$dbdata' => ['database-database',
244 L10n::t('Database Name'),
245 $configCache->get('database', 'database'),
248 '$lbl_10' => L10n::t('Please select a default timezone for your website'),
249 '$php_path' => $configCache->get('config', 'php_path'),
250 '$submit' => L10n::t('Submit')
254 case self::SITE_SETTINGS:
255 /* Installed langs */
256 $lang_choices = L10n::getAvailableLanguages();
258 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
259 $output .= Renderer::replaceMacros($tpl, [
260 '$title' => $install_title,
261 '$checks' => self::$installer->getChecks(),
262 '$pass' => L10n::t('Site settings'),
263 '$hostname' => $configCache->get('config', 'hostname'),
264 '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
265 '$basepath' => $configCache->get('system', 'basepath'),
266 '$urlpath' => $configCache->get('system', 'urlpath'),
267 '$dbhost' => $configCache->get('database', 'hostname'),
268 '$dbuser' => $configCache->get('database', 'username'),
269 '$dbpass' => $configCache->get('database', 'password'),
270 '$dbdata' => $configCache->get('database', 'database'),
271 '$adminmail' => ['config-admin_email',
272 L10n::t('Site administrator email address'),
273 $configCache->get('config', 'admin_email'),
274 L10n::t('Your account email address must match this in order to use the web admin panel.'),
275 'required', 'autofocus', 'email'],
276 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
277 L10n::t('Please select a default timezone for your website'),
278 $configCache->get('system', 'default_timezone'),
280 '$language' => ['system-language',
281 L10n::t('System Language:'),
282 $configCache->get('system', 'language'),
283 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
285 '$php_path' => $configCache->get('config', 'php_path'),
286 '$submit' => L10n::t('Submit')
291 $db_return_text = "";
293 if (count(self::$installer->getChecks()) == 0) {
294 $txt = '<p style="font-size: 130%;">';
295 $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
296 $db_return_text .= $txt;
299 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
300 $output .= Renderer::replaceMacros($tpl, [
301 '$title' => $install_title,
302 '$checks' => self::$installer->getChecks(),
303 '$pass' => L10n::t('Installation finished'),
304 '$text' => $db_return_text . self::whatNext($a),
314 * Creates the text for the next steps
316 * @param App $a The global App
318 * @return string The text for the next steps
319 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
321 private static function whatNext($a)
323 $baseurl = $a->getBaseUrl();
325 L10n::t('<h1>What next</h1>')
326 . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
327 . L10n::t('Please see the file "INSTALL.txt".')
329 . 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)
334 * Checks the $_POST settings and updates the config Cache for it
336 * @param Core\Config\Cache\ConfigCache $configCache The current config cache
337 * @param array $post The $_POST data
338 * @param string $cat The category of the setting
339 * @param string $key The key of the setting
340 * @param null|string $default The default value
342 private static function checkSetting(Core\Config\Cache\ConfigCache $configCache, array $post, $cat, $key, $default = null)
344 $configCache->set($cat, $key,
346 trim(defaults($post, sprintf('%s-%s', $cat, $key),
347 (!isset($default) ? $configCache->get($cat, $key) : $default))