aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/avatar/manager.php
blob: f2bb1a5dbee231c5cfee939be65469b842d0e7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

namespace phpbb\avatar;

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package avatar
*/
class manager
{
	/**
	* phpBB configuration
	* @var \phpbb\config\config
	*/
	protected $config;

	/**
	* Array that contains a list of enabled drivers
	* @var array
	*/
	static protected $enabled_drivers = false;

	/**
	* Array that contains all available avatar drivers which are passed via the
	* service container
	* @var array
	*/
	protected $avatar_drivers;

	/**
	* Service container object
	* @var object
	*/
	protected $container;

	/**
	* Default avatar data row
	* @var array
	*/
	static protected $default_row = array(
		'avatar'		=> '',
		'avatar_type'	=> '',
		'avatar_width'	=> '',
		'avatar_height'	=> '',
	);

	/**
	* Construct an avatar manager object
	*
	* @param \phpbb\config\config $config phpBB configuration
	* @param array $avatar_drivers Avatar drivers passed via the service container
	* @param object $container Container object
	*/
	public function __construct(\phpbb\config\config $config, $avatar_drivers, $container)
	{
		$this->config = $config;
		$this->avatar_drivers = $avatar_drivers;
		$this->container = $container;
	}

	/**
	* Get the driver object specified by the avatar type
	*
	* @param string $avatar_type Avatar type; by default an avatar's service container name
	* @param bool $load_enabled Load only enabled avatars
	*
	* @return object Avatar driver object
	*/
	public function get_driver($avatar_type, $load_enabled = true)
	{
		if (self::$enabled_drivers === false)
		{
			$this->load_enabled_drivers();
		}

		$avatar_drivers = ($load_enabled) ? self::$enabled_drivers : $this->get_all_drivers();

		// Legacy stuff...
		switch ($avatar_type)
		{
			case AVATAR_GALLERY:
				$avatar_type = 'avatar.driver.local';
			break;
			case AVATAR_UPLOAD:
				$avatar_type = 'avatar.driver.upload';
			break;
			case AVATAR_REMOTE:
				$avatar_type = 'avatar.driver.remote';
			break;
		}

		if (!isset($avatar_drivers[$avatar_type]))
		{
			return null;
		}

		/*
		* There is no need to handle invalid avatar types as the following code
		* will cause a ServiceNotFoundException if the type does not exist
		*/
		$driver = $this->container->get($avatar_type);

		return $driver;
	}

	/**
	* Load the list of enabled drivers
	* This is executed once and fills self::$enabled_drivers
	*/
	protected function load_enabled_drivers()
	{
		if (!empty($this->avatar_drivers))
		{
			self::$enabled_drivers = array();
			foreach ($this->avatar_drivers as $driver)
			{
				if ($this->is_enabled($driver))
				{
					self::$enabled_drivers[$driver->get_name()] = $driver->get_name();
				}
			}
			asort(self::$enabled_drivers);
		}
	}

	/**
	* Get a list of all avatar drivers
	*
	* As this function will only be called in the ACP avatar settings page, it
	* doesn't make much sense to cache the list of all avatar drivers like the
	* list of the enabled drivers.
	*
	* @return array Array containing a list of all avatar drivers
	*/
	public function get_all_drivers()
	{
		$drivers = array();

		if (!empty($this->avatar_drivers))
		{
			foreach ($this->avatar_drivers as $driver)
			{
				$drivers[$driver->get_name()] = $driver->get_name();
			}
			asort($drivers);
		}

		return $drivers;
	}

	/**
	* Get a list of enabled avatar drivers
	*
	* @return array Array containing a list of the enabled avatar drivers
	*/
	public function get_enabled_drivers()
	{
		if (self::$enabled_drivers === false)
		{
			$this->load_enabled_drivers();
		}

		return self::$enabled_drivers;
	}

	/**
	* Strip out user_, group_, or other prefixes from array keys
	*
	* @param array	$row User data or group data
	* @param string $prefix Prefix of data keys
	*
	* @return array User data or group data with keys that have been
	*        stripped from the preceding "user_" or "group_"
	*/
	static public function clean_row($row, $prefix = '')
	{
		// Upon creation of a user/group $row might be empty
		if (empty($row))
		{
			return self::$default_row;
		}

		$keys = array_keys($row);
		$values = array_values($row);

		array_walk($keys, array('\phpbb\avatar\manager', 'strip_prefix'), $prefix);

		return array_combine($keys, $values);
	}

	/**
	* Strip prepending user_ or group_ prefix from key
	*
	* @param string Array key
	* @return void
	*/
	static protected function strip_prefix(&$key, $null, $prefix)
	{
		$regex = ($prefix !== '') ? "#^(?:{$prefix}_)#" : '#^(?:user_|group_)#';
		$key = preg_replace($regex, '', $key);
	}

	/**
	* Clean driver names that are returned from template files
	* Underscores are replaced with dots
	*
	* @param string $name Driver name
	*
	* @return string Cleaned driver name
	*/
	static public function clean_driver_name($name)
	{
		return str_replace(array('\\', '_'), '.', $name);
	}

	/**
	* Prepare driver names for use in template files
	* Dots are replaced with underscores
	*
	* @param string $name Clean driver name
	*
	* @return string Prepared driver name
	*/
	static public function prepare_driver_name($name)
	{
		return str_replace('.', '_', $name);
	}

	/**
	* Check if avatar is enabled
	*
	* @param object $driver Avatar driver object
	*
	* @return bool True if avatar is enabled, false if it's disabled
	*/
	public function is_enabled($driver)
	{
		$config_name = $this->get_driver_config_name($driver);

		return $this->config["allow_avatar_{$config_name}"];
	}

	/**
	* Get the settings array for enabling/disabling an avatar driver
	*
	* @param object $driver Avatar driver object
	*
	* @return array Array of configuration options as consumed by acp_board
	*/
	public function get_avatar_settings($driver)
	{
		$config_name = $this->get_driver_config_name($driver);

		return array(
			'allow_avatar_' . $config_name	=> array('lang' => 'ALLOW_' . strtoupper(str_replace('\\', '_', $config_name)),		'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => false),
		);
	}

	/**
	* Get the config name of an avatar driver
	*
	* @param object $driver Avatar driver object
	*
	* @return string Avatar driver config name
	*/
	public function get_driver_config_name($driver)
	{
		return preg_replace('#^phpbb\\\\avatar\\\\driver\\\\#', '', get_class($driver));
	}

	/**
	* Replace "error" strings with their real, localized form
	*
	* @param \phpbb\user phpBB User object
	* @param array	$error Array containing error strings
	*        Key values can either be a string with a language key or an array
	*        that will be passed to vsprintf() with the language key in the
	*        first array key.
	*
	* @return array Array containing the localized error strings
	*/
	public function localize_errors(\phpbb\user $user, $error)
	{
		foreach ($error as $key => $lang)
		{
			if (is_array($lang))
			{
				$lang_key = array_shift($lang);
				$error[$key] = vsprintf($user->lang($lang_key), $lang);
			}
			else
			{
				$error[$key] = $user->lang("$lang");
			}
		}

		return $error;
	}
}