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