Users::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 19
c 1
b 1
f 0
nc 2
nop 8
dl 0
loc 22
rs 9.6333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
class Users
4
{
5
    private $db;
6
7
    public function __construct($database)
8
    {
9
        $this->db = $database;
10
    }
11
12
    public function user_exists($username)
13
    {
14
        $query = $this->db->prepare('SELECT COUNT(`id`) FROM `users` WHERE `username`= ?');
15
        $query->bindValue(1, $username);
16
17
        try {
18
            $query->execute();
19
            $rows = $query->fetchColumn();
20
            if ($rows == 1) {
21
                return true;
22
            } else {
23
                return false;
24
            }
25
        } catch (PDOException $e) {
26
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
27
        }
28
    }
29
30
    public function email_exists($email)
31
    {
32
        $query = $this->db->prepare('SELECT COUNT(`id`) FROM `users` WHERE `email`= ?');
33
        $query->bindValue(1, $email);
34
35
        try {
36
            $query->execute();
37
            $rows = $query->fetchColumn();
38
            if ($rows == 1) {
39
                return true;
40
            } else {
41
                return false;
42
            }
43
        } catch (PDOException $e) {
44
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
45
        }
46
    }
47
48
    public function register($username, $password, $email, $fullname, $Telp, $level, $locked)
0 ignored issues
show
Unused Code introduced by
The parameter $locked is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    public function register($username, $password, $email, $fullname, $Telp, $level, /** @scrutinizer ignore-unused */ $locked)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $time = time();
51
        $ip = $_SERVER['REMOTE_ADDR'];
52
        $email_code = sha1($username + microtime());
53
        $password = sha1($password);
54
        $query = $this->db->prepare('INSERT INTO `users` (`username`,`level`, `password`, `fullname`, `email`, `Telp`,`ip`, `time`, `email_code`, `confirmed`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)');
55
        $query->bindValue(1, $username);
56
        $query->bindValue(2, $level);
57
        $query->bindValue(3, $password);
58
        $query->bindValue(4, $fullname);
59
        $query->bindValue(5, $email);
60
        $query->bindValue(6, $Telp);
61
        $query->bindValue(7, $ip);
62
        $query->bindValue(8, $time);
63
        $query->bindValue(9, $email_code);
64
        $query->bindValue(10, 1);
65
66
        try {
67
            $query->execute();
68
        } catch (PDOException $e) {
69
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
        }
71
    }
72
73
    public function update($id, $username, $password, $email, $fullname, $Telp, $level, $locked)
74
    {
75
        $time = time();
76
        $ip = $_SERVER['REMOTE_ADDR'];
77
        $email_code = sha1($username + microtime());
78
        $password = sha1($password);
79
        $query = $this->db->prepare('UPDATE `users` SET `level` = ? , `password` = ? , `fullname` = ? , `email` = ? , `Telp` = ? ,`ip` = ? , `time` = ? , `email_code` = ? ,`confirmed` = ? WHERE `id` = ?');
80
        $query->bindValue(1, $level);
81
        $query->bindValue(2, $password);
82
        $query->bindValue(3, $fullname);
83
        $query->bindValue(4, $email);
84
        $query->bindValue(5, $Telp);
85
        $query->bindValue(6, $ip);
86
        $query->bindValue(7, $time);
87
        $query->bindValue(8, $email_code);
88
        $query->bindValue(9, $locked);
89
        $query->bindValue(10, $id);
90
91
        try {
92
            $query->execute();
93
        } catch (PDOException $e) {
94
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
95
        }
96
    }
97
98
    public function changepwd($id, $password)
99
    {
100
        $password = sha1($password);
101
        $query = $this->db->prepare('UPDATE `users` SET `password` = ? WHERE `id` = ?');
102
        $query->bindValue(1, $password);
103
        $query->bindValue(2, $id);
104
105
        try {
106
            $query->execute();
107
        } catch (PDOException $e) {
108
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
109
        }
110
    }
111
112
    public function delete($id)
113
    {
114
        $sql = 'DELETE FROM `users` WHERE `id` = ?';
115
        $query = $this->db->prepare($sql);
116
        $query->bindValue(1, $id);
117
118
        try {
119
            $query->execute();
120
        } catch (PDOException $e) {
121
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
122
        }
123
    }
124
125
    public function activate($email, $email_code)
