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