-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
69 lines (57 loc) · 1.41 KB
/
user.php
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
<?php
include 'database.php';
session_start();
class User {
private $id;
private $nick;
private $passwordHash;
private $loggedIn = false;
public function login($nick, $password) {
if($loggedIn) {
return;
}
$userData = Database::getUserByNick($nick);
if($userData === NULL) {
return;
}
$this->id = $userData->id();
$this->nick = $userData->nick();
$this->passwordHash = $userData->passwordHash();
$_SESSION['user'] = $this;
}
public function register($nick, $password) {
if($loggedIn) {
return;
}
$userData = Database::getUserByNick($nick);
if($userData !== NULL) {
return;
}
$this->nick = $nick;
$this->passwordHash = password_hash($password, PASSWORD_DEFAULT);
$this->id = Database::addUser($this);
User::login($nick, $password);
}
public function isLoggedIn() {
return $this->loggedIn;
}
public function id() {
return $this->id;
}
public function nick() {
return $this->nick;
}
public function passwordHash() {
return $this->passwordHash;
}
public function pickle() {
return strval($this->id).", ".strval($this->nick).", ".strval($this->passwordHash);
}
public static function unpickle($packedUser) {
$this->arrayedUser = str_getcsv($packedUser);
$this->id = $arrayedUser[0];
$this->nick = $arrayedUser[1];
$this->passwordHash = $arrayedUser[2];
}
}
?>