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