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