|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Authenticates a user |
|
4
|
|
|
* |
|
5
|
|
|
* @package Intraface |
|
6
|
|
|
* @author Lars Olesen <[email protected]> |
|
7
|
|
|
* @since 0.1.0 |
|
8
|
|
|
* @version @package-version@ |
|
9
|
|
|
*/ |
|
10
|
|
|
class Intraface_Auth_User |
|
11
|
|
|
{ |
|
12
|
|
|
private $db; |
|
13
|
|
|
private $email; |
|
14
|
|
|
private $password; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param object $db Databaseobject |
|
20
|
|
|
* @param string $email Username |
|
21
|
|
|
* @param string $password Password |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
3 |
|
function __construct(MDB2_Driver_Common $db, $session_id, $email = null, $password = null) |
|
26
|
|
|
{ |
|
27
|
3 |
|
$this->db = $db; |
|
28
|
3 |
|
$this->email = $email; |
|
29
|
3 |
|
$this->password = $password; |
|
30
|
3 |
|
$this->session_id = $session_id; |
|
|
|
|
|
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Auth |
|
35
|
|
|
* |
|
36
|
|
|
* @return object |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function auth() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
1 |
|
$result = $this->db->query("SELECT id FROM user WHERE email = ".$this->db->quote($this->email, 'text')." AND password = ".$this->db->quote(md5($this->password), 'text')); |
|
41
|
|
|
|
|
42
|
1 |
|
if (PEAR::isError($result)) { |
|
43
|
|
|
throw new Exception('result is an error' . $result->getMessage() . $result->getUserInfo()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if ($result->numRows() != 1) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
1 |
|
$row = $result->fetchRow(MDB2_FETCHMODE_ASSOC); |
|
50
|
|
|
|
|
51
|
1 |
|
$result = $this->db->exec("UPDATE user SET lastlogin = NOW(), session_id = ".$this->db->quote($this->session_id, 'text')." WHERE id = ". $this->db->quote($row['id'], 'integer')); |
|
52
|
1 |
|
if (PEAR::isError($result)) { |
|
53
|
|
|
throw new Exception('could not update user ' . $result->getMessage() . $result->getUserInfo()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$user = new Intraface_User($row['id']); |
|
57
|
1 |
|
if (!is_object($user) || $user->get('id') != $row['id']) { |
|
58
|
|
|
throw new Exception('Unable to return a valid user object on login'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$_SESSION['intraface_logged_in_user_id'] = $user->getId(); |
|
62
|
|
|
|
|
63
|
1 |
|
return $user; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
function isLoggedIn() |
|
67
|
|
|
{ |
|
68
|
1 |
|
$result = $this->db->query("SELECT id FROM user WHERE session_id = ".$this->db->quote($this->session_id, 'text')); |
|
69
|
1 |
|
if (PEAR::isError($result)) { |
|
70
|
|
|
throw new Exception('could not check if user is logged in ' . $result->getUserInfo()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
if ($result->numRows() == 0) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$row = $result->fetchRow(MDB2_FETCHMODE_ASSOC); |
|
78
|
1 |
|
return new Intraface_User($row['id']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* logout() |
|
83
|
|
|
* |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
2 |
View Code Duplication |
public function logout() |
|
|
|
|
|
|
87
|
2 |
|
{ |
|
88
|
|
|
$result = $this->db->exec("UPDATE user SET session_id = " . $this->db->quote('', 'text') . " WHERE session_id = " . $this->db->quote($this->session_id, 'text')); |
|
89
|
|
|
|
|
90
|
|
|
if (PEAR::isError($result)) { |
|
91
|
|
|
throw new Exception('could not log user out ' . $result->getUserInfo()); |
|
92
|
|
|
} |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns an identification string on the user |
|
98
|
|
|
* |
|
99
|
|
|
* @return string identification (email) |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getIdentification() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->email; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.