]> git.mxchange.org Git - friendica.git/blob - mod/install.php
install: fix install procedure
[friendica.git] / mod / install.php
1 <?php
2
3 $install_wizard_pass=1;
4
5
6 function install_init(&$a){
7         global $install_wizard_pass;
8         if (x($_POST,'pass'))
9                 $install_wizard_pass = intval($_POST['pass']);
10
11 }
12
13 function install_post(&$a) {
14         global $install_wizard_pass, $db;
15
16         switch($install_wizard_pass) {
17                 case 1:
18                 case 2:
19                         return;
20                         break; // just in case return don't return :)
21                 case 3:
22                         $urlpath = $a->get_path();
23                         $dbhost = notags(trim($_POST['dbhost']));
24                         $dbuser = notags(trim($_POST['dbuser']));
25                         $dbpass = notags(trim($_POST['dbpass']));
26                         $dbdata = notags(trim($_POST['dbdata']));
27                         $phpath = notags(trim($_POST['phpath']));
28
29                         require_once("dba.php");
30                         unset($db);
31                         $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
32                         /*if(get_db_errno()) {
33                                 unset($db);
34                                 $db = new dba($dbhost, $dbuser, $dbpass, '', true);
35
36                                 if(! get_db_errno()) {
37                                         $r = q("CREATE DATABASE '%s'",
38                                                         dbesc($dbdata)
39                                         );
40                                         if($r) {
41                                                 unset($db);
42                                                 $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
43                                         } else {
44                                                 $a->data['db_create_failed']=true;
45                                         }
46                                 } else {
47                                         $a->data['db_conn_failed']=true;
48                                         return;
49                                 }
50                         }*/
51                         if(get_db_errno()) {
52                                 $a->data['db_conn_failed']=true;
53                         }
54
55                         return; 
56                         break;
57                 case 4;
58                         $urlpath = $a->get_path();
59                         $dbhost = notags(trim($_POST['dbhost']));
60                         $dbuser = notags(trim($_POST['dbuser']));
61                         $dbpass = notags(trim($_POST['dbpass']));
62                         $dbdata = notags(trim($_POST['dbdata']));
63                         $phpath = notags(trim($_POST['phpath']));
64                         $timezone = notags(trim($_POST['timezone']));
65                         $adminmail = notags(trim($_POST['adminmail']));
66
67                         // connect to db
68                         $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
69
70                         $tpl = get_intltext_template('htconfig.tpl');
71                         $txt = replace_macros($tpl,array(
72                                 '$dbhost' => $dbhost,
73                                 '$dbuser' => $dbuser,
74                                 '$dbpass' => $dbpass,
75                                 '$dbdata' => $dbdata,
76                                 '$timezone' => $timezone,
77                                 '$urlpath' => $urlpath,
78                                 '$phpath' => $phpath,
79                                 '$adminmail' => $adminmail
80                         ));
81
82                         $result = file_put_contents('.htconfig.php', $txt);
83                         if(! $result) {
84                                 $a->data['txt'] = $txt;
85                         }
86
87                         $errors = load_database($db);
88
89                         if($errors)
90                                 $a->data['db_failed'] = $errors;
91                         else
92                                 $a->data['db_installed'] = true;
93
94                         return;
95                 break;
96         }
97 }
98
99 function get_db_errno() {
100         if(class_exists('mysqli'))
101                 return mysqli_connect_errno();
102         else
103                 return mysql_errno();
104 }               
105
106 function install_content(&$a) {
107
108         global $install_wizard_pass, $db;
109         $o = '';
110         $wizard_status = "";
111         $install_title = t('Friendica Social Communications Server - Setup');
112         
113         if(x($a->data,'txt') && strlen($a->data['txt'])) {
114                 $tpl = get_markup_template('install.tpl');
115                 return replace_macros($tpl, array(
116                         '$title' => $install_title,
117                         '$pass' => t('Database connection'),
118                         '$text' => manual_config($a),
119                 ));
120         }
121         
122         if(x($a->data,'db_conn_failed')) {
123                 $install_wizard_pass = 2;
124                 $wizard_status =  t('Could not connect to database.');
125         }
126         if(x($a->data,'db_create_failed')) {
127                 $install_wizard_pass = 2;
128                 $wizard_status =  t('Could not create table.');
129         }
130         
131         if(x($a->data,'db_installed')) {
132                 $txt = '<p style="font-size: 130%;">';
133                 $txt .= t('Your Friendica site database has been installed.') . EOL;
134                 $txt .= t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.') . EOL ;
135                 $txt .= t('Please see the file "INSTALL.txt".') . EOL ;
136                 $txt .= '<br />';
137                 $txt .= '<a href="' . $a->get_baseurl() . '/register' . '">' . t('Proceed to registration') . '</a>' ;
138                 $txt .= '</p>';
139                 
140                 $tpl = get_markup_template('install.tpl');
141                 return replace_macros($tpl, array(
142                         '$title' => $install_title,
143                         '$pass' => t('Proceed with Installation'),
144                         '$text' => $txt,
145                 ));
146
147         }
148
149         if(x($a->data,'db_failed')) {
150                 $txt = t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
151                 $txt .= t('Please see the file "INSTALL.txt".') . EOL ."<hr>" ;
152                 $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL ;
153
154                 $tpl = get_markup_template('install.tpl');
155                 return replace_macros($tpl, array(
156                         '$title' => $install_title,
157                         '$pass' => t('Database connection'),
158                         '$status' => t('Database import failed.'),
159                         '$text' => $txt,
160                 ));
161                 
162         }
163
164         if($db && $db->connected) {
165                 $r = q("SELECT COUNT(*) as `total` FROM `user`");
166                 if($r && count($r) && $r[0]['total']) {
167                         $tpl = get_markup_template('install.tpl');
168                         return replace_macros($tpl, array(
169                                 '$title' => $install_title,
170                                 '$pass' => '',
171                                 '$status' => t('Permission denied.'),
172                                 '$text' => '',
173                         ));
174                 }
175         }
176
177         switch ($install_wizard_pass){
178                 case 1: { // System check
179
180
181                         $checks = array();
182
183                         check_funcs($checks);
184
185                         check_htconfig($checks);
186
187                         check_keys($checks);
188                         
189                         if(x($_POST,'phppath'))
190                                 $phpath = notags(trim($_POST['phppath']));
191
192                         check_php($phpath, $checks);
193
194
195                         function check_passed($v, $c){
196                                 if ($c['required'])
197                                         $v = $v && $c['status'];
198                                 return $v;
199                         }
200                         $checkspassed = array_reduce($checks, "check_passed", true);
201                         
202
203                         $tpl = get_markup_template('install_checks.tpl');
204                         $o .= replace_macros($tpl, array(
205                                 '$title' => $install_title,
206                                 '$pass' => t('System check'),
207                                 '$checks' => $checks,
208                                 '$passed' => $checkspassed,
209                                 '$see_install' => t('Please see the file "INSTALL.txt".'),
210                                 '$next' => t('Next'),
211                                 '$reload' => t('Check again'),
212                                 '$phpath' => $phpath,
213                         ));
214                         return $o;
215                 }; break;
216                 
217                 case 2: { // Database config
218
219                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
220                         $dbuser = notags(trim($_POST['dbuser']));
221                         $dbpass = notags(trim($_POST['dbpass']));
222                         $dbdata = notags(trim($_POST['dbdata']));
223                         $phpath = notags(trim($_POST['phppath']));
224                         
225
226                         $tpl = get_markup_template('install_db.tpl');
227                         $o .= replace_macros($tpl, array(
228                                 '$title' => $install_title,
229                                 '$pass' => t('Database connection'),
230                                 '$info_01' => t('In order to install Friendica we need to know how to connect to your database.'),
231                                 '$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
232                                 '$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
233
234                                 '$status' => $wizard_status,
235                                 
236                                 '$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, ''),
237                                 '$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, ''),
238                                 '$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, ''),
239                                 '$dbdata' => array('dbdata', t('Database Name'), $dbdata, ''),
240                                 '$adminmail' => array('adminmail', t('Site administrator email address'), $adminmail, t('Your account email address must match this in order to use the web admin panel.')),
241
242                                 
243
244                                 '$lbl_10' => t('Please select a default timezone for your website'),
245                                 
246                                 '$baseurl' => $a->get_baseurl(),
247                                 
248                                 '$phpath' => $phpath,
249                                 
250                                 '$submit' => t('Submit'),
251                                 
252                         ));
253                         return $o;
254                 }; break;
255                 case 3: { // Site settings
256                         require_once('datetime.php');
257                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
258                         $dbuser = notags(trim($_POST['dbuser']));
259                         $dbpass = notags(trim($_POST['dbpass']));
260                         $dbdata = notags(trim($_POST['dbdata']));
261                         $phpath = notags(trim($_POST['phppath']));
262                         
263                         $adminmail = notags(trim($_POST['adminmail']));
264                         $timezone = ((x($_POST,'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
265                         
266                         $tpl = get_markup_template('install_settings.tpl');
267                         $o .= replace_macros($tpl, array(
268                                 '$title' => $install_title,
269                                 '$pass' => t('Site settings'),
270
271                                 '$status' => $wizard_status,
272                                 
273                                 '$dbhost' => $dbhost, 
274                                 '$dbuser' => $dbuser,
275                                 '$dbpass' => $dbpass,
276                                 '$dbdata' => $dbdata,
277                                 '$phpath' => $phpath,
278                                 
279                                 '$adminmail' => array('adminmail', t('Site administrator email address'), $adminmail, t('Your account email address must match this in order to use the web admin panel.')),
280
281                                 
282                                 '$timezone' => field_timezone('timezone', t('Please select a default timezone for your website'), $timezone, ''),
283                                 
284                                 '$baseurl' => $a->get_baseurl(),
285                                 
286                                 
287                                 
288                                 '$submit' => t('Submit'),
289                                 
290                         ));
291                         return $o;
292                 }; break;
293                         
294         }
295 }
296
297 /**
298  * checks   : array passed to template
299  * title    : string
300  * status   : boolean
301  * required : boolean
302  * help         : string optional
303  */
304 function check_add(&$checks, $title, $status, $required, $help){
305         $checks[] = array(
306                 'title' => $title,
307                 'status' => $status,
308                 'required' => $required,
309                 'help'  => $help,
310         );
311 }
312
313 function check_php(&$phpath, &$checks) {
314         if (strlen($phpath)){
315                 $passed = file_exists($phpath);
316         } else {
317                 $phpath = trim(shell_exec('which php'));
318                 $passed = strlen($phpath);
319         }
320         $help = "";
321         if(!$passed) {
322                 $help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
323                 $tpl = get_markup_template('field_input.tpl');
324                 $help .= replace_macros($tpl, array(
325                         '$field' => array('phppath', t('PHP executable path'), $phpath, t('Enter full path to php executable')),
326                 ));
327                 $phpath="";
328         }
329         
330         check_add($checks, t('Command line PHP'), $passed, true, $help);
331         
332         if($passed) {
333                 $str = autoname(8);
334                 $cmd = "$phpath testargs.php $str";
335                 $result = trim(shell_exec($cmd));
336                 $passed2 = $result == $str;
337                 $help = "";
338                 if(!$passed2) {
339                         $help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
340                         $help .= t('This is required for message delivery to work.');
341                 }
342                 check_add($checks, t('PHP "register_argc_argv"'), $passed, true, $help);
343         }
344         
345
346 }
347
348 function check_keys(&$checks) {
349
350         $help = '';
351
352         $res = false;
353
354         if(function_exists('openssl_pkey_new')) 
355                 $res=openssl_pkey_new(array(
356                 'digest_alg' => 'sha1',
357                 'private_key_bits' => 4096,
358                 'encrypt_key' => false ));
359
360         // Get private key
361
362         if(! $res) {
363                 $help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
364                 $help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
365         }
366         check_add($checks, t('Generate encryption keys'), $res, true, $help);
367
368 }
369
370
371 function check_funcs(&$checks) {
372         $ck_funcs = array();
373         check_add($ck_funcs, t('libCurl PHP module'), true, true, "");
374         check_add($ck_funcs, t('GD graphics PHP module'), true, true, "");
375         check_add($ck_funcs, t('OpenSSL PHP module'), true, true, "");
376         check_add($ck_funcs, t('mysqli PHP module'), true, true, "");
377         check_add($ck_funcs, t('mb_string PHP module'), true, true, "");
378                 
379         
380         if(function_exists('apache_get_modules')){
381                 if (! in_array('mod_rewrite',apache_get_modules())) {
382                         check_add($ck_funcs, t('Apace mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
383                 } else {
384                         check_add($ck_funcs, t('Apace mod_rewrite module'), true, true, "");
385                 }
386         }
387         if(! function_exists('curl_init')){
388                 $ck_funcs[0]['status']= false;
389                 $ck_funcs[0]['help']= t('Error: libCURL PHP module required but not installed.');
390         }
391         if(! function_exists('imagecreatefromjpeg')){
392                 $ck_funcs[1]['status']= false;
393                 $ck_funcs[1]['help']= t('Error: GD graphics PHP module with JPEG support required but not installed.');
394         }
395         if(! function_exists('openssl_public_encrypt')) {
396                 $ck_funcs[2]['status']= false;
397                 $ck_funcs[2]['help']= t('Error: openssl PHP module required but not installed.');
398         }
399         if(! function_exists('mysqli_connect')){
400                 $ck_funcs[3]['status']= false;
401                 $ck_funcs[3]['help']= t('Error: mysqli PHP module required but not installed.');
402         }
403         if(! function_exists('mb_strlen')){
404                 $ck_funcs[4]['status']= false;
405                 $ck_funcs[4]['help']= t('Error: mb_string PHP module required but not installed.');
406         }
407         
408         $checks = array_merge($checks, $ck_funcs);
409         
410         /*if((x($_SESSION,'sysmsg')) && is_array($_SESSION['sysmsg']) && count($_SESSION['sysmsg']))
411                 notice( t('Please see the file "INSTALL.txt".') . EOL);*/
412 }
413
414
415 function check_htconfig(&$checks) {
416         $status = true;
417         $help = "";
418         if(     (file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
419                 (!file_exists('.htconfig.php') && !is_writable('.')) ) {
420         
421                 $status=false;
422                 $help = t('The web installer needs to be able to create a file called ".htconfig.php" in the top folder of your web server and it is unable to do so.') .EOL;
423                 $help .= t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.').EOL;
424                 $help .= t('Please check with your site documentation or support people to see if this situation can be corrected.').EOL;
425                 $help .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL.txt" for instructions.').EOL; 
426         }
427
428         check_add($checks, t('.htconfig.php is writable'), $status, true, $help);
429         
430 }
431
432         
433 function manual_config(&$a) {
434         $data = htmlentities($a->data['txt']);
435         $o = t('The database configuration file ".htconfig.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.');
436         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
437         return $o;
438 }
439
440 function load_database_rem($v, $i){
441         $l = trim($i);
442         if (strlen($l)>1 && ($l[0]=="-" || ($l[0]=="/" && $l[1]=="*"))){
443                 return $v;
444         } else  {
445                 return $v."\n".$i;
446         }
447 }
448
449
450 function load_database($db) {
451
452         $str = file_get_contents('database.sql');
453         $str = array_reduce(explode("\n", $str),"load_database_rem","");
454         $arr = explode(';',$str);
455         $errors = false;
456         foreach($arr as $a) {
457                 if(strlen(trim($a))) {  
458                         $r = @$db->q(trim($a));
459                         if(! $r) {
460                                 $errors .=  t('Errors encountered creating database tables.') . $a . EOL;
461                         }
462                 }
463         }
464         return $errors;
465 }