Passed
Branch master (993dd4)
by Dark❶
03:40
created

status::mas_online_sql_query()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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