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