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