| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 172 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 1482 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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'])) { |
||
|
|
|||
| 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'] ) { |
||
| 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) { |
||
| 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 | |||
| 373 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.