]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Added test case for Router->getModuleClass
[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\Strings;
12 use Friendica\Util\Temporal;
13
14 class Install extends BaseModule
15 {
16         /**
17          * Step one - System check
18          */
19         const SYSTEM_CHECK = 1;
20         /**
21          * Step two - Database configuration
22          */
23         const DATABASE_CONFIG = 2;
24         /**
25          * Step three - Adapat site settings
26          */
27         const SITE_SETTINGS = 3;
28         /**
29          * Step four - All steps finished
30          */
31         const FINISHED = 4;
32
33         /**
34          * @var int The current step of the wizard
35          */
36         private static $currentWizardStep;
37
38         /**
39          * @var Core\Installer The installer
40          */
41         private static $installer;
42
43         public static function init()
44         {
45                 $a = self::getApp();
46
47                 if (!$a->getMode()->isInstall()) {
48                         Core\System::httpExit(403);
49                 }
50
51                 // route: install/testrwrite
52                 // $baseurl/install/testrwrite to test if rewrite in .htaccess is working
53                 if ($a->getArgumentValue(1, '') == 'testrewrite') {
54                         // Status Code 204 means that it worked without content
55                         Core\System::httpExit(204);
56                 }
57
58                 // We overwrite current theme css, because during install we may not have a working mod_rewrite
59                 // so we may not have a css at all. Here we set a static css file for the install procedure pages
60                 Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
61
62                 self::$installer = new Core\Installer();
63                 self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);
64         }
65
66         public static function post()
67         {
68                 $a = self::getApp();
69                 $configCache = $a->getConfigCache();
70
71                 switch (self::$currentWizardStep) {
72                         case self::SYSTEM_CHECK:
73                         case self::DATABASE_CONFIG:
74                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
75                                 break;
76
77                         case self::SITE_SETTINGS:
78                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
79
80                                 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
81                                 self::checkSetting($configCache, $_POST, 'database', 'username', '');
82                                 self::checkSetting($configCache, $_POST, 'database', 'password', '');
83                                 self::checkSetting($configCache, $_POST, 'database', 'database', '');
84
85                                 // If we cannot connect to the database, return to the previous step
86                                 if (!self::$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
87                                         self::$currentWizardStep = self::DATABASE_CONFIG;
88                                 }
89
90                                 break;
91
92                         case self::FINISHED:
93                                 self::checkSetting($configCache, $_POST, 'config', 'php_path');
94
95                                 self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
96                                 self::checkSetting($configCache, $_POST, 'database', 'username', '');
97                                 self::checkSetting($configCache, $_POST, 'database', 'password', '');
98                                 self::checkSetting($configCache, $_POST, 'database', 'database', '');
99
100                                 self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
101                                 self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
102                                 self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
103
104                                 // If we cannot connect to the database, return to the Database config wizard
105                                 if (!self::$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
106                                         self::$currentWizardStep = self::DATABASE_CONFIG;
107                                         return;
108                                 }
109
110                                 if (!self::$installer->createConfig($a, $configCache, $a->getBasePath())) {
111                                         return;
112                                 }
113
114                                 self::$installer->installDatabase($a->getBasePath());
115
116                                 break;
117                 }
118         }
119
120         public static function content()
121         {
122                 $a = self::getApp();
123                 $configCache = $a->getConfigCache();
124
125                 $output = '';
126
127                 $install_title = L10n::t('Friendica Communications Server - Setup');
128
129                 switch (self::$currentWizardStep) {
130                         case self::SYSTEM_CHECK:
131                                 $php_path = $configCache->get('config', 'php_path');
132
133                                 $status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
134
135                                 $tpl = Renderer::getMarkupTemplate('install_checks.tpl');
136                                 $output .= Renderer::replaceMacros($tpl, [
137                                         '$title'        => $install_title,
138                                         '$pass'         => L10n::t('System check'),
139                                         '$checks'       => self::$installer->getChecks(),
140                                         '$passed'       => $status,
141                                         '$see_install'  => L10n::t('Please see the file "INSTALL.txt".'),
142                                         '$next'         => L10n::t('Next'),
143                                         '$reload'       => L10n::t('Check again'),
144                                         '$php_path'     => $php_path,
145                                         '$baseurl'      => $a->getBaseURL()
146                                 ]);
147                                 break;
148
149                         case self::DATABASE_CONFIG:
150                                 $tpl = Renderer::getMarkupTemplate('install_db.tpl');
151                                 $output .= Renderer::replaceMacros($tpl, [
152                                         '$title'    => $install_title,
153                                         '$pass'     => L10n::t('Database connection'),
154                                         '$info_01'  => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
155                                         '$info_02'  => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
156                                         '$info_03'  => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
157                                         'checks'    => self::$installer->getChecks(),
158                                         '$dbhost'   => ['database-hostname',
159                                                                         L10n::t('Database Server Name'),
160                                                                         $configCache->get('database', 'hostname'),
161                                                                         '',
162                                                                         'required'],
163                                         '$dbuser'   => ['database-username',
164                                                                         L10n::t('Database Login Name'),
165                                                                         $configCache->get('database', 'username'),
166                                                                         '',
167                                                                         'required',
168                                                                         'autofocus'],
169                                         '$dbpass'   => ['database-password',
170                                                                         L10n::t('Database Login Password'),
171                                                                         $configCache->get('database', 'password'),
172                                                                         L10n::t("For security reasons the password must not be empty"),
173                                                                         'required'],
174                                         '$dbdata'   => ['database-database',
175                                                                         L10n::t('Database Name'),
176                                                                         $configCache->get('database', 'database'),
177                                                                         '',
178                                                                         'required'],
179                                         '$lbl_10'   => L10n::t('Please select a default timezone for your website'),
180                                         '$baseurl'  => $a->getBaseURL(),
181                                         '$php_path' => $configCache->get('config', 'php_path'),
182                                         '$submit'   => L10n::t('Submit')
183                                 ]);
184                                 break;
185
186                         case self::SITE_SETTINGS:
187                                 /* Installed langs */
188                                 $lang_choices = L10n::getAvailableLanguages();
189
190                                 $tpl = Renderer::getMarkupTemplate('install_settings.tpl');
191                                 $output .= Renderer::replaceMacros($tpl, [
192                                         '$title'                => $install_title,
193                                         '$checks'               => self::$installer->getChecks(),
194                                         '$pass'                 => L10n::t('Site settings'),
195                                         '$dbhost'               => $configCache->get('database', 'hostname'),
196                                         '$dbuser'               => $configCache->get('database', 'username'),
197                                         '$dbpass'               => $configCache->get('database', 'password'),
198                                         '$dbdata'               => $configCache->get('database', 'database'),
199                                         '$phpath'               => $configCache->get('config', 'php_path'),
200                                         '$adminmail'    => ['config-admin_email',
201                                                                                 L10n::t('Site administrator email address'),
202                                                                                 $configCache->get('config', 'admin_email'),
203                                                                                 L10n::t('Your account email address must match this in order to use the web admin panel.'),
204                                                                                 'required', 'autofocus', 'email'],
205                                         '$timezone'     => Temporal::getTimezoneField('system-default_timezone',
206                                                                                 L10n::t('Please select a default timezone for your website'),
207                                                                                 $configCache->get('system', 'default_timezone'),
208                                                                         ''),
209                                         '$language'     => ['system-language',
210                                                                                 L10n::t('System Language:'),
211                                                                                 $configCache->get('system', 'language'),
212                                                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
213                                                                                 $lang_choices],
214                                         '$baseurl'              => $a->getBaseURL(),
215                                         '$submit'               => L10n::t('Submit')
216                                 ]);
217                                 break;
218
219                         case self::FINISHED:
220                                 $db_return_text = "";
221
222                                 if (count(self::$installer->getChecks()) == 0) {
223                                         $txt = '<p style="font-size: 130%;">';
224                                         $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
225                                         $db_return_text .= $txt;
226                                 }
227
228                                 $tpl = Renderer::getMarkupTemplate('install_finished.tpl');
229                                 $output .= Renderer::replaceMacros($tpl, [
230                                         '$title'  => $install_title,
231                                         '$checks' => self::$installer->getChecks(),
232                                         '$pass'   => L10n::t('Installation finished'),
233                                         '$text'   => $db_return_text . self::whatNext($a),
234                                 ]);
235
236                                 break;
237                 }
238
239                 return $output;
240         }
241
242         /**
243          * Creates the text for the next steps
244          *
245          * @param App $a The global App
246          *
247          * @return string The text for the next steps
248          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
249          */
250         private static function whatNext($a)
251         {
252                 $baseurl = $a->getBaseUrl();
253                 return
254                         L10n::t('<h1>What next</h1>')
255                         . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
256                         . L10n::t('Please see the file "INSTALL.txt".')
257                         . "</p><p>"
258                         . 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)
259                         . "</p>";
260         }
261
262         /**
263          * Checks the $_POST settings and updates the config Cache for it
264          *
265          * @param IConfigCache $configCache The current config cache
266          * @param array        $post        The $_POST data
267          * @param string       $cat         The category of the setting
268          * @param string       $key         The key of the setting
269          * @param null|string  $default     The default value
270          */
271         private static function checkSetting(IConfigCache $configCache, array $post, $cat, $key, $default = null)
272         {
273                 $configCache->set($cat, $key,
274                         Strings::escapeTags(
275                                 trim(defaults($post, sprintf('%s-%s', $cat, $key),
276                                                 (!isset($default) ? $configCache->get($cat, $key) : $default))
277                                 )
278                         )
279                 );
280         }
281 }