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

avatar::mas_include()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\config\config;
17
use phpbb\language\language;
18
use phpbb\db\driver\driver_interface as db_driver;
19
20
/**
21
 * Member Avatar & Status Core Avatar Class.
22
 */
23
class avatar
24
{
25
	/** @var int No Avatar Size */
26
	const NO_AVATAR_SIZE = 1000;
27
28
	/** @var int Avatar Minimum Size */
29
	const AV_MIN_SZ = 9;
30
31
	/** @var int Avatar Default Size Small */
32
	const AV_DEF_SZ_SML = 20;
33
34
	/** @var int Avatar Default Size Big */
35
	const AV_DEF_SZ_BIG = 50;
36
37
	/** @var int Avatar Maximum Size Small */
38
	const AV_MAX_SZ_SML = 99;
39
40
	/** @var int Avatar Maximum Size Big */
41
	const AV_MAX_SZ_BIG = 999;
42
43
	/** @var string[] User Avatar List */
44
	private static $user_avatar = [];
45
46
	/** @var config */
47
	protected $config;
48
49
	/** @var language */
50
	protected $language;
51
52
	/** @var db_driver */
53
	protected $db;
54
55
	/** @var string phpBB root path */
56
	protected $phpbb_root_path;
57
58
	/** @var string phpBB php ext */
59
	protected $php_ext;
60
61
	/**
62
	 * Constructor for Member Avatar & Status Core Avatar Class.
63
	 *
64
	 * @param config		$config				phpBB config
65
	 * @param language		$language			phpBB language
66
	 * @param db_driver		$db					Database object
67
	 * @param string		$phpbb_root_path	phpBB root path
68
	 * @param string		$php_ext			phpBB php ext
69
	 * @access public
70
	 */
71
	public function __construct(config $config, language $language, db_driver $db, $phpbb_root_path, $php_ext)
72
	{
73
		$this->config			= $config;
74
		$this->language			= $language;
75
		$this->db				= $db;
76
		$this->phpbb_root_path	= $phpbb_root_path;
77
		$this->php_ext			= $php_ext;
78
79
		$this->mas_include();
80
	}
81
82
	/**
83
	* MAS Include
84
	*
85
	* @return void
86
	* @access private
87
	*/
88
	private function mas_include()
89
	{
90
		include_once($this->phpbb_root_path . 'includes/functions.' . $this->php_ext);
91
	}
92
93
94
95
	/**
96
	 * MAS Get No Avatar IMG
97
	 *
98
	 * @param void
99
	 * @return string String with No Avatar IMG
100
	 * @access public
101
	 */
102
	public function mas_get_no_avatar_img()
103
	{
104
		$avatar_row = [
105
			'avatar'		=> $this->phpbb_root_path . 'ext/dark1/memberavatarstatus/image/avatar.png',
106
			'avatar_type'	=> AVATAR_REMOTE,
107
			'avatar_width'	=> self::NO_AVATAR_SIZE,
108
			'avatar_height'	=> self::NO_AVATAR_SIZE,
109
		];
110
		return str_replace(
111
			'" />',
112
			'" title="' . $this->language->lang('MAS_NO_AVATAR_TEXT') . '" />',
113
			phpbb_get_user_avatar($avatar_row, $this->language->lang('MAS_NO_AVATAR_TEXT'), true)
114
		);
115
	}
116
117
118
119
	/**
120
	 * MAS Get Config Avatar
121
	 *
122
	 * @param string|bool $config_key takes Config Key String
123
	 * @return bool Bool with Avatar Enable
124
	 * @access public
125
	 */
126
	public function mas_get_config_avatar($config_key = false)
127
	{
128
		// Check if Avatar is Enabled.
129
		return (bool) ($this->config['allow_avatar'] && $this->config['dark1_mas_avatar'] && ($config_key !== false ? $this->config[$config_key] : true));
130
	}
131
132
133
134
	/**
135
	 * MAS Get Avatar Size
136
	 *
137
	 * @param int $av_sz takes Avatar Size
138
	 * @param int $av_default_sz Specifies Default Size in px
139
	 * @param int $av_max_sz Specifies Avatar MAX Size in px
140
	 * @return int Integer with Avatar Size in px
141
	 * @access public
142
	 */
143
	public function mas_get_avatar_size($av_sz, $av_default_sz = self::AV_DEF_SZ_SML, $av_max_sz = self::AV_MAX_SZ_SML)
144
	{
145
		// $av_sz , Need to set this between self::AV_MIN_SZ to $av_max_sz Only , Default is $av_default_sz.
146
		if ($av_sz < self::AV_MIN_SZ || $av_sz > $av_max_sz)
147
		{
148
			$av_sz = $av_default_sz;
149
		}
150
		return (int) $av_sz;
151
	}
152
153
154
155
	/**
156
	 * MAS Get Config Avatar Size
157
	 *
158
	 * @param string $config_key takes Config Key String
159
	 * @param int $av_default_sz Specifies Default Size in px
160
	 * @param int $av_max_sz Specifies Avatar MAX Size in px
161
	 * @return int Integer with Avatar Size in px
162
	 * @access public
163
	 */
164
	public function mas_get_config_avatar_size($config_key, $av_default_sz = self::AV_DEF_SZ_SML, $av_max_sz = self::AV_MAX_SZ_SML)
165
	{
166
		// config -> dark1_mas_XX_sz , Need to get this between self::AV_MIN_SZ to $av_max_sz Only , Default is $av_default_sz.
167
		$av_sz = $this->mas_get_avatar_size((int) $this->config[$config_key], $av_default_sz, $av_max_sz);
168
169
		// Check if correction is required then set it.
170
		if ($av_sz != $this->config[$config_key])
171
		{
172
			$this->config->set($config_key, $av_sz);
173
		}
174
175
		return (int) $this->config[$config_key];
176
	}
177
178
179
180
	/**
181
	 * MAS Get Avatar
182
	 *
183
	 * @param string $config_key takes Config Key String
184
	 * @param int $user_id User ID
185
	 * @return string String with Avatar Data
186
	 * @access public
187
	 */
188
	public function mas_get_avatar($config_key, $user_id)
189
	{
190
		$avatar = '';
191
		$config_key .= '_av';
192
193
		// Check for Config Avatar
194
		if ($this->mas_get_config_avatar($config_key))
195
		{
196
			// Get stored user avatar
197
			$avatar = $this->mas_user_avatar_store($user_id);
198
		}
199
200
		return $avatar;
201
	}
202
203
204
205
	/**
206
	 * MAS User Avatar Store to retrieve old or create new avatar
207
	 *
208
	 * @param int $user_id User ID
209
	 * @return string User Avatar HTML
210
	 * @access private
211
	 */
212
	private function mas_user_avatar_store(int $user_id)
213
	{
214
		// Check if user avatar not stored
215
		if (!isset(self::$user_avatar[$user_id]))
216
		{
217
			$sql = 'SELECT user_avatar, user_avatar_type, user_avatar_width, user_avatar_height' .
218
					' FROM ' . USERS_TABLE .
219
					' WHERE user_id = ' . (int) $user_id;
220
			$result = $this->db->sql_query($sql);
221
			$avatar_row = $this->db->sql_fetchrow($result);
222
			$this->db->sql_freeresult($result);
223
			self::$user_avatar[$user_id] = phpbb_get_user_avatar($avatar_row);
224
		}
225
226
		return self::$user_avatar[$user_id];
227
	}
228
229
}
230