1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package ElkArte Forum |
5
|
|
|
* @copyright ElkArte Forum contributors |
6
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
7
|
|
|
* |
8
|
|
|
* This file contains code covered by: |
9
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
10
|
|
|
* |
11
|
|
|
* @version 2.0 dev |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace ElkArte; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class holds all the data belonging to a certain member. |
19
|
|
|
*/ |
20
|
|
|
class User |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The user object |
24
|
|
|
* |
25
|
|
|
* @var \ElkArte\UserSettings |
26
|
|
|
*/ |
27
|
|
|
protected static $instance = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The user id |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected static $id = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The hashed password read from the cookies |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected static $session_password = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Contains data regarding the user in a form that may be useful in the code |
45
|
|
|
* Basically the former $user_info |
46
|
|
|
* |
47
|
|
|
* @var \ElkArte\ValuesContainer |
48
|
|
|
*/ |
49
|
|
|
public static $info = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Contains the data read from the db. |
53
|
|
|
* Read-only by means of ValuesContainerReadOnly |
54
|
|
|
* @var \ElkArte\ValuesContainerReadOnly |
55
|
|
|
*/ |
56
|
|
|
public static $settings = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Load all the important user information. |
60
|
|
|
* |
61
|
|
|
* @param bool $compat_mode if true sets the deprecated $user_info global |
62
|
|
|
*/ |
63
|
7 |
|
public static function load($compat_mode = false) |
64
|
|
|
{ |
65
|
7 |
|
if (self::$instance === null) |
66
|
|
|
{ |
67
|
1 |
|
$db = database(); |
68
|
1 |
|
$cache = \ElkArte\Cache\Cache::instance(); |
69
|
1 |
|
$req = request(); |
70
|
|
|
|
71
|
1 |
|
self::$instance = new \ElkArte\UserSettingsLoader($db, $cache, $req); |
|
|
|
|
72
|
1 |
|
$already_verified = self::loadFromIntegration(); |
73
|
1 |
|
self::loadFromCookie($req->user_agent()); |
74
|
1 |
|
self::$instance->loadUserById(self::$id, $already_verified, self::$session_password); |
75
|
1 |
|
self::$settings = self::$instance->getSettings(); |
76
|
1 |
|
self::$info = self::$instance->getInfo(); |
77
|
1 |
|
if ($compat_mode === true) |
78
|
|
|
{ |
79
|
1 |
|
global $user_info; |
80
|
1 |
|
$user_info = \ElkArte\User::$info; |
81
|
|
|
} |
82
|
|
|
} |
83
|
7 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Reload all the important user information into the static variables |
87
|
|
|
* based on the \ElkArte\UserSettings object passed to it |
88
|
|
|
* |
89
|
|
|
* @param \ElkArte\UserSettings $user An user |
90
|
|
|
* @param bool $compat_mode if true sets the deprecated $user_info global |
91
|
|
|
*/ |
92
|
2 |
|
public static function reloadByUser(\ElkArte\UserSettingsLoader $user, $compat_mode = false) |
93
|
|
|
{ |
94
|
2 |
|
self::$instance = $user; |
|
|
|
|
95
|
2 |
|
self::$settings = self::$instance->getSettings(); |
96
|
2 |
|
self::$info = self::$instance->getInfo(); |
97
|
2 |
|
if ($compat_mode === true) |
98
|
|
|
{ |
99
|
|
|
global $user_info; |
100
|
|
|
$user_info = \ElkArte\User::$info; |
101
|
|
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
public static function logOutUser($compat_mode = false) |
105
|
|
|
{ |
106
|
|
|
self::$instance->loadUserById(0, true, ''); |
|
|
|
|
107
|
|
|
reloadByUser(self::$instance, $compat_mode); |
|
|
|
|
108
|
|
|
} |
109
|
1 |
|
|
110
|
|
|
/** |
111
|
|
|
* Tests any hook set to integrate_verify_user to set users |
112
|
1 |
|
* according to alternative validations |
113
|
|
|
* @event integrate_verify_user allow for integration to verify a user |
114
|
|
|
*/ |
115
|
|
|
protected static function loadFromIntegration() |
116
|
|
|
{ |
117
|
|
|
// Check first the integration, then the cookie, and last the session. |
118
|
|
|
if (count($integration_ids = \ElkArte\Hooks::instance()->hook('integrate_verify_user')) > 0) |
119
|
|
|
{ |
120
|
|
|
foreach ($integration_ids as $integration_id) |
121
|
|
|
{ |
122
|
|
|
$integration_id = (int) $integration_id; |
123
|
|
|
if ($integration_id > 0) |
124
|
1 |
|
{ |
125
|
|
|
self::$id = $integration_id; |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
1 |
|
|
133
|
|
|
/** |
134
|
1 |
|
* Reads data from the cookie to load the user identity |
135
|
|
|
* @param string $user_agent the Browser user agent, used to do some checkes |
136
|
1 |
|
* based on the session data to reduce spamming and hacking |
137
|
|
|
*/ |
138
|
|
|
protected static function loadFromCookie($user_agent) |
139
|
|
|
{ |
140
|
|
|
global $cookiename, $modSettings; |
141
|
|
|
|
142
|
|
|
if (empty(self::$id) && isset($_COOKIE[$cookiename])) |
143
|
|
|
{ |
144
|
|
|
list ($id, self::$session_password) = serializeToJson($_COOKIE[$cookiename], function ($array_from) use ($cookiename) { |
145
|
|
|
global $modSettings; |
146
|
|
|
|
147
|
1 |
|
require_once(SUBSDIR . '/Auth.subs.php'); |
148
|
|
|
$_COOKIE[$cookiename] = json_encode($array_from); |
149
|
|
|
setLoginCookie(60 * $modSettings['cookieTime'], $array_from[0], $array_from[1]); |
150
|
|
|
}); |
151
|
|
|
self::$id = !empty($id) && strlen(self::$session_password) > 0 ? (int) $id : 0; |
152
|
|
|
} |
153
|
|
|
elseif (empty(self::$id) && isset($_SESSION['login_' . $cookiename]) && (!empty($modSettings['disableCheckUA']) || $_SESSION['USER_AGENT'] == $user_agent)) |
154
|
|
|
{ |
155
|
1 |
|
// @todo Perhaps we can do some more checking on this, such as on the first octet of the IP? |
156
|
|
|
list ($id, self::$session_password, $login_span) = serializeToJson($_SESSION['login_' . $cookiename], function ($array_from) use ($cookiename) { |
157
|
|
|
$_SESSION['login_' . $cookiename] = json_encode($array_from); |
158
|
|
|
}); |
159
|
|
|
self::$id = !empty($id) && strlen(self::$session_password) == 64 && $login_span > time() ? (int) $id : 0; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..