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