Login   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 173
Duplicated Lines 7.51 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 13
loc 173
rs 10
c 0
b 0
f 0
wmc 26
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A doLogout() 0 12 2
D dologinWithPostData() 13 106 18
A isUserLoggedIn() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
require_once("gfunctions.php");
3
/**
4
     * Class login
5
     * handles the user's login and logout process
6
     */
7
class Login
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var array Collection of error messages
11
     */
12
    public $errors = array();
13
    /**
14
     * @var array Collection of success / neutral messages
15
     */
16
    public $messages = array();
17
    /**
18
     * @var object The database connection
19
     */
20
    private $db_connection = null;
21
22
    /**
23
     * the function "__construct()" automatically starts whenever an object of this class is created,
24
     * you know, when you do "$login = new Login();"
25
     */
26
    public function __construct()
27
    {
28
        // create/read session, absolutely necessary
29
        //session_start();
30
        // check the possible login actions:
31
        // if user tried to log out (happen when user clicks logout button)
32
        if (isset($_GET["logout"])) {
33
            $this->doLogout();
34
        } // login via post data (if user just submitted a login form)
35
        elseif (isset($_POST["login"])) {
36
            $this->dologinWithPostData();
37
        }
38
    }
39
40
41
    /**
42
     * perform the logout
43
     */
44
    public function doLogout()
45
    {
46
        // delete the session of the user
47
        if (isset($_SESSION['user_name'])) {
48
            logAction($_SESSION['user_name'], 'Logged Out', 1);
49
        }
50
        $_SESSION = array();
51
        session_destroy();
52
        // return a little feeedback message
53
        $this->messages[] = 'You have been logged out';
54
55
    }
56
57
    /**
58
     * log in with post data
59
     */
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);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $result_of_login_check exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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']));
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
                            //if (!isset($verify->verify)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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'];
0 ignored issues
show
Bug introduced by
The variable $lang does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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'];
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143
                                if (isset($verify->message)) {
144
                                    $this->errors[] = $verify->message;
145
                                } else {
146
                                    $this->errors[] = "Verifcation Failed";
147
                                }
148
                            }*/
149 View Code Duplication
                        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
167
    /**
168
     * simply return the current state of the user's login
169
     * @return boolean user's login status
170
     */
171
    public function isUserLoggedIn()
172
    {
173
        if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
174
            return true;
175
        }
176
        // default return
177
        return false;
178
    }
179
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
180