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