Release 1.1. Enabled module addons and all the below bugfixes. No changes in database...
[fa-stable.git] / install / save.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set("display_errors", "On");
4
5 // Start a session
6 if(!defined('SESSION_STARTED')) 
7 {
8         session_name('ba_session_id');
9         session_start();
10         define('SESSION_STARTED', true);
11 }
12
13 // Function to set error
14 function set_error($message) 
15 {
16         global $_POST;
17         if(isset($message) AND $message != '') 
18         {
19                 // Copy values entered into session so user doesn't have to re-enter everything
20                 if(isset($_POST['company_name'])) 
21                 {
22                         $_SESSION['ba_url'] = $_POST['ba_url'];
23                         if(!isset($_POST['operating_system'])) 
24                         {
25                                 $_SESSION['operating_system'] = 'linux';
26                         } 
27                         else 
28                         {
29                                 $_SESSION['operating_system'] = $_POST['operating_system'];
30                         }
31                         if(!isset($_POST['world_writeable'])) 
32                         {
33                                 $_SESSION['world_writeable'] = false;
34                         } 
35                         else 
36                         {
37                                 $_SESSION['world_writeable'] = true;
38                         }
39                         $_SESSION['database_host'] = $_POST['database_host'];
40                         $_SESSION['database_username'] = $_POST['database_username'];
41                         $_SESSION['database_password'] = $_POST['database_password'];
42                         $_SESSION['database_name'] = $_POST['database_name'];
43                         $_SESSION['table_prefix'] = $_POST['table_prefix'];
44                         if(!isset($_POST['install_tables'])) 
45                         {
46                                 $_SESSION['install_tables'] = false;
47                         } 
48                         else 
49                         {
50                                 $_SESSION['install_tables'] = true;
51                         }
52                         $_SESSION['company_name'] = $_POST['company_name'];
53                         $_SESSION['admin_email'] = $_POST['admin_email'];
54                         $_SESSION['admin_password'] = $_POST['admin_password'];
55
56                 }
57                 // Set the message
58                 $_SESSION['message'] = $message;
59                 // Specify that session support is enabled
60                 $_SESSION['session_support'] = '<font class="good">Enabled</font>';
61                 // Redirect to first page again and exit
62                 header('Location: index.php?sessions_checked=true');
63                 exit();
64         }
65 }
66
67 // Function to workout what the default permissions are for files created by the webserver
68 function default_file_mode($temp_dir) 
69 {
70         $v = explode(".",PHP_VERSION);
71         $v = $v[0].$v[1];
72         if($v > 41 && is_writable($temp_dir)) 
73         {
74                 $filename = $temp_dir.'/test_permissions.txt';
75                 $handle = fopen($filename, 'w');
76                 fwrite($handle, 'This file is to get the default file permissions');
77                 fclose($handle);
78                 $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
79                 unlink($filename);
80         } 
81         else 
82         {
83                 $default_file_mode = '0777';
84         }
85         return $default_file_mode;
86 }
87
88 // Function to workout what the default permissions are for directories created by the webserver
89 function default_dir_mode($temp_dir) 
90 {
91         $v = explode(".",PHP_VERSION);
92         $v = $v[0].$v[1];
93         if ($v > 41 && is_writable($temp_dir)) 
94         {
95                 $dirname = $temp_dir.'/test_permissions/';
96                 mkdir($dirname);
97                 $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
98                 rmdir($dirname);
99         } 
100         else 
101         {
102                 $default_dir_mode = '0777';
103         }
104         return $default_dir_mode;
105 }
106
107 function add_slashes($input) 
108 {
109         if (get_magic_quotes_gpc() || (!is_string($input))) 
110         {
111                 return $input;
112         }
113         $output = addslashes($input);
114         return $output;
115 }
116 function check_db_error($err_msg, $sql)
117 {
118         return true;
119 }       
120
121 if (isset($_POST['path_to_root']))
122         $path_to_root = $_POST['path_to_root'];
123 else    
124         $path_to_root = "..";
125         
126 // Begin check to see if form was even submitted
127 // Set error if no post vars found
128 if (!isset($_POST['company_name'])) 
129 {
130         set_error('Please fill-in the form below');
131 }
132 // End check to see if form was even submitted
133
134 // Begin path and timezone details code
135
136 // Check if user has entered the installation url
137 if (!isset($_POST['ba_url']) || $_POST['ba_url'] == '') 
138 {
139         set_error('Please enter an absolute URL');
140
141 else 
142 {
143         $ba_url = $_POST['ba_url'];
144 }
145
146 // Remove any slashes at the end of the URL
147 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/") 
148 {
149         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
150 }
151 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\") 
152 {
153         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
154 }
155 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/") 
156 {
157         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
158 }
159 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\") 
160 {
161         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
162 }
163 // End path
164
165 // Begin operating system specific code
166 // Get operating system
167 if (!isset($_POST['operating_system']) || $_POST['operating_system'] != 'linux' && $_POST['operating_system'] != 'windows') 
168 {
169         set_error('Please select a valid operating system');
170
171 else 
172 {
173         $operating_system = $_POST['operating_system'];
174 }
175 // Work-out file permissions
176 if($operating_system == 'windows') 
177 {
178         $file_mode = '0777';
179         $dir_mode = '0777';
180
181 elseif (isset($_POST['world_writeable']) && $_POST['world_writeable'] == 'true') 
182 {
183         $file_mode = '0777';
184         $dir_mode = '0777';
185
186 else 
187 {
188         $file_mode = default_file_mode('../includes');
189         $dir_mode = default_dir_mode('../includes');
190 }
191 // End operating system specific code
192
193 // Begin database details code
194 // Check if user has entered a database host
195 if (!isset($_POST['database_host']) || $_POST['database_host'] == '') 
196 {
197         set_error('Please enter a database host name');
198
199 else 
200 {
201         $database_host = $_POST['database_host'];
202 }
203 // Check if user has entered a database username
204 if (!isset($_POST['database_username']) || $_POST['database_username'] == '') 
205 {
206         set_error('Please enter a database username');
207
208 else 
209 {
210         $database_username = $_POST['database_username'];
211 }
212 // Check if user has entered a database password
213 if (!isset($_POST['database_password'])) 
214 {
215         set_error('Please enter a database password');
216
217 else 
218 {
219         $database_password = $_POST['database_password'];
220 }
221 // Check if user has entered a database name
222 if (!isset($_POST['database_name']) || $_POST['database_name'] == '') 
223 {
224         set_error('Please enter a database name');
225
226 else 
227 {
228         $database_name = $_POST['database_name'];
229 }
230 // Get table prefix
231 $table_prefix = $_POST['table_prefix'];
232 // Find out if the user wants to install tables and data
233 if (isset($_POST['install_tables']) && $_POST['install_tables'] == 'true') 
234 {
235         $install_tables = true;
236
237 else 
238 {
239         $install_tables = false;
240 }
241 // End database details code
242
243 // Begin company name code
244 // Get company name
245 if (!isset($_POST['company_name']) || $_POST['company_name'] == '') 
246 {
247         set_error('Please enter a company name');
248
249 else 
250 {
251         $company_name = add_slashes($_POST['company_name']);
252 }
253 // End website company name
254
255 // Check if the user has entered a correct path
256 if (!file_exists($path_to_root.'/sql/basic.sql')) 
257 {
258         set_error('It appears the Absolute path that you entered is incorrect');
259 }
260
261 // Get admin email and validate it
262 if (!isset($_POST['admin_email']) || $_POST['admin_email'] == '') 
263 {
264         set_error('Please enter an email for the Administrator account');
265
266 else 
267 {
268         if (eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['admin_email'])) 
269         {
270                 $admin_email = $_POST['admin_email'];
271         } 
272         else 
273         {
274                 set_error('Please enter a valid email address for the Administrator account');
275         }
276 }
277 // Get the two admin passwords entered, and check that they match
278 if (!isset($_POST['admin_password']) || $_POST['admin_password'] == '') 
279 {
280         set_error('Please enter a password for the Administrator account');
281
282 else 
283 {
284         $admin_password = $_POST['admin_password'];
285 }
286 if (!isset($_POST['admin_repassword']) || $_POST['admin_repassword'] == '') 
287 {
288         set_error('Please make sure you re-enter the password for the Administrator account');
289
290 else 
291 {
292         $admin_repassword = $_POST['admin_repassword'];
293 }
294 if ($admin_password != $admin_repassword) 
295 {
296         set_error('Sorry, the two Administrator account passwords you entered do not match');
297 }
298 // End admin user details code
299
300 include_once($path_to_root . "/includes/db/connect_db.inc");
301 include_once($path_to_root . "/admin/db/maintenance_db.inc");
302 include_once($path_to_root . "/config_db.php");
303
304 $id = count($db_connections);
305 if ($table_prefix != "" && $id > 0)
306         $table_prefix = $tb_pref_counter . "_";
307 $db_connections[$id]['name'] = $company_name;
308 $db_connections[$id]['host'] = $database_host;
309 $db_connections[$id]['dbuser'] = $database_username;
310 $db_connections[$id]['dbpassword'] = $database_password;
311 $db_connections[$id]['dbname'] = $database_name;
312 $db_connections[$id]['tbpref'] = $table_prefix;
313
314 $def_coy = $id;
315
316 $config_filename = $path_to_root . '/config_db.php';
317
318 $err = write_config_db($table_prefix != "");
319 if ($err == -1)
320         set_error("Cannot open the configuration file ($config_filename)");
321 else if ($err == -2)    
322         set_error("Cannot write to the configuration file ($config_filename)");
323 else if ($err == -3) 
324         set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
325
326 // Try connecting to database   
327
328 $db = mysql_connect($database_host, $database_username, $database_password);
329 if (!$db)
330 {
331         set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
332 }
333
334 if($install_tables == true) 
335 {
336         if (!mysql_select_db($database_name, $db))
337         {
338
339                 // Try to create the database
340                 mysql_query('CREATE DATABASE '.$database_name);
341                 mysql_select_db($database_name, $db);
342         }       
343         $import_filename = $path_to_root."/sql/en_US-demo.sql";
344         if (!db_import($import_filename, $db_connections[$id]))
345                 set_error("Import error, try to import $import_filename manually via phpMyAdmin");
346 }
347 else
348 {
349         mysql_select_db($database_name, $db);
350 }
351 $sql = "UPDATE ".$table_prefix."users SET password = '" . md5($admin_password) . "', email = '$admin_email' WHERE user_id = 'admin'";
352 db_query($sql, "could not update admin account");
353 $sql = "UPDATE ".$table_prefix."company SET coy_name = '$company_name' WHERE coy_code = 1";
354 db_query($sql, "could not update company name. Do it manually later in Setup");
355
356 session_unset();
357 session_destroy();
358 $_SESSION = array(); 
359
360 header("Location: ".$path_to_root."/index.php");
361 exit();
362
363 ?>