1e568b653d8d6a9f4a399fccb0f899d1bb1b4712
[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, $path_to_root;
31
32         if(isset($message) AND $message != '')
33         {
34                 // Copy values entered into session so user doesn't have to re-enter everything
35                 if(isset($_POST['company_name']))
36                 {
37                         $_SESSION['ba_url'] = $_POST['ba_url'];
38                         $_SESSION['operating_system'] = isset($_POST['operating_system']);
39                         $_SESSION['world_writeable'] = isset($_POST['world_writeable']);
40                         $_SESSION['database_host'] = $_POST['database_host'];
41                         $_SESSION['database_username'] = $_POST['database_username'];
42                         $_SESSION['database_password'] = $_POST['database_password'];
43                         $_SESSION['database_name'] = $_POST['database_name'];
44                         $_SESSION['demo_data'] = isset($_POST['demo_data']);
45                         $_SESSION['table_prefix'] = isset($_POST['table_prefix']);
46                         $_SESSION['install_tables'] = isset($_POST['install_tables']);
47                         $_SESSION['company_name'] = $_POST['company_name'];
48                         $_SESSION['admin_email'] = $_POST['admin_email'];
49                         $_SESSION['admin_password'] = $_POST['admin_password'];
50
51                 }
52                 // Set the message
53                 $_SESSION['message'] = $message;
54                 // Specify that session support is enabled
55                 $_SESSION['session_support'] = '<font class="good">Enabled</font>';
56                 // Redirect to first page again and exit
57                 @unlink($path_to_root."/config.php"); // remove just created config file
58                 header('Location: index.php?sessions_checked=true');
59                 exit();
60         }
61 }
62
63 // Function to workout what the default permissions are for files created by the webserver
64 function default_file_mode($temp_dir)
65 {
66         $v = explode(".",PHP_VERSION);
67         $v = $v[0].$v[1];
68         if($v > 41 && is_writable($temp_dir))
69         {
70                 $filename = $temp_dir.'/test_permissions.txt';
71                 $handle = fopen($filename, 'w');
72                 fwrite($handle, 'This file is to get the default file permissions');
73                 fclose($handle);
74                 $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
75                 unlink($filename);
76         }
77         else
78         {
79                 $default_file_mode = '0777';
80         }
81         return $default_file_mode;
82 }
83
84 // Function to workout what the default permissions are for directories created by the webserver
85 function default_dir_mode($temp_dir)
86 {
87         $v = explode(".",PHP_VERSION);
88         $v = $v[0].$v[1];
89         if ($v > 41 && is_writable($temp_dir))
90         {
91                 $dirname = $temp_dir.'/test_permissions/';
92                 mkdir($dirname);
93                 $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
94                 rmdir($dirname);
95         }
96         else
97         {
98                 $default_dir_mode = '0777';
99         }
100         return $default_dir_mode;
101 }
102
103 function add_slashes($input)
104 {
105         if (get_magic_quotes_gpc() || (!is_string($input)))
106         {
107                 return $input;
108         }
109         $output = addslashes($input);
110         return $output;
111 }
112 function check_db_error($err_msg, $sql)
113 {
114         return true;
115 }
116
117 if (isset($_POST['path_to_root']))
118         $path_to_root = $_POST['path_to_root'];
119 else
120         $path_to_root = "..";
121
122 // Begin check to see if form was even submitted
123 // Set error if no post vars found
124 if (!isset($_POST['company_name']))
125 {
126         display_error('Please fill-in the form below');
127 }
128 // End check to see if form was even submitted
129
130 // Begin path and timezone details code
131
132 // Check if user has entered the installation url
133 if (!isset($_POST['ba_url']) || $_POST['ba_url'] == '')
134 {
135         display_error('Please enter an absolute URL');
136 }
137 else
138 {
139         $ba_url = $_POST['ba_url'];
140 }
141
142 // Remove any slashes at the end of the URL
143 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
144 {
145         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
146 }
147 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
148 {
149         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
150 }
151 if(substr($ba_url, strlen($ba_url) - 1, 1) == "/")
152 {
153         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
154 }
155 if(substr($ba_url, strlen($ba_url) - 1, 1) == "\\")
156 {
157         $ba_url = substr($ba_url, 0, strlen($ba_url) - 1);
158 }
159 // End path
160
161 // Begin operating system specific code
162 // Get operating system
163 if (!isset($_POST['operating_system']) || $_POST['operating_system'] != 'linux' && $_POST['operating_system'] != 'windows')
164 {
165         display_error('Please select a valid operating system');
166 }
167 else
168 {
169         $operating_system = $_POST['operating_system'];
170 }
171 // Work-out file permissions
172 if($operating_system == 'windows')
173 {
174         $file_mode = '0777';
175         $dir_mode = '0777';
176 }
177 elseif (isset($_POST['world_writeable']) && $_POST['world_writeable'] == 'true')
178 {
179         $file_mode = '0777';
180         $dir_mode = '0777';
181 }
182 else
183 {
184         $file_mode = default_file_mode('../includes');
185         $dir_mode = default_dir_mode('../includes');
186 }
187 // End operating system specific code
188
189 // Begin database details code
190 // Check if user has entered a database host
191 if (!isset($_POST['database_host']) || $_POST['database_host'] == '')
192 {
193         display_error('Please enter a database host name');
194 }
195 else
196 {
197         $database_host = $_POST['database_host'];
198 }
199 // Check if user has entered a database username
200 if (!isset($_POST['database_username']) || $_POST['database_username'] == '')
201 {
202         display_error('Please enter a database username');
203 }
204 else
205 {
206         $database_username = $_POST['database_username'];
207 }
208 // Check if user has entered a database password
209 if (!isset($_POST['database_password']))
210 {
211         display_error('Please enter a database password');
212 }
213 else
214 {
215         $database_password = $_POST['database_password'];
216 }
217 // Check if user has entered a database name
218 if (!isset($_POST['database_name']) || $_POST['database_name'] == '')
219 {
220         display_error('Please enter a database name');
221 }
222 else
223 {
224         $database_name = $_POST['database_name'];
225 }
226 // Get table prefix
227 if (isset($_POST['table_prefix']) && $_POST['table_prefix'] == 'true')
228         $table_prefix = "0_";
229 else
230         $table_prefix = "";
231
232 // Find out if the user wants to install tables and data
233 if (isset($_POST['install_tables']) && $_POST['install_tables'] == 'true')
234 {
235         $install_tables = true;
236 }
237 else
238 {
239         $install_tables = false;
240 }
241 // End database details code
242
243 // Begin company name code
244 // Get company name
245 if (!isset($_POST['company_name']) || $_POST['company_name'] == '')
246 {
247         display_error('Please enter a company name');
248 }
249 else
250 {
251         $company_name = add_slashes($_POST['company_name']);
252 }
253 // End website company name
254
255 // Check if the user has entered a correct path
256 if (!file_exists($path_to_root.'/sql/en_US-'.(isset($_POST['demo_data']) ? 'demo':'new').'.sql'))
257 {
258         display_error('It appears the Absolute path that you entered is incorrect');
259 }
260
261 // Get admin email and validate it
262 if (!isset($_POST['admin_email']) || $_POST['admin_email'] == '')
263 {
264         display_error('Please enter an email for the Administrator account');
265 }
266 else
267 {
268         if (preg_match("/^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/i", $_POST['admin_email']))
269         {
270                 $admin_email = $_POST['admin_email'];
271         }
272         else
273         {
274                 display_error('Please enter a valid email address for the Administrator account');
275         }
276 }
277 // Get the two admin passwords entered, and check that they match
278 if (!isset($_POST['admin_password']) || $_POST['admin_password'] == '')
279 {
280         display_error('Please enter a password for the Administrator account');
281 }
282 else
283 {
284         $admin_password = $_POST['admin_password'];
285 }
286 if (!isset($_POST['admin_repassword']) || $_POST['admin_repassword'] == '')
287 {
288         display_error('Please make sure you re-enter the password for the Administrator account');
289 }
290 else
291 {
292         $admin_repassword = $_POST['admin_repassword'];
293 }
294 if ($admin_password != $admin_repassword)
295 {
296         display_error('Sorry, the two Administrator account passwords you entered do not match');
297 }
298 // End admin user details code
299
300 copy($path_to_root. "/config.default.php", $path_to_root. "/config.php");
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_regenerate_id();
386 session_destroy();
387 $_SESSION = array();
388
389 header("Location: ".$path_to_root."/index.php");
390 exit();
391
392 ?>