]> git.mxchange.org Git - friendica.git/blob - mod/install.php
Cleanups: isResult() more used, readability improved (#5608)
[friendica.git] / mod / install.php
1 <?php
2 /**
3  * @file mod/install.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Install;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Util\Temporal;
12
13 $install_wizard_pass = 1;
14
15 function install_init(App $a) {
16
17         // $baseurl/install/testrwrite to test if rewite in .htaccess is working
18         if ($a->argc == 2 && $a->argv[1] == "testrewrite") {
19                 echo "ok";
20                 killme();
21         }
22
23         // We overwrite current theme css, because during install we could not have a working mod_rewrite
24         // so we could not have a css at all. Here we set a static css file for the install procedure pages
25
26         $a->setConfigValue('system', 'value', '../install');
27         $a->theme['stylesheet'] = System::baseUrl()."/view/install/style.css";
28
29         Install::setInstallMode();
30
31         global $install_wizard_pass;
32         if (x($_POST, 'pass')) {
33                 $install_wizard_pass = intval($_POST['pass']);
34         }
35
36 }
37
38 function install_post(App $a) {
39         global $install_wizard_pass;
40
41         switch($install_wizard_pass) {
42                 case 1:
43                 case 2:
44                         return;
45                         break; // just in case return don't return :)
46                 case 3:
47                         $urlpath = $a->get_path();
48                         $dbhost = notags(trim($_POST['dbhost']));
49                         $dbuser = notags(trim($_POST['dbuser']));
50                         $dbpass = notags(trim($_POST['dbpass']));
51                         $dbdata = notags(trim($_POST['dbdata']));
52                         $phpath = notags(trim($_POST['phpath']));
53
54                         require_once("include/dba.php");
55                         if (!DBA::connect($dbhost, $dbuser, $dbpass, $dbdata)) {
56                                 $a->data['db_conn_failed'] = true;
57                         }
58
59                         return;
60                         break;
61                 case 4:
62                         $urlpath = $a->get_path();
63                         $dbhost = notags(trim($_POST['dbhost']));
64                         $dbuser = notags(trim($_POST['dbuser']));
65                         $dbpass = notags(trim($_POST['dbpass']));
66                         $dbdata = notags(trim($_POST['dbdata']));
67                         $phpath = notags(trim($_POST['phpath']));
68                         $timezone = notags(trim($_POST['timezone']));
69                         $language = notags(trim($_POST['language']));
70                         $adminmail = notags(trim($_POST['adminmail']));
71
72                         // connect to db
73                         DBA::connect($dbhost, $dbuser, $dbpass, $dbdata);
74
75                         $errors = Install::createConfig($urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $phpath, $timezone, $language, $adminmail);
76
77                         if ($errors) {
78                                 $a->data['db_failed'] = $errors;
79                                 return;
80                         }
81
82                         $errors = Install::installDatabaseStructure();
83
84                         if ($errors) {
85                                 $a->data['db_failed'] = $errors;
86                         } else {
87                                 $a->data['db_installed'] = true;
88                         }
89
90                         return;
91                 break;
92         }
93 }
94
95 function install_content(App $a) {
96
97         global $install_wizard_pass;
98         $o = '';
99         $wizard_status = "";
100         $install_title = L10n::t('Friendica Communications Server - Setup');
101
102
103
104         if (x($a->data, 'db_conn_failed')) {
105                 $install_wizard_pass = 2;
106                 $wizard_status = L10n::t('Could not connect to database.');
107         }
108         if (x($a->data, 'db_create_failed')) {
109                 $install_wizard_pass = 2;
110                 $wizard_status = L10n::t('Could not create table.');
111         }
112
113         $db_return_text = "";
114         if (x($a->data, 'db_installed')) {
115                 $txt = '<p style="font-size: 130%;">';
116                 $txt .= L10n::t('Your Friendica site database has been installed.') . EOL;
117                 $db_return_text .= $txt;
118         }
119
120         if (x($a->data, 'db_failed')) {
121                 $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
122                 $txt .= L10n::t('Please see the file "INSTALL.txt".') . EOL ."<hr>";
123                 $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL;
124                 $db_return_text .= $txt;
125         }
126
127         if (DBA::$connected) {
128                 $r = q("SELECT COUNT(*) as `total` FROM `user`");
129                 if (DBA::isResult($r) && $r[0]['total']) {
130                         $tpl = get_markup_template('install.tpl');
131                         return replace_macros($tpl, [
132                                 '$title' => $install_title,
133                                 '$pass' => '',
134                                 '$status' => L10n::t('Database already in use.'),
135                                 '$text' => '',
136                         ]);
137                 }
138         }
139
140         if (x($a->data, 'txt') && strlen($a->data['txt'])) {
141                 $db_return_text .= manual_config($a);
142         }
143
144         if ($db_return_text != "") {
145                 $tpl = get_markup_template('install.tpl');
146                 return replace_macros($tpl, [
147                         '$title' => $install_title,
148                         '$pass' => "",
149                         '$text' => $db_return_text . what_next(),
150                 ]);
151         }
152
153         switch ($install_wizard_pass) {
154                 case 1: { // System check
155
156                         $phpath = defaults($_POST, 'phpath', 'php');
157
158                         list($checks, $checkspassed) = Install::check($phpath);
159
160                         $tpl = get_markup_template('install_checks.tpl');
161                         $o .= replace_macros($tpl, [
162                                 '$title' => $install_title,
163                                 '$pass' => L10n::t('System check'),
164                                 '$checks' => $checks,
165                                 '$passed' => $checkspassed,
166                                 '$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
167                                 '$next' => L10n::t('Next'),
168                                 '$reload' => L10n::t('Check again'),
169                                 '$phpath' => $phpath,
170                                 '$baseurl' => System::baseUrl(),
171                         ]);
172                         return $o;
173                 }; break;
174
175                 case 2: { // Database config
176
177                         $dbhost    = notags(trim(defaults($_POST, 'dbhost'   , 'localhost')));
178                         $dbuser    = notags(trim(defaults($_POST, 'dbuser'   , ''         )));
179                         $dbpass    = notags(trim(defaults($_POST, 'dbpass'   , ''         )));
180                         $dbdata    = notags(trim(defaults($_POST, 'dbdata'   , ''         )));
181                         $phpath    = notags(trim(defaults($_POST, 'phpath'   , ''         )));
182                         $adminmail = notags(trim(defaults($_POST, 'adminmail', ''         )));
183
184                         $tpl = get_markup_template('install_db.tpl');
185                         $o .= replace_macros($tpl, [
186                                 '$title' => $install_title,
187                                 '$pass' => L10n::t('Database connection'),
188                                 '$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
189                                 '$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
190                                 '$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
191
192                                 '$status' => $wizard_status,
193
194                                 '$dbhost' => ['dbhost', L10n::t('Database Server Name'), $dbhost, '', 'required'],
195                                 '$dbuser' => ['dbuser', L10n::t('Database Login Name'), $dbuser, '', 'required', 'autofocus'],
196                                 '$dbpass' => ['dbpass', L10n::t('Database Login Password'), $dbpass, L10n::t("For security reasons the password must not be empty"), 'required'],
197                                 '$dbdata' => ['dbdata', L10n::t('Database Name'), $dbdata, '', 'required'],
198                                 '$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'],
199
200                                 '$lbl_10' => L10n::t('Please select a default timezone for your website'),
201
202                                 '$baseurl' => System::baseUrl(),
203
204                                 '$phpath' => $phpath,
205
206                                 '$submit' => L10n::t('Submit'),
207                         ]);
208                         return $o;
209                 }; break;
210                 case 3: { // Site settings
211                         $dbhost = ((x($_POST, 'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
212                         $dbuser = notags(trim($_POST['dbuser']));
213                         $dbpass = notags(trim($_POST['dbpass']));
214                         $dbdata = notags(trim($_POST['dbdata']));
215                         $phpath = notags(trim($_POST['phpath']));
216
217                         $adminmail = notags(trim($_POST['adminmail']));
218                         $timezone = ((x($_POST, 'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
219                         /* Installed langs */
220                         $lang_choices = L10n::getAvailableLanguages();
221
222                         $tpl = get_markup_template('install_settings.tpl');
223                         $o .= replace_macros($tpl, [
224                                 '$title' => $install_title,
225                                 '$pass' => L10n::t('Site settings'),
226
227                                 '$status' => $wizard_status,
228
229                                 '$dbhost' => $dbhost,
230                                 '$dbuser' => $dbuser,
231                                 '$dbpass' => $dbpass,
232                                 '$dbdata' => $dbdata,
233                                 '$phpath' => $phpath,
234
235                                 '$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'],
236
237
238                                 '$timezone' => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
239                                 '$language' => ['language', L10n::t('System Language:'), 'en', L10n::t('Set the default language for your Friendica installation interface and to send emails.'), $lang_choices],
240                                 '$baseurl' => System::baseUrl(),
241
242
243
244                                 '$submit' => L10n::t('Submit'),
245
246                         ]);
247                         return $o;
248                 }; break;
249
250         }
251 }
252
253 function manual_config(App $a) {
254         $data = htmlentities($a->data['txt'],ENT_COMPAT, 'UTF-8');
255         $o = 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.');
256         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
257         return $o;
258 }
259
260 function load_database_rem($v, $i) {
261         $l = trim($i);
262         if (strlen($l)>1 && ($l[0] == "-" || ($l[0] == "/" && $l[1] == "*"))) {
263                 return $v;
264         } else  {
265                 return $v."\n".$i;
266         }
267 }
268
269 function what_next() {
270         $baseurl = System::baseUrl();
271         return
272                 L10n::t('<h1>What next</h1>')
273                 ."<p>".L10n::t('IMPORTANT: You will need to [manually] setup a scheduled task for the worker.')
274                 .L10n::t('Please see the file "INSTALL.txt".')
275                 ."</p><p>"
276                 .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)
277                 ."</p>";
278 }