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