2 error_reporting(E_ALL);
3 ini_set("display_errors", "On");
4 ini_set("max_execution_time", "120");
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 $_SESSION['table_prefix'] = $_POST['table_prefix'];
45 if(!isset($_POST['install_tables']))
47 $_SESSION['install_tables'] = false;
51 $_SESSION['install_tables'] = true;
53 $_SESSION['company_name'] = $_POST['company_name'];
54 $_SESSION['admin_email'] = $_POST['admin_email'];
55 $_SESSION['admin_password'] = $_POST['admin_password'];
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');
68 // Function to workout what the default permissions are for files created by the webserver
69 function default_file_mode($temp_dir)
71 $v = explode(".",PHP_VERSION);
73 if($v > 41 && is_writable($temp_dir))
75 $filename = $temp_dir.'/test_permissions.txt';
76 $handle = fopen($filename, 'w');
77 fwrite($handle, 'This file is to get the default file permissions');
79 $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
84 $default_file_mode = '0777';
86 return $default_file_mode;
89 // Function to workout what the default permissions are for directories created by the webserver
90 function default_dir_mode($temp_dir)
92 $v = explode(".",PHP_VERSION);
94 if ($v > 41 && is_writable($temp_dir))
96 $dirname = $temp_dir.'/test_permissions/';
98 $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
103 $default_dir_mode = '0777';
105 return $default_dir_mode;
108 function add_slashes($input)
110 if (get_magic_quotes_gpc() || (!is_string($input)))
114 $output = addslashes($input);
117 function check_db_error($err_msg, $sql)
122 if (isset($_POST['path_to_root']))
123 $path_to_root = $_POST['path_to_root'];
125 $path_to_root = "..";
127 // Begin check to see if form was even submitted
128 // Set error if no post vars found
129 if (!isset($_POST['company_name']))
131 set_error('Please fill-in the form below');
133 // End check to see if form was even submitted
135 // Begin path and timezone details code
137 // Check if user has entered the installation url
138 if (!isset($_POST['ba_url']) || $_POST['ba_url'] == '')
140 set_error('Please enter an absolute URL');
144 $ba_url = $_POST['ba_url'];
147 // Remove any slashes at the end of the URL
148 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
150 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
152 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
154 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
156 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
158 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
160 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
162 $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
166 // Begin operating system specific code
167 // Get operating system
168 if (!isset($_POST['operating_system']) || $_POST['operating_system'] != 'linux' && $_POST['operating_system'] != 'windows')
170 set_error('Please select a valid operating system');
174 $operating_system = $_POST['operating_system'];
176 // Work-out file permissions
177 if($operating_system == 'windows')
182 elseif (isset($_POST['world_writeable']) && $_POST['world_writeable'] == 'true')
189 $file_mode = default_file_mode('../includes');
190 $dir_mode = default_dir_mode('../includes');
192 // End operating system specific code
194 // Begin database details code
195 // Check if user has entered a database host
196 if (!isset($_POST['database_host']) || $_POST['database_host'] == '')
198 set_error('Please enter a database host name');
202 $database_host = $_POST['database_host'];
204 // Check if user has entered a database username
205 if (!isset($_POST['database_username']) || $_POST['database_username'] == '')
207 set_error('Please enter a database username');
211 $database_username = $_POST['database_username'];
213 // Check if user has entered a database password
214 if (!isset($_POST['database_password']))
216 set_error('Please enter a database password');
220 $database_password = $_POST['database_password'];
222 // Check if user has entered a database name
223 if (!isset($_POST['database_name']) || $_POST['database_name'] == '')
225 set_error('Please enter a database name');
229 $database_name = $_POST['database_name'];
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')
236 $install_tables = true;
240 $install_tables = false;
242 // End database details code
244 // Begin company name code
246 if (!isset($_POST['company_name']) || $_POST['company_name'] == '')
248 set_error('Please enter a company name');
252 $company_name = add_slashes($_POST['company_name']);
254 // End website company name
256 // Check if the user has entered a correct path
257 if (!file_exists($path_to_root.'/sql/en_US-demo.sql'))
259 set_error('It appears the Absolute path that you entered is incorrect');
262 // Get admin email and validate it
263 if (!isset($_POST['admin_email']) || $_POST['admin_email'] == '')
265 set_error('Please enter an email for the Administrator account');
269 if (eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['admin_email']))
271 $admin_email = $_POST['admin_email'];
275 set_error('Please enter a valid email address for the Administrator account');
278 // Get the two admin passwords entered, and check that they match
279 if (!isset($_POST['admin_password']) || $_POST['admin_password'] == '')
281 set_error('Please enter a password for the Administrator account');
285 $admin_password = $_POST['admin_password'];
287 if (!isset($_POST['admin_repassword']) || $_POST['admin_repassword'] == '')
289 set_error('Please make sure you re-enter the password for the Administrator account');
293 $admin_repassword = $_POST['admin_repassword'];
295 if ($admin_password != $admin_repassword)
297 set_error('Sorry, the two Administrator account passwords you entered do not match');
299 // End admin user details code
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");
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;
317 $config_filename = $path_to_root . '/config_db.php';
319 $err = write_config_db($table_prefix != "");
321 set_error("Cannot open the configuration file ($config_filename)");
323 set_error("Cannot write to the configuration file ($config_filename)");
325 set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
327 // Try connecting to database
329 $db = mysql_connect($database_host, $database_username, $database_password);
332 set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
335 if($install_tables == true)
337 if (!mysql_select_db($database_name, $db))
340 // Try to create the database
341 mysql_query('CREATE DATABASE '.$database_name);
342 mysql_select_db($database_name, $db);
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");
350 mysql_select_db($database_name, $db);
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");
361 header("Location: ".$path_to_root."/index.php");