|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The PHP Skeleton App |
|
4
|
|
|
* |
|
5
|
|
|
* @author Goran Halusa <[email protected]> |
|
6
|
|
|
* @copyright 2015 Goran Halusa |
|
7
|
|
|
* @link https://github.com/ghalusa/PHP-Skeleton-App |
|
8
|
|
|
* @license https://github.com/ghalusa/PHP-Skeleton-App/wiki/License |
|
9
|
|
|
* @version 0.1.1 |
|
10
|
|
|
* @package PHP Skeleton App |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace PHPSkeleton; |
|
17
|
|
|
use PDO; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Authenticate |
|
21
|
|
|
* |
|
22
|
|
|
* Class for the Authenticate module, providing methods for authentication. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Goran Halusa <[email protected]> |
|
25
|
|
|
* @since 0.1.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class Authenticate |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string|bool $session_key The session key |
|
32
|
|
|
*/ |
|
33
|
|
|
private $session_key = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var object $db The database connection object |
|
37
|
|
|
*/ |
|
38
|
|
|
public $db; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* @param object $db_connection The database connection object |
|
43
|
|
|
* @param string $session_key The session key |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function __construct($db_connection=false, $session_key=false) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($db_connection && is_object($db_connection)) { |
|
48
|
|
|
$this->db = $db_connection; |
|
49
|
|
|
} |
|
50
|
|
|
$this->session_key = $session_key; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Generate Hash |
|
55
|
|
|
* |
|
56
|
|
|
* Hash a password using BCrypt as the hashing technique. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $password The data value |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function generate_hashed_password($password) |
|
62
|
|
|
{ |
|
63
|
|
|
if (defined("CRYPT_BLOWFISH") && CRYPT_BLOWFISH) { |
|
64
|
|
|
$salt = '$2y$11$' . substr(md5(uniqid(rand(), true)), 0, 22); |
|
65
|
|
|
return crypt($password, $salt); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Verify Hashed Password |
|
71
|
|
|
* |
|
72
|
|
|
* Verify a hashed password. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $password The data value |
|
75
|
|
|
* @param string $hashedPassword The data value |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
private function verify_hashed_password($password, $hashedPassword) |
|
79
|
|
|
{ |
|
80
|
|
|
return crypt($password, $hashedPassword) == $hashedPassword; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Authenticate Local |
|
85
|
|
|
* |
|
86
|
|
|
* Run a query to find an active local user account. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $username The data value |
|
89
|
|
|
* @param string $password The data value |
|
90
|
|
|
* @return array|bool The query result |
|
91
|
|
|
*/ |
|
92
|
|
|
public function authenticate_local($username, $password) |
|
93
|
|
|
{ |
|
94
|
|
|
$result = false; |
|
95
|
|
|
if ($username && $password) { |
|
96
|
|
|
$statement = $this->db->prepare(" |
|
97
|
|
|
SELECT |
|
98
|
|
|
user_account_id |
|
99
|
|
|
,user_account_email |
|
100
|
|
|
,user_account_password |
|
101
|
|
|
,first_name |
|
102
|
|
|
,last_name |
|
103
|
|
|
FROM user_account |
|
104
|
|
|
WHERE user_account_email = :user_account_email |
|
105
|
|
|
AND active = 1 |
|
106
|
|
|
"); |
|
107
|
|
|
$statement->bindValue(":user_account_email", $username, PDO::PARAM_STR); |
|
108
|
|
|
$statement->execute(); |
|
109
|
|
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
|
110
|
|
|
|
|
111
|
|
|
$result = $this->verify_hashed_password($password, $data["user_account_password"]) ? $data : false; |
|
112
|
|
|
unset($result["user_account_password"]); |
|
113
|
|
|
} |
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Log Login Attempt |
|
119
|
|
|
* |
|
120
|
|
|
* Run a query to insert a login attempt. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $user_account_email The data value |
|
123
|
|
|
* @param string $result The data value |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function log_login_attempt($user_account_email, $result) |
|
127
|
|
|
{ |
|
128
|
|
|
$statement = $this->db->prepare(" |
|
129
|
|
|
INSERT INTO login_attempt |
|
130
|
|
|
(user_account_email |
|
131
|
|
|
,ip_address |
|
132
|
|
|
,result |
|
133
|
|
|
,page |
|
134
|
|
|
,created_date) |
|
135
|
|
|
VALUES |
|
136
|
|
|
(:user_account_email |
|
137
|
|
|
,:ip_address |
|
138
|
|
|
,:result |
|
139
|
|
|
,:page |
|
140
|
|
|
,NOW()) |
|
141
|
|
|
"); |
|
142
|
|
|
$statement->bindValue(":user_account_email", $user_account_email, PDO::PARAM_STR); |
|
143
|
|
|
$statement->bindValue(":ip_address", $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR); |
|
144
|
|
|
$statement->bindValue(":result", $result, PDO::PARAM_STR); |
|
145
|
|
|
$statement->bindValue(":page", $_SERVER['REQUEST_URI'], PDO::PARAM_STR); |
|
146
|
|
|
$statement->execute(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|