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