]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Improved protocol storing
[friendica.git] / src / Module / Install.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\App;
6 use Friendica\BaseModule;
7 use Friendica\Core;
8 use Friendica\Core\Config\Cache\IConfigCache;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Util\BasePath;
12 use Friendica\Util\BaseURL;
13 use Friendica\Util\Strings;
14 use Friendica\Util\Temporal;
15
16 class Install extends BaseModule
17 {
18         /**
19          * Step one - System check
20          */
21         const SYSTEM_CHECK = 1;
22         /**
23          * Step two - Base information
24          */
25         const BASE_CONFIG = 2;
26         /**
27          * Step three - Database configuration
28          */
29         const DATABASE_CONFIG = 3;
30         /**
31          * Step four - Adapt site settings
32          */
33         const SITE_SETTINGS = 4;
34         /**
35          * Step five - All steps finished
36          */
37         const FINISHED = 5;
38
39         /**
40          * @var int The current step of the wizard
41          */
42         private static $currentWizardStep;
43
44         /**
45          * @var Core\Installer The installer
46          */
47         private static $installer;
48
49         public static function init()
50         {
51                 $a = self::getApp();
52
53                 if (!$a->getMode()->isInstall()) {
54                         Core\System::httpExit(403);
55                 }
56
57                 // route: install/testrwrite
58                 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
59                 if ($a->getArgumentValue(1, '') == 'testrewrite') {
60                         // Status Code 204 means that it worked without content
61                         Core\System::httpExit(204);
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                                         '$baseurl'     => $a->getBaseURL()
176                                 ]);
177                                 break;
178
179                         case self::BASE_CONFIG:
180                                 $ssl_choices = [
181                                         BaseUrl::SSL_POLICY_NONE     => L10n::t("No SSL policy, links will track page SSL state"),
182                                         BaseUrl::SSL_POLICY_FULL     => L10n::t("Force all links to use SSL"),
183                                         BaseUrl::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
184                                 ];
185
186                                 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
187                                 $output .= Renderer::replaceMacros($tpl, [
188                                         '$title'      => $install_title,
189                                         '$pass'       => L10n::t('Base settings'),
190                                         '$ssl_policy' => ['system-ssl_policy',
191                                                 L10n::t("SSL link policy"),
192                                                 $configCache->get('system', 'ssl_policy'),
193                                                 L10n::t("Determines whether generated links should be forced to use SSL"),
194                                                 $ssl_choices],
195                                         '$hostname'   => ['config-hostname',
196                                                 L10n::t('Host name'),
197                                                 $configCache->get('config', 'hostname'),
198                                                 L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
199                                                 'required'],
200                                         '$basepath'   => ['system-basepath',
201                                                 L10n::t("Base path to installation"),
202                                                 $configCache->get('system', 'basepath'),
203                                                 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."),
204                                                 'required'],
205                                         '$urlpath'    => ['system-urlpath',
206                                                 L10n::t('Sub path of the URL'),
207                                                 $configCache->get('system', 'urlpath'),
208                                                 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.'),
209                                                 ''],
210                                         '$baseurl'    => $a->getBaseURL(),
211                                         '$php_path'   => $configCache->get('config', 'php_path'),
212                                         '$submit'     => L10n::t('Submit'),
213                                 ]);
214                                 break;
215
216                         case self::DATABASE_CONFIG:
217                                 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
218                                 $output .= Renderer::replaceMacros($tpl, [
219                                         '$title'      => $install_title,
220                                         '$pass'       => L10n::t('Database connection'),
221                                         '$info_01'    => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
222                                         '$info_02'    => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
223                                         '$info_03'    => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
224                                         'checks'      => self::$installer->getChecks(),
225                                         '$hostname'   => $configCache->get('config', 'hostname'),
226                                         '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
227                                         '$basepath'   => $configCache->get('system', 'basepath'),
228                                         '$urlpath'    => $configCache->get('system', 'urlpath'),
229                                         '$dbhost'     => ['database-hostname',
230                                                 L10n::t('Database Server Name'),
231                                                 $configCache->get('database', 'hostname'),
232                                                 '',
233                                                 'required'],
234                                         '$dbuser'     => ['database-username',
235                                                 L10n::t('Database Login Name'),
236                                                 $configCache->get('database', 'username'),
237                                                 '',
238                                                 'required',
239                                                 'autofocus'],
240                                         '$dbpass'     => ['database-password',
241                                                 L10n::t('Database Login Password'),
242                                                 $configCache->get('database', 'password'),
243                                                 L10n::t("For security reasons the password must not be empty"),
244                                                 'required'],
245                                         '$dbdata'     => ['database-database',
246                                                 L10n::t('Database Name'),
247                                                 $configCache->get('database', 'database'),
248                                                 '',
249                                                 'required'],
250                                         '$lbl_10'     => L10n::t('Please select a default timezone for your website'),
251                                         '$baseurl'    => $a->getBaseURL(),
252                                         '$php_path'   => $configCache->get('config', 'php_path'),
253                                         '$submit'     => L10n::t('Submit')
254                                 ]);
255                                 break;
256
257                         case self::SITE_SETTINGS:
258                                 /* Installed langs */
259                                 $lang_choices = L10n::getAvailableLanguages();
260
261                                 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
262                                 $output .= Renderer::replaceMacros($tpl, [
263                                         '$title'      => $install_title,
264                                         '$checks'     => self::$installer->getChecks(),
265                                         '$pass'       => L10n::t('Site settings'),
266                                         '$hostname'   => $configCache->get('config', 'hostname'),
267                                         '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
268                                         '$basepath'   => $configCache->get('system', 'basepath'),
269                                         '$urlpath'    => $configCache->get('system', 'urlpath'),
270                                         '$dbhost'     => $configCache->get('database', 'hostname'),
271                                         '$dbuser'     => $configCache->get('database', 'username'),
272                                         '$dbpass'     => $configCache->get('database', 'password'),
273                                         '$dbdata'     => $configCache->get('database', 'database'),
274                                         '$adminmail'  => ['config-admin_email',
275                                                 L10n::t('Site administrator email address'),
276                                                 $configCache->get('config', 'admin_email'),
277                                                 L10n::t('Your account email address must match this in order to use the web admin panel.'),
278                                                 'required', 'autofocus', 'email'],
279                                         '$timezone'   => Temporal::getTimezoneField('system-default_timezone',
280                                                 L10n::t('Please select a default timezone for your website'),
281                                                 $configCache->get('system', 'default_timezone'),
282                                                 ''),
283                                         '$language'   => ['system-language',
284                                                 L10n::t('System Language:'),
285                                                 $configCache->get('system', 'language'),
286                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
287                                                 $lang_choices],
288                                         '$baseurl'    => $a->getBaseURL(),
289                                         '$php_path'   => $configCache->get('config', 'php_path'),
290                                         '$submit'     => L10n::t('Submit')
291                                 ]);
292                                 break;
293
294                         case self::FINISHED:
295                                 $db_return_text = "";
296
297                                 if (count(self::$installer->getChecks()) == 0) {
298                                         $txt = '<p style="font-size: 130%;">';
299                                         $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
300                                         $db_return_text .= $txt;
301                                 }
302
303                                 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
304                                 $output .= Renderer::replaceMacros($tpl, [
305                                         '$title'  => $install_title,
306                                         '$checks' => self::$installer->getChecks(),
307                                         '$pass'   => L10n::t('Installation finished'),
308                                         '$text'   => $db_return_text . self::whatNext($a),
309                                 ]);
310
311                                 break;
312                 }
313
314                 return $output;
315         }
316
317         /**
318          * Creates the text for the next steps
319          *
320          * @param App $a The global App
321          *
322          * @return string The text for the next steps
323          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
324          */
325         private static function whatNext($a)
326         {
327                 $baseurl = $a->getBaseUrl();
328                 return
329                         L10n::t('<h1>What next</h1>')
330                         . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
331                         . L10n::t('Please see the file "INSTALL.txt".')
332                         . "</p><p>"
333                         . 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)
334                         . "</p>";
335         }
336
337         /**
338          * Checks the $_POST settings and updates the config Cache for it
339          *
340          * @param IConfigCache $configCache The current config cache
341          * @param array        $post        The $_POST data
342          * @param string       $cat         The category of the setting
343          * @param string       $key         The key of the setting
344          * @param null|string  $default     The default value
345          */
346         private static function checkSetting(IConfigCache $configCache, array $post, $cat, $key, $default = null)
347         {
348                 $configCache->set($cat, $key,
349                         Strings::escapeTags(
350                                 trim(defaults($post, sprintf('%s-%s', $cat, $key),
351                                                 (!isset($default) ? $configCache->get($cat, $key) : $default))
352                                 )
353                         )
354                 );
355         }
356 }