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