Passed
Pull Request — master (#24)
by Dark❶
02:46
created

status::mas_get_user_online()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Member Avatar & Status [MAS]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2018-forever, Dark❶ [dark1]
7
 * @license GNU General Public License, version 2 (GPL-2.0-only)
8
 *
9
 */
10
11
namespace dark1\memberavatarstatus\core;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\auth\auth;
17
use phpbb\config\config;
18
use phpbb\db\driver\driver_interface as db_driver;
19
20
/**
21
 * Member Avatar & Status Core Status Class.
22
 */
23
class status
24
{
25
	/** @var int Color Default Offline */
26
	const COL_DEF_OFF = '000000';
27
28
	/** @var int Color Default Online */
29
	const COL_DEF_ON = '00FF00';
30
31
	/** @var bool[] User Online List */
32
	private static $user_online = [];
33
34
	/** @var auth */
35
	protected $auth;
36
37
	/** @var config */
38
	protected $config;
39
40
	/** @var db_driver */
41
	protected $db;
42
43
	/**
44
	 * Constructor for Member Avatar & Status Core Status Class.
45
	 *
46
	 * @param auth			$auth		phpBB auth
47
	 * @param config		$config		phpBB config
48
	 * @param db_driver		$db			Database object
49
	 * @access public
50
	 */
51
	public function __construct(auth $auth, config $config, db_driver $db)
52
	{
53
		$this->auth		= $auth;
54
		$this->config	= $config;
55
		$this->db		= $db;
56
	}
57
58
59
60
	/**
61
	 * MAS Get Config Online
62
	 *
63
	 * @param string|bool $config_key takes Config Key String
64
	 * @return bool Bool with Online Enable
65
	 * @access public
66
	 */
67
	public function mas_get_config_online($config_key = false)
68
	{
69
		// Check if Online is Enabled.
70
		return (bool) ($this->config['load_onlinetrack'] && $this->config['dark1_mas_online'] && ($config_key !== false ? $this->config[$config_key] : true));
71
	}
72
73
74
75
	/**
76
	 * MAS Get Config Online/Offline Color
77
	 *
78
	 * @param string $key takes `on/off` String
79
	 * @param string $color takes Hex Color String
80
	 * @return string String with Hex Color
81
	 * @access public
82
	 */
83
	public function mas_config_color($key, $color)
84
	{
85
		// Check if Color is in Hexadecimal else Default.
86
		if (!preg_match('/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/', $color))
87
		{
88
			$color = (strtoupper($key) === 'ON') ? self::COL_DEF_ON : self::COL_DEF_OFF ;
89
		}
90
91
		return $color;
92
	}
93
94
95
96
	/**
97
	 * MAS Get Config Online/Offline Color
98
	 *
99
	 * @param string $key takes `on/off` String
100
	 * @return string String with Hex Color
101
	 * @access public
102
	 */
103
	public function mas_get_config_color($key)
104
	{
105
		// config -> dark1_mas_col_XX , Need to get this in Hexadecimal Only else Default.
106
		$color = $this->mas_config_color($key, $this->config['dark1_mas_col_' . $key]);
107
108
		// Check if correction is required then set it.
109
		if ($color != $this->config['dark1_mas_col_' . $key])
110
		{
111
			$this->config->set('dark1_mas_col_' . $key, $color);
112
		}
113
114
		return $this->config['dark1_mas_col_' . $key];
115
	}
116
117
118
119
	/**
120
	 * MAS Get User Online
121
	 *
122
	 * @param array $online_row takes user details to find Online Status
123
	 * @return bool Online Status
124
	 * @access private
125
	 */
126
	private function mas_get_user_online($online_row)
127
	{
128
		return (bool) (
129
			(time() - ($this->config['load_online_time'] * 60) < $online_row['session_time']) &&
130
			((isset($online_row['session_viewonline']) && $online_row['session_viewonline']) || $this->auth->acl_get('u_viewonline'))
131
		);
132
	}
133
134
135
136
	/**
137
	 * MAS Get Online
138
	 *
139
	 * @param string $config_key takes Config Key String
140
	 * @param int $user_id User ID
141
	 * @return string Online Data
142
	 * @access public
143
	 */
144
	public function mas_get_online($config_key, $user_id)
145
	{
146
		$online = '';
147
		$config_key .= '_ol';
148
149
		// Check for Config Online
150
		if ($this->mas_get_config_online($config_key))
151
		{
152
			// Get stored user online status
153
			$online = $this->mas_user_online_store($user_id);
154
		}
155
156
		return $online;
157
	}
158
159
160
161
	/**
162
	 * MAS User Online Store to retrieve old or create new online status
163
	 *
164
	 * @param int $user_id User ID
165
	 * @return bool User Online Status
166
	 * @access private
167
	 */
168
	private function mas_user_online_store(int $user_id)
169
	{
170
		// Check if user online status not stored
171
		if (!isset(self::$user_online[$user_id]))
172
		{
173
			$sql = 'SELECT MAX(session_time) as session_time, MIN(session_viewonline) as session_viewonline' .
174
					' FROM ' . SESSIONS_TABLE .
175
					' WHERE session_time >= ' . (time() - ($this->config['load_online_time'] * 60)) . ' AND session_user_id <> ' . ANONYMOUS . ' AND session_user_id = ' . (int) $user_id;
176
			$result = $this->db->sql_query($sql);
177
			$online_row = $this->db->sql_fetchrow($result);
178
			$this->db->sql_freeresult($result);
179
			self::$user_online[$user_id] = $this->mas_get_user_online($online_row);
180
		}
181
182
		return self::$user_online[$user_id];
183
	}
184
185
}
186