]> git.mxchange.org Git - friendica.git/blob - mod/install.php
bug #232
[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         $adminmail = notags(trim($_POST['adminmail']));
16
17         require_once("dba.php");
18         unset($db);
19         $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
20
21         if(get_db_errno()) {
22                 unset($db);
23                 $db = new dba($dbhost, $dbuser, $dbpass, '', true);
24                 if(! get_db_errno()) {
25                         $r = q("CREATE DATABASE '%s'",
26                                         dbesc($dbdata)
27                         );
28                         if($r) {
29                                 unset($db);
30                                 $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
31                         }
32                 }
33                 if(get_db_errno()) {
34                         notice( t('Could not create/connect to database.') . EOL);
35                         return;
36                 }
37         }
38
39         info( t('Connected to database.') . EOL);
40
41         $tpl = get_intltext_template('htconfig.tpl');
42         $txt = replace_macros($tpl,array(
43                 '$dbhost' => $dbhost,
44                 '$dbuser' => $dbuser,
45                 '$dbpass' => $dbpass,
46                 '$dbdata' => $dbdata,
47                 '$timezone' => $timezone,
48                 '$urlpath' => $urlpath,
49                 '$phpath' => $phpath,
50                 '$adminmail' => $adminmail
51         ));
52
53         $result = file_put_contents('.htconfig.php', $txt);
54         if(! $result) {
55                 $a->data['txt'] = $txt;
56         }
57
58         $errors = load_database($db);
59
60         if($errors)
61                 $a->data['db_failed'] = true;
62         else
63                 $a->data['db_installed'] = true;
64
65         return;
66 }
67
68 function get_db_errno() {
69         if(class_exists('mysqli'))
70                 return mysqli_connect_errno();
71         else
72                 return mysql_errno();
73 }               
74
75 function install_content(&$a) {
76
77         global $db;
78         $o = '';
79         
80         if(x($a->data,'db_installed')) {
81                 $o .= '<h2>' . t('Proceed with Installation') . '</h2>';
82                 $o .= '<p style="font-size: 130%;">';
83                 $o .= t('Your Friendica site database has been installed.') . EOL;
84                 $o .= t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.') . EOL ;
85                 $o .= t('Please see the file "INSTALL.txt".') . EOL ;
86                 $o .= '<br />';
87                 $o .= '<a href="' . $a->get_baseurl() . '/register' . '">' . t('Proceed to registration') . '</a>' ;
88                 $o .= '</p>';
89                 return $o;
90         }
91
92         if(x($a->data,'db_failed')) {
93                 $o .= t('Database import failed.') . EOL;
94                 $o .= t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
95                 $o .= t('Please see the file "INSTALL.txt".') . EOL ;
96                 return $o;
97         }
98
99         if($db && $db->connected) {
100                 $r = q("SELECT COUNT(*) as `total` FROM `user`");
101                 if($r && count($r) && $r[0]['total']) {
102                         notice( t('Permission denied.') . EOL);
103                         return '';
104                 }
105         }
106
107         info( t('Welcome to Friendica.') . EOL);
108
109
110         check_funcs();
111
112         $o .= check_htconfig();
113         if(strlen($o))
114                 return $o;
115
116         if(strlen($a->data['txt'])) {
117                 $o .= manual_config($a);
118                 return;
119         }
120
121         $o .= check_php($phpath);
122
123         $o .= check_keys();
124
125
126         require_once('datetime.php');
127
128         $tpl = get_markup_template('install_db.tpl');
129         $o .= replace_macros($tpl, array(
130                 '$lbl_01' => t('Friendica Social Communications Server'),
131                 '$lbl_02' => t('Installation'),
132                 '$lbl_03' => t('In order to install Friendica we need to know how to connect to your database.'),
133                 '$lbl_04' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
134                 '$lbl_05' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
135                 '$lbl_06' => t('Database Server Name'),
136                 '$lbl_07' => t('Database Login Name'),
137                 '$lbl_08' => t('Database Login Password'),
138                 '$lbl_09' => t('Database Name'),
139                 '$lbl_10' => t('Please select a default timezone for your website'),
140                 '$lbl_11' => t('Site administrator email address. Your account email address must match this in order to use the web admin panel.'), 
141                 '$baseurl' => $a->get_baseurl(),
142                 '$tzselect' => ((x($_POST,'timezone')) ? select_timezone($_POST['timezone']) : select_timezone()),
143                 '$submit' => t('Submit'),
144                 '$dbhost' => ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost'),
145                 '$dbuser' => notags(trim($_POST['dbuser'])),
146                 '$dbpass' => notags(trim($_POST['dbpass'])),
147                 '$dbdata' => notags(trim($_POST['dbdata'])),
148                 '$phpath' => $phpath,
149                 '$adminmail' => notags(trim($_POST['adminmail']))
150         ));
151
152         return $o;
153 }
154
155 function check_php(&$phpath) {
156         $o = '';
157         $phpath = trim(shell_exec('which php'));
158         if(! strlen($phpath)) {
159                 $o .= t('Could not find a command line version of PHP in the web server PATH.') . EOL;
160                 $o .= t('This is required. Please adjust the configuration file .htconfig.php accordingly.') . EOL;
161         }
162         if(strlen($phpath)) {
163                 $str = autoname(8);
164                 $cmd = "$phpath testargs.php $str";
165                 $result = trim(shell_exec($cmd));
166                 if($result != $str) {
167                         $o .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
168                         $o .= t('This is required for message delivery to work.') . EOL;
169                 }
170         }
171         return $o;
172
173 }
174
175 function check_keys() {
176
177         $o = '';
178
179         $res = false;
180
181         if(function_exists('openssl_pkey_new')) 
182                 $res=openssl_pkey_new(array(
183                 'digest_alg' => 'sha1',
184                 'private_key_bits' => 4096,
185                 'encrypt_key' => false ));
186
187         // Get private key
188
189         if(! $res) {
190                 $o .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
191                 $o .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".') . EOL;
192         }
193         return $o;
194
195 }
196
197
198 function check_funcs() {
199         if((function_exists('apache_get_modules')) && (! in_array('mod_rewrite',apache_get_modules())))
200                 notice( t('Error: Apache webserver mod-rewrite module is required but not installed.') . EOL);
201         if(! function_exists('curl_init')) 
202                 notice( t('Error: libCURL PHP module required but not installed.') . EOL);
203         if(! function_exists('imagecreatefromjpeg')) 
204                 notice( t('Error: GD graphics PHP module with JPEG support required but not installed.') . EOL);
205         if(! function_exists('openssl_public_encrypt')) 
206                 notice( t('Error: openssl PHP module required but not installed.') . EOL);      
207         if(! function_exists('mysqli_connect')) 
208                 notice( t('Error: mysqli PHP module required but not installed.') . EOL);
209         if(! function_exists('mb_strlen'))
210                 notice( t('Error: mb_string PHP module required but not installed.') . EOL);
211         
212         if((x($_SESSION,'sysmsg')) && is_array($_SESSION['sysmsg']) && count($_SESSION['sysmsg']))
213                 notice( t('Please see the file "INSTALL.txt".') . EOL);
214 }
215
216
217 function check_htconfig() {
218
219         if(((file_exists('.htconfig.php')) && (! is_writable('.htconfig.php')))
220                 || (! is_writable('.'))) {
221
222                 $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.');
223                 $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.');
224                 $o .= t('Please check with your site documentation or support people to see if this situation can be corrected.');
225                 $o .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL.txt" for instructions.'); 
226         }
227
228         return $o;
229 }
230
231         
232 function manual_config(&$a) {
233         $data = htmlentities($a->data['txt']);
234         $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.');
235         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
236         return $o;
237 }
238
239
240 function load_database($db) {
241
242         $str = file_get_contents('database.sql');
243         $arr = explode(';',$str);
244         $errors = 0;
245         foreach($arr as $a) {
246                 if(strlen(trim($a))) {  
247                         $r = @$db->q(trim($a));
248                         if(! $r) {
249                                 notice( t('Errors encountered creating database tables.') . $a . EOL);
250                                 $errors ++;
251                         }
252                 }
253         }
254         return $errors;
255 }