2 error_reporting(E_ALL);
3 ini_set("display_errors", "On");
6 if(!defined('SESSION_STARTED'))
8 session_name('ba_session_id');
10 define('SESSION_STARTED', true);
13 // Function to set error
14 function set_error($message)
17 if(isset($message) AND $message != '')
19 // Copy values entered into session so user doesn't have to re-enter everything
20 if(isset($_POST['company_name']))
22 $_SESSION['ba_url'] = $_POST['ba_url'];
23 if(!isset($_POST['operating_system']))
25 $_SESSION['operating_system'] = 'linux';
29 $_SESSION['operating_system'] = $_POST['operating_system'];
31 if(!isset($_POST['world_writeable']))
33 $_SESSION['world_writeable'] = false;
37 $_SESSION['world_writeable'] = true;
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']))
46 $_SESSION['install_tables'] = false;
50 $_SESSION['install_tables'] = true;
52 $_SESSION['company_name'] = $_POST['company_name'];
53 $_SESSION['admin_email'] = $_POST['admin_email'];
54 $_SESSION['admin_password'] = $_POST['admin_password'];
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');
67 // Function to workout what the default permissions are for files created by the webserver
68 function default_file_mode($temp_dir)
70 $v = explode(".",PHP_VERSION);
72 if($v > 41 && is_writable($temp_dir))
74 $filename = $temp_dir.'/test_permissions.txt';
75 $handle = fopen($filename, 'w');
76 fwrite($handle, 'This file is to get the default file permissions');
78 $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
83 $default_file_mode = '0777';
85 return $default_file_mode;
88 // Function to workout what the default permissions are for directories created by the webserver
89 function default_dir_mode($temp_dir)
91 $v = explode(".",PHP_VERSION);
93 if ($v > 41 && is_writable($temp_dir))
95 $dirname = $temp_dir.'/test_permissions/';
97 $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
102 $default_dir_mode = '0777';
104 return $default_dir_mode;
107 function add_slashes($input)
109 if (get_magic_quotes_gpc() || (!is_string($input)))
113 $output = addslashes($input);
116 function check_db_error($err_msg, $sql)
121 if (isset($_POST['path_to_root']))
122 $path_to_root = $_POST['path_to_root'];
124 $path_to_root = "..";
126 // Begin check to see if form was even submitted
127 // Set error if no post vars found
128 if (!isset($_POST['company_name']))
130 set_error('Please fill-in the form below');
132 // End check to see if form was even submitted
134 // Begin path and timezone details code
136 // Check if user has entered the installation url
137 if (!isset($_POST['ba_url']) || $_POST['ba_url'] == '')
139 set_error('Please enter an absolute URL');
143 $ba_url = $_POST['ba_url'];
146 // Remove any slashes at the end of the URL
147 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
149 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
151 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
153 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
155 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
157 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
159 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
161 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
165 // Begin operating system specific code
166 // Get operating system
167 if (!isset($_POST['operating_system']) || $_POST['operating_system'] != 'linux' && $_POST['operating_system'] != 'windows')
169 set_error('Please select a valid operating system');
173 $operating_system = $_POST['operating_system'];
175 // Work-out file permissions
176 if($operating_system == 'windows')
181 elseif (isset($_POST['world_writeable']) && $_POST['world_writeable'] == 'true')
188 $file_mode = default_file_mode('../temp');
189 $dir_mode = default_dir_mode('../temp');
191 // End operating system specific code
193 // Begin database details code
194 // Check if user has entered a database host
195 if (!isset($_POST['database_host']) || $_POST['database_host'] == '')
197 set_error('Please enter a database host name');
201 $database_host = $_POST['database_host'];
203 // Check if user has entered a database username
204 if (!isset($_POST['database_username']) || $_POST['database_username'] == '')
206 set_error('Please enter a database username');
210 $database_username = $_POST['database_username'];
212 // Check if user has entered a database password
213 if (!isset($_POST['database_password']))
215 set_error('Please enter a database password');
219 $database_password = $_POST['database_password'];
221 // Check if user has entered a database name
222 if (!isset($_POST['database_name']) || $_POST['database_name'] == '')
224 set_error('Please enter a database name');
228 $database_name = $_POST['database_name'];
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')
235 $install_tables = true;
239 $install_tables = false;
241 // End database details code
243 // Begin company name code
245 if (!isset($_POST['company_name']) || $_POST['company_name'] == '')
247 set_error('Please enter a company name');
251 $company_name = add_slashes($_POST['company_name']);
253 // End website company name
255 // Check if the user has entered a correct path
256 if (!file_exists($path_to_root.'/sql/basic.sql'))
258 set_error('It appears the Absolute path that you entered is incorrect');
261 // Get admin email and validate it
262 if (!isset($_POST['admin_email']) || $_POST['admin_email'] == '')
264 set_error('Please enter an email for the Administrator account');
268 if (eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['admin_email']))
270 $admin_email = $_POST['admin_email'];
274 set_error('Please enter a valid email address for the Administrator account');
277 // Get the two admin passwords entered, and check that they match
278 if (!isset($_POST['admin_password']) || $_POST['admin_password'] == '')
280 set_error('Please enter a password for the Administrator account');
284 $admin_password = $_POST['admin_password'];
286 if (!isset($_POST['admin_repassword']) || $_POST['admin_repassword'] == '')
288 set_error('Please make sure you re-enter the password for the Administrator account');
292 $admin_repassword = $_POST['admin_repassword'];
294 if ($admin_password != $admin_repassword)
296 set_error('Sorry, the two Administrator account passwords you entered do not match');
298 // End admin user details code
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");
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;
316 $config_filename = $path_to_root . '/config_db.php';
318 $err = write_config_db($table_prefix != "");
320 set_error("Cannot open the configuration file ($config_filename)");
322 set_error("Cannot write to the configuration file ($config_filename)");
324 set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
326 // Try connecting to database
328 $db = mysql_connect($database_host, $database_username, $database_password);
331 set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
334 if($install_tables == true)
336 if (!mysql_select_db($database_name, $db))
339 // Try to create the database
340 mysql_query('CREATE DATABASE '.$database_name);
341 mysql_select_db($database_name, $db);
343 $import_filename = $path_to_root."/sql/basic.sql";
344 if (!db_import($import_filename, $db_connections[$id]))
345 set_error("Import error, try to import $import_filename and $path_to_root/en_US-demo.sql manually via phpMyAdmin");
346 $import_filename = $path_to_root."/sql/en_US-demo.sql";
347 if (!db_import($import_filename, $db_connections[$id]))
348 set_error("Import error, try to import $import_filename manually via phpMyAdmin");
352 mysql_select_db($database_name, $db);
354 $sql = "UPDATE ".$table_prefix."users SET password = '" . md5($admin_password) . "', email = '$admin_email' WHERE user_id = 'admin'";
355 db_query($sql, "could not update admin account");
356 $sql = "UPDATE ".$table_prefix."company SET coy_name = '$company_name' WHERE coy_code = 1";
357 db_query($sql, "could not update company name. Do it manually later in Setup");
363 header("Location: ".$path_to_root."/index.php");