2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
42 function checkDBSettings($silent=false) {
44 installLog("Begin DB Check Process *************");
47 copyInputsIntoSession();
49 $db = getInstallDbInstance();
51 installLog("testing with {$db->dbType}:{$db->variant}");
54 if( trim($_SESSION['setup_db_database_name']) == '' ){
55 $errors['ERR_DB_NAME'] = $mod_strings['ERR_DB_NAME'];
56 installLog("ERROR:: {$errors['ERR_DB_NAME']}");
60 if (!$db->isDatabaseNameValid($_SESSION['setup_db_database_name'])) {
61 $errIdx = 'ERR_DB_' . strtoupper($_SESSION['setup_db_type']) . '_DB_NAME_INVALID';
62 $errors[$errIdx] = $mod_strings[$errIdx];
63 installLog("ERROR:: {$errors[$errIdx]}");
66 if($_SESSION['setup_db_type'] != 'oci8') {
67 // Oracle doesn't need host name, others do
68 if( trim($_SESSION['setup_db_host_name']) == '' ){
69 $errors['ERR_DB_HOSTNAME'] = $mod_strings['ERR_DB_HOSTNAME'];
70 installLog("ERROR:: {$errors['ERR_DB_HOSTNAME']}");
74 //check to see that password and retype are same, if needed
75 if(!empty($_SESSION['dbUSRData']) && ($_SESSION['dbUSRData']=='create' || $_SESSION['dbUSRData']=='provide'))
77 if( $_SESSION['setup_db_sugarsales_password'] != $_SESSION['setup_db_sugarsales_password_retype'] ){
78 $errors['ERR_DBCONF_PASSWORD_MISMATCH'] = $mod_strings['ERR_DBCONF_PASSWORD_MISMATCH'];
79 installLog("ERROR:: {$errors['ERR_DBCONF_PASSWORD_MISMATCH']}");
83 // bail if the basic info isn't valid
84 if( count($errors) > 0 ){
85 installLog("Basic form info is INVALID, exit Process.");
86 return printErrors($errors);
88 installLog("Basic form info is valid, continuing Process.");
92 "db_host_name" => $_SESSION['setup_db_host_name'],
93 "db_host_instance" => $_SESSION['setup_db_host_instance'],
96 if(!empty($_SESSION['setup_db_port_num'])) {
97 $dbconfig["db_port"] = $_SESSION['setup_db_port_num'];
99 $_SESSION['setup_db_port_num'] = '';
102 // Needed for database implementation that do not allow connections to the server directly
103 // and that typically require the manual setup of a database instances such as DB2
104 if(empty($_SESSION['setup_db_create_database'])) {
105 $dbconfig["db_name"] = $_SESSION['setup_db_database_name'];
108 // Bug 29855 - Check to see if given db name is valid
109 if (preg_match("![\"'*/\\?:<>-]+!i", $_SESSION['setup_db_database_name']) ) {
110 $errors['ERR_DB_MSSQL_DB_NAME'] = $mod_strings['ERR_DB_MSSQL_DB_NAME_INVALID'];
111 installLog("ERROR:: {$errors['ERR_DB_MSSQL_DB_NAME']}");
114 // test the account that will talk to the db if we're not creating it
115 if( $_SESSION['setup_db_sugarsales_user'] != '' && !$_SESSION['setup_db_create_sugarsales_user'] ){
116 $dbconfig["db_user_name"] = $_SESSION['setup_db_sugarsales_user'];
117 $dbconfig["db_password"] = $_SESSION['setup_db_sugarsales_password'];
118 installLog("Testing user account...");
120 // try connecting to the DB
121 if(!$db->connect($dbconfig, false)) {
122 $error = $db->lastError();
123 $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
124 installLog("ERROR:: {$errors['ERR_DB_LOGIN_FAILURE']}");
126 installLog("Connection made using host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
131 // privileged account tests
132 if( empty($_SESSION['setup_db_admin_user_name']) ){
133 $errors['ERR_DB_PRIV_USER'] = $mod_strings['ERR_DB_PRIV_USER'];
134 installLog("ERROR:: {$errors['ERR_DB_PRIV_USER']}");
136 installLog("Testing priviliged account...");
137 $dbconfig["db_user_name"] = $_SESSION['setup_db_admin_user_name'];
138 $dbconfig["db_password"] = $_SESSION['setup_db_admin_password'];
139 if(!$db->connect($dbconfig, false)) {
140 $error = $db->lastError();
141 $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
142 installLog("ERROR:: {$errors['ERR_DB_LOGIN_FAILURE']}");
144 installLog("Connection made using host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
145 $db_selected = $db->dbExists($_SESSION['setup_db_database_name']);
146 if($silent==false && $db_selected && $_SESSION['setup_db_create_database'] && empty($_SESSION['setup_db_drop_tables'])) {
147 // DB exists but user didn't agree to overwrite it
148 $errStr = $mod_strings['ERR_DB_EXISTS_PROCEED'];
149 $errors['ERR_DB_EXISTS_PROCEED'] = $errStr;
150 installLog("ERROR:: {$errors['ERR_DB_EXISTS_PROCEED']}");
151 } elseif($silent==false && !$db_selected && !$_SESSION['setup_db_create_database'] ) {
152 // DB does not exist but user did not allow to create it
153 $errors['ERR_DB_EXISTS_NOT'] = $mod_strings['ERR_DB_EXISTS_NOT'];
154 installLog("ERROR:: {$errors['ERR_DB_EXISTS_NOT']}");
157 installLog("DB Selected, will reuse {$_SESSION['setup_db_database_name']}");
158 if($db->tableExists('config')) {
159 include('sugar_version.php');
160 $versions = $db->getOne("SELECT COUNT(*) FROM config WHERE category='info' AND name='sugar_version' AND VALUE LIKE '$sugar_db_version'");
161 if($versions > 0 && $silent==false) {
162 $errors['ERR_DB_EXISTS_WITH_CONFIG'] = $mod_strings['ERR_DB_EXISTS_WITH_CONFIG'];
163 installLog("ERROR:: {$errors['ERR_DB_EXISTS_WITH_CONFIG']}");
167 installLog("DB not selected, will create {$_SESSION['setup_db_database_name']}");
169 if($_SESSION['setup_db_create_sugarsales_user'] && $_SESSION['setup_db_sugarsales_user'] != '' && $db_selected) {
170 if($db->userExists($_SESSION['setup_db_sugarsales_user'])) {
171 $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
172 installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
178 $check = $db->canInstall();
179 if($check !== true) {
180 $error = array_shift($check);
181 array_unshift($check, $mod_strings[$error]);
182 $errors[$error] = call_user_func_array('sprintf', $check);
183 installLog("ERROR:: {$errors[$error]}");
185 installLog("Passed DB install check");
196 printErrors($errors);
198 installLog("End DB Check Process *************");
201 function printErrors($errors ){
204 if(count($errors) == 0){
205 echo 'dbCheckPassed';
206 installLog("SUCCESS:: no errors detected!");
207 }else if((count($errors) == 1 && (isset($errors["ERR_DB_EXISTS_PROCEED"])||isset($errors["ERR_DB_EXISTS_WITH_CONFIG"]))) ||
208 (count($errors) == 2 && isset($errors["ERR_DB_EXISTS_PROCEED"]) && isset($errors["ERR_DB_EXISTS_WITH_CONFIG"])) ){
209 ///throw alert asking to overwwrite db
211 installLog("WARNING:: no errors detected, but DB tables will be dropped!, issuing warning to user");
213 installLog("FATAL:: errors have been detected! User will not be allowed to continue. Errors are as follow:");
215 $validationErr = "<p><b>{$mod_strings['ERR_DBCONF_VALIDATION']}</b></p>";
216 $validationErr .= '<ul>';
218 foreach($errors as $key =>$erMsg){
219 if($key != "ERR_DB_EXISTS_PROCEED" && $key != "ERR_DB_EXISTS_WITH_CONFIG"){
220 if($_SESSION['dbUSRData'] == 'same' && $key == 'ERR_DB_ADMIN'){
221 installLog(".. {$erMsg}");
224 $validationErr .= '<li class="error">' . $erMsg . '</li>';
225 installLog(".. {$erMsg}");
228 $validationErr .= '</ul>';
229 $validationErr .= '</div>';
237 function copyInputsIntoSession(){
238 if(isset($_REQUEST['setup_db_type'])){$_SESSION['setup_db_type'] = $_REQUEST['setup_db_type'];}
239 if(isset($_REQUEST['setup_db_admin_user_name'])){$_SESSION['setup_db_admin_user_name'] = $_REQUEST['setup_db_admin_user_name'];}
240 if(isset($_REQUEST['setup_db_admin_password'])){$_SESSION['setup_db_admin_password'] = $_REQUEST['setup_db_admin_password'];}
241 if(isset($_REQUEST['setup_db_database_name'])){$_SESSION['setup_db_database_name'] = $_REQUEST['setup_db_database_name'];}
242 if(isset($_REQUEST['setup_db_host_name'])){$_SESSION['setup_db_host_name'] = $_REQUEST['setup_db_host_name'];}
245 $_SESSION['fts_type'] = isset($_REQUEST['fts_type']) ? $_REQUEST['fts_type'] : "";
246 if(isset($_REQUEST['fts_host'])){$_SESSION['fts_host'] = $_REQUEST['fts_host'];}
247 if(isset($_REQUEST['fts_port'])){$_SESSION['fts_port'] = $_REQUEST['fts_port'];}
249 if(isset($_SESSION['setup_db_type']) && (!isset($_SESSION['setup_db_manager']) || isset($_REQUEST['setup_db_type']))) {
250 $_SESSION['setup_db_manager'] = DBManagerFactory::getManagerByType($_SESSION['setup_db_type']);
253 if(isset($_REQUEST['setup_db_host_instance'])){
254 $_SESSION['setup_db_host_instance'] = $_REQUEST['setup_db_host_instance'];
257 if(isset($_REQUEST['setup_db_port_num'])){
258 $_SESSION['setup_db_port_num'] = $_REQUEST['setup_db_port_num'];
261 // on a silent install, copy values from $_SESSION into $_REQUEST
262 if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall') {
263 if (isset($_SESSION['dbUSRData']) && !empty($_SESSION['dbUSRData']))
264 $_REQUEST['dbUSRData'] = $_SESSION['dbUSRData'];
265 else $_REQUEST['dbUSRData'] = 'same';
267 if (isset($_SESSION['setup_db_sugarsales_user']) && !empty($_SESSION['setup_db_sugarsales_user']))
268 $_REQUEST['setup_db_sugarsales_user'] = $_SESSION['setup_db_sugarsales_user'];
269 else $_REQUEST['dbUSRData'] = 'same';
271 $_REQUEST['setup_db_sugarsales_password'] = $_SESSION['setup_db_sugarsales_password'];
272 $_REQUEST['setup_db_sugarsales_password_retype'] = $_SESSION['setup_db_sugarsales_password'];
275 //make sure we are creating or using provided user for app db connections
276 $_SESSION['setup_db_create_sugarsales_user'] = true;//get_boolean_from_request('setup_db_create_sugarsales_user');
277 $db = getInstallDbInstance();
278 if( !$db->supports("create_user") ){
279 //if the DB doesn't support creating users, make the admin user/password same as connecting user/password
280 $_SESSION['setup_db_sugarsales_user'] = $_SESSION['setup_db_admin_user_name'];
281 $_SESSION['setup_db_sugarsales_password'] = $_SESSION['setup_db_admin_password'];
282 $_SESSION['setup_db_sugarsales_password_retype'] = $_SESSION['setup_db_sugarsales_password'];
283 $_SESSION['setup_db_create_sugarsales_user'] = false;
284 $_SESSION['setup_db_create_database'] = false;
287 $_SESSION['setup_db_create_database'] = true;
288 //retrieve the value from dropdown in order to know what settings the user
289 //wants to use for the sugar db user.
291 //use provided db admin by default
292 $_SESSION['dbUSRData'] = 'same';
294 if(isset($_REQUEST['dbUSRData']) && !empty($_REQUEST['dbUSRData'])){
295 $_SESSION['dbUSRData'] = $_REQUEST['dbUSRData'];
299 if($_SESSION['dbUSRData'] == 'auto'){
300 //create user automatically
301 $_SESSION['setup_db_create_sugarsales_user'] = true;
302 $_SESSION['setup_db_sugarsales_user'] = "sugar".create_db_user_creds(5);
303 $_SESSION['setup_db_sugarsales_password'] = create_db_user_creds(10);
304 $_SESSION['setup_db_sugarsales_password_retype'] = $_SESSION['setup_db_sugarsales_password'];
305 }elseif($_SESSION['dbUSRData'] == 'provide'){
306 //use provided user info
307 $_SESSION['setup_db_create_sugarsales_user'] = false;
308 $_SESSION['setup_db_sugarsales_user'] = $_REQUEST['setup_db_sugarsales_user'];
309 $_SESSION['setup_db_sugarsales_password'] = $_REQUEST['setup_db_sugarsales_password'];
310 $_SESSION['setup_db_sugarsales_password_retype'] = $_REQUEST['setup_db_sugarsales_password_retype'];
311 }elseif($_SESSION['dbUSRData'] == 'create'){
312 // create user with provided info
313 $_SESSION['setup_db_create_sugarsales_user'] = true;
314 $_SESSION['setup_db_sugarsales_user'] = $_REQUEST['setup_db_sugarsales_user'];
315 $_SESSION['setup_db_sugarsales_password'] = $_REQUEST['setup_db_sugarsales_password'];
316 $_SESSION['setup_db_sugarsales_password_retype'] = $_REQUEST['setup_db_sugarsales_password_retype'];
318 //Use the same login as provided admin user
319 $_SESSION['setup_db_create_sugarsales_user'] = false;
320 $_SESSION['setup_db_sugarsales_user'] = $_SESSION['setup_db_admin_user_name'];
321 $_SESSION['setup_db_sugarsales_password'] = $_SESSION['setup_db_admin_password'];
322 $_SESSION['setup_db_sugarsales_retype'] = $_SESSION['setup_db_admin_password'];
326 if(!isset($_SESSION['demoData']) || empty($_SESSION['demoData'])){
327 $_SESSION['demoData'] = 'no';
329 if(isset($_REQUEST['demoData'])){$_SESSION['demoData'] = $_REQUEST['demoData'] ;}
331 if($db->supports('create_db')) {
332 if(!empty($_SESSION['setup_db_create_database'])) {
333 // if we're dropping DB, no need to drop tables
334 $_SESSION['setup_db_drop_tables'] = false;
337 // we can't create DB, so can't drop it
338 $_SESSION['setup_db_create_database'] = false;
341 if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall' && isset($_SESSION['setup_db_drop_tables'])) {
342 //set up for Oracle Silent Installer
343 $_REQUEST['setup_db_drop_tables'] = $_SESSION['setup_db_drop_tables'] ;
348 ///////////////////////////////////////////////////////////////////////////////