]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Make BaseModule methods dynamic
[friendica.git] / src / Module / Install.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core;
27 use Friendica\Core\Config\ValueObject\Cache;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Theme;
30 use Friendica\DI;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\BasePath;
33 use Friendica\Util\Temporal;
34
35 class Install extends BaseModule
36 {
37         /**
38          * Step one - System check
39          */
40         const SYSTEM_CHECK = 1;
41         /**
42          * Step two - Base information
43          */
44         const BASE_CONFIG = 2;
45         /**
46          * Step three - Database configuration
47          */
48         const DATABASE_CONFIG = 3;
49         /**
50          * Step four - Adapt site settings
51          */
52         const SITE_SETTINGS = 4;
53         /**
54          * Step five - All steps finished
55          */
56         const FINISHED = 5;
57
58         /**
59          * @var int The current step of the wizard
60          */
61         private static $currentWizardStep;
62
63         /**
64          * @var Core\Installer The installer
65          */
66         private static $installer;
67
68         public function init()
69         {
70                 $a = DI::app();
71
72                 if (!DI::mode()->isInstall()) {
73                         throw new HTTPException\ForbiddenException();
74                 }
75
76                 // route: install/testrwrite
77                 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
78                 if (DI::args()->get(1, '') == 'testrewrite') {
79                         // Status Code 204 means that it worked without content
80                         throw new HTTPException\NoContentException();
81                 }
82
83                 self::$installer = new Core\Installer();
84
85                 // get basic installation information and save them to the config cache
86                 $configCache = $a->getConfigCache();
87                 $basePath = new BasePath($a->getBasePath());
88                 self::$installer->setUpCache($configCache, $basePath->getPath());
89
90                 // We overwrite current theme css, because during install we may not have a working mod_rewrite
91                 // so we may not have a css at all. Here we set a static css file for the install procedure pages
92                 Renderer::$theme['stylesheet'] = DI::baseUrl()->get() . '/view/install/style.css';
93
94                 self::$currentWizardStep = ($_POST['pass'] ?? '') ?: self::SYSTEM_CHECK;
95         }
96
97         public function post()
98         {
99                 $a           = DI::app();
100                 $configCache = $a->getConfigCache();
101
102                 switch (self::$currentWizardStep) {
103                         case self::SYSTEM_CHECK:
104                         case self::BASE_CONFIG:
105                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
106                                 break;
107
108                         case self::DATABASE_CONFIG:
109                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
110
111                                 self::checkSetting($configCache, $_POST, 'config', 'hostname');
112                                 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
113                                 self::checkSetting($configCache, $_POST, 'system', 'basepath');
114                                 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
115                                 break;
116
117                         case self::SITE_SETTINGS:
118                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
119
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');
124
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', '');
129
130                                 // If we cannot connect to the database, return to the previous step
131                                 if (!self::$installer->checkDB(DI::dba())) {
132                                         self::$currentWizardStep = self::DATABASE_CONFIG;
133                                 }
134
135                                 break;
136
137                         case self::FINISHED:
138                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
139
140                                 self::checkSetting($configCache, $_POST, 'config', 'hostname');
141                                 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
142                                 self::checkSetting($configCache, $_POST, 'system', 'basepath');
143                                 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
144
145                                 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
146                                 self::checkSetting($configCache, $_POST, 'database', 'username', '');
147                                 self::checkSetting($configCache, $_POST, 'database', 'password', '');
148                                 self::checkSetting($configCache, $_POST, 'database', 'database', '');
149
150                                 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
151                                 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
152                                 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
153
154                                 // If we cannot connect to the database, return to the Database config wizard
155                                 if (!self::$installer->checkDB(DI::dba())) {
156                                         self::$currentWizardStep = self::DATABASE_CONFIG;
157                                         return;
158                                 }
159
160                                 if (!self::$installer->createConfig($configCache)) {
161                                         return;
162                                 }
163
164                                 self::$installer->installDatabase($configCache->get('system', 'basepath'));
165                         
166                                 // install allowed themes to register theme hooks
167                                 // this is same as "Reload active theme" in /admin/themes
168                                 $allowed_themes = Theme::getAllowedList();
169                                 $allowed_themes = array_unique($allowed_themes);
170                                 foreach ($allowed_themes as $theme) {
171                                         Theme::uninstall($theme);
172                                         Theme::install($theme);
173                                 }
174                                 Theme::setAllowedList($allowed_themes);
175
176                                 break;
177                 }
178         }
179
180         public function content(): string
181         {
182                 $a           = DI::app();
183                 $configCache = $a->getConfigCache();
184
185                 $output = '';
186
187                 $install_title = DI::l10n()->t('Friendica Communications Server - Setup');
188
189                 switch (self::$currentWizardStep) {
190                         case self::SYSTEM_CHECK:
191                                 $php_path = $configCache->get('config', 'php_path');
192
193                                 $status = self::$installer->checkEnvironment(DI::baseUrl()->get(), $php_path);
194
195                                 $tpl    = Renderer::getMarkupTemplate('install_checks.tpl');
196                                 $output .= Renderer::replaceMacros($tpl, [
197                                         '$title'       => $install_title,
198                                         '$pass'        => DI::l10n()->t('System check'),
199                                         '$required'    => DI::l10n()->t('Required'),
200                                         '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
201                                         '$optional_requirement_not_satisfied' => DI::l10n()->t('Optional requirement not satisfied'),
202                                         '$ok'          => DI::l10n()->t('OK'),
203                                         '$checks'      => self::$installer->getChecks(),
204                                         '$passed'      => $status,
205                                         '$see_install' => DI::l10n()->t('Please see the file "doc/INSTALL.md".'),
206                                         '$next'        => DI::l10n()->t('Next'),
207                                         '$reload'      => DI::l10n()->t('Check again'),
208                                         '$php_path'    => $php_path,
209                                 ]);
210                                 break;
211
212                         case self::BASE_CONFIG:
213                                 $ssl_choices = [
214                                         App\BaseURL::SSL_POLICY_NONE     => DI::l10n()->t("No SSL policy, links will track page SSL state"),
215                                         App\BaseURL::SSL_POLICY_FULL     => DI::l10n()->t("Force all links to use SSL"),
216                                         App\BaseURL::SSL_POLICY_SELFSIGN => DI::l10n()->t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
217                                 ];
218
219                                 $tpl    = Renderer::getMarkupTemplate('install_base.tpl');
220                                 $output .= Renderer::replaceMacros($tpl, [
221                                         '$title'      => $install_title,
222                                         '$pass'       => DI::l10n()->t('Base settings'),
223                                         '$ssl_policy' => ['system-ssl_policy',
224                                                 DI::l10n()->t("SSL link policy"),
225                                                 $configCache->get('system', 'ssl_policy'),
226                                                 DI::l10n()->t("Determines whether generated links should be forced to use SSL"),
227                                                 $ssl_choices],
228                                         '$hostname'   => ['config-hostname',
229                                                 DI::l10n()->t('Host name'),
230                                                 $configCache->get('config', 'hostname'),
231                                                 DI::l10n()->t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
232                                                 DI::l10n()->t('Required')],
233                                         '$basepath'   => ['system-basepath',
234                                                 DI::l10n()->t("Base path to installation"),
235                                                 $configCache->get('system', 'basepath'),
236                                                 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."),
237                                                 DI::l10n()->t('Required')],
238                                         '$urlpath'    => ['system-urlpath',
239                                                 DI::l10n()->t('Sub path of the URL'),
240                                                 $configCache->get('system', 'urlpath'),
241                                                 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.'),
242                                                 ''],
243                                         '$php_path'   => $configCache->get('config', 'php_path'),
244                                         '$submit'     => DI::l10n()->t('Submit'),
245                                 ]);
246                                 break;
247
248                         case self::DATABASE_CONFIG:
249                                 $tpl    = Renderer::getMarkupTemplate('install_db.tpl');
250                                 $output .= Renderer::replaceMacros($tpl, [
251                                         '$title'      => $install_title,
252                                         '$pass'       => DI::l10n()->t('Database connection'),
253                                         '$info_01'    => DI::l10n()->t('In order to install Friendica we need to know how to connect to your database.'),
254                                         '$info_02'    => DI::l10n()->t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
255                                         '$info_03'    => DI::l10n()->t('The database you specify below should already exist. If it does not, please create it before continuing.'),
256                                         '$required'   => DI::l10n()->t('Required'),
257                                         '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
258                                         '$checks'     => self::$installer->getChecks(),
259                                         '$hostname'   => $configCache->get('config', 'hostname'),
260                                         '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
261                                         '$basepath'   => $configCache->get('system', 'basepath'),
262                                         '$urlpath'    => $configCache->get('system', 'urlpath'),
263                                         '$dbhost'     => ['database-hostname',
264                                                 DI::l10n()->t('Database Server Name'),
265                                                 $configCache->get('database', 'hostname'),
266                                                 '',
267                                                 DI::l10n()->t('Required')],
268                                         '$dbuser'     => ['database-username',
269                                                 DI::l10n()->t('Database Login Name'),
270                                                 $configCache->get('database', 'username'),
271                                                 '',
272                                                 DI::l10n()->t('Required'),
273                                                 'autofocus'],
274                                         '$dbpass'     => ['database-password',
275                                                 DI::l10n()->t('Database Login Password'),
276                                                 $configCache->get('database', 'password'),
277                                                 DI::l10n()->t("For security reasons the password must not be empty"),
278                                                 DI::l10n()->t('Required')],
279                                         '$dbdata'     => ['database-database',
280                                                 DI::l10n()->t('Database Name'),
281                                                 $configCache->get('database', 'database'),
282                                                 '',
283                                                 DI::l10n()->t('Required')],
284                                         '$lbl_10'     => DI::l10n()->t('Please select a default timezone for your website'),
285                                         '$php_path'   => $configCache->get('config', 'php_path'),
286                                         '$submit'     => DI::l10n()->t('Submit')
287                                 ]);
288                                 break;
289
290                         case self::SITE_SETTINGS:
291                                 /* Installed langs */
292                                 $lang_choices = DI::l10n()->getAvailableLanguages();
293
294                                 $tpl    = Renderer::getMarkupTemplate('install_settings.tpl');
295                                 $output .= Renderer::replaceMacros($tpl, [
296                                         '$title'      => $install_title,
297                                         '$required'   => DI::l10n()->t('Required'),
298                                         '$checks'     => self::$installer->getChecks(),
299                                         '$pass'       => DI::l10n()->t('Site settings'),
300                                         '$hostname'   => $configCache->get('config', 'hostname'),
301                                         '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
302                                         '$basepath'   => $configCache->get('system', 'basepath'),
303                                         '$urlpath'    => $configCache->get('system', 'urlpath'),
304                                         '$dbhost'     => $configCache->get('database', 'hostname'),
305                                         '$dbuser'     => $configCache->get('database', 'username'),
306                                         '$dbpass'     => $configCache->get('database', 'password'),
307                                         '$dbdata'     => $configCache->get('database', 'database'),
308                                         '$adminmail'  => ['config-admin_email',
309                                                 DI::l10n()->t('Site administrator email address'),
310                                                 $configCache->get('config', 'admin_email'),
311                                                 DI::l10n()->t('Your account email address must match this in order to use the web admin panel.'),
312                                                 DI::l10n()->t('Required'), 'autofocus', 'email'],
313                                         '$timezone'   => Temporal::getTimezoneField('system-default_timezone',
314                                                 DI::l10n()->t('Please select a default timezone for your website'),
315                                                 $configCache->get('system', 'default_timezone'),
316                                                 ''),
317                                         '$language'   => ['system-language',
318                                                 DI::l10n()->t('System Language:'),
319                                                 $configCache->get('system', 'language'),
320                                                 DI::l10n()->t('Set the default language for your Friendica installation interface and to send emails.'),
321                                                 $lang_choices],
322                                         '$php_path'   => $configCache->get('config', 'php_path'),
323                                         '$submit'     => DI::l10n()->t('Submit')
324                                 ]);
325                                 break;
326
327                         case self::FINISHED:
328                                 $db_return_text = "";
329
330                                 if (count(self::$installer->getChecks()) == 0) {
331                                         $txt            = '<p style="font-size: 130%;">';
332                                         $txt            .= DI::l10n()->t('Your Friendica site database has been installed.') . EOL;
333                                         $db_return_text .= $txt;
334                                 }
335
336                                 $tpl    = Renderer::getMarkupTemplate('install_finished.tpl');
337                                 $output .= Renderer::replaceMacros($tpl, [
338                                         '$title'    => $install_title,
339                                         '$required' => DI::l10n()->t('Required'),
340                                         '$requirement_not_satisfied' => DI::l10n()->t('Requirement not satisfied'),
341                                         '$checks'   => self::$installer->getChecks(),
342                                         '$pass'     => DI::l10n()->t('Installation finished'),
343                                         '$text'     => $db_return_text . self::whatNext(),
344                                 ]);
345
346                                 break;
347                 }
348
349                 return $output;
350         }
351
352         /**
353          * Creates the text for the next steps
354          *
355          * @return string The text for the next steps
356          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
357          */
358         private static function whatNext()
359         {
360                 $baseurl = DI::baseUrl()->get();
361                 return
362                         DI::l10n()->t('<h1>What next</h1>')
363                         . "<p>" . DI::l10n()->t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
364                         . DI::l10n()->t('Please see the file "doc/INSTALL.md".')
365                         . "</p><p>"
366                         . 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)
367                         . "</p>";
368         }
369
370         /**
371          * Checks the $_POST settings and updates the config Cache for it
372          *
373          * @param \Friendica\Core\Config\ValueObject\Cache $configCache The current config cache
374          * @param array                                    $post        The $_POST data
375          * @param string                                   $cat         The category of the setting
376          * @param string                                   $key         The key of the setting
377          * @param null|string                              $default     The default value
378          */
379         private static function checkSetting(Cache $configCache, array $post, $cat, $key, $default = null)
380         {
381                 $configCache->set($cat, $key,
382                         trim(($post[sprintf('%s-%s', $cat, $key)] ?? '') ?:
383                                         ($default ?? $configCache->get($cat, $key))
384                         )
385                 );
386         }
387 }