]> git.mxchange.org Git - friendica.git/blob - mod/install.php
86edc4c287f19c72cc39d0be76e44ce23ea50b86
[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         require_once('datetime.php');
99
100         $tpl = load_view_file('view/install_db.tpl');
101         $o .= replace_macros($tpl, array(
102                 '$tzselect' => ((x($_POST,'timezone')) ? select_timezone($_POST['timezone']) : select_timezone()),
103                 '$submit' => t('Submit'),
104                 '$dbhost' => ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost'),
105                 '$dbuser' => notags(trim($_POST['dbuser'])),
106                 '$dbpass' => notags(trim($_POST['dbpass'])),
107                 '$dbdata' => notags(trim($_POST['dbdata'])),
108                 '$phpath' => $phpath
109         ));
110
111         return $o;
112 }
113
114 function check_php(&$phpath) {
115         $o = '';
116         $phpath = trim(shell_exec('which php'));
117         if(! strlen($phpath)) {
118                 $o .= t('Could not find a command line version of PHP in the web server PATH.');
119                 $o .= t('This is required. Please adjust the configuration file .htconfig.php accordingly.');
120         }
121         return $o;
122 }
123
124 function check_funcs() {
125         if(! function_exists('curl_init')) 
126                 notice( t('Error: libCURL PHP module required but not installed.') . EOL);
127         if(! function_exists('imagecreatefromjpeg')) 
128                 notice( t('Error: GD graphics PHP module with JPEG support required but not installed.') . EOL);
129         if(! function_exists('openssl_public_encrypt')) 
130                 notice( t('Error: openssl PHP module required but not installed.') . EOL);      
131         if(! function_exists('mysqli_connect')) 
132                 notice( t('Error: mysqli PHP module required but not installed.') . EOL);       
133 }
134
135
136 function check_htconfig() {
137
138         if(((file_exists('.htconfig.php')) && (! is_writable('.htconfig.php')))
139                 || (! is_writable('.'))) {
140
141                 $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.');
142                 $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.');
143                 $o .= t('Please check with your site documentation or support people to see if this situation can be corrected.');
144                 $o .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL" for instructions.'); 
145         }
146
147         return $o;
148 }
149
150         
151 function manual_config(&$a) {
152         $data = htmlentities($a->data);
153         $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.');
154         $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
155         return $o;
156 }
157
158
159 function load_database($db) {
160
161         $str = file_get_contents('database.sql');
162         $arr = explode(';',$str);
163         $errors = 0;
164         foreach($arr as $a) {
165                 if(strlen(trim($a))) {  
166                         $r = @$db->q(trim($a));
167                         if(! $r) {
168                                 notice( t('Errors encountered creating database tables.') . $a . EOL);
169                                 $errors ++;
170                         }
171                 }
172         }
173         return $errors;
174 }