Passed
Branch development (176841)
by Elk
15:26
created

User::logOutUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new ElkArte\UserSettingsLoader($db, $cache, $req) of type ElkArte\UserSettingsLoader is incompatible with the declared type ElkArte\UserSettings of property $instance.

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..

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type ElkArte\UserSettingsLoader is incompatible with the declared type ElkArte\UserSettings of property $instance.

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..

Loading history...
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, '');
0 ignored issues
show
Bug introduced by
The method loadUserById() does not exist on ElkArte\UserSettings. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
		self::$instance->/** @scrutinizer ignore-call */ 
107
                   loadUserById(0, true, '');
Loading history...
107
		reloadByUser(self::$instance, $compat_mode);
0 ignored issues
show
Bug introduced by
The function reloadByUser was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
		/** @scrutinizer ignore-call */ 
108
  reloadByUser(self::$instance, $compat_mode);
Loading history...
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