]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Make the site admin theme setting link go to the currently selected theme
[friendica.git] / src / Module / Install.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\App;
6 use Friendica\BaseModule;
7 use Friendica\Core;
8 use Friendica\Core\Config\Cache\IConfigCache;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Util\BasePath;
12 use Friendica\Util\BaseURL;
13 use Friendica\Util\Strings;
14 use Friendica\Util\Temporal;
15
16 class Install extends BaseModule
17 {
18         /**
19          * Step one - System check
20          */
21         const SYSTEM_CHECK = 1;
22         /**
23          * Step two - Base information
24          */
25         const BASE_CONFIG = 2;
26         /**
27          * Step three - Database configuration
28          */
29         const DATABASE_CONFIG = 3;
30         /**
31          * Step four - Adapt site settings
32          */
33         const SITE_SETTINGS = 4;
34         /**
35          * Step five - All steps finished
36          */
37         const FINISHED = 5;
38
39         /**
40          * @var int The current step of the wizard
41          */
42         private static $currentWizardStep;
43
44         /**
45          * @var Core\Installer The installer
46          */
47         private static $installer;
48
49         public static function init()
50         {
51                 $a = self::getApp();
52
53                 if (!$a->getMode()->isInstall()) {
54                         Core\System::httpExit(403);
55                 }
56
57                 // route: install/testrwrite
58                 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
59                 // @TODO: Replace with parameter from router
60                 if ($a->getArgumentValue(1, '') == 'testrewrite') {
61                         // Status Code 204 means that it worked without content
62                         Core\System::httpExit(204);
63                 }
64
65                 self::$installer = new Core\Installer();
66
67                 // get basic installation information and save them to the config cache
68                 $configCache = $a->getConfigCache();
69                 self::$installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
70
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'] = $a->getBaseURL() . '/view/install/style.css';
74
75                 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
76         }
77
78         public static function post()
79         {
80                 $a = self::getApp();
81                 $configCache = $a->getConfigCache();
82
83                 switch (self::$currentWizardStep) {
84                         case self::SYSTEM_CHECK:
85                         case self::BASE_CONFIG:
86                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
87                                 break;
88
89                         case self::DATABASE_CONFIG:
90                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
91
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');
96                                 break;
97
98                         case self::SITE_SETTINGS:
99                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
100
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');
105
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', '');
110
111                                 // If we cannot connect to the database, return to the previous step
112                                 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
113                                         self::$currentWizardStep = self::DATABASE_CONFIG;
114                                 }
115
116                                 break;
117
118                         case self::FINISHED:
119                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
120
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');
125
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', '');
130
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', '');
134
135                                 // If we cannot connect to the database, return to the Database config wizard
136                                 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
137                                         self::$currentWizardStep = self::DATABASE_CONFIG;
138                                         return;
139                                 }
140
141                                 if (!self::$installer->createConfig($configCache)) {
142                                         return;
143                                 }
144
145                                 self::$installer->installDatabase($configCache->get('system', 'basepath'));
146
147                                 break;
148                 }
149         }
150
151         public static function content()
152         {
153                 $a = self::getApp();
154                 $configCache = $a->getConfigCache();
155
156                 $output = '';
157
158                 $install_title = L10n::t('Friendica Communications Server - Setup');
159
160                 switch (self::$currentWizardStep) {
161                         case self::SYSTEM_CHECK:
162                                 $php_path = $configCache->get('config', 'php_path');
163
164                                 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
165
166                                 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
167                                 $output .= Renderer::replaceMacros($tpl, [
168                                         '$title'       => $install_title,
169                                         '$pass'        => L10n::t('System check'),
170                                         '$checks'      => self::$installer->getChecks(),
171                                         '$passed'      => $status,
172                                         '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
173                                         '$next'        => L10n::t('Next'),
174                                         '$reload'      => L10n::t('Check again'),
175                                         '$php_path'    => $php_path,
176                                         '$baseurl'     => $a->getBaseURL()
177                                 ]);
178                                 break;
179
180                         case self::BASE_CONFIG:
181                                 $ssl_choices = [
182                                         BaseUrl::SSL_POLICY_NONE     => L10n::t("No SSL policy, links will track page SSL state"),
183                                         BaseUrl::SSL_POLICY_FULL     => L10n::t("Force all links to use SSL"),
184                                         BaseUrl::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
185                                 ];
186
187                                 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
188                                 $output .= Renderer::replaceMacros($tpl, [
189                                         '$title'      => $install_title,
190                                         '$pass'       => L10n::t('Base settings'),
191                                         '$ssl_policy' => ['system-ssl_policy',
192                                                 L10n::t("SSL link policy"),
193                                                 $configCache->get('system', 'ssl_policy'),
194                                                 L10n::t("Determines whether generated links should be forced to use SSL"),
195                                                 $ssl_choices],
196                                         '$hostname'   => ['config-hostname',
197                                                 L10n::t('Host name'),
198                                                 $configCache->get('config', 'hostname'),
199                                                 L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
200                                                 'required'],
201                                         '$basepath'   => ['system-basepath',
202                                                 L10n::t("Base path to installation"),
203                                                 $configCache->get('system', 'basepath'),
204                                                 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                                                 'required'],
206                                         '$urlpath'    => ['system-urlpath',
207                                                 L10n::t('Sub path of the URL'),
208                                                 $configCache->get('system', 'urlpath'),
209                                                 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                                                 ''],
211                                         '$baseurl'    => $a->getBaseURL(),
212                                         '$php_path'   => $configCache->get('config', 'php_path'),
213                                         '$submit'     => L10n::t('Submit'),
214                                 ]);
215                                 break;
216
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'),
233                                                 '',
234                                                 'required'],
235                                         '$dbuser'     => ['database-username',
236                                                 L10n::t('Database Login Name'),
237                                                 $configCache->get('database', 'username'),
238                                                 '',
239                                                 'required',
240                                                 'autofocus'],
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"),
245                                                 'required'],
246                                         '$dbdata'     => ['database-database',
247                                                 L10n::t('Database Name'),
248                                                 $configCache->get('database', 'database'),
249                                                 '',
250                                                 'required'],
251                                         '$lbl_10'     => L10n::t('Please select a default timezone for your website'),
252                                         '$baseurl'    => $a->getBaseURL(),
253                                         '$php_path'   => $configCache->get('config', 'php_path'),
254                                         '$submit'     => L10n::t('Submit')
255                                 ]);
256                                 break;
257
258                         case self::SITE_SETTINGS:
259                                 /* Installed langs */
260                                 $lang_choices = L10n::getAvailableLanguages();
261
262                                 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
263                                 $output .= Renderer::replaceMacros($tpl, [
264                                         '$title'      => $install_title,
265                                         '$checks'     => self::$installer->getChecks(),
266                                         '$pass'       => L10n::t('Site settings'),
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'     => $configCache->get('database', 'hostname'),
272                                         '$dbuser'     => $configCache->get('database', 'username'),
273                                         '$dbpass'     => $configCache->get('database', 'password'),
274                                         '$dbdata'     => $configCache->get('database', 'database'),
275                                         '$adminmail'  => ['config-admin_email',
276                                                 L10n::t('Site administrator email address'),
277                                                 $configCache->get('config', 'admin_email'),
278                                                 L10n::t('Your account email address must match this in order to use the web admin panel.'),
279                                                 'required', 'autofocus', 'email'],
280                                         '$timezone'   => Temporal::getTimezoneField('system-default_timezone',
281                                                 L10n::t('Please select a default timezone for your website'),
282                                                 $configCache->get('system', 'default_timezone'),
283                                                 ''),
284                                         '$language'   => ['system-language',
285                                                 L10n::t('System Language:'),
286                                                 $configCache->get('system', 'language'),
287                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
288                                                 $lang_choices],
289                                         '$baseurl'    => $a->getBaseURL(),
290                                         '$php_path'   => $configCache->get('config', 'php_path'),
291                                         '$submit'     => L10n::t('Submit')
292                                 ]);
293                                 break;
294
295                         case self::FINISHED:
296                                 $db_return_text = "";
297
298                                 if (count(self::$installer->getChecks()) == 0) {
299                                         $txt = '<p style="font-size: 130%;">';
300                                         $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
301                                         $db_return_text .= $txt;
302                                 }
303
304                                 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
305                                 $output .= Renderer::replaceMacros($tpl, [
306                                         '$title'  => $install_title,
307                                         '$checks' => self::$installer->getChecks(),
308                                         '$pass'   => L10n::t('Installation finished'),
309                                         '$text'   => $db_return_text . self::whatNext($a),
310                                 ]);
311
312                                 break;
313                 }
314
315                 return $output;
316         }
317
318         /**
319          * Creates the text for the next steps
320          *
321          * @param App $a The global App
322          *
323          * @return string The text for the next steps
324          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
325          */
326         private static function whatNext($a)
327         {
328                 $baseurl = $a->getBaseUrl();
329                 return
330                         L10n::t('<h1>What next</h1>')
331                         . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
332                         . L10n::t('Please see the file "INSTALL.txt".')
333                         . "</p><p>"
334                         . 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)
335                         . "</p>";
336         }
337
338         /**
339          * Checks the $_POST settings and updates the config Cache for it
340          *
341          * @param IConfigCache $configCache The current config cache
342          * @param array        $post        The $_POST data
343          * @param string       $cat         The category of the setting
344          * @param string       $key         The key of the setting
345          * @param null|string  $default     The default value
346          */
347         private static function checkSetting(IConfigCache $configCache, array $post, $cat, $key, $default = null)
348         {
349                 $configCache->set($cat, $key,
350                         Strings::escapeTags(
351                                 trim(defaults($post, sprintf('%s-%s', $cat, $key),
352                                                 (!isset($default) ? $configCache->get($cat, $key) : $default))
353                                 )
354                         )
355                 );
356         }
357 }