2 error_reporting(E_ALL);
3 ini_set("display_errors", "On");
4 ini_set("max_execution_time", "180");
7 if(!defined('SESSION_STARTED'))
9 session_name('ba_session_id');
11 define('SESSION_STARTED', true);
14 // Function to set error
15 function set_error($message)
18 if(isset($message) AND $message != '')
20 // Copy values entered into session so user doesn't have to re-enter everything
21 if(isset($_POST['company_name']))
23 $_SESSION['ba_url'] = $_POST['ba_url'];
24 if(!isset($_POST['operating_system']))
26 $_SESSION['operating_system'] = 'linux';
30 $_SESSION['operating_system'] = $_POST['operating_system'];
32 if(!isset($_POST['world_writeable']))
34 $_SESSION['world_writeable'] = false;
38 $_SESSION['world_writeable'] = true;
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 if(!isset($_POST['table_prefix']))
46 $_SESSION['table_prefix'] = false;
50 $_SESSION['table_prefix'] = true;
52 if(!isset($_POST['install_tables']))
54 $_SESSION['install_tables'] = false;
58 $_SESSION['install_tables'] = true;
60 $_SESSION['company_name'] = $_POST['company_name'];
61 $_SESSION['admin_email'] = $_POST['admin_email'];
62 $_SESSION['admin_password'] = $_POST['admin_password'];
66 $_SESSION['message'] = $message;
67 // Specify that session support is enabled
68 $_SESSION['session_support'] = '<font class="good">Enabled</font>';
69 // Redirect to first page again and exit
70 header('Location: index.php?sessions_checked=true');
75 // Function to workout what the default permissions are for files created by the webserver
76 function default_file_mode($temp_dir)
78 $v = explode(".",PHP_VERSION);
80 if($v > 41 && is_writable($temp_dir))
82 $filename = $temp_dir.'/test_permissions.txt';
83 $handle = fopen($filename, 'w');
84 fwrite($handle, 'This file is to get the default file permissions');
86 $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
91 $default_file_mode = '0777';
93 return $default_file_mode;
96 // Function to workout what the default permissions are for directories created by the webserver
97 function default_dir_mode($temp_dir)
99 $v = explode(".",PHP_VERSION);
101 if ($v > 41 && is_writable($temp_dir))
103 $dirname = $temp_dir.'/test_permissions/';
105 $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
110 $default_dir_mode = '0777';
112 return $default_dir_mode;
115 function add_slashes($input)
117 if (get_magic_quotes_gpc() || (!is_string($input)))
121 $output = addslashes($input);
124 function check_db_error($err_msg, $sql)
129 if (isset($_POST['path_to_root']))
130 $path_to_root = $_POST['path_to_root'];
132 $path_to_root = "..";
134 // Begin check to see if form was even submitted
135 // Set error if no post vars found
136 if (!isset($_POST['company_name']))
138 set_error('Please fill-in the form below');
140 // End check to see if form was even submitted
142 // Begin path and timezone details code
144 // Check if user has entered the installation url
145 if (!isset($_POST['ba_url']) || $_POST['ba_url'] == '')
147 set_error('Please enter an absolute URL');
151 $ba_url = $_POST['ba_url'];
154 // Remove any slashes at the end of the URL
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);
163 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
165 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
167 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
169 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
173 // Begin operating system specific code
174 // Get operating system
175 if (!isset($_POST['operating_system']) || $_POST['operating_system'] != 'linux' && $_POST['operating_system'] != 'windows')
177 set_error('Please select a valid operating system');
181 $operating_system = $_POST['operating_system'];
183 // Work-out file permissions
184 if($operating_system == 'windows')
189 elseif (isset($_POST['world_writeable']) && $_POST['world_writeable'] == 'true')
196 $file_mode = default_file_mode('../includes');
197 $dir_mode = default_dir_mode('../includes');
199 // End operating system specific code
201 // Begin database details code
202 // Check if user has entered a database host
203 if (!isset($_POST['database_host']) || $_POST['database_host'] == '')
205 set_error('Please enter a database host name');
209 $database_host = $_POST['database_host'];
211 // Check if user has entered a database username
212 if (!isset($_POST['database_username']) || $_POST['database_username'] == '')
214 set_error('Please enter a database username');
218 $database_username = $_POST['database_username'];
220 // Check if user has entered a database password
221 if (!isset($_POST['database_password']))
223 set_error('Please enter a database password');
227 $database_password = $_POST['database_password'];
229 // Check if user has entered a database name
230 if (!isset($_POST['database_name']) || $_POST['database_name'] == '')
232 set_error('Please enter a database name');
236 $database_name = $_POST['database_name'];
239 if (isset($_POST['table_prefix']) && $_POST['table_prefix'] == 'true')
240 $table_prefix = "0_";
244 // Find out if the user wants to install tables and data
245 if (isset($_POST['install_tables']) && $_POST['install_tables'] == 'true')
247 $install_tables = true;
251 $install_tables = false;
253 // End database details code
255 // Begin company name code
257 if (!isset($_POST['company_name']) || $_POST['company_name'] == '')
259 set_error('Please enter a company name');
263 $company_name = add_slashes($_POST['company_name']);
265 // End website company name
267 // Check if the user has entered a correct path
268 if (!file_exists($path_to_root.'/sql/en_US-demo.sql'))
270 set_error('It appears the Absolute path that you entered is incorrect');
273 // Get admin email and validate it
274 if (!isset($_POST['admin_email']) || $_POST['admin_email'] == '')
276 set_error('Please enter an email for the Administrator account');
280 if (eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['admin_email']))
282 $admin_email = $_POST['admin_email'];
286 set_error('Please enter a valid email address for the Administrator account');
289 // Get the two admin passwords entered, and check that they match
290 if (!isset($_POST['admin_password']) || $_POST['admin_password'] == '')
292 set_error('Please enter a password for the Administrator account');
296 $admin_password = $_POST['admin_password'];
298 if (!isset($_POST['admin_repassword']) || $_POST['admin_repassword'] == '')
300 set_error('Please make sure you re-enter the password for the Administrator account');
304 $admin_repassword = $_POST['admin_repassword'];
306 if ($admin_password != $admin_repassword)
308 set_error('Sorry, the two Administrator account passwords you entered do not match');
310 // End admin user details code
312 include_once($path_to_root . "/includes/db/connect_db.inc");
313 include_once($path_to_root . "/admin/db/maintenance_db.inc");
314 include_once($path_to_root . "/config_db.php");
316 $id = count($db_connections);
317 if ($table_prefix != "" && $id > 0)
318 $table_prefix = $tb_pref_counter . "_";
319 $db_connections[$id]['name'] = $company_name;
320 $db_connections[$id]['host'] = $database_host;
321 $db_connections[$id]['dbuser'] = $database_username;
322 $db_connections[$id]['dbpassword'] = $database_password;
323 $db_connections[$id]['dbname'] = $database_name;
324 $db_connections[$id]['tbpref'] = $table_prefix;
328 $config_filename = $path_to_root . '/config_db.php';
330 $err = write_config_db($table_prefix != "");
332 set_error("Cannot open the configuration file ($config_filename)");
334 set_error("Cannot write to the configuration file ($config_filename)");
336 set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
338 // Try connecting to database
340 $db = mysql_connect($database_host, $database_username, $database_password);
343 set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
346 if($install_tables == true)
348 if (!mysql_select_db($database_name, $db))
351 // Try to create the database
352 mysql_query('CREATE DATABASE '.$database_name);
353 mysql_select_db($database_name, $db);
355 $import_filename = $path_to_root."/sql/en_US-demo.sql";
356 if (!db_import($import_filename, $db_connections[$id]))
357 set_error("Import error, try to import $import_filename manually via phpMyAdmin");
361 mysql_select_db($database_name, $db);
363 $sql = "UPDATE ".$table_prefix."users SET password = '" . md5($admin_password) . "', email = ".db_escape($admin_email)." WHERE user_id = 'admin'";
364 db_query($sql, "could not update admin account");
365 $sql = "UPDATE ".$table_prefix."company SET coy_name = ".db_escape($company_name)." WHERE coy_code = 1";
366 db_query($sql, "could not update company name. Do it manually later in Setup");
372 header("Location: ".$path_to_root."/index.php");