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