126
    {
127
        $query = $this->db->prepare('SELECT COUNT(`id`) FROM `users` WHERE `email` = ? AND `email_code` = ? AND `confirmed` = ?');
128
        $query->bindValue(1, $email);
129
        $query->bindValue(2, $email_code);
130
        $query->bindValue(3, 0);
131
132
        try {
133
            $query->execute();
134
            $rows = $query->fetchColumn();
135
            if ($rows == 1) {
136
                $query_2 = $this->db->prepare('UPDATE `users` SET `confirmed` = ? WHERE `email` = ?');
137
                $query_2->bindValue(1, 1);
138
                $query_2->bindValue(2, $email);
139
                $query_2->execute();
140
141
                return true;
142
            } else {
143
                return false;
144
            }
145
        } catch (PDOException $e) {
146
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
147
        }
148
    }
149
150
    public function email_confirmed($username)
151
    {
152
        $query = $this->db->prepare('SELECT COUNT(`id`) FROM `users` WHERE `username`= ? AND `confirmed` = ?');
153
        $query->bindValue(1, $username);
154
        $query->bindValue(2, 1);
155
156
        try {
157
            $query->execute();
158
            $rows = $query->fetchColumn();
159
            if ($rows == 1) {
160
                return true;
161
            } else {
162
                return false;
163
            }
164
        } catch (PDOException $e) {
165
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
166
        }
167
    }
168
169
    public function login($username, $password)
170
    {
171
        $query = $this->db->prepare('SELECT `password`, `id` FROM `users` WHERE `username` = ?');
172
        $query->bindValue(1, $username);
173
174
        try {
175
            $query->execute();
176
            $data = $query->fetch();
177
            $stored_password = $data['password'];
178
            $id = $data['id'];
179
            if ($stored_password === sha1($password)) {
180
                return $id;
181
            } else {
182
                return false;
183
            }
184
        } catch (PDOException $e) {
185
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
186
        }
187
    }
188
189
    public function userdata($id)
190
    {
191
        $query = $this->db->prepare('SELECT * FROM `users` WHERE `id`= ?');
192
        $query->bindValue(1, $id);
193
194
        try {
195
            $query->execute();
196
197
            return $query->fetch();
198
        } catch (PDOException $e) {
199
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
200
        }
201
    }
202
203
    public function get_user_by_id($id)
204
    {
205
        $query = $this->db->prepare('SELECT * FROM `users` WHERE `id`= ?');
206
        $query->bindValue(1, $id);
207
208
        try {
209
            $query->execute();
210
211
            return $query->fetch();
212
        } catch (PDOException $e) {
213
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
214
        }
215
    }
216
217
    public function get_user_by_level($level)
218
    {
219
        $query = $this->db->prepare('SELECT * FROM `users` WHERE `level`= ?');
220
        $query->bindValue(1, $level);
221
222
        try {
223
            $query->execute();
224
        } catch (PDOException $e) {
225
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
226
        }
227
228
        return $query->fetchAll();
229
    }
230
231
    public function get_user_random_by_level($level)
232
    {
233
        $query = $this->db->prepare('SELECT * FROM `users` WHERE `level`= ? ORDER BY RAND() LIMIT 1');
234
        $query->bindValue(1, $level);
235
236
        try {
237
            $query->execute();
238
        } catch (PDOException $e) {
239
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
240
        }
241
242
        return $query->fetch();
243
    }
244
245
    public function get_users()
246
    {
247
        $query = $this->db->prepare('SELECT * FROM `users` ORDER BY `time` DESC');
248
249
        try {
250
            $query->execute();
251
        } catch (PDOException $e) {
252
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
253
        }
254
255
        return $query->fetchAll();
256
    }
257
258
    public function log_users($iduser, $log)
259
    {
260
        $time = time();
261
        $ip = $_SERVER['REMOTE_ADDR'];
262
        $browser = $_SERVER['HTTP_USER_AGENT'];
263
        $query = $this->db->prepare('INSERT INTO `log_users` (`iduser`,`time`,`ip`,`browser`,`log`) VALUES (?, ?, ?, ?, ?)');
264
        $query->bindValue(1, $iduser);
265
        $query->bindValue(2, $time);
266
        $query->bindValue(3, $ip);
267
        $query->bindValue(4, $browser);
268
        $query->bindValue(5, $log);
269
270
        try {
271
            $query->execute();
272
        } catch (PDOException $e) {
273
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
274
        }
275
    }
276
277
    public function get_users_log()
278
    {
279
        $query = $this->db->prepare('SELECT * FROM `log_users` ORDER BY `time` DESC');
280
281
        try {
282
            $query->execute();
283
        } catch (PDOException $e) {
284
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
285
        }
286
287
        return $query->fetchAll();
288
    }
289
}
290