Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.64% |
9 / 66 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| helper | |
13.64% |
9 / 66 |
|
28.57% |
2 / 7 |
226.71 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| get_template_vars | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| get_user_avatar | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_group_avatar | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_avatar | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
56 | |||
| get_no_avatar_source | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| get_avatar_html | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\avatar; |
| 15 | |
| 16 | use phpbb\avatar\driver\driver_interface; |
| 17 | use phpbb\config\config; |
| 18 | use phpbb\event\dispatcher; |
| 19 | use phpbb\language\language; |
| 20 | use phpbb\path_helper; |
| 21 | use phpbb\user; |
| 22 | |
| 23 | /** |
| 24 | * Avatar helper object. |
| 25 | * |
| 26 | * Generates avatars and their variables for display. |
| 27 | */ |
| 28 | class helper |
| 29 | { |
| 30 | /** @var config */ |
| 31 | protected $config; |
| 32 | |
| 33 | /** @var dispatcher */ |
| 34 | protected $dispatcher; |
| 35 | |
| 36 | /** @var language */ |
| 37 | protected $language; |
| 38 | |
| 39 | /** @var manager */ |
| 40 | protected $manager; |
| 41 | |
| 42 | /** @var path_helper */ |
| 43 | protected $path_helper; |
| 44 | |
| 45 | /** @var user */ |
| 46 | protected $user; |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * @param config $config Config object |
| 52 | * @param dispatcher $dispatcher Event dispatcher object |
| 53 | * @param language $language Language object |
| 54 | * @param manager $manager Avatar manager object |
| 55 | * @param path_helper $path_helper Path helper object |
| 56 | * @param user $user User object |
| 57 | */ |
| 58 | public function __construct( |
| 59 | config $config, |
| 60 | dispatcher $dispatcher, |
| 61 | language $language, |
| 62 | manager $manager, |
| 63 | path_helper $path_helper, |
| 64 | user $user |
| 65 | ) |
| 66 | { |
| 67 | $this->config = $config; |
| 68 | $this->dispatcher = $dispatcher; |
| 69 | $this->language = $language; |
| 70 | $this->manager = $manager; |
| 71 | $this->path_helper = $path_helper; |
| 72 | $this->user = $user; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get an avatar's template variables. |
| 77 | * |
| 78 | * @param array $avatar The avatar's data |
| 79 | * @param string $prefix The variables' prefix |
| 80 | * @return array The avatar's template variables |
| 81 | */ |
| 82 | public function get_template_vars(array $avatar, string $prefix = ''): array |
| 83 | { |
| 84 | $prefix = $prefix && !str_ends_with($prefix, '_') ? "{$prefix}_" : $prefix; |
| 85 | |
| 86 | return [ |
| 87 | "{$prefix}AVATAR" => $avatar, |
| 88 | |
| 89 | "{$prefix}AVATAR_SOURCE" => $avatar['src'], |
| 90 | "{$prefix}AVATAR_TITLE" => $avatar['title'], |
| 91 | "{$prefix}AVATAR_TYPE" => $avatar['type'], |
| 92 | |
| 93 | "{$prefix}AVATAR_WIDTH" => $avatar['width'], |
| 94 | "{$prefix}AVATAR_HEIGHT" => $avatar['height'], |
| 95 | |
| 96 | "{$prefix}AVATAR_LAZY" => $avatar['lazy'], |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get user avatar data. |
| 102 | * |
| 103 | * @param array $row The user's table row |
| 104 | * @param string $title Optional language string/key for the title |
| 105 | * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP |
| 106 | * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not |
| 107 | * @return array The avatar data array |
| 108 | */ |
| 109 | public function get_user_avatar(array $row, string $title = 'USER_AVATAR', bool $ignore_config = false, bool $lazy = false): array |
| 110 | { |
| 111 | $row = manager::clean_row($row, 'user'); |
| 112 | |
| 113 | return $this->get_avatar($row, $title, $ignore_config, $lazy); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get group avatar data. |
| 118 | * |
| 119 | * @param array $row The group's table row |
| 120 | * @param string $title Optional language string/key for the title |
| 121 | * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP |
| 122 | * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not |
| 123 | * @return array The avatar data array |
| 124 | */ |
| 125 | public function get_group_avatar(array $row, string $title = 'GROUP_AVATAR', bool $ignore_config = false, bool $lazy = false): array |
| 126 | { |
| 127 | $row = manager::clean_row($row, 'group'); |
| 128 | |
| 129 | return $this->get_avatar($row, $title, $ignore_config, $lazy); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get avatar data. |
| 134 | * |
| 135 | * @param array $row The cleaned table row |
| 136 | * @param string $title Optional language string/key for the title |
| 137 | * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP |
| 138 | * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not |
| 139 | * @return array The avatar data array |
| 140 | */ |
| 141 | public function get_avatar(array $row, string $title, bool $ignore_config = false, bool $lazy = false): array |
| 142 | { |
| 143 | if (!$this->config['allow_avatar'] && !$ignore_config) |
| 144 | { |
| 145 | return [ |
| 146 | 'id' => 0, |
| 147 | 'username' => '', |
| 148 | 'lazy' => false, |
| 149 | 'src' => '', |
| 150 | 'title' => '', |
| 151 | 'type' => '', |
| 152 | 'width' => 0, |
| 153 | 'height' => 0, |
| 154 | ]; |
| 155 | } |
| 156 | |
| 157 | $data = [ |
| 158 | 'id' => $row['id'] ?? 0, |
| 159 | 'username' => $row['username'] ?? '', |
| 160 | 'src' => $row['avatar'], |
| 161 | 'width' => $row['avatar_width'], |
| 162 | 'height' => $row['avatar_height'], |
| 163 | 'title' => $this->language->lang($title), |
| 164 | 'lazy' => $lazy, |
| 165 | 'type' => '', |
| 166 | 'html' => '', |
| 167 | ]; |
| 168 | |
| 169 | /** @var driver_interface $driver */ |
| 170 | $driver = $this->manager->get_driver($row['avatar_type'], !$ignore_config); |
| 171 | |
| 172 | if ($driver !== null) |
| 173 | { |
| 174 | $data = array_merge($data, $driver->get_data($row), [ |
| 175 | 'type' => $driver->get_name(), |
| 176 | 'html' => $driver->get_custom_html($this->user, $row, $title), |
| 177 | ]); |
| 178 | |
| 179 | /** |
| 180 | * The type is used in the template to determine what driver is used, |
| 181 | * and potentially to add an additional class to the avatar <img> element. |
| 182 | * |
| 183 | * While it's possible to str_replace('avatar.driver.', '', $data['type']) |
| 184 | * for all the core drivers, this will be awkward for extensions' avatar drivers. |
| 185 | * As they will most likely want to adjust the type in the event below, |
| 186 | * and then have to search for a different definition than they used in their services.yml |
| 187 | * |
| 188 | * For example, 'ext.vendor.avatar.driver.custom_driver' |
| 189 | * They will then have to look for: 'ext.vendor.custom_driver' |
| 190 | * |
| 191 | * So only remove 'avatar.driver.' if it is at the beginning of the driver's name. |
| 192 | */ |
| 193 | if (strpos($data['type'], 'avatar.driver.') === 0) |
| 194 | { |
| 195 | $data['type'] = substr($data['type'], strlen('avatar.driver.')); |
| 196 | } |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | $data['src'] = ''; |
| 201 | } |
| 202 | |
| 203 | if (empty($data['html']) && !empty($data['src'])) |
| 204 | { |
| 205 | $data['html'] = $this->get_avatar_html($data); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Event to modify avatar data array |
| 210 | * |
| 211 | * @event core.avatar_helper_get_avatar |
| 212 | * @var array row The cleaned table row |
| 213 | * @var string title The language string/key for the title |
| 214 | * @var bool ignore_config Ignores the config setting, to still be able to view the avatar in the UCP |
| 215 | * @var bool lazy Indicator whether the avatar should be lazy loaded (requires JS) or not |
| 216 | * @var array data The avatar data array |
| 217 | * @since 4.0.0 |
| 218 | */ |
| 219 | $vars = ['row', 'title', 'ignore_config', 'lazy', 'data']; |
| 220 | extract($this->dispatcher->trigger_event('core.avatar_helper_get_avatar', compact($vars))); |
| 221 | |
| 222 | return $data; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the "no avatar" source string. |
| 227 | * |
| 228 | * @return string The "no avatar" source string |
| 229 | */ |
| 230 | public function get_no_avatar_source(): string |
| 231 | { |
| 232 | /** |
| 233 | * We need to correct the phpBB root path in case this is called from a controller, |
| 234 | * because the web path will be incorrect otherwise. |
| 235 | */ |
| 236 | $web_path = $this->path_helper->get_web_root_path(); |
| 237 | $style_path = rawurlencode($this->user->style['style_path']); |
| 238 | |
| 239 | return "{$web_path}styles/{$style_path}/theme/images/no_avatar.gif"; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get an avatar's HTML <img> element. |
| 244 | * |
| 245 | * Created for Backwards Compatibility (BC). |
| 246 | * Styles should generate their own HTML element instead. |
| 247 | * |
| 248 | * @deprecated 4.1.0 After admin style is reworked aswell |
| 249 | * |
| 250 | * @param array $data The avatar data array |
| 251 | * @return string The avatar's HTML <img> element |
| 252 | */ |
| 253 | private function get_avatar_html(array $data): string |
| 254 | { |
| 255 | if ($data['lazy']) |
| 256 | { |
| 257 | $data['src'] = $this->get_no_avatar_source() . '" data-src="' . $data['src']; |
| 258 | } |
| 259 | |
| 260 | $src = ' src="' . $data['src'] . '"'; |
| 261 | $alt = ' alt="' . $data['title'] . '"'; |
| 262 | |
| 263 | $width = $data['width'] ? ' width="' . $data['width'] . '"' : ''; |
| 264 | $height = $data['height'] ? ' height="' . $data['height'] . '"' : ''; |
| 265 | |
| 266 | return '<img class="avatar"' . $src . $width . $height . $alt . ' />'; |
| 267 | } |
| 268 | } |