]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Adding basepath, urlpath, hostname and ssl_policy to installation
[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 - Adapat 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                 // route: install/testrwrite
54                 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
55                 if ($a->getArgumentValue(1, '') == 'testrewrite') {
56                         // Status Code 204 means that it worked without content
57                         Core\System::httpExit(204);
58                 }
59
60                 self::$installer = new Core\Installer();
61
62                 // get basic installation information and save them to the config cache
63                 $configCache = $a->getConfigCache();
64                 self::$installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
65
66                 // We overwrite current theme css, because during install we may not have a working mod_rewrite
67                 // so we may not have a css at all. Here we set a static css file for the install procedure pages
68                 Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
69
70                 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
71         }
72
73         public static function post()
74         {
75                 $a = self::getApp();
76                 $configCache = $a->getConfigCache();
77
78                 switch (self::$currentWizardStep) {
79                         case self::SYSTEM_CHECK:
80                         case self::BASE_CONFIG:
81                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
82                                 break;
83
84                         case self::DATABASE_CONFIG:
85                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
86
87                                 self::checkSetting($configCache, $_POST, 'config', 'hostname');
88                                 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
89                                 self::checkSetting($configCache, $_POST, 'system', 'basepath');
90                                 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
91                                 break;
92
93                         case self::SITE_SETTINGS:
94                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
95
96                                 self::checkSetting($configCache, $_POST, 'config', 'hostname');
97                                 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
98                                 self::checkSetting($configCache, $_POST, 'system', 'basepath');
99                                 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
100
101                                 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
102                                 self::checkSetting($configCache, $_POST, 'database', 'username', '');
103                                 self::checkSetting($configCache, $_POST, 'database', 'password', '');
104                                 self::checkSetting($configCache, $_POST, 'database', 'database', '');
105
106                                 // If we cannot connect to the database, return to the previous step
107                                 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
108                                         self::$currentWizardStep = self::DATABASE_CONFIG;
109                                 }
110
111                                 break;
112
113                         case self::FINISHED:
114                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
115
116                                 self::checkSetting($configCache, $_POST, 'config', 'hostname');
117                                 self::checkSetting($configCache, $_POST, 'system', 'ssl_policy');
118                                 self::checkSetting($configCache, $_POST, 'system', 'basepath');
119                                 self::checkSetting($configCache, $_POST, 'system', 'urlpath');
120
121                                 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
122                                 self::checkSetting($configCache, $_POST, 'database', 'username', '');
123                                 self::checkSetting($configCache, $_POST, 'database', 'password', '');
124                                 self::checkSetting($configCache, $_POST, 'database', 'database', '');
125
126                                 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
127                                 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
128                                 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
129
130                                 // If we cannot connect to the database, return to the Database config wizard
131                                 if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
132                                         self::$currentWizardStep = self::DATABASE_CONFIG;
133                                         return;
134                                 }
135
136                                 if (!self::$installer->createConfig($configCache)) {
137                                         return;
138                                 }
139
140                                 self::$installer->installDatabase($configCache->get('system', 'basepath'));
141
142                                 break;
143                 }
144         }
145
146         public static function content()
147         {
148                 $a = self::getApp();
149                 $configCache = $a->getConfigCache();
150
151                 $output = '';
152
153                 $install_title = L10n::t('Friendica Communications Server - Setup');
154
155                 switch (self::$currentWizardStep) {
156                         case self::SYSTEM_CHECK:
157                                 $php_path = $configCache->get('config', 'php_path');
158
159                                 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
160
161                                 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
162                                 $output .= Renderer::replaceMacros($tpl, [
163                                         '$title'        => $install_title,
164                                         '$pass'         => L10n::t('System check'),
165                                         '$checks'       => self::$installer->getChecks(),
166                                         '$passed'       => $status,
167                                         '$see_install'  => L10n::t('Please see the file "INSTALL.txt".'),
168                                         '$next'         => L10n::t('Next'),
169                                         '$reload'       => L10n::t('Check again'),
170                                         '$php_path'     => $php_path,
171                                         '$baseurl'      => $a->getBaseURL()
172                                 ]);
173                                 break;
174
175                         case self::BASE_CONFIG:
176                                 $ssl_choices = [
177                                         BaseUrl::SSL_POLICY_NONE     => L10n::t("No SSL policy, links will track page SSL state"),
178                                         BaseUrl::SSL_POLICY_FULL     => L10n::t("Force all links to use SSL"),
179                                         BaseUrl::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
180                                 ];
181
182                                 $tpl = Renderer::getMarkupTemplate('install_base.tpl');
183                                 $output .= Renderer::replaceMacros($tpl, [
184                                         '$title'      => $install_title,
185                                         '$pass'       => L10n::t('Base settings'),
186                                         '$ssl_policy' => ['system-ssl_policy',
187                                                                                 L10n::t("SSL link policy"),
188                                                                                 $configCache->get('system', 'ssl_policy'),
189                                                                                 L10n::t("Determines whether generated links should be forced to use SSL"),
190                                                                                 $ssl_choices],
191                                         '$hostname'   => ['config-hostname',
192                                                                                 L10n::t('Host name'),
193                                                                                 $configCache->get('config', 'hostname'),
194                                                                                 L10n::t('Overwrite this field in case the determinated hostname isn\'t right, otherweise leave it as is.'),
195                                                                                 'required'],
196                                         '$basepath'   => ['system-basepath',
197                                                                                 L10n::t("Base path to installation"),
198                                                                                 $configCache->get('system', 'basepath'),
199                                                                                 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."),
200                                                                                 'required'],
201                                         '$urlpath'    => ['system-urlpath',
202                                                                                 L10n::t('Sub path of the URL'),
203                                                                                 $configCache->get('system', 'urlpath'),
204                                                                                 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.'),
205                                                                                 ''],
206                                         '$baseurl'    => $a->getBaseURL(),
207                                         '$php_path'   => $configCache->get('config', 'php_path'),
208                                         '$submit'     => L10n::t('Submit'),
209                                 ]);
210                                 break;
211
212                         case self::DATABASE_CONFIG:
213                                 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
214                                 $output .= Renderer::replaceMacros($tpl, [
215                                         '$title'    => $install_title,
216                                         '$pass'     => L10n::t('Database connection'),
217                                         '$info_01'  => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
218                                         '$info_02'  => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
219                                         '$info_03'  => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
220                                         'checks'    => self::$installer->getChecks(),
221                                         '$hostname'   => $configCache->get('config', 'hostname'),
222                                         '$ssl_policy' => $configCache->get('system', 'ssl_policy'),
223                                         '$basepath'   => $configCache->get('system', 'basepath'),
224                                         '$urlpath'    => $configCache->get('system', 'urlpath'),
225                                         '$dbhost'   => ['database-hostname',
226                                                                         L10n::t('Database Server Name'),
227                                                                         $configCache->get('database', 'hostname'),
228                                                                         '',
229                                                                         'required'],
230                                         '$dbuser'   => ['database-username',
231                                                                         L10n::t('Database Login Name'),
232                                                                         $configCache->get('database', 'username'),
233                                                                         '',
234                                                                         'required',
235                                                                         'autofocus'],
236                                         '$dbpass'   => ['database-password',
237                                                                         L10n::t('Database Login Password'),
238                                                                         $configCache->get('database', 'password'),
239                                                                         L10n::t("For security reasons the password must not be empty"),
240                                                                         'required'],
241                                         '$dbdata'   => ['database-database',
242                                                                         L10n::t('Database Name'),
243                                                                         $configCache->get('database', 'database'),
244                                                                         '',
245                                                                         'required'],
246                                         '$lbl_10'   => L10n::t('Please select a default timezone for your website'),
247                                         '$baseurl'  => $a->getBaseURL(),
248                                         '$php_path' => $configCache->get('config', 'php_path'),
249                                         '$submit'   => L10n::t('Submit')
250                                 ]);
251                                 break;
252
253                         case self::SITE_SETTINGS:
254                                 /* Installed langs */
255                                 $lang_choices = L10n::getAvailableLanguages();
256
257                                 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
258                                 $output .= Renderer::replaceMacros($tpl, [
259                                         '$title'                => $install_title,
260                                         '$checks'               => self::$installer->getChecks(),
261                                         '$pass'                 => L10n::t('Site settings'),
262                                         '$hostname'     => $configCache->get('config', 'hostname'),
263                                         '$ssl_policy'   => $configCache->get('system', 'ssl_policy'),
264                                         '$basepath'     => $configCache->get('system', 'basepath'),
265                                         '$urlpath'      => $configCache->get('system', 'urlpath'),
266                                         '$dbhost'               => $configCache->get('database', 'hostname'),
267                                         '$dbuser'               => $configCache->get('database', 'username'),
268                                         '$dbpass'               => $configCache->get('database', 'password'),
269                                         '$dbdata'               => $configCache->get('database', 'database'),
270                                         '$adminmail'    => ['config-admin_email',
271                                                                                 L10n::t('Site administrator email address'),
272                                                                                 $configCache->get('config', 'admin_email'),
273                                                                                 L10n::t('Your account email address must match this in order to use the web admin panel.'),
274                                                                                 'required', 'autofocus', 'email'],
275                                         '$timezone'     => Temporal::getTimezoneField('system-default_timezone',
276                                                                                 L10n::t('Please select a default timezone for your website'),
277                                                                                 $configCache->get('system', 'default_timezone'),
278                                                                         ''),
279                                         '$language'     => ['system-language',
280                                                                                 L10n::t('System Language:'),
281                                                                                 $configCache->get('system', 'language'),
282                                                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
283                                                                                 $lang_choices],
284                                         '$baseurl'              => $a->getBaseURL(),
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 IConfigCache $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(IConfigCache $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 }