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