Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
manager.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\avatar;
015
016 class manager
017 {
018 /**
019 * phpBB configuration
020 * @var \phpbb\config\config
021 */
022 protected $config;
023
024 /**
025 * Array that contains a list of enabled drivers
026 * @var array
027 */
028 static protected $enabled_drivers = false;
029
030 /**
031 * Array that contains all available avatar drivers which are passed via the
032 * service container
033 * @var array
034 */
035 protected $avatar_drivers;
036
037 /**
038 * Default avatar data row
039 * @var array
040 */
041 static protected $default_row = array(
042 'avatar' => '',
043 'avatar_type' => '',
044 'avatar_width' => 0,
045 'avatar_height' => 0,
046 );
047
048 /**
049 * Construct an avatar manager object
050 *
051 * @param \phpbb\config\config $config phpBB configuration
052 * @param array $avatar_drivers Avatar drivers passed via the service container
053 */
054 public function __construct(\phpbb\config\config $config, $avatar_drivers)
055 {
056 $this->config = $config;
057 $this->register_avatar_drivers($avatar_drivers);
058 }
059
060 /**
061 * Register avatar drivers
062 *
063 * @param array $avatar_drivers Service collection of avatar drivers
064 */
065 protected function register_avatar_drivers($avatar_drivers)
066 {
067 if (!empty($avatar_drivers))
068 {
069 foreach ($avatar_drivers as $driver)
070 {
071 $this->avatar_drivers[$driver->get_name()] = $driver;
072 }
073 }
074 }
075
076 /**
077 * Get the driver object specified by the avatar type
078 *
079 * @param string $avatar_type Avatar type; by default an avatar's service container name
080 * @param bool $load_enabled Load only enabled avatars
081 *
082 * @return object Avatar driver object
083 */
084 public function get_driver($avatar_type, $load_enabled = true)
085 {
086 if (self::$enabled_drivers === false)
087 {
088 $this->load_enabled_drivers();
089 }
090
091 $avatar_drivers = ($load_enabled) ? self::$enabled_drivers : $this->get_all_drivers();
092
093 // Legacy stuff...
094 switch ($avatar_type)
095 {
096 case AVATAR_GALLERY:
097 $avatar_type = 'avatar.driver.local';
098 break;
099 case AVATAR_UPLOAD:
100 $avatar_type = 'avatar.driver.upload';
101 break;
102 case AVATAR_REMOTE:
103 $avatar_type = 'avatar.driver.remote';
104 break;
105 }
106
107 if (!isset($avatar_drivers[$avatar_type]))
108 {
109 return null;
110 }
111
112 /*
113 * There is no need to handle invalid avatar types as the following code
114 * will cause a ServiceNotFoundException if the type does not exist
115 */
116 $driver = $this->avatar_drivers[$avatar_type];
117
118 return $driver;
119 }
120
121 /**
122 * Load the list of enabled drivers
123 * This is executed once and fills self::$enabled_drivers
124 */
125 protected function load_enabled_drivers()
126 {
127 if (!empty($this->avatar_drivers))
128 {
129 self::$enabled_drivers = array();
130 foreach ($this->avatar_drivers as $driver)
131 {
132 if ($this->is_enabled($driver))
133 {
134 self::$enabled_drivers[$driver->get_name()] = $driver->get_name();
135 }
136 }
137 asort(self::$enabled_drivers);
138 }
139 }
140
141 /**
142 * Get a list of all avatar drivers
143 *
144 * As this function will only be called in the ACP avatar settings page, it
145 * doesn't make much sense to cache the list of all avatar drivers like the
146 * list of the enabled drivers.
147 *
148 * @return array Array containing a list of all avatar drivers
149 */
150 public function get_all_drivers()
151 {
152 $drivers = array();
153
154 if (!empty($this->avatar_drivers))
155 {
156 foreach ($this->avatar_drivers as $driver)
157 {
158 $drivers[$driver->get_name()] = $driver->get_name();
159 }
160 asort($drivers);
161 }
162
163 return $drivers;
164 }
165
166 /**
167 * Get a list of enabled avatar drivers
168 *
169 * @return array Array containing a list of the enabled avatar drivers
170 */
171 public function get_enabled_drivers()
172 {
173 if (self::$enabled_drivers === false)
174 {
175 $this->load_enabled_drivers();
176 }
177
178 return self::$enabled_drivers;
179 }
180
181 /**
182 * Strip out user_, group_, or other prefixes from array keys
183 *
184 * @param array $row User data or group data
185 * @param string $prefix Prefix of data keys (e.g. user), should not include the trailing underscore
186 *
187 * @return array User or group data with keys that have been
188 * stripped from the preceding "user_" or "group_"
189 * Also the group id is prefixed with g, when the prefix group is removed.
190 */
191 static public function clean_row($row, $prefix = '')
192 {
193 // Upon creation of a user/group $row might be empty
194 if (empty($row))
195 {
196 return self::$default_row;
197 }
198
199 $output = array();
200 foreach ($row as $key => $value)
201 {
202 $key = preg_replace("#^(?:{$prefix}_)#", '', $key);
203 $output[$key] = $value;
204 }
205
206 if ($prefix === 'group' && isset($output['id']))
207 {
208 $output['id'] = 'g' . $output['id'];
209 }
210
211 return $output;
212 }
213
214 /**
215 * Clean driver names that are returned from template files
216 * Underscores are replaced with dots
217 *
218 * @param string $name Driver name
219 *
220 * @return string Cleaned driver name
221 */
222 static public function clean_driver_name($name)
223 {
224 return str_replace(array('\\', '_'), '.', $name);
225 }
226
227 /**
228 * Prepare driver names for use in template files
229 * Dots are replaced with underscores
230 *
231 * @param string $name Clean driver name
232 *
233 * @return string Prepared driver name
234 */
235 static public function prepare_driver_name($name)
236 {
237 return str_replace('.', '_', $name);
238 }
239
240 /**
241 * Check if avatar is enabled
242 *
243 * @param object $driver Avatar driver object
244 *
245 * @return bool True if avatar is enabled, false if it's disabled
246 */
247 public function is_enabled($driver)
248 {
249 $config_name = $driver->get_config_name();
250
251 return $this->config["allow_avatar_{$config_name}"];
252 }
253
254 /**
255 * Get the settings array for enabling/disabling an avatar driver
256 *
257 * @param object $driver Avatar driver object
258 *
259 * @return array Array of configuration options as consumed by acp_board
260 */
261 public function get_avatar_settings($driver)
262 {
263 $config_name = $driver->get_config_name();
264
265 return array(
266 'allow_avatar_' . $config_name => array('lang' => 'ALLOW_' . strtoupper(str_replace('\\', '_', $config_name)), 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
267 );
268 }
269
270 /**
271 * Replace "error" strings with their real, localized form
272 *
273 * @param \phpbb\user phpBB User object
274 * @param array $error Array containing error strings
275 * Key values can either be a string with a language key or an array
276 * that will be passed to vsprintf() with the language key in the
277 * first array key.
278 *
279 * @return array Array containing the localized error strings
280 */
281 public function localize_errors(\phpbb\user $user, $error)
282 {
283 foreach ($error as $key => $lang)
284 {
285 if (is_array($lang))
286 {
287 $lang_key = array_shift($lang);
288 $error[$key] = vsprintf($user->lang($lang_key), $lang);
289 }
290 else
291 {
292 $error[$key] = $user->lang("$lang");
293 }
294 }
295
296 return $error;
297 }
298
299 /**
300 * Handle deleting avatars
301 *
302 * @param \phpbb\db\driver\driver_interface $db phpBB dbal
303 * @param \phpbb\user $user phpBB user object
304 * @param array $avatar_data Cleaned user data containing the user's
305 * avatar data
306 * @param string $table Database table from which the avatar should be deleted
307 * @param string $prefix Prefix of user data columns in database
308 * @return null
309 */
310 public function handle_avatar_delete(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $avatar_data, $table, $prefix)
311 {
312 if ($driver = $this->get_driver($avatar_data['avatar_type']))
313 {
314 $driver->delete($avatar_data);
315 }
316
317 $result = $this->prefix_avatar_columns($prefix, self::$default_row);
318
319 $sql = 'UPDATE ' . $table . '
320 SET ' . $db->sql_build_array('UPDATE', $result) . '
321 WHERE ' . $prefix . 'id = ' . (int) $avatar_data['id'];
322 $db->sql_query($sql);
323
324 // Make sure we also delete this avatar from the users
325 if ($prefix === 'group_')
326 {
327 $result = $this->prefix_avatar_columns('user_', self::$default_row);
328
329 $sql = 'UPDATE ' . USERS_TABLE . '
330 SET ' . $db->sql_build_array('UPDATE', $result) . "
331 WHERE user_avatar = '" . $db->sql_escape($avatar_data['avatar']) . "'";
332 $db->sql_query($sql);
333 }
334 }
335
336 /**
337 * Prefix avatar columns
338 *
339 * @param string $prefix Column prefix
340 * @param array $data Column data
341 *
342 * @return array Column data with prefixed column names
343 */
344 public function prefix_avatar_columns($prefix, $data)
345 {
346 foreach ($data as $key => $value)
347 {
348 $data[$prefix . $key] = $value;
349 unset($data[$key]);
350 }
351
352 return $data;
353 }
354 }
355