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