-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
346 lines (313 loc) · 9.09 KB
/
User.php
File metadata and controls
346 lines (313 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Devbr\User
* PHP version 7
*
* @category Database
* @package Util
* @author Bill Rocha <prbr@ymail.com>
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
* @license <https://opensource.org/licenses/MIT> MIT
* @version GIT: 0.0.1
* @link http://dbrasil.tk/devbr
*/
namespace Devbr;
use Devbr\Database;
/**
* User Class
*
* @category Database
* @package Util
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link http://dbrasil.tk/devbr
*/
class User
{
public static $node = null;
private $_login = false;
private $_data = ['id'=>null,
'name'=>null,
'token'=>null,
'life'=>null,
'login'=>null,
'password'=>null,
'level'=>null,
'status'=>null];
private $_db = null;
private $_dbConfig = ['table'=>'user',
'id'=>'id',
'name'=>'name',
'token'=>'token',
'life'=>'life',
'login'=>'login',
'password'=>'password',
'level'=>'level',
'status'=>'status'];
/**
* Constructor
*
* @param array|null $config Configuration data (or null)
*/
public function __construct($config = null)
{
if ($config !== null) {
foreach ($config as $i => $d) {
if (isset($this->_dbConfig[$i])) {
$this->_dbConfig[$i] = $d;
}
}
} elseif (method_exists('\Config\Devbr\Database', 'getUserConfig')) {
$this->_dbConfig = \Config\Devbr\Database::getUserConfig();
}
$this->_db = new Database;
}
/**
* Singleton instance
*
* @return object this
*/
public static function this()
{
if (is_object(static::$node)) {
return static::$node;
}
//else...
list($config) = array_merge(func_get_args(), [null]);
return static::$node = new static($config);
}
/**
* Initialize user
*
* @param string $login user login
* @param string $password user password
*
* @return bool True/false to login
*/
public function login($login, $password)
{
$this->_db->query(
'SELECT * FROM '.$this->_dbConfig['table']
.' WHERE '.$this->_dbConfig['login'].' = :lg
AND '.$this->_dbConfig['password'].' = :pw',
[':lg'=>$login, ':pw'=>$password]
);
$row = $this->_db->result();
if (isset($row[0])) {
$row = $row[0]->getAll();
foreach ($this->_data as $i => $d) {
if (isset($row[$this->_dbConfig[$i]])) {
$this->_data[$i] = $row[$this->_dbConfig[$i]];
}
}
$this->_login = true;
return true;
}
$this->_login = false;
return false;
}
/**
* Performer LOGOUT
*
* @param integer $id the ID from user
*
* @return array Array of data from user login
*/
public function logout($id = null)
{
if ($id !== null) {
$this->_data['id'] = $id;
}
//Reset
$this->_data['life'] = 0;
$this->_data['token'] = md5(microtime());
$this->_db->query(
'UPDATE '.$this->_dbConfig['table']
.' SET '.$this->_dbConfig['token'].'="'
.$this->_data['token'].'", '
.$this->_dbConfig['life'].' = "'
.$this->_data['life'].'" WHERE '
.$this->_dbConfig['id'].' = '
.$this->_data['id']
);
}
/**
* Initialize user by ID
*
* @param integer $id the ID
*
* @return bool|string false or $this->_data by ID
*/
public function getById($id)
{
$this->_db->query(
'SELECT * FROM '.$this->_dbConfig['table']
.' WHERE '.$this->_dbConfig['id'].' = :id',
[':id'=>$id]
);
$row = $this->_db->result();
if (isset($row[0])) {
$row = $row[0]->getAll();
$this->_login = true; //Setando LOGIN como válido/logado
foreach ($this->_data as $i => $d) {
if (isset($row[$this->_dbConfig[$i]])) {
$this->_data[$i] = $row[$this->_dbConfig[$i]];
}
}
return $this->_data;
}
return false;
}
/**
* Get User by Login
*
* @param string $login login name
*
* @return object|bool User data or false
*/
public function getByLogin($login)
{
$this->_db->query(
'SELECT * FROM '.$this->_dbConfig['table']
.' WHERE '.$this->_dbConfig['login'].' = :lg',
[':lg'=>$login]
);
$row = $this->_db->result();
if (isset($row[0])) {
$row = $row[0]->getAll();
$this->_login = true; //Setando LOGIN como válido/logado
foreach ($this->_data as $i => $d) {
if (isset($row[$this->_dbConfig[$i]])) {
$this->_data[$i] = $row[$this->_dbConfig[$i]];
}
}
return $this->_data;
}
return false;
}
/**
* Set TOKEN data key
*
* @param string $token The TOKEN string
*
* @return bool False/true for ok
*/
public function saveToken($token)
{
$rows = $this->_db->query(
'UPDATE '.$this->_dbConfig['table']
.' SET '.$this->_dbConfig['token'].' = :tk'
.' WHERE '.$this->_dbConfig['id'].' = :id',
[':tk'=>$token, ':id'=>$this->_data['id']]
);
if ($rows > 0) {
$this->_data['token'] = $token;
return true;
}
return false;
}
/**
* Get Token by id
*
* @param integer $id The user ID (integer)
*
* @return bool|string return TOKEN or false
*/
public function getToken($id)
{
$row = $this->_db->query(
'SELECT token FROM '.$this->_dbConfig['table']
.' WHERE '.$this->_dbConfig['id'].' = :id',
[':id'=>$id]
);
if (isset($row[0])) {
$row = $row[0]->getAll();
return $row[$this->_dbConfig['token']];
}
return false;
}
/**
* Is loged?!
*
* @return bool true/false for LOGIN
*/
public function loged()
{
return $this->_login;
}
/**
* Universal GET
*
* @param string $node null return ALL data (array)
*
* @return array return $this->_data
*/
public function get($node = null)
{
if ($node === null) {
return $this->_data;
}
return (isset($this->_data[$node])) ? $this->_data[$node] : false;
}
/**
* Universal SET
*
* @param array|string $node Se for um array, grava todos os dados
* Se for um string e existir, grava $value
* Se for um string e existir, grava $value
* Se for um string e existir, grava $value
* @param string|integer $value [optional] valor a ser gravado
*
* @return object (this)
*/
public function set($node, $value = null)
{
if (!is_array($node)
&& isset($this->_data[$node])) {
$this->_data[$node] = $value;
}
foreach ($node as $i => $d) {
if (isset($this->_data[$i])) {
$this->_data[$i] = $d;
}
}
return $this;
}
/**
* Save this USER on DataBase
*
* @param Integer $id [optional] set a ID from this user
* $id = "null" (or none) gera INSERT new user
*
* @return array INSERT/UPDATE & rows (0 => indica não salvo)
*/
public function save($id = null)
{
//update this user id value
if ($id !== null) {
$this->_data['id'] = $id;
}
if ($this->_data['id'] !== null) {
$action = 'UPDATE ';
$where = ' WHERE '.$this->_dbConfig['id'].' = :id';
} else {
$action = 'INSERT INTO ';
$where = '';
}
$cols = '';
$vals = [];
foreach ($this->_data as $k => $v) {
if ($k !== 'id') {
$cols .= $this->_dbConfig[$k].' = :'.$k.',';
}
$vals[':'.$k] = $v;
}
$cols = substr($cols, 0, -1); //tirando a ultima vírgula
$this->_db->query(
$action.$this->_dbConfig['table']
.' SET '.$cols.$where, $vals
);
return ['action'=>substr($action, 0, 5),
'rows'=>$this->_db->getRows()];
}
}