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