]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Refactoring 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\Database\DBA;
8 use Friendica\Database\DBStructure;
9 use Friendica\Core;
10 use Friendica\Core\L10n;
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                 $a->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  = notags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
73                                 $dbuser  = notags(trim(defaults($_POST, 'dbuser', '')));
74                                 $dbpass  = notags(trim(defaults($_POST, 'dbpass', '')));
75                                 $dbdata  = notags(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    = notags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
87                                 $dbuser    = notags(trim(defaults($_POST, 'dbuser', '')));
88                                 $dbpass    = notags(trim(defaults($_POST, 'dbpass', '')));
89                                 $dbdata    = notags(trim(defaults($_POST, 'dbdata', '')));
90                                 $phpath    = notags(trim(defaults($_POST, 'phpath', '')));
91                                 $timezone  = notags(trim(defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ)));
92                                 $language  = notags(trim(defaults($_POST, 'language', Core\Installer::DEFAULT_LANG)));
93                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', '')));
94
95                                 // If we cannot connect to the database, return to the Database config wizard
96                                 if (!self::$installer->checkDB($dbhost, $dbuser, $dbpass, $dbdata)) {
97                                         self::$currentWizardStep = self::DATABASE_CONFIG;
98                                 }
99
100                                 if (!self::$installer->createConfig($phpath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $a->getBasePath())) {
101                                         return;
102                                 }
103
104                                 self::$installer->installDatabase();
105
106                                 break;
107                 }
108         }
109
110         public static function content()
111         {
112                 $a = self::getApp();
113
114                 $output = '';
115
116                 $install_title = L10n::t('Friendica Communctions Server - Setup');
117
118                 switch (self::$currentWizardStep) {
119                         case self::SYSTEM_CHECK:
120                                 $phppath = defaults($_POST, 'phpath', null);
121
122                                 $status = self::$installer->checkEnvironment($a->getBaseURL(), $phppath);
123
124                                 $tpl = get_markup_template('install_checks.tpl');
125                                 $output .= replace_macros($tpl, [
126                                         '$title'                => $install_title,
127                                         '$pass'                 => L10n::t('System check'),
128                                         '$checks'               => self::$installer->getChecks(),
129                                         '$passed'               => $status,
130                                         '$see_install'  => L10n::t('Please see the file "Install.txt".'),
131                                         '$next'                 => L10n::t('Next'),
132                                         '$reload'               => L10n::t('Check again'),
133                                         '$phpath'               => $phppath,
134                                         '$baseurl'              => $a->getBaseURL()
135                                 ]);
136                                 break;
137
138                         case self::DATABASE_CONFIG:
139                                 $dbhost    = notags(trim(defaults($_POST, 'dbhost'   , Core\Installer::DEFAULT_HOST)));
140                                 $dbuser    = notags(trim(defaults($_POST, 'dbuser'   , ''                          )));
141                                 $dbpass    = notags(trim(defaults($_POST, 'dbpass'   , ''                          )));
142                                 $dbdata    = notags(trim(defaults($_POST, 'dbdata'   , ''                          )));
143                                 $phpath    = notags(trim(defaults($_POST, 'phpath'   , ''                          )));
144                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', ''                          )));
145
146                                 $tpl = get_markup_template('install_db.tpl');
147                                 $output .= replace_macros($tpl, [
148                                         '$title'        => $install_title,
149                                         '$pass'         => L10n::t('Database connection'),
150                                         '$info_01'      => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
151                                         '$info_02'      => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
152                                         '$info_03'      => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
153                                         'checks'        => self::$installer->getChecks(),
154                                         '$dbhost'       => ['dbhost',
155                                                                         L10n::t('Database Server Name'),
156                                                                         $dbhost,
157                                                                         '',
158                                                                         'required'],
159                                         '$dbuser'       => ['dbuser',
160                                                                         L10n::t('Database Login Name'),
161                                                                         $dbuser,
162                                                                         '',
163                                                                         'required',
164                                                                         'autofocus'],
165                                         '$dbpass'       => ['dbpass',
166                                                                         L10n::t('Database Login Password'),
167                                                                         $dbpass,
168                                                                         L10n::t("For security reasons the password must not be empty"),
169                                                                         'required'],
170                                         '$dbdata'       => ['dbdata',
171                                                                         L10n::t('Database Name'),
172                                                                         $dbdata,
173                                                                         '',
174                                                                         'required'],
175                                         '$adminmail' => ['adminmail',
176                                                                         L10n::t('Site administrator email address'),
177                                                                         $adminmail,
178                                                                         L10n::t('Your account email address must match this in order to use the web admin panel.'),
179                                                                         'required',
180                                                                         'autofocus',
181                                                                         'email'],
182                                         '$lbl_10'       => L10n::t('Please select a default timezone for your website'),
183                                         '$baseurl'      => $a->getBaseURL(),
184                                         '$phpath'       => $phpath,
185                                         '$submit'       => L10n::t('Submit')
186                                 ]);
187                                 break;
188
189                         case self::SITE_SETTINGS:
190                                 $dbhost = notags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
191                                 $dbuser = notags(trim(defaults($_POST, 'dbuser', ''                          )));
192                                 $dbpass = notags(trim(defaults($_POST, 'dbpass', ''                          )));
193                                 $dbdata = notags(trim(defaults($_POST, 'dbdata', ''                          )));
194                                 $phpath = notags(trim(defaults($_POST, 'phpath', ''                          )));
195
196                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', '')));
197
198                                 $timezone = defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ);
199                                 /* Installed langs */
200                                 $lang_choices = L10n::getAvailableLanguages();
201
202                                 $tpl = get_markup_template('install_settings.tpl');
203                                 $output .= replace_macros($tpl, [
204                                         '$title'                => $install_title,
205                                         '$checks'               => self::$installer->getChecks(),
206                                         '$pass'                 => L10n::t('Site settings'),
207                                         '$dbhost'               => $dbhost,
208                                         '$dbuser'               => $dbuser,
209                                         '$dbpass'               => $dbpass,
210                                         '$dbdata'               => $dbdata,
211                                         '$phpath'               => $phpath,
212                                         '$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'],
213                                         '$timezone'     => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
214                                         '$language'     => ['language',
215                                                                                 L10n::t('System Language:'),
216                                                                                 Core\Installer::DEFAULT_LANG,
217                                                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
218                                                                                 $lang_choices],
219                                         '$baseurl'              => $a->getBaseURL(),
220                                         '$submit'               => L10n::t('Submit')
221                                 ]);
222                                 break;
223
224                         case self::FINISHED:
225                                 $db_return_text = "";
226
227                                 if (count(self::$installer->getChecks()) == 0) {
228                                         $txt = '<p style="font-size: 130%;">';
229                                         $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
230                                         $db_return_text .= $txt;
231                                 }
232
233                                 $tpl = get_markup_template('install_finished.tpl');
234                                 $output .= replace_macros($tpl, [
235                                         '$title'  => $install_title,
236                                         '$checks' => self::$installer->getChecks(),
237                                         '$pass'   => L10n::t('Installation finished'),
238                                         '$text'   => $db_return_text . self::whatNext($a),
239                                 ]);
240
241                                 break;
242                 }
243
244                 return $output;
245         }
246
247         /**
248          * Creates the text for the next steps
249          *
250          * @param App $a The global App
251          *
252          * @return string The text for the next steps
253          */
254         private static function whatNext($a)
255         {
256                 $baseurl = $a->getBaseUrl();
257                 return
258                         L10n::t('<h1>What next</h1>')
259                         . "<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
260                         . L10n::t('Please see the file "INSTALL.txt".')
261                         . "</p><p>"
262                         . 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)
263                         . "</p>";
264         }
265 }