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