checkDBSettings.php ➔ checkDBSettings()   F
last analyzed

Complexity

Conditions 38
Paths > 20000

Size

Total Lines 172
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1482
Metric Value
cc 38
eloc 109
nc 155556
nop 1
dl 0
loc 172
ccs 0
cts 136
cp 0
crap 1482
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42
43
44
45
function checkDBSettings($silent=false) {
46
47
    installLog("Begin DB Check Process *************");
48
    global $mod_strings;
49
    $errors = array();
50
    copyInputsIntoSession();
51
52
    $db = getInstallDbInstance();
53
54
    installLog("testing with {$db->dbType}:{$db->variant}");
55
56
57
        if( trim($_SESSION['setup_db_database_name']) == '' ){
58
            $errors['ERR_DB_NAME'] = $mod_strings['ERR_DB_NAME'];
59
            installLog("ERROR::  {$errors['ERR_DB_NAME']}");
60
        }
61
62
63
        if (!$db->isDatabaseNameValid($_SESSION['setup_db_database_name'])) {
64
            $errIdx = 'ERR_DB_' . strtoupper($_SESSION['setup_db_type']) . '_DB_NAME_INVALID';
65
            $errors[$errIdx] = $mod_strings[$errIdx];
66
            installLog("ERROR::  {$errors[$errIdx]}");
67
        }
68
69
        if($_SESSION['setup_db_type'] != 'oci8') {
70
            // Oracle doesn't need host name, others do
71
            if( trim($_SESSION['setup_db_host_name']) == '' ){
72
                $errors['ERR_DB_HOSTNAME'] = $mod_strings['ERR_DB_HOSTNAME'];
73
                installLog("ERROR::  {$errors['ERR_DB_HOSTNAME']}");
74
            }
75
        }
76
77
        //check to see that password and retype are same, if needed
78
        if(!empty($_SESSION['dbUSRData']) && ($_SESSION['dbUSRData']=='create' || $_SESSION['dbUSRData']=='provide'))
79
        {
80
            if( $_SESSION['setup_db_sugarsales_password'] != $_SESSION['setup_db_sugarsales_password_retype'] ){
81
                $errors['ERR_DBCONF_PASSWORD_MISMATCH'] = $mod_strings['ERR_DBCONF_PASSWORD_MISMATCH'];
82
                installLog("ERROR::  {$errors['ERR_DBCONF_PASSWORD_MISMATCH']}");
83
            }
84
        }
85
86
        // bail if the basic info isn't valid
87
        if( count($errors) > 0 ){
88
                installLog("Basic form info is INVALID, exit Process.");
89
            return printErrors($errors);
90
        } else {
91
            installLog("Basic form info is valid, continuing Process.");
92
        }
93
94
        $dbconfig = array(
95
                "db_host_name" => $_SESSION['setup_db_host_name'],
96
                "db_host_instance" => $_SESSION['setup_db_host_instance'],
97
        );
98
99
        if(!empty($_SESSION['setup_db_port_num'])) {
100
            $dbconfig["db_port"] = $_SESSION['setup_db_port_num'];
101
        } else {
102
            $_SESSION['setup_db_port_num'] = '';
103
        }
104
105
        // Needed for database implementation that do not allow connections to the server directly
106
        // and that typically require the manual setup of a database instances such as DB2
107
        if(empty($_SESSION['setup_db_create_database'])) {
108
            $dbconfig["db_name"] = $_SESSION['setup_db_database_name'];
109
        }
110
111
        // check database name validation in different database types (default is mssql)
112
        switch (strtolower($db->dbType)) {
113
114
            case 'mysql':
115
                if (preg_match("![/\\.]+!i", $_SESSION['setup_db_database_name']) ) {
116
                    $errors['ERR_DB_MYSQL_DB_NAME'] = $mod_strings['ERR_DB_MYSQL_DB_NAME_INVALID'];
117
                    installLog("ERROR::  {$errors['ERR_DB_MYSQL_DB_NAME']}");
118
                }
119
                break;
120
121
            case 'mssql':
122
            default:
123
                // Bug 29855 - Check to see if given db name is valid
124
                if (preg_match("![\"'*/\\?:<>-]+!i", $_SESSION['setup_db_database_name']) ) {
125
                    $errors['ERR_DB_MSSQL_DB_NAME'] = $mod_strings['ERR_DB_MSSQL_DB_NAME_INVALID'];
126
                    installLog("ERROR::  {$errors['ERR_DB_MSSQL_DB_NAME']}");
127
                }
128
                break;
129
        }
130
131
        // test the account that will talk to the db if we're not creating it
132
        if( $_SESSION['setup_db_sugarsales_user'] != '' && !$_SESSION['setup_db_create_sugarsales_user'] ){
133
            $dbconfig["db_user_name"] = $_SESSION['setup_db_sugarsales_user'];
134
            $dbconfig["db_password"] = $_SESSION['setup_db_sugarsales_password'];
135
            installLog("Testing user account...");
136
137
            // try connecting to the DB
138
            if(!$db->connect($dbconfig, false)) {
139
                $error = $db->lastError();
140
                $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
141
                installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
142
            } else {
143
                installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
144
                $db->disconnect();
145
            }
146
        }
147
148
        // privileged account tests
149
        if( empty($_SESSION['setup_db_admin_user_name']) ){
150
            $errors['ERR_DB_PRIV_USER'] = $mod_strings['ERR_DB_PRIV_USER'];
151
            installLog("ERROR:: {$errors['ERR_DB_PRIV_USER']}");
152
        } else {
153
            installLog("Testing priviliged account...");
154
            $dbconfig["db_user_name"] = $_SESSION['setup_db_admin_user_name'];
155
            $dbconfig["db_password"] = $_SESSION['setup_db_admin_password'];
156
            if(!$db->connect($dbconfig, false)) {
157
                $error = $db->lastError();
158
                $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE'];
159
                installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
160
            } else {
161
                installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
162
                $db_selected = $db->dbExists($_SESSION['setup_db_database_name']);
163
                if($silent==false && $db_selected && $_SESSION['setup_db_create_database'] && empty($_SESSION['setup_db_drop_tables'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
164
                    // DB exists but user didn't agree to overwrite it
165
                        $errStr = $mod_strings['ERR_DB_EXISTS_PROCEED'];
166
                        $errors['ERR_DB_EXISTS_PROCEED'] = $errStr;
167
                        installLog("ERROR:: {$errors['ERR_DB_EXISTS_PROCEED']}");
168
                } elseif($silent==false && !$db_selected && !$_SESSION['setup_db_create_database'] ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
169
                    // DB does not exist but user did not allow to create it
170
                        $errors['ERR_DB_EXISTS_NOT'] = $mod_strings['ERR_DB_EXISTS_NOT'];
171
                        installLog("ERROR:: {$errors['ERR_DB_EXISTS_NOT']}");
172
                } else {
173
                    if($db_selected) {
174
                        installLog("DB Selected, will reuse {$_SESSION['setup_db_database_name']}");
175
                        if($db->tableExists('config')) {
176
                           include('sugar_version.php');
177
                           $versions = $db->getOne("SELECT COUNT(*) FROM config WHERE category='info' AND name='sugar_version' AND VALUE LIKE '$sugar_db_version'");
178
                           if($versions > 0 && $silent==false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
179
                               $errors['ERR_DB_EXISTS_WITH_CONFIG'] = $mod_strings['ERR_DB_EXISTS_WITH_CONFIG'];
180
                               installLog("ERROR:: {$errors['ERR_DB_EXISTS_WITH_CONFIG']}");
181
                           }
182
                        }
183
                    } else {
184
                        installLog("DB not selected, will create {$_SESSION['setup_db_database_name']}");
185
                    }
186
                    if($_SESSION['setup_db_create_sugarsales_user'] && $_SESSION['setup_db_sugarsales_user'] != '' && $db_selected) {
187
                        if($db->userExists($_SESSION['setup_db_sugarsales_user'])) {
188
                            $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
189
                            installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
190
                        }
191
                    }
192
                }
193
194
                // DB SPECIFIC
195
                $check = $db->canInstall();
196
                if($check !== true) {
197
                    $error = array_shift($check);
198
                    array_unshift($check, $mod_strings[$error]);
199
                    $errors[$error] = call_user_func_array('sprintf', $check);
200
                    installLog("ERROR:: {$errors[$error]}");
201
                } else {
202
                    installLog("Passed DB install check");
203
                }
204
205
                $db->disconnect();
206
            }
207
        }
208
209
210
        if($silent){
211
            return $errors;
212
        }else{
213
            printErrors($errors);
214
        }
215
        installLog("End DB Check Process *************");
216
}
217
218
function printErrors($errors ){
219
220
global $mod_strings;
221
    if(count($errors) == 0){
222
        echo 'dbCheckPassed';
223
        installLog("SUCCESS:: no errors detected!");
224
    }else if((count($errors) == 1 && (isset($errors["ERR_DB_EXISTS_PROCEED"])||isset($errors["ERR_DB_EXISTS_WITH_CONFIG"])))  ||
225
    (count($errors) == 2 && isset($errors["ERR_DB_EXISTS_PROCEED"]) && isset($errors["ERR_DB_EXISTS_WITH_CONFIG"])) ){
226
        ///throw alert asking to overwwrite db
227
        echo 'preexeest';
228
        installLog("WARNING:: no errors detected, but DB tables will be dropped!, issuing warning to user");
229
    }else{
230
        installLog("FATAL:: errors have been detected!  User will not be allowed to continue.  Errors are as follow:");
231
         //print out errors
232
        $validationErr  = "<p><b>{$mod_strings['ERR_DBCONF_VALIDATION']}</b></p>";
233
        $validationErr .= '<ul>';
234
235
        foreach($errors as $key =>$erMsg){
236
            if($key != "ERR_DB_EXISTS_PROCEED" && $key != "ERR_DB_EXISTS_WITH_CONFIG"){
237
                if($_SESSION['dbUSRData'] == 'same' && $key == 'ERR_DB_ADMIN'){
238
                    installLog(".. {$erMsg}");
239
                    break;
240
                }
241
                $validationErr .= '<li class="error">' . $erMsg . '</li>';
242
                installLog(".. {$erMsg}");
243
            }
244
        }
245
        $validationErr .= '</ul>';
246
        $validationErr .= '</div>';
247
248
         echo $validationErr;
249
    }
250
251
}
252
253
254
function copyInputsIntoSession(){
255
            if(isset($_REQUEST['setup_db_type'])){$_SESSION['setup_db_type']                        = $_REQUEST['setup_db_type'];}
256
            if(isset($_REQUEST['setup_db_admin_user_name'])){$_SESSION['setup_db_admin_user_name']  = $_REQUEST['setup_db_admin_user_name'];}
257
            if(isset($_REQUEST['setup_db_admin_password'])){$_SESSION['setup_db_admin_password']    = $_REQUEST['setup_db_admin_password'];}
258
            if(isset($_REQUEST['setup_db_database_name'])){$_SESSION['setup_db_database_name']      = $_REQUEST['setup_db_database_name'];}
259
            if(isset($_REQUEST['setup_db_host_name'])){$_SESSION['setup_db_host_name']              = $_REQUEST['setup_db_host_name'];}
260
261
            //FTS Support
262
            if (isset($_REQUEST['setup_fts_type'])) {
263
                $_SESSION['setup_fts_type'] = $_REQUEST['setup_fts_type'];
264
            }
265
            if (isset($_REQUEST['setup_fts_host'])) {
266
                $_SESSION['setup_fts_host'] = $_REQUEST['setup_fts_host'];
267
            }
268
            if (isset($_REQUEST['setup_fts_port'])) {
269
                $_SESSION['setup_fts_port'] = $_REQUEST['setup_fts_port'];
270
            }
271
272
            if(isset($_SESSION['setup_db_type']) && (!isset($_SESSION['setup_db_manager']) || isset($_REQUEST['setup_db_type']))) {
273
                $_SESSION['setup_db_manager'] = DBManagerFactory::getManagerByType($_SESSION['setup_db_type']);
274
            }
275
276
            if(isset($_REQUEST['setup_db_host_instance'])){
277
                $_SESSION['setup_db_host_instance'] = $_REQUEST['setup_db_host_instance'];
278
            }
279
280
            if(isset($_REQUEST['setup_db_port_num'])){
281
                $_SESSION['setup_db_port_num'] = $_REQUEST['setup_db_port_num'];
282
            }
283
284
            // on a silent install, copy values from $_SESSION into $_REQUEST
285
            if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall') {
286
                if (isset($_SESSION['dbUSRData']) && !empty($_SESSION['dbUSRData']))
287
                    $_REQUEST['dbUSRData'] = $_SESSION['dbUSRData'];
288
                else $_REQUEST['dbUSRData'] = 'same';
289
290
                if (isset($_SESSION['setup_db_sugarsales_user']) && !empty($_SESSION['setup_db_sugarsales_user']))
291
                    $_REQUEST['setup_db_sugarsales_user'] = $_SESSION['setup_db_sugarsales_user'];
292
                else $_REQUEST['dbUSRData'] = 'same';
293
294
                $_REQUEST['setup_db_sugarsales_password'] = $_SESSION['setup_db_sugarsales_password'];
295
                $_REQUEST['setup_db_sugarsales_password_retype'] = $_SESSION['setup_db_sugarsales_password'];
296
            }
297
298
            //make sure we are creating or using provided user for app db connections
299
            $_SESSION['setup_db_create_sugarsales_user']  = true;//get_boolean_from_request('setup_db_create_sugarsales_user');
300
            $db = getInstallDbInstance();
301
            if( !$db->supports("create_user") ){
302
             //if the DB doesn't support creating users, make the admin user/password same as connecting user/password
303
              $_SESSION['setup_db_sugarsales_user']             = $_SESSION['setup_db_admin_user_name'];
304
              $_SESSION['setup_db_sugarsales_password']         = $_SESSION['setup_db_admin_password'];
305
              $_SESSION['setup_db_sugarsales_password_retype']  = $_SESSION['setup_db_sugarsales_password'];
306
              $_SESSION['setup_db_create_sugarsales_user']      = false;
307
              $_SESSION['setup_db_create_database']             = false;
308
309
            } else {
310
            	$_SESSION['setup_db_create_database']             = true;
311
                //retrieve the value from dropdown in order to know what settings the user
312
                //wants to use for the sugar db user.
313
314
                //use provided db admin by default
315
                $_SESSION['dbUSRData'] = 'same';
316
317
                if(isset($_REQUEST['dbUSRData'])  && !empty($_REQUEST['dbUSRData'])){
318
                    $_SESSION['dbUSRData'] = $_REQUEST['dbUSRData'];
319
                }
320
321
322
                  if($_SESSION['dbUSRData'] == 'auto'){
323
                    //create user automatically
324
                      $_SESSION['setup_db_create_sugarsales_user']          = true;
325
                      $_SESSION['setup_db_sugarsales_user']                 = "sugar".create_db_user_creds(5);
326
                      $_SESSION['setup_db_sugarsales_password']             = create_db_user_creds(10);
327
                      $_SESSION['setup_db_sugarsales_password_retype']      = $_SESSION['setup_db_sugarsales_password'];
328
                  }elseif($_SESSION['dbUSRData'] == 'provide'){
329
                    //use provided user info
330
                      $_SESSION['setup_db_create_sugarsales_user']          = false;
331
                      $_SESSION['setup_db_sugarsales_user']                 = $_REQUEST['setup_db_sugarsales_user'];
332
                      $_SESSION['setup_db_sugarsales_password']             = $_REQUEST['setup_db_sugarsales_password'];
333
                      $_SESSION['setup_db_sugarsales_password_retype']      = $_REQUEST['setup_db_sugarsales_password_retype'];
334
                  }elseif($_SESSION['dbUSRData'] == 'create'){
335
                    // create user with provided info
336
                      $_SESSION['setup_db_create_sugarsales_user']        = true;
337
                      $_SESSION['setup_db_sugarsales_user']               = $_REQUEST['setup_db_sugarsales_user'];
338
                      $_SESSION['setup_db_sugarsales_password']           = $_REQUEST['setup_db_sugarsales_password'];
339
                      $_SESSION['setup_db_sugarsales_password_retype']    = $_REQUEST['setup_db_sugarsales_password_retype'];
340
                  }else{
341
                   //Use the same login as provided admin user
342
                      $_SESSION['setup_db_create_sugarsales_user']      = false;
343
                      $_SESSION['setup_db_sugarsales_user']             = $_SESSION['setup_db_admin_user_name'];
344
                      $_SESSION['setup_db_sugarsales_password']         = $_SESSION['setup_db_admin_password'];
345
                      $_SESSION['setup_db_sugarsales_retype']           = $_SESSION['setup_db_admin_password'];
346
                  }
347
            }
348
349
            if(!isset($_SESSION['demoData']) || empty($_SESSION['demoData'])){
350
                $_SESSION['demoData'] = 'no';
351
            }
352
            if(isset($_REQUEST['demoData'])){$_SESSION['demoData'] = $_REQUEST['demoData'] ;}
353
354
            if($db->supports('create_db')) {
355
                if(!empty($_SESSION['setup_db_create_database'])) {
356
            	// if we're dropping DB, no need to drop tables
357
                	$_SESSION['setup_db_drop_tables']  = false;
358
                }
359
            } else {
360
                // we can't create DB, so can't drop it
361
                $_SESSION['setup_db_create_database'] = false;
362
            }
363
364
            if (isset($_REQUEST['goto']) && $_REQUEST['goto'] == 'SilentInstall' && isset($_SESSION['setup_db_drop_tables'])) {
365
                //set up for Oracle Silent Installer
366
                $_REQUEST['setup_db_drop_tables'] = $_SESSION['setup_db_drop_tables'] ;
367
            }
368
}
369
370
////    END PAGEOUTPUT
371
///////////////////////////////////////////////////////////////////////////////
372
?>
373