]> git.mxchange.org Git - friendica.git/blob - src/Module/Install.php
Install to Module
[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         // Default values for the install page
33         const DEFAULT_LANG = 'en';
34         const DEFAULT_TZ   = 'America/Los_Angeles';
35         const DEFAULT_HOST = 'localhost';
36
37         /**
38          * @var int The current step of the wizard
39          */
40         private static $currentWizardStep;
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 clould not have a working mod_rewrite
54                 // so we could not have a css at all. Here we set a static css file for the install procedure pages
55                 $a->setConfigValue('system', 'value', '../install');
56                 $a->theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
57
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                                 return;
70
71                         case self::SITE_SETTINGS:
72                                 $dbhost  = notags(trim(defaults($_POST, 'dbhost', self::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                                 require_once 'include/dba.php';
78                                 if (!DBA::connect($dbhost, $dbuser, $dbpass, $dbdata)) {
79                                         $a->data['db_conn_failed'] = true;
80                                 }
81
82                                 return;
83
84                         case self::FINISHED:
85                                 $urlpath   = $a->getURLPath();
86                                 $dbhost    = notags(trim(defaults($_POST, 'dbhost', self::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', self::DEFAULT_TZ)));
92                                 $language  = notags(trim(defaults($_POST, 'language', self::DEFAULT_LANG)));
93                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', '')));
94
95                                 // connect to db
96                                 DBA::connect($dbhost, $dbuser, $dbpass, $dbdata);
97
98                                 $install = new Core\Install();
99
100                                 $errors = $install->createConfig($phpath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $a->getBasePath());
101
102                                 if ($errors !== true) {
103                                         $a->data['txt'] = $errors;
104                                         return;
105                                 }
106
107                                 $errors = DBStructure::update(false, true, true);
108
109                                 if ($errors) {
110                                         $a->data['db_failed'] = $errors;
111                                 } else {
112                                         $a->data['db_installed'] = true;
113                                 }
114
115                                 return;
116
117                         default:
118                                 return;
119                 }
120         }
121
122         public static function content()
123         {
124                 $a = self::getApp();
125
126                 $output = '';
127
128                 $install_title = L10n::t('Friendica Communctions Server - Setup');
129                 $wizard_status = self::checkWizardStatus($a);
130
131                 switch (self::$currentWizardStep) {
132                         case self::SYSTEM_CHECK:
133                                 $phppath = defaults($_POST, 'phpath', null);
134
135                                 $install = new Core\Install();
136                                 $status = $install->checkAll($a->getBaseURL(), $phppath);
137
138                                 $tpl = get_markup_template('install_checks.tpl');
139                                 $output .= replace_macros($tpl, [
140                                         '$title'                => $install_title,
141                                         '$pass'                 => L10n::t('System check'),
142                                         '$checks'               => $install->getChecks(),
143                                         '$passed'               => $status,
144                                         '$see_install'  => L10n::t('Please see the file "Install.txt".'),
145                                         '$next'                 => L10n::t('Next'),
146                                         '$reload'               => L10n::t('Check again'),
147                                         '$phpath'               => $phppath,
148                                         '$baseurl'              => $a->getBaseURL()
149                                 ]);
150                                 break;
151
152                         case self::DATABASE_CONFIG:
153                                 $dbhost    = notags(trim(defaults($_POST, 'dbhost'   , self::DEFAULT_HOST)));
154                                 $dbuser    = notags(trim(defaults($_POST, 'dbuser'   , '')));
155                                 $dbpass    = notags(trim(defaults($_POST, 'dbpass'   , '')));
156                                 $dbdata    = notags(trim(defaults($_POST, 'dbdata'   , '')));
157                                 $phpath    = notags(trim(defaults($_POST, 'phpath'   , '')));
158                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', '')));
159
160                                 $tpl = get_markup_template('install_db.tpl');
161                                 $output .= replace_macros($tpl, [
162                                         '$title'        => $install_title,
163                                         '$pass'         => L10n::t('Database connection'),
164                                         '$info_01'      => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
165                                         '$info_02'      => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
166                                         '$info_03'      => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
167                                         '$status'       => $wizard_status,
168                                         '$dbhost'       => ['dbhost',
169                                                                         L10n::t('Database Server Name'),
170                                                                         $dbhost,
171                                                                         '',
172                                                                         'required'],
173                                         '$dbuser'       => ['dbuser',
174                                                                         L10n::t('Database Login Name'),
175                                                                         $dbuser,
176                                                                         '',
177                                                                         'required',
178                                                                         'autofocus'],
179                                         '$dbpass'       => ['dbpass',
180                                                                         L10n::t('Database Login Password'),
181                                                                         $dbpass,
182                                                                         L10n::t("For security reasons the password must not be empty"),
183                                                                         'required'],
184                                         '$dbdata'       => ['dbdata',
185                                                                         L10n::t('Database Name'),
186                                                                         $dbdata,
187                                                                         '',
188                                                                         'required'],
189                                         '$adminmail' => ['adminmail',
190                                                                         L10n::t('Site administrator email address'),
191                                                                         $adminmail,
192                                                                         L10n::t('Your account email address must match this in order to use the web admin panel.'),
193                                                                         'required',
194                                                                         'autofocus',
195                                                                         'email'],
196                                         '$lbl_10'       => L10n::t('Please select a default timezone for your website'),
197                                         '$baseurl'      => $a->getBaseURL(),
198                                         '$phpath'       => $phpath,
199                                         '$submit'       => L10n::t('Submit')
200                                 ]);
201                                 break;
202                         case self::SITE_SETTINGS:
203                                 $dbhost = notags(trim(defaults($_POST, 'dbhost', self::DEFAULT_HOST)));
204                                 $dbuser = notags(trim(defaults($_POST, 'dbuser', ''                )));
205                                 $dbpass = notags(trim(defaults($_POST, 'dbpass', ''                )));
206                                 $dbdata = notags(trim(defaults($_POST, 'dbdata', ''                )));
207                                 $phpath = notags(trim(defaults($_POST, 'phpath', ''                )));
208
209                                 $adminmail = notags(trim(defaults($_POST, 'adminmail', '')));
210
211                                 $timezone = defaults($_POST, 'timezone', self::DEFAULT_TZ);
212                                 /* Installed langs */
213                                 $lang_choices = L10n::getAvailableLanguages();
214
215                                 $tpl = get_markup_template('install_settings.tpl');
216                                 $output .= replace_macros($tpl, [
217                                         '$title'                => $install_title,
218                                         '$pass'                 => L10n::t('Site settings'),
219                                         '$status'               => $wizard_status,
220                                         '$dbhost'               => $dbhost,
221                                         '$dbuser'               => $dbuser,
222                                         '$dbpass'               => $dbpass,
223                                         '$dbdata'               => $dbdata,
224                                         '$phpath'               => $phpath,
225                                         '$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'],
226                                         '$timezone'     => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
227                                         '$language'     => ['language',
228                                                                                 L10n::t('System Language:'), #
229                                                                                 self::DEFAULT_LANG,
230                                                                                 L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
231                                                                                 $lang_choices],
232                                         '$baseurl'              => $a->getBaseURL(),
233                                         '$submit'               => L10n::t('Submit')
234                                 ]);
235                                 break;
236
237                         case self::FINISHED:
238                                 $db_return_text = "";
239
240                                 if (defaults($a->data, 'db_installed', false)) {
241                                         $txt = '<p style="font-size: 130%;">';
242                                         $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
243                                         $db_return_text .= $txt;
244                                 }
245
246                                 if (defaults($a->data, 'db_failed', false)) {
247                                         $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
248                                         $txt .= L10n::t('Please see the file "INSTALL.txt".') . EOL ."<hr>";
249                                         $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL;
250                                         $db_return_text .= $txt;
251                                 }
252
253                                 if (isset($a->data['txt']) && strlen($a->data['txt'])) {
254                                         $db_return_text .= self::manualConfig($a);
255                                 }
256
257                                 $tpl = get_markup_template('install.tpl');
258                                 $output .= replace_macros($tpl, [
259                                         '$title' => $install_title,
260                                         '$pass' => "",
261                                         '$text' => $db_return_text . self::whatNext($a),
262                                 ]);
263
264                                 break;
265                 }
266
267                 return $output;
268         }
269
270         /**
271          * @param App $a The global Friendica App
272          *
273          * @return string The status of Wizard steps
274          */
275         private static function checkWizardStatus($a)
276         {
277                 $wizardStatus = "";
278
279                 if (defaults($a->data, 'db_conn_failed', false)) {
280                         self::$currentWizardStep = 2;
281                         $wizardStatus = L10n::t('Could not connect to database.');
282                 }
283
284                 if (defaults($a->data, 'db_create_failed', false)) {
285                         self::$currentWizardStep = 2;
286                         $wizardStatus = L10n::t('Could not create table.');
287                 }
288
289                 if (DBA::connected()) {
290                         if (DBA::count('user')) {
291                                 self::$currentWizardStep = 2;
292                                 $wizardStatus = L10n::t('Database already in use.');
293                         }
294                 }
295
296                 return $wizardStatus;
297         }
298
299         /**
300          * Creates the text for manual config
301          *
302          * @param App $a The global App
303          *
304          * @return string The manual config text
305          */
306         private static function manualConfig($a) {
307                 $data = htmlentities($a->data['txt'],ENT_COMPAT, 'UTF-8');
308                 $output = L10n::t('The database configuration file "config/local.ini.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.');
309                 $output .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
310                 return $output;
311         }
312
313         /**
314          * Creates the text for the next steps
315          *
316          * @param App $a The global App
317          *
318          * @return string The text for the next steps
319          */
320         private static function whatNext($a) {
321                 $baseurl = $a->getBaseUrl();
322                 return
323                         L10n::t('<h1>What next</h1>')
324                         ."<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
325                         .L10n::t('Please see the file "INSTALL.txt".')
326                         ."</p><p>"
327                         .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)
328                         ."</p>";
329         }
330 }