3 namespace Friendica\Module;
6 use Friendica\BaseModule;
8 use Friendica\Core\Config\Cache\IConfigCache;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Util\Strings;
12 use Friendica\Util\Temporal;
14 class Install extends BaseModule
17 * Step one - System check
19 const SYSTEM_CHECK = 1;
21 * Step two - Database configuration
23 const DATABASE_CONFIG = 2;
25 * Step three - Adapat site settings
27 const SITE_SETTINGS = 3;
29 * Step four - All steps finished
34 * @var int The current step of the wizard
36 private static $currentWizardStep;
39 * @var Core\Installer The installer
41 private static $installer;
43 public static function init()
47 if (!$a->getMode()->isInstall()) {
48 Core\System::httpExit(403);
51 // route: install/testrwrite
52 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
53 if ($a->getArgumentValue(1, '') == 'testrewrite') {
54 // Status Code 204 means that it worked without content
55 Core\System::httpExit(204);
58 // We overwrite current theme css, because during install we may not have a working mod_rewrite
59 // so we may not have a css at all. Here we set a static css file for the install procedure pages
60 Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
62 self::$installer = new Core\Installer();
63 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
66 public static function post()
69 $configCache = $a->getConfigCache();
71 switch (self::$currentWizardStep) {
72 case self::SYSTEM_CHECK:
73 case self::DATABASE_CONFIG:
74 self::checkSetting($configCache, $_POST, 'config', 'php_path');
77 case self::SITE_SETTINGS:
78 self::checkSetting($configCache, $_POST, 'config', 'php_path');
80 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
81 self::checkSetting($configCache, $_POST, 'database', 'username', '');
82 self::checkSetting($configCache, $_POST, 'database', 'password', '');
83 self::checkSetting($configCache, $_POST, 'database', 'database', '');
85 // If we cannot connect to the database, return to the previous step
86 if (!self::$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
87 self::$currentWizardStep = self::DATABASE_CONFIG;
93 self::checkSetting($configCache, $_POST, 'config', 'php_path');
95 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
96 self::checkSetting($configCache, $_POST, 'database', 'username', '');
97 self::checkSetting($configCache, $_POST, 'database', 'password', '');
98 self::checkSetting($configCache, $_POST, 'database', 'database', '');
100 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
101 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
102 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
104 // If we cannot connect to the database, return to the Database config wizard
105 if (!self::$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
106 self::$currentWizardStep = self::DATABASE_CONFIG;
110 if (!self::$installer->createConfig($a, $configCache, $a->getBasePath())) {
114 self::$installer->installDatabase($a->getBasePath());
120 public static function content()
123 $configCache = $a->getConfigCache();
127 $install_title = L10n::t('Friendica Communications Server - Setup');
129 switch (self::$currentWizardStep) {
130 case self::SYSTEM_CHECK:
131 $php_path = $configCache->get('config', 'php_path');
133 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
135 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
136 $output .= Renderer::replaceMacros($tpl, [
137 '$title' => $install_title,
138 '$pass' => L10n::t('System check'),
139 '$checks' => self::$installer->getChecks(),
140 '$passed' => $status,
141 '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
142 '$next' => L10n::t('Next'),
143 '$reload' => L10n::t('Check again'),
144 '$php_path' => $php_path,
145 '$baseurl' => $a->getBaseURL()
149 case self::DATABASE_CONFIG:
150 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
151 $output .= Renderer::replaceMacros($tpl, [
152 '$title' => $install_title,
153 '$pass' => L10n::t('Database connection'),
154 '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
155 '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
156 '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
157 'checks' => self::$installer->getChecks(),
158 '$dbhost' => ['database-hostname',
159 L10n::t('Database Server Name'),
160 $configCache->get('database', 'hostname'),
163 '$dbuser' => ['database-username',
164 L10n::t('Database Login Name'),
165 $configCache->get('database', 'username'),
169 '$dbpass' => ['database-password',
170 L10n::t('Database Login Password'),
171 $configCache->get('database', 'password'),
172 L10n::t("For security reasons the password must not be empty"),
174 '$dbdata' => ['database-database',
175 L10n::t('Database Name'),
176 $configCache->get('database', 'database'),
179 '$lbl_10' => L10n::t('Please select a default timezone for your website'),
180 '$baseurl' => $a->getBaseURL(),
181 '$php_path' => $configCache->get('config', 'php_path'),
182 '$submit' => L10n::t('Submit')
186 case self::SITE_SETTINGS:
187 /* Installed langs */
188 $lang_choices = L10n::getAvailableLanguages();
190 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
191 $output .= Renderer::replaceMacros($tpl, [
192 '$title' => $install_title,
193 '$checks' => self::$installer->getChecks(),
194 '$pass' => L10n::t('Site settings'),
195 '$dbhost' => $configCache->get('database', 'hostname'),
196 '$dbuser' => $configCache->get('database', 'username'),
197 '$dbpass' => $configCache->get('database', 'password'),
198 '$dbdata' => $configCache->get('database', 'database'),
199 '$phpath' => $configCache->get('config', 'php_path'),
200 '$adminmail' => ['config-admin_email',
201 L10n::t('Site administrator email address'),
202 $configCache->get('config', 'admin_email'),
203 L10n::t('Your account email address must match this in order to use the web admin panel.'),
204 'required', 'autofocus', 'email'],
205 '$timezone' => Temporal::getTimezoneField('system-default_timezone',
206 L10n::t('Please select a default timezone for your website'),
207 $configCache->get('system', 'default_timezone'),
209 '$language' => ['system-language',
210 L10n::t('System Language:'),
211 $configCache->get('system', 'language'),
212 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
214 '$baseurl' => $a->getBaseURL(),
215 '$submit' => L10n::t('Submit')
220 $db_return_text = "";
222 if (count(self::$installer->getChecks()) == 0) {
223 $txt = '<p style="font-size: 130%;">';
224 $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
225 $db_return_text .= $txt;
228 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
229 $output .= Renderer::replaceMacros($tpl, [
230 '$title' => $install_title,
231 '$checks' => self::$installer->getChecks(),
232 '$pass' => L10n::t('Installation finished'),
233 '$text' => $db_return_text . self::whatNext($a),
243 * Creates the text for the next steps
245 * @param App $a The global App
247 * @return string The text for the next steps
248 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250 private static function whatNext($a)
252 $baseurl = $a->getBaseUrl();
254 L10n::t('<h1>What next</h1>')
255 . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
256 . L10n::t('Please see the file "INSTALL.txt".')
258 . 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)
263 * Checks the $_POST settings and updates the config Cache for it
265 * @param IConfigCache $configCache The current config cache
266 * @param array $post The $_POST data
267 * @param string $cat The category of the setting
268 * @param string $key The key of the setting
269 * @param null|string $default The default value
271 private static function checkSetting(IConfigCache $configCache, array $post, $cat, $key, $default = null)
273 $configCache->set($cat, $key,
275 trim(defaults($post, sprintf('%s-%s', $cat, $key),
276 (!isset($default) ? $configCache->get($cat, $key) : $default))