]> git.mxchange.org Git - friendica.git/blob - mod/install.php
bypass smarty wherever using intltext_templates (install, register, friend confirmati...
[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("include/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                         // disable smarty for this template
77                         $engine = $a->get_template_engine();
78                         $a->set_template_engine();
79                         $tpl = get_intltext_template('htconfig.tpl');
80                         $txt = replace_macros($tpl,array(
81                                 '$dbhost' => $dbhost,
82                                 '$dbuser' => $dbuser,
83                                 '$dbpass' => $dbpass,
84                                 '$dbdata' => $dbdata,
85                                 '$timezone' => $timezone,
86                                 '$urlpath' => $urlpath,
87                                 '$phpath' => $phpath,
88                                 '$adminmail' => $adminmail
89                         ));
90                         $a->set_template_engine($engine);
91
92                         $result = file_put_contents('.htconfig.php', $txt);
93                         if(! $result) {
94                                 $a->data['txt'] = $txt;
95                         }
96
97                         $errors = load_database($db);
98
99                         if($errors)
100                                 $a->data['db_failed'] = $errors;
101                         else
102                                 $a->data['db_installed'] = true;
103
104                         return;
105                 break;
106         }
107 }
108
109 function get_db_errno() {
110         if(class_exists('mysqli'))
111                 return mysqli_connect_errno();
112         else
113                 return mysql_errno();
114 }               
115
116 function install_content(&$a) {
117
118         global $install_wizard_pass, $db;
119         $o = '';
120         $wizard_status = "";
121         $install_title = t('Friendica Social Communications Server - Setup');
122         
123
124         
125         if(x($a->data,'db_conn_failed')) {
126                 $install_wizard_pass = 2;
127                 $wizard_status =  t('Could not connect to database.');
128         }
129         if(x($a->data,'db_create_failed')) {
130                 $install_wizard_pass = 2;
131                 $wizard_status =  t('Could not create table.');
132         }
133         
134         $db_return_text="";
135         if(x($a->data,'db_installed')) {
136                 $txt = '<p style="font-size: 130%;">';
137                 $txt .= t('Your Friendica site database has been installed.') . EOL;
138                 $db_return_text .= $txt;
139         }
140
141         if(x($a->data,'db_failed')) {
142                 $txt = t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
143                 $txt .= t('Please see the file "INSTALL.txt".') . EOL ."<hr>" ;
144                 $txt .= "<pre>".$a->data['db_failed'] . "</pre>". EOL ;
145                 $db_return_text .= $txt;
146         }
147         
148         if($db && $db->connected) {
149                 $r = q("SELECT COUNT(*) as `total` FROM `user`");
150                 if($r && count($r) && $r[0]['total']) {
151                         $tpl = get_markup_template('install.tpl');
152                         return replace_macros($tpl, array(
153                                 '$title' => $install_title,
154                                 '$pass' => '',
155                                 '$status' => t('Permission denied.'),
156                                 '$text' => '',
157                         ));
158                 }
159         }
160
161         if(x($a->data,'txt') && strlen($a->data['txt'])) {
162                 $db_return_text .= manual_config($a);
163         }
164         
165         if ($db_return_text!="") {
166                 $tpl = get_markup_template('install.tpl');
167                 return replace_macros($tpl, array(
168                         '$title' => $install_title,
169                         '$pass' => "",
170                         '$text' => $db_return_text . what_next(),
171                 ));
172         }
173         
174         switch ($install_wizard_pass){
175                 case 1: { // System check
176
177
178                         $checks = array();
179
180                         check_funcs($checks);
181
182                         check_htconfig($checks);
183
184                         check_smarty3($checks);
185
186                         check_keys($checks);
187                         
188                         if(x($_POST,'phpath'))
189                                 $phpath = notags(trim($_POST['phpath']));
190
191                         check_php($phpath, $checks);
192
193             check_htaccess($checks);
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
204                         $tpl = get_markup_template('install_checks.tpl');
205                         $o .= replace_macros($tpl, array(
206                                 '$title' => $install_title,
207                                 '$pass' => t('System check'),
208                                 '$checks' => $checks,
209                                 '$passed' => $checkspassed,
210                                 '$see_install' => t('Please see the file "INSTALL.txt".'),
211                                 '$next' => t('Next'),
212                                 '$reload' => t('Check again'),
213                                 '$phpath' => $phpath,
214                                 '$baseurl' => $a->get_baseurl(),
215                         ));
216                         return $o;
217                 }; break;
218                 
219                 case 2: { // Database config
220
221                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
222                         $dbuser = notags(trim($_POST['dbuser']));
223                         $dbpass = notags(trim($_POST['dbpass']));
224                         $dbdata = notags(trim($_POST['dbdata']));
225                         $phpath = notags(trim($_POST['phpath']));
226                         
227
228                         $tpl = get_markup_template('install_db.tpl');
229                         $o .= replace_macros($tpl, array(
230                                 '$title' => $install_title,
231                                 '$pass' => t('Database connection'),
232                                 '$info_01' => t('In order to install Friendica we need to know how to connect to your database.'),
233                                 '$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
234                                 '$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
235
236                                 '$status' => $wizard_status,
237                                 
238                                 '$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, ''),
239                                 '$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, ''),
240                                 '$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, ''),
241                                 '$dbdata' => array('dbdata', t('Database Name'), $dbdata, ''),
242                                 '$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.')),
243
244                                 
245
246                                 '$lbl_10' => t('Please select a default timezone for your website'),
247                                 
248                                 '$baseurl' => $a->get_baseurl(),
249                                 
250                                 '$phpath' => $phpath,
251                                 
252                                 '$submit' => t('Submit'),
253                                 
254                         ));
255                         return $o;
256                 }; break;
257                 case 3: { // Site settings
258                         require_once('include/datetime.php');
259                         $dbhost = ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost');
260                         $dbuser = notags(trim($_POST['dbuser']));
261                         $dbpass = notags(trim($_POST['dbpass']));
262                         $dbdata = notags(trim($_POST['dbdata']));
263                         $phpath = notags(trim($_POST['phpath']));
264                         
265                         $adminmail = notags(trim($_POST['adminmail']));
266                         $timezone = ((x($_POST,'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
267                         
268                         $tpl = get_markup_template('install_settings.tpl');
269                         $o .= replace_macros($tpl, array(
270                                 '$title' => $install_title,
271                                 '$pass' => t('Site settings'),
272
273                                 '$status' => $wizard_status,
274                                 
275                                 '$dbhost' => $dbhost, 
276                                 '$dbuser' => $dbuser,
277                                 '$dbpass' => $dbpass,
278                                 '$dbdata' => $dbdata,
279                                 '$phpath' => $phpath,
280                                 
281                                 '$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.')),
282
283                                 
284                                 '$timezone' => field_timezone('timezone', t('Please select a default timezone for your website'), $timezone, ''),
285                                 
286                                 '$baseurl' => $a->get_baseurl(),
287                                 
288                                 
289                                 
290                                 '$submit' => t('Submit'),
291                                 
292                         ));
293                         return $o;
294                 }; break;
295                         
296         }
297 }
298
299 /**
300  * checks   : array passed to template
301  * title    : string
302  * status   : boolean
303  * required : boolean
304  * help         : string optional
305  */
306 function check_add(&$checks, $title, $status, $required, $help){
307         $checks[] = array(
308                 'title' => $title,
309                 'status' => $status,
310                 'required' => $required,
311                 'help'  => $help,
312         );
313 }
314
315 function check_php(&$phpath, &$checks) {
316         if (strlen($phpath)){
317                 $passed = file_exists($phpath);
318         } else {
319                 $phpath = trim(shell_exec('which php'));
320                 $passed = strlen($phpath);
321         }
322         $help = "";
323         if(!$passed) {
324                 $help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
325                 $help .= t("If you don't have a command line version of PHP installed on server, you will not be able to run background polling via cron. See <a href='http://friendica.com/node/27'>'Activating scheduled tasks'</a>") . EOL ;
326                 $help .= EOL . EOL ;
327                 $tpl = get_markup_template('field_input.tpl');
328                 $help .= replace_macros($tpl, array(
329                         '$field' => array('phpath', t('PHP executable path'), $phpath, t('Enter full path to php executable. You can leave this blank to continue the installation.')),
330                 ));
331                 $phpath="";
332         }
333         
334         check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
335         
336         if($passed) {
337                 $str = autoname(8);
338                 $cmd = "$phpath testargs.php $str";
339                 $result = trim(shell_exec($cmd));
340                 $passed2 = $result == $str;
341                 $help = "";
342                 if(!$passed2) {
343                         $help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
344                         $help .= t('This is required for message delivery to work.');
345                 }
346                 check_add($checks, t('PHP register_argc_argv'), $passed, true, $help);
347         }
348         
349
350 }
351
352 function check_keys(&$checks) {
353
354         $help = '';
355
356         $res = false;
357
358         if(function_exists('openssl_pkey_new')) 
359                 $res=openssl_pkey_new(array(
360                 'digest_alg' => 'sha1',
361                 'private_key_bits' => 4096,
362                 'encrypt_key' => false ));
363
364         // Get private key
365
366         if(! $res) {
367                 $help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
368                 $help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
369         }
370         check_add($checks, t('Generate encryption keys'), $res, true, $help);
371
372 }
373
374
375 function check_funcs(&$checks) {
376         $ck_funcs = array();
377         check_add($ck_funcs, t('libCurl PHP module'), true, true, "");
378         check_add($ck_funcs, t('GD graphics PHP module'), true, true, "");
379         check_add($ck_funcs, t('OpenSSL PHP module'), true, true, "");
380         check_add($ck_funcs, t('mysqli PHP module'), true, true, "");
381         check_add($ck_funcs, t('mb_string PHP module'), true, true, "");
382                 
383         
384         if(function_exists('apache_get_modules')){
385                 if (! in_array('mod_rewrite',apache_get_modules())) {
386                         check_add($ck_funcs, t('Apache mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
387                 } else {
388                         check_add($ck_funcs, t('Apache mod_rewrite module'), true, true, "");
389                 }
390         }
391
392         if(! function_exists('curl_init')){
393                 $ck_funcs[0]['status']= false;
394                 $ck_funcs[0]['help']= t('Error: libCURL PHP module required but not installed.');
395         }
396         if(! function_exists('imagecreatefromjpeg')){
397                 $ck_funcs[1]['status']= false;
398                 $ck_funcs[1]['help']= t('Error: GD graphics PHP module with JPEG support required but not installed.');
399         }
400         if(! function_exists('openssl_public_encrypt')) {
401                 $ck_funcs[2]['status']= false;
402                 $ck_funcs[2]['help']= t('Error: openssl PHP module required but not installed.');
403         }
404         if(! function_exists('mysqli_connect')){
405                 $ck_funcs[3]['status']= false;
406                 $ck_funcs[3]['help']= t('Error: mysqli PHP module required but not installed.');
407         }
408         if(! function_exists('mb_strlen')){
409                 $ck_funcs[4]['status']= false;
410                 $ck_funcs[4]['help']= t('Error: mb_string PHP module required but not installed.');
411         }
412         
413         $checks = array_merge($checks, $ck_funcs);
414         
415         /*if((x($_SESSION,'sysmsg')) && is_array($_SESSION['sysmsg']) && count($_SESSION['sysmsg']))
416                 notice( t('Please see the file "INSTALL.txt".') . EOL);*/
417 }
418
419
420 function check_htconfig(&$checks) {
421         $status = true;
422         $help = "";
423         if(     (file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
424                 (!file_exists('.htconfig.php') && !is_writable('.')) ) {
425         
426                 $status=false;
427                 $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;
428                 $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;
429                 $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;
430                 $help .= t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.').EOL; 
431         }
432     
433         check_add($checks, t('.htconfig.php is writable'), $status, false, $help);
434
435 }
436
437 function check_smarty3(&$checks) {
438         $status = true;
439         $help = "";
440         if(     !is_writable('view/smarty3') ) {
441         
442                 $status=false;
443                 $help = t('Friendica uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') .EOL;
444                 $help .= t('In order to store these compiled templates, the web server needs to have write access to the directory view/smarty3/ under the Friendica top level folder.').EOL;
445                 $help .= t('Please ensure that the user that your web server runs as (e.g. www-data) has write access to this folder.').EOL;
446                 $help .= t('Note: as a security measure, you should give the web server write access to view/smarty3/ only--not the template files (.tpl) that it contains.').EOL; 
447         }
448     
449         check_add($checks, t('view/smarty3 is writable'), $status, true, $help);
450
451 }
452
453 function check_htaccess(&$checks) {
454         $a = get_app();
455         $status = true;
456         $help = "";
457         if (function_exists('curl_init')){
458         $test = fetch_url($a->get_baseurl()."/install/testrewrite");
459         if ($test!="ok") {
460             $status = false;
461             $help = t('Url rewrite in .htaccess is not working. Check your server configuration.');
462         }
463         check_add($checks, t('Url rewrite is working'), $status, true, $help); 
464     } else {
465         // cannot check modrewrite if libcurl is not installed
466     }
467         
468 }
469
470         
471 function manual_config(&$a) {
472         $data = htmlentities($a->data['txt'],ENT_COMPAT,'UTF-8');
473         $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.');
474         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
475         return $o;
476 }
477
478 function load_database_rem($v, $i){
479         $l = trim($i);
480         if (strlen($l)>1 && ($l[0]=="-" || ($l[0]=="/" && $l[1]=="*"))){
481                 return $v;
482         } else  {
483                 return $v."\n".$i;
484         }
485 }
486
487
488 function load_database($db) {
489
490         $str = file_get_contents('database.sql');
491         $arr = explode(';',$str);
492         $errors = false;
493         foreach($arr as $a) {
494                 if(strlen(trim($a))) {  
495                         $r = @$db->q(trim($a));
496                         if(! $r) {
497                                 $errors .=  t('Errors encountered creating database tables.') . $a . EOL;
498                         }
499                 }
500         }
501         return $errors;
502 }
503
504 function what_next() {
505         $a = get_app();
506         $baseurl = $a->get_baseurl();
507         return 
508                 t('<h1>What next</h1>')
509                 ."<p>".t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.')
510                 .t('Please see the file "INSTALL.txt".')                        
511                 ."</p><p>"
512                 .t("Go to your new Friendica 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.")
513                 ."</p>";
514 }
515
516