|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cookie Class |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
7
|
|
|
* @author Omar El Gabry <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
class Cookie{ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @access public |
|
14
|
|
|
* @var string User ID |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $userId = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @access public |
|
20
|
|
|
* @var string Cookie Token |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $token = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @access public |
|
26
|
|
|
* @var string Hashed Token = hash(User ID . ":" . Token . Cookie Secret) |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $hashedCookie = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This is the constructor for Cookie object. |
|
32
|
|
|
* |
|
33
|
|
|
* @access private |
|
34
|
|
|
*/ |
|
35
|
|
|
private function __construct() {} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Getters for $userId |
|
39
|
|
|
* |
|
40
|
|
|
* @access public |
|
41
|
|
|
* @static static method |
|
42
|
|
|
* @return string User ID |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getUserId(){ |
|
45
|
|
|
return (int)self::$userId; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Extract and validate cookie |
|
50
|
|
|
* |
|
51
|
|
|
* @access public |
|
52
|
|
|
* @static static method |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function isCookieValid(){ |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
// "auth" or "remember me" cookie |
|
|
|
|
|
|
58
|
|
|
if(empty($_COOKIE['auth'])) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// check the count before using explode |
|
63
|
|
|
$cookie_auth = explode(':', $_COOKIE['auth']); |
|
64
|
|
|
if(count ($cookie_auth) !== 3){ |
|
65
|
|
|
self::remove(); |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
list ($encryptedUserId, self::$token, self::$hashedCookie) = $cookie_auth; |
|
70
|
|
|
|
|
71
|
|
|
// Remember? $hashedCookie was generated from the original user Id, NOT from the encrypted one. |
|
72
|
|
|
self::$userId = Encryption::decrypt($encryptedUserId); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if (self::$hashedCookie === hash('sha256', self::$userId . ':' . self::$token . Config::get('COOKIE_SECRET_KEY')) && !empty(self::$token) && !empty(self::$userId)) { |
|
75
|
|
|
|
|
76
|
|
|
$database = Database::openConnection(); |
|
77
|
|
|
$query = "SELECT id, cookie_token FROM users WHERE id = :id AND cookie_token = :cookie_token LIMIT 1"; |
|
78
|
|
|
$database->prepare($query); |
|
79
|
|
|
$database->bindValue(':id', self::$userId); |
|
80
|
|
|
$database->bindValue(':cookie_token', self::$token); |
|
81
|
|
|
$database->execute(); |
|
82
|
|
|
|
|
83
|
|
|
$isValid = $database->countRows() === 1? true: false; |
|
84
|
|
|
|
|
85
|
|
|
}else{ |
|
86
|
|
|
|
|
87
|
|
|
$isValid = false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if(!$isValid){ |
|
91
|
|
|
|
|
92
|
|
|
Logger::log("COOKIE", self::$userId . " is trying to login using invalid cookie: " . self::$token, __FILE__, __LINE__); |
|
93
|
|
|
self::remove(self::$userId); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $isValid; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Remove cookie from the database of a user(if exists), |
|
101
|
|
|
* and also from the browser. |
|
102
|
|
|
* |
|
103
|
|
|
* @static static method |
|
104
|
|
|
* @param string $userId |
|
105
|
|
|
* |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function remove($userId = null){ |
|
108
|
|
|
|
|
109
|
|
|
if(!empty($userId)){ |
|
110
|
|
|
|
|
111
|
|
|
$database = Database::openConnection(); |
|
112
|
|
|
$query = "UPDATE users SET cookie_token = NULL WHERE id = :id"; |
|
113
|
|
|
$database->prepare($query); |
|
114
|
|
|
$database->bindValue(":id", $userId); |
|
115
|
|
|
$result = $database->execute(); |
|
116
|
|
|
|
|
117
|
|
|
if(!$result) { |
|
118
|
|
|
Logger::log("COOKIE", "Couldn't remove cookie from the database for user ID: " . $userId, __FILE__, __LINE__); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
self::$userId = self::$token = self::$hashedCookie = null; |
|
123
|
|
|
|
|
124
|
|
|
// How to kill/delete a cookie in a browser? |
|
125
|
|
|
setcookie('auth', false, time() - (3600 * 3650), Config::get('COOKIE_PATH'), Config::get('COOKIE_DOMAIN'), Config::get('COOKIE_SECURE'), Config::get('COOKIE_HTTP')); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Reset Cookie, |
|
130
|
|
|
* resetting is done by updating the database, |
|
131
|
|
|
* and resetting the "auth" cookie in the browser |
|
132
|
|
|
* |
|
133
|
|
|
* @static static method |
|
134
|
|
|
* @param string $userId |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function reset($userId){ |
|
137
|
|
|
|
|
138
|
|
|
self::$userId = $userId; |
|
139
|
|
|
self::$token = hash('sha256', mt_rand()); |
|
140
|
|
|
$database = Database::openConnection(); |
|
141
|
|
|
|
|
142
|
|
|
$query = "UPDATE users SET cookie_token = :cookie_token WHERE id = :id"; |
|
143
|
|
|
$database->prepare($query); |
|
144
|
|
|
|
|
145
|
|
|
// generate random hash for cookie token (64 char string) |
|
146
|
|
|
$database->bindValue(":cookie_token", self::$token); |
|
147
|
|
|
$database->bindValue(":id", self::$userId); |
|
148
|
|
|
$result = $database->execute(); |
|
149
|
|
|
|
|
150
|
|
|
if(!$result) { |
|
151
|
|
|
Logger::log("COOKIE", "Couldn't remove cookie from the database for user ID: " . $userId, __FILE__, __LINE__); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// generate cookie string(remember me) |
|
155
|
|
|
// Don't expose the original user id in the cookie, Encrypt It! |
|
156
|
|
|
$cookieFirstPart = Encryption::encrypt(self::$userId) . ':' . self::$token ; |
|
157
|
|
|
|
|
158
|
|
|
// $hashedCookie generated from the original user Id, NOT from the encrypted one. |
|
159
|
|
|
self::$hashedCookie = hash('sha256', self::$userId . ':' . self::$token . Config::get('COOKIE_SECRET_KEY')); |
|
160
|
|
|
$authCookie = $cookieFirstPart . ':' . self::$hashedCookie; |
|
161
|
|
|
|
|
162
|
|
|
setcookie('auth', $authCookie, time() + Config::get('COOKIE_EXPIRY'), Config::get('COOKIE_PATH'), Config::get('COOKIE_DOMAIN'), Config::get('COOKIE_SECURE'), Config::get('COOKIE_HTTP')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.