]> git.mxchange.org Git - friendica.git/blob - mod/install.php
Merge branch 'master' of https://github.com/friendica/friendica
[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,'phpath'))
190                                 $phpath = notags(trim($_POST['phpath']));
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                                 '$baseurl' => $a->get_baseurl(),
214                         ));
215                         return $o;
216                 }; break;
217                 
218                 case 2: { // Database config
219
220                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
221                         $dbuser = notags(trim($_POST['dbuser']));
222                         $dbpass = notags(trim($_POST['dbpass']));
223                         $dbdata = notags(trim($_POST['dbdata']));
224                         $phpath = notags(trim($_POST['phpath']));
225                         
226
227                         $tpl = get_markup_template('install_db.tpl');
228                         $o .= replace_macros($tpl, array(
229                                 '$title' => $install_title,
230                                 '$pass' => t('Database connection'),
231                                 '$info_01' => t('In order to install Friendica we need to know how to connect to your database.'),
232                                 '$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
233                                 '$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
234
235                                 '$status' => $wizard_status,
236                                 
237                                 '$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, ''),
238                                 '$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, ''),
239                                 '$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, ''),
240                                 '$dbdata' => array('dbdata', t('Database Name'), $dbdata, ''),
241                                 '$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.')),
242
243                                 
244
245                                 '$lbl_10' => t('Please select a default timezone for your website'),
246                                 
247                                 '$baseurl' => $a->get_baseurl(),
248                                 
249                                 '$phpath' => $phpath,
250                                 
251                                 '$submit' => t('Submit'),
252                                 
253                         ));
254                         return $o;
255                 }; break;
256                 case 3: { // Site settings
257                         require_once('datetime.php');
258                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
259                         $dbuser = notags(trim($_POST['dbuser']));
260                         $dbpass = notags(trim($_POST['dbpass']));
261                         $dbdata = notags(trim($_POST['dbdata']));
262                         $phpath = notags(trim($_POST['phpath']));
263                         
264                         $adminmail = notags(trim($_POST['adminmail']));
265                         $timezone = ((x($_POST,'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
266                         
267                         $tpl = get_markup_template('install_settings.tpl');
268                         $o .= replace_macros($tpl, array(
269                                 '$title' => $install_title,
270                                 '$pass' => t('Site settings'),
271
272                                 '$status' => $wizard_status,
273                                 
274                                 '$dbhost' => $dbhost, 
275                                 '$dbuser' => $dbuser,
276                                 '$dbpass' => $dbpass,
277                                 '$dbdata' => $dbdata,
278                                 '$phpath' => $phpath,
279                                 
280                                 '$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.')),
281
282                                 
283                                 '$timezone' => field_timezone('timezone', t('Please select a default timezone for your website'), $timezone, ''),
284                                 
285                                 '$baseurl' => $a->get_baseurl(),
286                                 
287                                 
288                                 
289                                 '$submit' => t('Submit'),
290                                 
291                         ));
292                         return $o;
293                 }; break;
294                         
295         }
296 }
297
298 /**
299  * checks   : array passed to template
300  * title    : string
301  * status   : boolean
302  * required : boolean
303  * help         : string optional
304  */
305 function check_add(&$checks, $title, $status, $required, $help){
306         $checks[] = array(
307                 'title' => $title,
308                 'status' => $status,
309                 'required' => $required,
310                 'help'  => $help,
311         );
312 }
313
314 function check_php(&$phpath, &$checks) {
315         if (strlen($phpath)){
316                 $passed = file_exists($phpath);
317         } else {
318                 $phpath = trim(shell_exec('which php'));
319                 $passed = strlen($phpath);
320         }
321         $help = "";
322         if(!$passed) {
323                 $help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
324                 $tpl = get_markup_template('field_input.tpl');
325                 $help .= replace_macros($tpl, array(
326                         '$field' => array('phpath', t('PHP executable path'), $phpath, t('Enter full path to php executable')),
327                 ));
328                 $phpath="";
329         }
330         
331         check_add($checks, t('Command line PHP'), $passed, true, $help);
332         
333         if($passed) {
334                 $str = autoname(8);
335                 $cmd = "$phpath testargs.php $str";
336                 $result = trim(shell_exec($cmd));
337                 $passed2 = $result == $str;
338                 $help = "";
339                 if(!$passed2) {
340                         $help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
341                         $help .= t('This is required for message delivery to work.');
342                 }
343                 check_add($checks, t('PHP register_argc_argv'), $passed, true, $help);
344         }
345         
346
347 }
348
349 function check_keys(&$checks) {
350
351         $help = '';
352
353         $res = false;
354
355         if(function_exists('openssl_pkey_new')) 
356                 $res=openssl_pkey_new(array(
357                 'digest_alg' => 'sha1',
358                 'private_key_bits' => 4096,
359                 'encrypt_key' => false ));
360
361         // Get private key
362
363         if(! $res) {
364                 $help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
365                 $help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
366         }
367         check_add($checks, t('Generate encryption keys'), $res, true, $help);
368
369 }
370
371
372 function check_funcs(&$checks) {
373         $ck_funcs = array();
374         check_add($ck_funcs, t('libCurl PHP module'), true, true, "");
375         check_add($ck_funcs, t('GD graphics PHP module'), true, true, "");
376         check_add($ck_funcs, t('OpenSSL PHP module'), true, true, "");
377         check_add($ck_funcs, t('mysqli PHP module'), true, true, "");
378         check_add($ck_funcs, t('mb_string PHP module'), true, true, "");
379                 
380         
381         if(function_exists('apache_get_modules')){
382                 if (! in_array('mod_rewrite',apache_get_modules())) {
383                         check_add($ck_funcs, t('Apache mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
384                 } else {
385                         check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
386                 }
387         }
388         if(! function_exists('curl_init')){
389                 $ck_funcs[0]['status']= false;
390                 $ck_funcs[0]['help']= t('Error: libCURL PHP module required but not installed.');
391         }
392         if(! function_exists('imagecreatefromjpeg')){
393                 $ck_funcs[1]['status']= false;
394                 $ck_funcs[1]['help']= t('Error: GD graphics PHP module with JPEG support required but not installed.');
395         }
396         if(! function_exists('openssl_public_encrypt')) {
397                 $ck_funcs[2]['status']= false;
398                 $ck_funcs[2]['help']= t('Error: openssl PHP module required but not installed.');
399         }
400         if(! function_exists('mysqli_connect')){
401                 $ck_funcs[3]['status']= false;
402                 $ck_funcs[3]['help']= t('Error: mysqli PHP module required but not installed.');
403         }
404         if(! function_exists('mb_strlen')){
405                 $ck_funcs[4]['status']= false;
406                 $ck_funcs[4]['help']= t('Error: mb_string PHP module required but not installed.');
407         }
408         
409         $checks = array_merge($checks, $ck_funcs);
410         
411         /*if((x($_SESSION,'sysmsg')) && is_array($_SESSION['sysmsg']) && count($_SESSION['sysmsg']))
412                 notice( t('Please see the file "INSTALL.txt".') . EOL);*/
413 }
414
415
416 function check_htconfig(&$checks) {
417         $status = true;
418         $help = "";
419         if(     (file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
420                 (!file_exists('.htconfig.php') && !is_writable('.')) ) {
421         
422                 $status=false;
423                 $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;
424                 $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;
425                 $help .= t('Please check with your site documentation or support people to see if this situation can be corrected.').EOL;
426                 $help .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL.txt" for instructions.').EOL; 
427         }
428
429         check_add($checks, t('.htconfig.php is writable'), $status, true, $help);
430         
431 }
432
433         
434 function manual_config(&$a) {
435         $data = htmlentities($a->data['txt']);
436         $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.');
437         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
438         return $o;
439 }
440
441 function load_database_rem($v, $i){
442         $l = trim($i);
443         if (strlen($l)>1 && ($l[0]=="-" || ($l[0]=="/" && $l[1]=="*"))){
444                 return $v;
445         } else  {
446                 return $v."\n".$i;
447         }
448 }
449
450
451 function load_database($db) {
452
453         $str = file_get_contents('database.sql');
454 //      $str = array_reduce(explode("\n", $str),"load_database_rem","");
455         $arr = explode(';',$str);
456         $errors = false;
457         foreach($arr as $a) {
458                 if(strlen(trim($a))) {  
459                         $r = @$db->q(trim($a));
460                         if(! $r) {
461                                 $errors .=  t('Errors encountered creating database tables.') . $a . EOL;
462                         }
463                 }
464         }
465         return $errors;
466 }
467
468
469