| Conditions | 18 |
| Paths | 163 |
| Total Lines | 106 |
| Code Lines | 59 |
| Lines | 13 |
| Ratio | 12.26 % |
| Changes | 0 | ||
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 |
||
| 60 | private function dologinWithPostData() |
||
| 61 | { |
||
| 62 | $settings = require('config/settings.php'); |
||
| 63 | |||
| 64 | // check login form contents |
||
| 65 | if (empty($_POST['user_name'])) { |
||
| 66 | $this->errors[] = "Username field was empty."; |
||
| 67 | } elseif (empty($_POST['user_password'])) { |
||
| 68 | $this->errors[] = "Password field was empty."; |
||
| 69 | } elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) { |
||
| 70 | |||
| 71 | if (isset($settings['db']['port'])) { |
||
| 72 | $this->db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name']), decrypt($settings['db']['port'])); |
||
| 73 | } else { |
||
| 74 | $this->db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name'])); |
||
| 75 | } |
||
| 76 | |||
| 77 | // change character set to utf8 and check it |
||
| 78 | if (!$this->db_connection->set_charset("utf8")) { |
||
| 79 | $this->errors[] = $this->db_connection->error; |
||
| 80 | } |
||
| 81 | |||
| 82 | // if no connection errors (= working database connection) |
||
| 83 | if (!$this->db_connection->connect_errno) { |
||
| 84 | |||
| 85 | // escape the POST stuff |
||
| 86 | $user_name = $this->db_connection->real_escape_string($_POST['user_name']); |
||
| 87 | |||
| 88 | // database query, getting all the info of the selected user (allows login via email address in the |
||
| 89 | // username field) |
||
| 90 | $sql = "SELECT user_name, user_email, user_level, user_profile, permissions, user_password_hash, user_id, playerid, twoFactor, token |
||
| 91 | FROM users |
||
| 92 | WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';"; |
||
| 93 | $result_of_login_check = $this->db_connection->query($sql); |
||
| 94 | |||
| 95 | // if this user exists |
||
| 96 | if ($result_of_login_check->num_rows == 1) { |
||
| 97 | |||
| 98 | // get result row (as an object) |
||
| 99 | $result_row = $result_of_login_check->fetch_object(); |
||
| 100 | |||
| 101 | // using PHP 5.5's password_verify() function to check if the provided password fits |
||
| 102 | // the hash of that user's password |
||
| 103 | //var_dump(password_hash($_POST['user_password'], PASSWORD_DEFAULT)); |
||
| 104 | if (password_verify($_POST['user_password'], $result_row->user_password_hash)) { |
||
| 105 | if ($result_row->user_level <> 0) { |
||
| 106 | //$verify = json_decode(file_get_contents('http://cyberbyte.org.uk/hooks/cyberworks/messages.php?id=' . $settings['id'])); |
||
| 107 | //if (!isset($verify->verify)) { |
||
| 108 | $_SESSION['2factor'] = 0; |
||
| 109 | if (!empty($result_row->twoFactor)) { |
||
| 110 | if ($settings['2factor']) $_SESSION['2factor'] = 1; else { |
||
| 111 | $sql = "UPDATE `users` SET `backup`=NULL,`twoFactor`=NULL WHERE `userid` = '" . $result_row->user_id . "';"; |
||
| 112 | $this->db_connection->query($sql); |
||
| 113 | $this->errors[] = $lang['2factorForceRevoke']; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | if (isset($_COOKIE['token']) && !empty($result_row->token)) { |
||
| 118 | if (decrypt($result_row->token) == $_COOKIE['token']) { |
||
| 119 | $_SESSION['2factor'] = 2; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | $_SESSION['sudo'] = time(); |
||
| 123 | //$_SESSION['message'] = $verify; |
||
| 124 | $_SESSION['user_name'] = $result_row->user_name; |
||
| 125 | $_SESSION['user_level'] = $result_row->user_level; |
||
| 126 | $_SESSION['user_profile'] = $result_row->user_profile; |
||
| 127 | $_SESSION['user_email'] = $result_row->user_email; |
||
| 128 | $_SESSION['playerid'] = $result_row->playerid; |
||
| 129 | $_SESSION['user_id'] = $result_row->user_id; |
||
| 130 | $_SESSION['steamsignon'] = false; |
||
| 131 | $_SESSION['permissions'] = json_decode($result_row->permissions, true); |
||
| 132 | View Code Duplication | if (isset($result_row->items))$_SESSION['items'] = $result_row->items; else $_SESSION['items'] = $settings['items']; |
|
| 133 | if (isset($_POST['lang'])) { |
||
| 134 | setcookie('lang', $_POST['lang'], time() + (3600 * 24 * 30)); |
||
| 135 | $_SESSION['lang'] = $_POST['lang']; |
||
| 136 | } |
||
| 137 | $_SESSION['steamsignon'] = false; |
||
| 138 | $_SESSION['user_login_status'] = 1; |
||
| 139 | |||
| 140 | multiDB(); |
||
| 141 | logAction($_SESSION['user_name'], 'Successful Login (' . $_SERVER['REMOTE_ADDR'] . ')', 2); |
||
| 142 | /*} else { |
||
| 143 | if (isset($verify->message)) { |
||
| 144 | $this->errors[] = $verify->message; |
||
| 145 | } else { |
||
| 146 | $this->errors[] = "Verifcation Failed"; |
||
| 147 | } |
||
| 148 | }*/ |
||
| 149 | View Code Duplication | } else { |
|
| 150 | $this->errors[] = "User is banned."; |
||
| 151 | logAction($_POST['user_name'], 'Login Failed - Banned User (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
||
| 152 | } |
||
| 153 | View Code Duplication | } else { |
|
| 154 | $this->errors[] = "Wrong password. Try again."; |
||
| 155 | logAction($_POST['user_name'], 'Login Failed - Wrong Password (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
||
| 156 | } |
||
| 157 | View Code Duplication | } else { |
|
| 158 | $this->errors[] = "This user does not exist."; |
||
| 159 | logAction($_POST['user_name'], 'Login Failed - Wrong Username (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
||
| 160 | } |
||
| 161 | } else { |
||
| 162 | $this->errors[] = "Database connection problem."; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 180 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.