]> git.mxchange.org Git - friendica.git/blob - mod/install.php
more pre-install checks to sort out broken environments
[friendica.git] / mod / install.php
1 <?php
2
3
4 function install_post(&$a) {
5
6         global $db;
7
8         $dbhost = notags(trim($_POST['dbhost']));
9         $dbuser = notags(trim($_POST['dbuser']));
10         $dbpass = notags(trim($_POST['dbpass']));
11         $dbdata = notags(trim($_POST['dbdata']));
12         $timezone = notags(trim($_POST['timezone']));
13         $phpath = notags(trim($_POST['phpath']));
14
15         require_once("dba.php");
16
17         $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, $true);
18
19         if(mysqli_connect_errno()) {
20                 $db = new dba($dbhost, $dbuser, $dbpass, '', true);
21                 if(! mysql_connect_errno()) {
22                         $r = q("CREATE DATABASE '%s'",
23                                         dbesc($dbdata)
24                         );
25                         if($r) 
26                                 $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, $true);
27                 }
28                 if(mysqli_connect_errno()) {
29                         notice( t('Could not create/connect to database.') . EOL);
30                         return;
31                 }
32         }
33
34         notice( t('Connected to database.') . EOL);
35
36         $tpl = load_view_file('view/htconfig.tpl');
37         $txt = replace_macros($tpl,array(
38                 '$dbhost' => $dbhost,
39                 '$dbuser' => $dbuser,
40                 '$dbpass' => $dbpass,
41                 '$dbdata' => $dbdata,
42                 '$timezone' => $timezone,
43                 '$phpath' => $phpath
44         ));
45         $result = file_put_contents('.htconfig.php', $txt);
46         if(! $result) {
47                 $a->data = $txt;
48         }
49
50         $errors = load_database($db);
51         if(! $errors) {
52                 // Our sessions normally are stored in the database. But as we have only managed 
53                 // to get it bootstrapped milliseconds ago, we have to apply a bit of trickery so 
54                 // that you'll see the following important notice (which is stored in the session). 
55
56                 session_write_close();
57
58                 require_once('session.php');
59                 session_start();
60                 session_regenerate_id();
61
62                 $_SESSION['sysmsg'] = '';
63
64                 notice( t('Database import succeeded.') . EOL 
65                         . t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.') . EOL 
66                         . t('Please see the file INSTALL.') . EOL );
67                 goaway($a->get_baseurl() . '/register' );
68         }
69         else {
70                 $db = null; // start fresh
71                 notice( t('Database import failed.') . EOL
72                         . t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL
73                         . t('Please see the file INSTALL.') . EOL );
74         }
75 }
76
77
78 function install_content(&$a) {
79
80         $o = '';
81
82         notice( t('Welcome to Friendika.') . EOL);
83
84
85         check_funcs();
86
87         $o .= check_htconfig();
88         if(strlen($o))
89                 return $o;
90
91         if(strlen($a->data)) {
92                 $o .= manual_config($a);
93                 return;
94         }
95
96         $o .= check_php($phpath);
97
98         $o .= check_keys();
99
100
101         require_once('datetime.php');
102
103         $tpl = load_view_file('view/install_db.tpl');
104         $o .= replace_macros($tpl, array(
105                 '$tzselect' => ((x($_POST,'timezone')) ? select_timezone($_POST['timezone']) : select_timezone()),
106                 '$submit' => t('Submit'),
107                 '$dbhost' => ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost'),
108                 '$dbuser' => notags(trim($_POST['dbuser'])),
109                 '$dbpass' => notags(trim($_POST['dbpass'])),
110                 '$dbdata' => notags(trim($_POST['dbdata'])),
111                 '$phpath' => $phpath
112         ));
113
114         return $o;
115 }
116
117 function check_php(&$phpath) {
118         $o = '';
119         $phpath = trim(shell_exec('which php'));
120         if(! strlen($phpath)) {
121                 $o .= t('Could not find a command line version of PHP in the web server PATH.') . EOL;
122                 $o .= t('This is required. Please adjust the configuration file .htconfig.php accordingly.') . EOL;
123         }
124         if(strlen($phpath)) {
125                 $str = autoname(8);
126                 $cmd = "$phpath testargs.php $str";
127                 $result = trim(shell_exec($cmd));
128                 if($result != $str) {
129                         $o .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
130                         $o .= t('This is required for message delivery to work.') . EOL;
131                 }
132         }
133         return $o;
134
135 }
136
137 function check_keys() {
138
139         $o = '';
140
141         $res = false;
142
143         if(function_exists('openssl_pkey_new')) 
144                 $res=openssl_pkey_new(array(
145                 'digest_alg' => 'sha1',
146                 'private_key_bits' => 4096,
147                 'encrypt_key' => false ));
148
149         // Get private key
150
151         if(! $res)
152                 $o .=  t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
153
154         return $o;
155
156 }
157
158
159 function check_funcs() {
160         if((function_exists('apache_get_modules')) && (! in_array('mod_rewrite',apache_get_modules())))
161                 notice( t('Error: Apache webserver mod-rewrite module is required but not installed.') . EOL);
162         if(! function_exists('curl_init')) 
163                 notice( t('Error: libCURL PHP module required but not installed.') . EOL);
164         if(! function_exists('imagecreatefromjpeg')) 
165                 notice( t('Error: GD graphics PHP module with JPEG support required but not installed.') . EOL);
166         if(! function_exists('openssl_public_encrypt')) 
167                 notice( t('Error: openssl PHP module required but not installed.') . EOL);      
168         if(! function_exists('mysqli_connect')) 
169                 notice( t('Error: mysqli PHP module required but not installed.') . EOL);       
170         if((x($_SESSION,'sysmsg')) && strlen($_SESSION['sysmsg']))
171                 notice( t('Please see the file "INSTALL".') . EOL);
172 }
173
174
175 function check_htconfig() {
176
177         if(((file_exists('.htconfig.php')) && (! is_writable('.htconfig.php')))
178                 || (! is_writable('.'))) {
179
180                 $o = 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.');
181                 $o .= 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.');
182                 $o .= t('Please check with your site documentation or support people to see if this situation can be corrected.');
183                 $o .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL" for instructions.'); 
184         }
185
186         return $o;
187 }
188
189         
190 function manual_config(&$a) {
191         $data = htmlentities($a->data);
192         $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.');
193         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
194         return $o;
195 }
196
197
198 function load_database($db) {
199
200         $str = file_get_contents('database.sql');
201         $arr = explode(';',$str);
202         $errors = 0;
203         foreach($arr as $a) {
204                 if(strlen(trim($a))) {  
205                         $r = @$db->q(trim($a));
206                         if(! $r) {
207                                 notice( t('Errors encountered creating database tables.') . $a . EOL);
208                                 $errors ++;
209                         }
210                 }
211         }
212         return $errors;
213 }