Passed
Branch dev-v3.2.0 (ab60cf)
by Dark❶
02:55
created

mas_status::mas_get_online()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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