From 7e3d22063a0807640bb9838ba7b6c6bda507a943 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Mon, 16 Dec 2019 20:23:45 +0100 Subject: [PATCH 1/6] [ticket/15233] Standardize avatar output variables PHPBB3-15233 --- phpBB/config/default/container/services.yml | 1 + .../default/container/services_user.yml | 1 + phpBB/includes/acp/acp_groups.php | 12 ++++--- phpBB/includes/acp/acp_users.php | 14 ++++++-- phpBB/includes/functions.php | 19 ++++++++++- phpBB/includes/functions_display.php | 16 ++++++++-- phpBB/includes/mcp/mcp_notes.php | 15 +++++++-- phpBB/includes/mcp/mcp_warn.php | 32 +++++++++++++++---- phpBB/includes/ucp/ucp_groups.php | 14 +++++--- phpBB/includes/ucp/ucp_pm_viewmessage.php | 15 +++++++-- phpBB/includes/ucp/ucp_profile.php | 15 +++++++-- phpBB/memberlist.php | 11 +++++-- phpBB/phpbb/group/helper.php | 14 +++++--- phpBB/phpbb/notification/type/base.php | 20 +++++++++--- phpBB/phpbb/user_loader.php | 17 ++++++---- phpBB/viewtopic.php | 16 +++++++--- 16 files changed, 182 insertions(+), 50 deletions(-) diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index b34e5306e3..05f50d1622 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -123,6 +123,7 @@ services: class: phpbb\group\helper arguments: - '@auth' + - '@avatar.helper' - '@cache' - '@config' - '@language' diff --git a/phpBB/config/default/container/services_user.yml b/phpBB/config/default/container/services_user.yml index 7e634c60c3..154e9aebf6 100644 --- a/phpBB/config/default/container/services_user.yml +++ b/phpBB/config/default/container/services_user.yml @@ -14,6 +14,7 @@ services: user_loader: class: phpbb\user_loader arguments: + - '@avatar.helper' - '@dbal.conn' - '%core.root_path%' - '%core.php_ext%' diff --git a/phpBB/includes/acp/acp_groups.php b/phpBB/includes/acp/acp_groups.php index 7b1dc706db..8cb8e778b7 100644 --- a/phpBB/includes/acp/acp_groups.php +++ b/phpBB/includes/acp/acp_groups.php @@ -721,7 +721,7 @@ class acp_groups } } - $avatar = phpbb_get_group_avatar($group_row, 'GROUP_AVATAR', true); + $group_avatar = $group_helper->get_avatar($group_row, 'GROUP_AVATAR', true); if (isset($phpbb_avatar_manager) && !$update) { @@ -771,10 +771,14 @@ class acp_groups 'S_RANK_OPTIONS' => $rank_options, 'S_GROUP_OPTIONS' => group_select_options(false, false, (($user->data['user_type'] == USER_FOUNDER) ? false : 0)), - 'AVATAR' => empty($avatar) ? '' : $avatar, + 'AVATAR' => empty($group_avatar['html']) ? '' : $group_avatar['html'], + 'AVATAR_LAZY' => $group_avatar['lazy'], + 'AVATAR_SRC' => $group_avatar['src'], + 'AVATAR_TITLE' => $group_avatar['title'], + 'AVATAR_TYPE' => $group_avatar['type'], + 'AVATAR_WIDTH' => $group_avatar['width'], + 'AVATAR_HEIGHT' => $group_avatar['height'], 'AVATAR_MAX_FILESIZE' => $config['avatar_filesize'], - 'AVATAR_WIDTH' => (isset($group_row['group_avatar_width'])) ? $group_row['group_avatar_width'] : '', - 'AVATAR_HEIGHT' => (isset($group_row['group_avatar_height'])) ? $group_row['group_avatar_height'] : '', 'GROUP_TYPE_FREE' => GROUP_FREE, 'GROUP_TYPE_OPEN' => GROUP_OPEN, diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index 94a9e50a7b..e3045ffe19 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1968,12 +1968,22 @@ class acp_users $error = $phpbb_avatar_manager->localize_errors($user, $error); } - $avatar = phpbb_get_user_avatar($user_row, 'USER_AVATAR', true); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($user_row, 'USER_AVATAR', true); $template->assign_vars(array( 'S_AVATAR' => true, 'ERROR' => (!empty($error)) ? implode('
', $error) : '', - 'AVATAR' => (empty($avatar) ? '' : $avatar), + + 'AVATAR' => empty($avatar['html']) ? '' : $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], 'S_FORM_ENCTYPE' => ' enctype="multipart/form-data"', diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index db6704ce86..f8122c4272 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3523,6 +3523,8 @@ function phpbb_quoteattr($data, $entities = null) /** * Get user avatar * +* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_user_avatar() instead +* * @param array $user_row Row from the users table * @param string $alt Optional language string for alt tag within image, can be a language key or text * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP @@ -3539,6 +3541,8 @@ function phpbb_get_user_avatar($user_row, $alt = 'USER_AVATAR', $ignore_config = /** * Get group avatar * +* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_group_avatar() instead +* * @param array $group_row Row from the groups table * @param string $alt Optional language string for alt tag within image, can be a language key or text * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP @@ -3555,6 +3559,8 @@ function phpbb_get_group_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_confi /** * Get avatar * +* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_avatar() instead +* * @param array $row Row cleaned by \phpbb\avatar\manager::clean_row * @param string $alt Optional language string for alt tag within image, can be a language key or text * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP @@ -3862,6 +3868,11 @@ function page_header($page_title = '', $display_online_list = false, $item_id = // Add form token for login box, in case page is presenting a login form. add_form_key('login', '_LOGIN'); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($user->data); + // The following assigns all _common_ variables that may be used at any point in a template. $template->assign_vars(array( 'SITENAME' => $config['sitename'], @@ -3876,7 +3887,13 @@ function page_header($page_title = '', $display_online_list = false, $item_id = 'RECORD_USERS' => $l_online_record, 'PRIVATE_MESSAGE_COUNT' => (!empty($user->data['user_unread_privmsg'])) ? $user->data['user_unread_privmsg'] : 0, - 'CURRENT_USER_AVATAR' => phpbb_get_user_avatar($user->data), + 'CURRENT_USER_AVATAR' => $avatar['html'], + 'CURRENT_USER_AVATAR_LAZY' => $avatar['lazy'], + 'CURRENT_USER_AVATAR_SOURCE' => $avatar['src'], + 'CURRENT_USER_AVATAR_TITLE' => $avatar['title'], + 'CURRENT_USER_AVATAR_TYPE' => $avatar['type'], + 'CURRENT_USER_AVATAR_WIDTH' => $avatar['width'], + 'CURRENT_USER_AVATAR_HEIGHT' => $avatar['height'], 'CURRENT_USERNAME_SIMPLE' => get_username_string('no_profile', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), 'CURRENT_USERNAME_FULL' => get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), 'CURRENT_USER_GROUP_COLOR' => $user->data['user_colour'], diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php index db6ae7e6f1..b4ab8a9c7a 100644 --- a/phpBB/includes/functions_display.php +++ b/phpBB/includes/functions_display.php @@ -1570,7 +1570,7 @@ function phpbb_get_user_rank($user_data, $user_posts) */ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabled = false, $check_can_receive_pm = true) { - global $config, $auth, $user, $phpEx, $phpbb_root_path, $phpbb_dispatcher; + global $config, $auth, $user, $phpEx, $phpbb_root_path, $phpbb_dispatcher, $phpbb_container; $username = $data['username']; $user_id = $data['user_id']; @@ -1653,6 +1653,11 @@ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabl (($auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_')) || $data['user_allow_pm']) ); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($data); + // Dump it out to the template $template_data = array( 'AGE' => $age, @@ -1669,7 +1674,14 @@ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabl 'A_USERNAME' => addslashes(get_username_string('username', $user_id, $username, $data['user_colour'])), - 'AVATAR_IMG' => phpbb_get_user_avatar($data), + 'AVATAR_IMG' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], + 'ONLINE_IMG' => (!$config['load_onlinetrack']) ? '' : (($online) ? $user->img('icon_user_online', 'ONLINE') : $user->img('icon_user_offline', 'OFFLINE')), 'S_ONLINE' => ($config['load_onlinetrack'] && $online) ? true : false, 'RANK_IMG' => $user_rank_data['img'], diff --git a/phpBB/includes/mcp/mcp_notes.php b/phpBB/includes/mcp/mcp_notes.php index 47dc97cc8b..4690790dab 100644 --- a/phpBB/includes/mcp/mcp_notes.php +++ b/phpBB/includes/mcp/mcp_notes.php @@ -192,7 +192,11 @@ class mcp_notes // Generate the appropriate user information for the user we are looking at $rank_data = phpbb_get_user_rank($userrow, $userrow['user_posts']); - $avatar_img = phpbb_get_user_avatar($userrow); + + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($userrow); $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']); $sort_by_text = array('a' => $user->lang['SORT_USERNAME'], 'b' => $user->lang['SORT_DATE'], 'c' => $user->lang['SORT_IP'], 'd' => $user->lang['SORT_ACTION']); @@ -252,10 +256,15 @@ class mcp_notes 'USERNAME' => get_username_string('username', $userrow['user_id'], $userrow['username'], $userrow['user_colour']), 'U_PROFILE' => get_username_string('profile', $userrow['user_id'], $userrow['username'], $userrow['user_colour']), - 'AVATAR_IMG' => $avatar_img, + 'AVATAR_IMG' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $rank_data['img'], 'RANK_TITLE' => $rank_data['title'], )); } - } diff --git a/phpBB/includes/mcp/mcp_warn.php b/phpBB/includes/mcp/mcp_warn.php index 74d6346ffd..1e146bb004 100644 --- a/phpBB/includes/mcp/mcp_warn.php +++ b/phpBB/includes/mcp/mcp_warn.php @@ -190,7 +190,7 @@ class mcp_warn function mcp_warn_post_view($action) { global $phpEx, $phpbb_root_path, $config, $request; - global $template, $db, $user, $phpbb_dispatcher; + global $template, $db, $user, $phpbb_dispatcher, $phpbb_container; $post_id = $request->variable('p', 0); $forum_id = $request->variable('f', 0); @@ -342,7 +342,11 @@ class mcp_warn } $user_rank_data = phpbb_get_user_rank($user_row, $user_row['user_posts']); - $avatar_img = phpbb_get_user_avatar($user_row); + + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($user_row); $template->assign_vars(array( 'U_POST_ACTION' => $this->u_action, @@ -355,7 +359,13 @@ class mcp_warn 'POSTS' => ($user_row['user_posts']) ? $user_row['user_posts'] : 0, 'WARNINGS' => ($user_row['user_warnings']) ? $user_row['user_warnings'] : 0, - 'AVATAR_IMG' => $avatar_img, + 'AVATAR_IMG' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $user_rank_data['img'], 'L_WARNING_POST_DEFAULT' => sprintf($user->lang['WARNING_POST_DEFAULT'], generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&p=$post_id#p$post_id"), @@ -370,7 +380,7 @@ class mcp_warn function mcp_warn_user_view($action) { global $phpEx, $phpbb_root_path, $config, $request; - global $template, $db, $user, $phpbb_dispatcher; + global $template, $db, $user, $phpbb_dispatcher, $phpbb_container; $user_id = $request->variable('u', 0); $username = $request->variable('username', '', true); @@ -491,7 +501,11 @@ class mcp_warn include($phpbb_root_path . 'includes/functions_display.' . $phpEx); } $user_rank_data = phpbb_get_user_rank($user_row, $user_row['user_posts']); - $avatar_img = phpbb_get_user_avatar($user_row); + + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($user_row); // OK, they didn't submit a warning so lets build the page for them to do so $template->assign_vars(array( @@ -507,7 +521,13 @@ class mcp_warn 'USERNAME' => get_username_string('username', $user_row['user_id'], $user_row['username'], $user_row['user_colour']), 'U_PROFILE' => get_username_string('profile', $user_row['user_id'], $user_row['username'], $user_row['user_colour']), - 'AVATAR_IMG' => $avatar_img, + 'AVATAR_IMG' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $user_rank_data['img'], 'S_CAN_NOTIFY' => $s_can_notify, diff --git a/phpBB/includes/ucp/ucp_groups.php b/phpBB/includes/ucp/ucp_groups.php index 79842a08b4..92c9ee6078 100644 --- a/phpBB/includes/ucp/ucp_groups.php +++ b/phpBB/includes/ucp/ucp_groups.php @@ -431,7 +431,7 @@ class ucp_groups $group_name = $group_row['group_name']; $group_type = $group_row['group_type']; - $avatar = phpbb_get_group_avatar($group_row, 'GROUP_AVATAR', true); + $group_avatar = $group_helper->get_avatar($group_row, 'GROUP_AVATAR', true); $template->assign_vars(array( 'GROUP_NAME' => $group_helper->get_name($group_name), @@ -440,10 +440,14 @@ class ucp_groups 'GROUP_DESC_DISP' => generate_text_for_display($group_row['group_desc'], $group_row['group_desc_uid'], $group_row['group_desc_bitfield'], $group_row['group_desc_options']), 'GROUP_TYPE' => $group_row['group_type'], - 'AVATAR' => !empty($avatar) ? $avatar : '', - 'AVATAR_IMAGE' => !empty($avatar) ? $avatar : '', - 'AVATAR_WIDTH' => isset($group_row['group_avatar_width']) ? $group_row['group_avatar_width'] : '', - 'AVATAR_HEIGHT' => isset($group_row['group_avatar_height']) ? $group_row['group_avatar_height'] : '', + 'AVATAR' => empty($group_avatar['html']) ? '' : $group_avatar['html'], + 'AVATAR_IMAGE' => empty($group_avatar['html']) ? '' : $group_avatar['html'], + 'AVATAR_LAZY' => $group_avatar['lazy'], + 'AVATAR_SRC' => $group_avatar['src'], + 'AVATAR_TITLE' => $group_avatar['title'], + 'AVATAR_TYPE' => $group_avatar['type'], + 'AVATAR_WIDTH' => $group_avatar['width'], + 'AVATAR_HEIGHT' => $group_avatar['height'], )); } diff --git a/phpBB/includes/ucp/ucp_pm_viewmessage.php b/phpBB/includes/ucp/ucp_pm_viewmessage.php index 6657aa4fd2..d1c5b16b4c 100644 --- a/phpBB/includes/ucp/ucp_pm_viewmessage.php +++ b/phpBB/includes/ucp/ucp_pm_viewmessage.php @@ -221,7 +221,13 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) 'RANK_TITLE' => $user_info['rank_title'], 'RANK_IMG' => $user_info['rank_image'], - 'AUTHOR_AVATAR' => (isset($user_info['avatar'])) ? $user_info['avatar'] : '', + 'AUTHOR_AVATAR' => !empty($user_info['avatar']) ? $user_info['avatar']['html'] : '', + 'AUTHOR_AVATAR_LAZY' => !empty($user_info['avatar']) ? $user_info['avatar']['lazy'] : false, + 'AUTHOR_AVATAR_SOURCE' => !empty($user_info['avatar']) ? $user_info['avatar']['src'] : '', + 'AUTHOR_AVATAR_TITLE' => !empty($user_info['avatar']) ? $user_info['avatar']['title'] : '', + 'AUTHOR_AVATAR_TYPE' => !empty($user_info['avatar']) ? $user_info['avatar']['type'] : '', + 'AUTHOR_AVATAR_WIDTH' => !empty($user_info['avatar']) ? $user_info['avatar']['width'] : 0, + 'AUTHOR_AVATAR_HEIGHT' => !empty($user_info['avatar']) ? $user_info['avatar']['height'] : 0, 'AUTHOR_JOINED' => $user->format_date($user_info['user_regdate']), 'AUTHOR_POSTS' => (int) $user_info['user_posts'], 'U_AUTHOR_POSTS' => ($config['load_search'] && $auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$author_id&sr=posts") : '', @@ -409,7 +415,7 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) */ function get_user_information($user_id, $user_row) { - global $db, $auth, $user; + global $db, $auth, $user, $phpbb_container; global $phpbb_root_path, $phpEx, $config; if (!$user_id) @@ -449,7 +455,10 @@ function get_user_information($user_id, $user_row) } } - $user_row['avatar'] = ($user->optionget('viewavatars')) ? phpbb_get_user_avatar($user_row) : ''; + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $user_row['avatar'] = ($user->optionget('viewavatars')) ? $avatar_helper->get_user_avatar($user_row) : []; if (!function_exists('phpbb_get_user_rank')) { diff --git a/phpBB/includes/ucp/ucp_profile.php b/phpBB/includes/ucp/ucp_profile.php index 229f3fc06a..2394cc5667 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -757,11 +757,20 @@ class ucp_profile $error = $phpbb_avatar_manager->localize_errors($user, $error); } - $avatar = phpbb_get_user_avatar($user->data, 'USER_AVATAR', true); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($user->data, 'USER_AVATAR', true); $template->assign_vars(array( - 'ERROR' => (count($error)) ? implode('
', $error) : '', - 'AVATAR' => $avatar, + 'ERROR' => (count($error)) ? implode('
', $error) : '', + 'AVATAR' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SOURCE' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], 'S_FORM_ENCTYPE' => ' enctype="multipart/form-data"', diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index fb4f82f38c..b5122b93ab 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -1290,7 +1290,7 @@ switch ($mode) break; } - $avatar_img = phpbb_get_group_avatar($group_row); + $avatar = $group_helper->get_avatar($group_row); // ... same for group rank $group_rank_data = array( @@ -1338,7 +1338,14 @@ switch ($mode) 'GROUP_TYPE' => $user->lang['GROUP_IS_' . $group_row['l_group_type']], 'GROUP_RANK' => $group_rank_data['title'], - 'AVATAR_IMG' => $avatar_img, + 'AVATAR_IMG' => $avatar['html'], + 'AVATAR_LAZY' => $avatar['lazy'], + 'AVATAR_SRC' => $avatar['src'], + 'AVATAR_TITLE' => $avatar['title'], + 'AVATAR_TYPE' => $avatar['type'], + 'AVATAR_WIDTH' => $avatar['width'], + 'AVATAR_HEIGHT' => $avatar['height'], + 'RANK_IMG' => $group_rank_data['img'], 'RANK_IMG_SRC' => $group_rank_data['img_src'], diff --git a/phpBB/phpbb/group/helper.php b/phpBB/phpbb/group/helper.php index 639315c960..92dd71384f 100644 --- a/phpBB/phpbb/group/helper.php +++ b/phpBB/phpbb/group/helper.php @@ -14,6 +14,7 @@ namespace phpbb\group; use phpbb\auth\auth; +use phpbb\avatar\helper as avatar_helper; use phpbb\cache\service as cache; use phpbb\config\config; use phpbb\language\language; @@ -26,6 +27,9 @@ class helper /** @var auth */ protected $auth; + /** @var avatar_helper */ + protected $avatar_helper; + /** @var cache */ protected $cache; @@ -54,6 +58,7 @@ class helper * Constructor * * @param auth $auth Authentication object + * @param avatar_helper $avatar_helper Avatar helper object * @param cache $cache Cache service object * @param config $config Configuration object * @param language $language Language object @@ -61,9 +66,10 @@ class helper * @param path_helper $path_helper Path helper object * @param user $user User object */ - public function __construct(auth $auth, cache $cache, config $config, language $language, dispatcher_interface $dispatcher, path_helper $path_helper, user $user) + public function __construct(auth $auth, avatar_helper $avatar_helper, cache $cache, config $config, language $language, dispatcher_interface $dispatcher, path_helper $path_helper, user $user) { $this->auth = $auth; + $this->avatar_helper = $avatar_helper; $this->cache = $cache; $this->config = $config; $this->language = $language; @@ -278,17 +284,17 @@ class helper /** * Get group avatar. - * Wrapper function for phpbb_get_group_avatar() + * Wrapper function for \phpbb\avatar\helper::get_group_avatar() * * @param array $group_row Row from the groups table * @param string $alt Optional language string for alt tag within image, can be a language key or text * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP * @param bool $lazy If true, will be lazy loaded (requires JS) * - * @return string Avatar html + * @return array Avatar data */ function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false) { - return phpbb_get_group_avatar($group_row, $alt, $ignore_config, $lazy); + return $this->avatar_helper->get_group_avatar($group_row, $alt, $ignore_config, $lazy); } } diff --git a/phpBB/phpbb/notification/type/base.php b/phpBB/phpbb/notification/type/base.php index b5238a0e9a..e59c5b9638 100644 --- a/phpBB/phpbb/notification/type/base.php +++ b/phpBB/phpbb/notification/type/base.php @@ -284,10 +284,11 @@ abstract class base implements \phpbb\notification\type\type_interface $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&hash=' . $mark_hash . '&redirect=' . urlencode($redirect)); } - return array( + $avatar = $this->get_avatar(); + + return [ 'NOTIFICATION_ID' => $this->notification_id, 'STYLING' => $this->get_style_class(), - 'AVATAR' => $this->get_avatar(), 'FORMATTED_TITLE' => $this->get_title(), 'REFERENCE' => $this->get_reference(), 'FORUM' => $this->get_forum(), @@ -295,8 +296,17 @@ abstract class base implements \phpbb\notification\type\type_interface 'URL' => $this->get_url(), 'TIME' => $this->user->format_date($this->notification_time), 'UNREAD' => !$this->notification_read, + + 'AVATAR' => $avatar ? $avatar['html'] : '', + 'AVATAR_LAZY' => $avatar ? $avatar['lazy'] : true, + 'AVATAR_SRC' => $avatar ? $avatar['src'] : '', + 'AVATAR_TITLE' => $avatar ? $avatar['title'] : '', + 'AVATAR_TYPE' => $avatar ? $avatar['type'] : '', + 'AVATAR_WIDTH' => $avatar ? $avatar['width'] : 0, + 'AVATAR_HEIGHT' => $avatar ? $avatar['height'] : 0, + 'U_MARK_READ' => (!$this->notification_read) ? $u_mark_read : '', - ); + ]; } /** @@ -327,11 +337,11 @@ abstract class base implements \phpbb\notification\type\type_interface /** * Get the user's avatar (fall back) * - * @return string + * @return array */ public function get_avatar() { - return ''; + return []; } /** diff --git a/phpBB/phpbb/user_loader.php b/phpBB/phpbb/user_loader.php index 3dacf07ff5..4a682ffbad 100644 --- a/phpBB/phpbb/user_loader.php +++ b/phpBB/phpbb/user_loader.php @@ -23,6 +23,9 @@ namespace phpbb; */ class user_loader { + /** @var \phpbb\avatar\helper */ + protected $avatar_helper; + /** @var \phpbb\db\driver\driver_interface */ protected $db = null; @@ -45,13 +48,15 @@ class user_loader /** * User loader constructor * + * @param \phpbb\avatar\helper $avatar_helper Avatar helper object * @param \phpbb\db\driver\driver_interface $db A database connection * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $php_ext php file extension * @param string $users_table The name of the database table (phpbb_users) */ - public function __construct(\phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $users_table) + public function __construct(\phpbb\avatar\helper $avatar_helper, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $users_table) { + $this->avatar_helper = $avatar_helper; $this->db = $db; $this->phpbb_root_path = $phpbb_root_path; @@ -182,23 +187,23 @@ class user_loader * Typically this should be left as false and you should make sure * you load users ahead of time with load_users() * @param bool @lazy If true, will be lazy loaded (requires JS) - * @return string + * @return array */ public function get_avatar($user_id, $query = false, $lazy = false) { if (!($user = $this->get_user($user_id, $query))) { - return ''; + return []; } - $row = array( + $row = [ 'avatar' => $user['user_avatar'], 'avatar_type' => $user['user_avatar_type'], 'avatar_width' => $user['user_avatar_width'], 'avatar_height' => $user['user_avatar_height'], - ); + ]; - return phpbb_get_avatar($row, 'USER_AVATAR', false, $lazy); + return $this->avatar_helper->get_avatar($row, 'USER_AVATAR', false, $lazy); } /** diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 3d9de55a84..7bfa29d70d 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -1179,6 +1179,9 @@ if (!empty($topic_data['poll_start'])) unset($poll_end, $poll_info, $poll_options_template_data, $poll_template_data, $voted_id); } +/** @var \phpbb\avatar\helper $avatar_helper */ +$avatar_helper = $phpbb_container->get('avatar.helper'); + // If the user is trying to reach the second half of the topic, fetch it starting from the end $store_reverse = false; $sql_limit = $config['posts_per_page']; @@ -1417,7 +1420,7 @@ while ($row = $db->sql_fetchrow($result)) 'sig_bbcode_bitfield' => '', 'online' => false, - 'avatar' => ($user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '', + 'avatar' => ($user->optionget('viewavatars')) ? $avatar_helper->get_user_avatar($row) : [], 'rank_title' => '', 'rank_image' => '', 'rank_image_src' => '', @@ -1481,7 +1484,7 @@ while ($row = $db->sql_fetchrow($result)) 'viewonline' => $row['user_allow_viewonline'], 'allow_pm' => $row['user_allow_pm'], - 'avatar' => ($user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '', + 'avatar' => ($user->optionget('viewavatars')) ? $this->get_user_avatar($row) : [], 'age' => '', 'rank_title' => '', @@ -2026,7 +2029,6 @@ for ($i = 0, $end = count($post_list); $i < $end; ++$i) $u_pm = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=compose&action=quotepost&p=' . $row['post_id']); } - // $post_row = array( 'POST_AUTHOR_FULL' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_full'] : get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), 'POST_AUTHOR_COLOUR' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_colour'] : get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), @@ -2038,7 +2040,13 @@ for ($i = 0, $end = count($post_list); $i < $end; ++$i) 'RANK_IMG_SRC' => $user_cache[$poster_id]['rank_image_src'], 'POSTER_JOINED' => $user_cache[$poster_id]['joined'], 'POSTER_POSTS' => $user_cache[$poster_id]['posts'], - 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'], + 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['html'] : '', + 'POSTER_AVATAR_LAZY' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['lazy'] : false, + 'POSTER_AVATAR_SOURCE' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['src'] : '', + 'POSTER_AVATAR_TITLE' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['title'] : '', + 'POSTER_AVATAR_TYPE' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['type'] : '', + 'POSTER_AVATAR_WIDTH' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['width'] : 0, + 'POSTER_AVATAR_HEIGHT' => $user_cache[$poster_id]['avatar'] ? $user_cache[$poster_id]['avatar']['height'] : 0, 'POSTER_WARNINGS' => $auth->acl_get('m_warn') ? $user_cache[$poster_id]['warnings'] : '', 'POSTER_AGE' => $user_cache[$poster_id]['age'], 'CONTACT_USER' => $user_cache[$poster_id]['contact_user'], From 34aaf5e6d29f189f5c86968b3b0e5a8400bcb92e Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Mon, 16 Dec 2019 20:24:29 +0100 Subject: [PATCH 2/6] [ticket/15233] Add avatar helper object PHPBB3-15233 --- .../default/container/services_avatar.yml | 10 + phpBB/phpbb/avatar/helper.php | 213 ++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 phpBB/phpbb/avatar/helper.php diff --git a/phpBB/config/default/container/services_avatar.yml b/phpBB/config/default/container/services_avatar.yml index 1cbc44115d..89dbb2e0bf 100644 --- a/phpBB/config/default/container/services_avatar.yml +++ b/phpBB/config/default/container/services_avatar.yml @@ -6,6 +6,16 @@ services: - '@dispatcher' - '@avatar.driver_collection' + avatar.helper: + class: phpbb\avatar\helper + arguments: + - '@config' + - '@dispatcher' + - '@language' + - '@avatar.manager' + - '@path_helper' + - '@user' + # ----- Avatar drivers ----- avatar.driver_collection: class: phpbb\di\service_collection diff --git a/phpBB/phpbb/avatar/helper.php b/phpBB/phpbb/avatar/helper.php new file mode 100644 index 0000000000..4c3f12b770 --- /dev/null +++ b/phpBB/phpbb/avatar/helper.php @@ -0,0 +1,213 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\avatar; + +class helper +{ + /** @var \phpbb\config\config */ + protected $config; + + /** @var \phpbb\event\dispatcher */ + protected $dispatcher; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\avatar\manager */ + protected $manager; + + /** @var \phpbb\path_helper */ + protected $path_helper; + + /** @var \phpbb\user */ + protected $user; + + /** + * Constructor. + * + * @param \phpbb\config\config $config Config object + * @param \phpbb\event\dispatcher $dispatcher Event dispatcher object + * @param \phpbb\language\language $language Language object + * @param \phpbb\avatar\manager $manager Avatar manager object + * @param \phpbb\path_helper $path_helper Path helper object + * @param \phpbb\user $user User object + */ + public function __construct( + \phpbb\config\config $config, + \phpbb\event\dispatcher $dispatcher, + \phpbb\language\language $language, + manager $manager, + \phpbb\path_helper $path_helper, + \phpbb\user $user + ) + { + $this->config = $config; + $this->dispatcher = $dispatcher; + $this->language = $language; + $this->manager = $manager; + $this->path_helper = $path_helper; + $this->user = $user; + } + + /** + * Get user avatar data. + * + * @param array $row The user's table row + * @param string $title Optional language string/key for the title + * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP + * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not + * @return array The avatar data array + */ + public function get_user_avatar(array $row, $title = 'USER_AVATAR', $ignore_config = false, $lazy = false) + { + $row = $this->manager->clean_row($row, 'user'); + + return $this->get_avatar($row, $title, $ignore_config, $lazy); + } + + /** + * Get group avatar data. + * + * @param array $row The group's table row + * @param string $title Optional language string/key for the title + * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP + * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not + * @return array The avatar data array + */ + public function get_group_avatar(array $row, $title = 'GROUP_AVATAR', $ignore_config = false, $lazy = false) + { + $row = $this->manager->clean_row($row, 'group'); + + return $this->get_avatar($row, $title, $ignore_config, $lazy); + } + + /** + * Get avatar data. + * + * @param array $row The cleaned table row + * @param string $title Optional language string/key for the title + * @param bool $ignore_config Ignores the config setting, to still be able to view the avatar in the UCP + * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not + * @return array The avatar data array + */ + public function get_avatar(array $row, $title, $ignore_config = false, $lazy = false) + { + if (!$this->config['allow_avatar'] && !$ignore_config) + { + return []; + } + + $data = [ + 'src' => $row['avatar'], + 'width' => $row['avatar_width'], + 'height' => $row['avatar_height'], + 'title' => $this->language->lang($title), + 'lazy' => $lazy, + 'type' => '', + 'html' => '', + ]; + + /** @var \phpbb\avatar\driver\driver_interface $driver */ + $driver = $this->manager->get_driver($row['avatar_type'], !$ignore_config); + + if ($driver !== null) + { + $data = array_merge($data, $driver->get_data($row), [ + 'type' => $driver->get_name(), + 'html' => $driver->get_custom_html($this->user, $row, $title), + ]); + + /** + * The type is used in the template to determine what driver is used, + * and potentially to add an additional class to the avatar element. + * + * While it's possible to str_replace('avatar.driver.', '', $data['type']) + * for all the core drivers, this will be awkward for extensions' avatar drivers. + * As they will most likely want to adjust the type in the event below, + * and then have to search for a different definition than they used in their services.yml + * + * For example, 'ext.vendor.avatar.driver.custom_driver' + * They will then have to look for: 'ext.vendor.custom_driver' + * + * So only remove 'avatar.driver.' if it is at the beginning of the driver's name. + */ + if (strpos($data['type'], 'avatar.driver.') === 0) + { + $data['type'] = substr($data['type'], strlen('avatar.driver.')); + } + } + else + { + $data['src'] = ''; + } + + if (empty($data['html']) && !empty($data['src'])) + { + $data['html'] = $this->get_avatar_html($data); + } + + /** + * Event to modify avatar data array + * + * @event core.avatar_helper_get_avatar + * @var array row The cleaned table row + * @var string title The language string/key for the title + * @var bool ignore_config Ignores the config setting, to still be able to view the avatar in the UCP + * @var bool lazy Indicator whether the avatar should be lazy loaded (requires JS) or not + * @var array data The avatar data array + * @since 4.0.0 + */ + $vars = ['row', 'title', 'ignore_config', 'lazy', 'data']; + extract($this->dispatcher->trigger_event('core.avatar_helper_get_avatar', compact($vars))); + + return $data; + } + + /** + * Get an avatar's HTML element. + * + * Created for Backwards Compatibility (BC). + * Styles should generate their own HTML element instead. + * + * @deprecated 4.0.0 + * + * @param array $data The avatar data array + * @return string The avatar's HTML element + */ + protected function get_avatar_html(array $data) + { + if ($data['lazy']) + { + /** + * We need to correct the phpBB root path in case this is called from a controller, + * because the web path will be incorrect otherwise. + */ + $board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH; + $web_path = $board_url ? generate_board_url() . '/' : $this->path_helper->get_web_root_path(); + $style_path = rawurlencode($this->user->style['style_path']); + + $theme = "{$web_path}styles/{$style_path}/theme"; + + $data['src'] = $theme . '/images/no_avatar.gif" data-src="' . $data['src']; + } + + $src = ' src="' . $data['src'] . '"'; + $alt = ' alt="' . $data['title'] . '"'; + + $width = $data['width'] ? ' width="' . $data['width'] . '"' : ''; + $height = $data['height'] ? ' height="' . $data['height'] . '"' : ''; + + return ''; + } +} From a9b63a47244ea16d29339b0454b88b58f50e84bb Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Tue, 17 Dec 2019 20:28:59 +0100 Subject: [PATCH 3/6] [ticket/15233] Amend tests for avatar helper PHPBB3-15233 --- phpBB/phpbb/avatar/helper.php | 2 +- phpBB/viewtopic.php | 2 +- tests/console/user/base.php | 6 +++++- tests/group/helper_test_case.php | 6 +++++- tests/notification/base.php | 5 ++++- tests/notification/group_request_test.php | 1 + tests/notification/submit_post_base.php | 6 +++++- tests/notification/user_list_trim_test.php | 6 +++++- tests/user/user_loader_test.php | 5 ++++- 9 files changed, 31 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/avatar/helper.php b/phpBB/phpbb/avatar/helper.php index 4c3f12b770..0638f1f69c 100644 --- a/phpBB/phpbb/avatar/helper.php +++ b/phpBB/phpbb/avatar/helper.php @@ -180,7 +180,7 @@ class helper * Created for Backwards Compatibility (BC). * Styles should generate their own HTML element instead. * - * @deprecated 4.0.0 + * @deprecated 4.1.0 After admin style is reworked aswell * * @param array $data The avatar data array * @return string The avatar's HTML element diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 7bfa29d70d..65289b041f 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -1484,7 +1484,7 @@ while ($row = $db->sql_fetchrow($result)) 'viewonline' => $row['user_allow_viewonline'], 'allow_pm' => $row['user_allow_pm'], - 'avatar' => ($user->optionget('viewavatars')) ? $this->get_user_avatar($row) : [], + 'avatar' => ($user->optionget('viewavatars')) ? $avatar_helper->get_user_avatar($row) : [], 'age' => '', 'rank_title' => '', diff --git a/tests/console/user/base.php b/tests/console/user/base.php index c4e454f6f7..c0eff42a9a 100644 --- a/tests/console/user/base.php +++ b/tests/console/user/base.php @@ -69,7 +69,11 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case )); $user->data['user_email'] = ''; - $this->user_loader = new \phpbb\user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE); + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); + + $this->user_loader = new \phpbb\user_loader($avatar_helper, $db, $phpbb_root_path, $phpEx, USERS_TABLE); $driver_helper = new \phpbb\passwords\driver\helper($this->config); $passwords_drivers = array( diff --git a/tests/group/helper_test_case.php b/tests/group/helper_test_case.php index c7abaea76e..f42dd13fa0 100644 --- a/tests/group/helper_test_case.php +++ b/tests/group/helper_test_case.php @@ -113,7 +113,11 @@ class phpbb_group_helper_test_case extends phpbb_test_case $user = new \phpbb\user($lang, '\phpbb\datetime'); $user->data['user_id'] = ANONYMOUS; - $this->group_helper = new \phpbb\group\helper($auth, $cache_service, $config, $lang, $phpbb_dispatcher, $path_helper, $user); + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); + + $this->group_helper = new \phpbb\group\helper($auth, $avatar_helper, $cache_service, $config, $lang, $phpbb_dispatcher, $path_helper, $user); } protected function setUp(): void diff --git a/tests/notification/base.php b/tests/notification/base.php index 42b0febfbe..59c7956ee8 100644 --- a/tests/notification/base.php +++ b/tests/notification/base.php @@ -63,6 +63,9 @@ abstract class phpbb_tests_notification_base extends phpbb_database_test_case global $db, $config, $user, $auth, $cache, $phpbb_container; + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); $db = $this->db = $this->new_dbal(); $config = $this->config = new \phpbb\config\config(array( 'allow_privmsg' => true, @@ -77,7 +80,7 @@ abstract class phpbb_tests_notification_base extends phpbb_database_test_case $user->data['user_id'] = 0; $user->data['user_type'] = USER_NORMAL; $this->user = $user; - $this->user_loader = new \phpbb\user_loader($this->db, $phpbb_root_path, $phpEx, 'phpbb_users'); + $this->user_loader = new \phpbb\user_loader($avatar_helper, $this->db, $phpbb_root_path, $phpEx, 'phpbb_users'); $auth = $this->auth = new phpbb_mock_notifications_auth(); $cache_driver = new \phpbb\cache\driver\dummy(); $cache = $this->cache = new \phpbb\cache\service( diff --git a/tests/notification/group_request_test.php b/tests/notification/group_request_test.php index 9a082f08a4..d523c80f34 100644 --- a/tests/notification/group_request_test.php +++ b/tests/notification/group_request_test.php @@ -48,6 +48,7 @@ class phpbb_notification_group_request_test extends phpbb_tests_notification_bas )); $this->container->set('group_helper', new \phpbb\group\helper( $this->getMockBuilder('\phpbb\auth\auth')->disableOriginalConstructor()->getMock(), + $this->getMockBuilder('\phpbb\avatar\helper')->disableOriginalConstructor()->getMock(), $this->cache, $this->config, new \phpbb\language\language( diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index 26be8f7da9..2d17b601a2 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -112,8 +112,12 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c $type_cast_helper = $this->createMock('\phpbb\request\type_cast_helper_interface'); $request = $this->createMock('\phpbb\request\request'); + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); - $user_loader = new \phpbb\user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE); + $user_loader = new \phpbb\user_loader($avatar_helper, $db, $phpbb_root_path, $phpEx, USERS_TABLE); // Container $phpbb_container = new ContainerBuilder(); diff --git a/tests/notification/user_list_trim_test.php b/tests/notification/user_list_trim_test.php index 4ddfcb82cd..2704b36c01 100644 --- a/tests/notification/user_list_trim_test.php +++ b/tests/notification/user_list_trim_test.php @@ -57,7 +57,11 @@ class phpbb_notification_user_list_trim_test extends phpbb_database_test_case ]; $lang->add_lang('common'); - $user_loader = new phpbb\user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE); + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); + + $user_loader = new phpbb\user_loader($avatar_helper, $db, $phpbb_root_path, $phpEx, USERS_TABLE); $user_loader->load_users(array(2, 3, 4, 5, 6)); $this->notification = new phpbb_mock_notification_type_post( diff --git a/tests/user/user_loader_test.php b/tests/user/user_loader_test.php index dd00d6eb06..ec1d4e588a 100644 --- a/tests/user/user_loader_test.php +++ b/tests/user/user_loader_test.php @@ -25,8 +25,11 @@ class phpbb_user_loader_test extends phpbb_database_test_case { parent::setUp(); + $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') + ->disableOriginalConstructor() + ->getMock(); $this->db = $this->new_dbal(); - $this->user_loader = new \phpbb\user_loader($this->db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users'); + $this->user_loader = new \phpbb\user_loader($avatar_helper, $this->db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users'); } public function test_load_get() From 3796a4207f3ed422795505538e3b41c40f64debf Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Tue, 17 Dec 2019 20:37:10 +0100 Subject: [PATCH 4/6] [ticket/15233] Normalize empty avatar array PHPBB3-15233 --- phpBB/phpbb/avatar/helper.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/avatar/helper.php b/phpBB/phpbb/avatar/helper.php index 0638f1f69c..2780fe3c57 100644 --- a/phpBB/phpbb/avatar/helper.php +++ b/phpBB/phpbb/avatar/helper.php @@ -105,7 +105,15 @@ class helper { if (!$this->config['allow_avatar'] && !$ignore_config) { - return []; + return [ + 'html' => '', + 'lazy' => false, + 'src' => '', + 'title' => '', + 'type' => '', + 'width' => 0, + 'height' => 0, + ]; } $data = [ From d77415dbab6ed9b934f4213af1ec5183d08fb241 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 10 May 2020 14:58:57 +0200 Subject: [PATCH 5/6] [ticket/15233] Standardize variable generation PHPBB3-15233 --- phpBB/adm/style/acp_groups.html | 2 +- phpBB/adm/style/acp_users_avatar.html | 2 +- phpBB/includes/acp/acp_groups.php | 15 +-- phpBB/includes/acp/acp_users.php | 15 +-- phpBB/includes/functions.php | 9 +- phpBB/includes/functions_display.php | 13 +- phpBB/includes/mcp/mcp_notes.php | 34 +++-- phpBB/includes/mcp/mcp_warn.php | 16 +-- phpBB/includes/ucp/ucp_groups.php | 15 +-- phpBB/includes/ucp/ucp_pm_viewmessage.php | 16 +-- phpBB/includes/ucp/ucp_profile.php | 10 +- phpBB/memberlist.php | 16 +-- phpBB/phpbb/avatar/helper.php | 116 ++++++++++++------ phpBB/phpbb/notification/type/base.php | 8 +- .../prosilver/template/mcp_notes_user.html | 2 +- .../prosilver/template/mcp_warn_post.html | 2 +- .../prosilver/template/mcp_warn_user.html | 2 +- .../prosilver/template/memberlist_body.html | 2 +- .../prosilver/template/memberlist_view.html | 6 +- .../prosilver/template/navbar_header.html | 4 +- .../template/notification_dropdown.html | 2 +- .../template/ucp_avatar_options.html | 2 +- .../prosilver/template/ucp_notifications.html | 2 +- .../template/ucp_pm_viewmessage.html | 4 +- .../prosilver/template/viewtopic_body.html | 6 +- phpBB/viewtopic.php | 12 +- 26 files changed, 163 insertions(+), 170 deletions(-) diff --git a/phpBB/adm/style/acp_groups.html b/phpBB/adm/style/acp_groups.html index 8651b63b7f..8d53ae459c 100644 --- a/phpBB/adm/style/acp_groups.html +++ b/phpBB/adm/style/acp_groups.html @@ -113,7 +113,7 @@ {L_GROUP_AVATAR}

{L_AVATAR_EXPLAIN}
-
{AVATAR}
+
{% if AVATAR_HTML %}{{ AVATAR_HTML }}{% else %}{% endif %}
diff --git a/phpBB/adm/style/acp_users_avatar.html b/phpBB/adm/style/acp_users_avatar.html index 8466985fb3..5e3fb04bee 100644 --- a/phpBB/adm/style/acp_users_avatar.html +++ b/phpBB/adm/style/acp_users_avatar.html @@ -5,7 +5,7 @@

{ERROR}


{L_AVATAR_EXPLAIN}
-
{AVATAR}
+
{% if AVATAR_HTML %}{{ AVATAR_HTML }}{% else %}{% endif %}
diff --git a/phpBB/includes/acp/acp_groups.php b/phpBB/includes/acp/acp_groups.php index 8cb8e778b7..b24014cb39 100644 --- a/phpBB/includes/acp/acp_groups.php +++ b/phpBB/includes/acp/acp_groups.php @@ -721,8 +721,6 @@ class acp_groups } } - $group_avatar = $group_helper->get_avatar($group_row, 'GROUP_AVATAR', true); - if (isset($phpbb_avatar_manager) && !$update) { // Merge any avatar errors into the primary error array @@ -742,6 +740,12 @@ class acp_groups break; } + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $group_avatar = $avatar_helper->get_group_avatar($group_row, 'GROUP_AVATAR', true); + $template->assign_vars($avatar_helper->get_template_vars($group_avatar)); + $template->assign_vars(array( 'S_EDIT' => true, 'S_ADD_GROUP' => ($action == 'add') ? true : false, @@ -771,13 +775,6 @@ class acp_groups 'S_RANK_OPTIONS' => $rank_options, 'S_GROUP_OPTIONS' => group_select_options(false, false, (($user->data['user_type'] == USER_FOUNDER) ? false : 0)), - 'AVATAR' => empty($group_avatar['html']) ? '' : $group_avatar['html'], - 'AVATAR_LAZY' => $group_avatar['lazy'], - 'AVATAR_SRC' => $group_avatar['src'], - 'AVATAR_TITLE' => $group_avatar['title'], - 'AVATAR_TYPE' => $group_avatar['type'], - 'AVATAR_WIDTH' => $group_avatar['width'], - 'AVATAR_HEIGHT' => $group_avatar['height'], 'AVATAR_MAX_FILESIZE' => $config['avatar_filesize'], 'GROUP_TYPE_FREE' => GROUP_FREE, diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index e3045ffe19..4a041886b7 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1972,24 +1972,17 @@ class acp_users $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($user_row, 'USER_AVATAR', true); + $template->assign_vars($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( - 'S_AVATAR' => true, - 'ERROR' => (!empty($error)) ? implode('
', $error) : '', - - 'AVATAR' => empty($avatar['html']) ? '' : $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], + 'S_AVATAR' => true, + 'ERROR' => !empty($error) ? implode('
', $error) : '', 'S_FORM_ENCTYPE' => ' enctype="multipart/form-data"', 'L_AVATAR_EXPLAIN' => $user->lang(($config['avatar_filesize'] == 0) ? 'AVATAR_EXPLAIN_NO_FILESIZE' : 'AVATAR_EXPLAIN', $config['avatar_max_width'], $config['avatar_max_height'], $config['avatar_filesize'] / 1024), - 'S_AVATARS_ENABLED' => ($config['allow_avatar'] && $avatars_enabled), + 'S_AVATARS_ENABLED' => ($config['allow_avatar'] && $avatars_enabled), )); break; diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index f8122c4272..0f3ec942be 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3872,6 +3872,7 @@ function page_header($page_title = '', $display_online_list = false, $item_id = $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($user->data); + $template->assign_vars($avatar_helper->get_template_vars($avatar, 'CURRENT_USER_')); // The following assigns all _common_ variables that may be used at any point in a template. $template->assign_vars(array( @@ -3886,14 +3887,8 @@ function page_header($page_title = '', $display_online_list = false, $item_id = 'LOGGED_IN_USER_LIST' => $online_userlist, 'RECORD_USERS' => $l_online_record, + 'NO_AVATAR_SOURCE' => $avatar_helper->get_no_avatar_source(), 'PRIVATE_MESSAGE_COUNT' => (!empty($user->data['user_unread_privmsg'])) ? $user->data['user_unread_privmsg'] : 0, - 'CURRENT_USER_AVATAR' => $avatar['html'], - 'CURRENT_USER_AVATAR_LAZY' => $avatar['lazy'], - 'CURRENT_USER_AVATAR_SOURCE' => $avatar['src'], - 'CURRENT_USER_AVATAR_TITLE' => $avatar['title'], - 'CURRENT_USER_AVATAR_TYPE' => $avatar['type'], - 'CURRENT_USER_AVATAR_WIDTH' => $avatar['width'], - 'CURRENT_USER_AVATAR_HEIGHT' => $avatar['height'], 'CURRENT_USERNAME_SIMPLE' => get_username_string('no_profile', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), 'CURRENT_USERNAME_FULL' => get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), 'CURRENT_USER_GROUP_COLOR' => $user->data['user_colour'], diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php index b4ab8a9c7a..cec42ddfd9 100644 --- a/phpBB/includes/functions_display.php +++ b/phpBB/includes/functions_display.php @@ -1657,9 +1657,10 @@ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabl $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($data); + $avatar_vars = $avatar_helper->get_template_vars($avatar); // Dump it out to the template - $template_data = array( + $template_data = array_merge($avatar_vars, [ 'AGE' => $age, 'RANK_TITLE' => $user_rank_data['title'], 'JOINED' => $user->format_date($data['user_regdate']), @@ -1674,14 +1675,6 @@ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabl 'A_USERNAME' => addslashes(get_username_string('username', $user_id, $username, $data['user_colour'])), - 'AVATAR_IMG' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], - 'ONLINE_IMG' => (!$config['load_onlinetrack']) ? '' : (($online) ? $user->img('icon_user_online', 'ONLINE') : $user->img('icon_user_offline', 'OFFLINE')), 'S_ONLINE' => ($config['load_onlinetrack'] && $online) ? true : false, 'RANK_IMG' => $user_rank_data['img'], @@ -1703,7 +1696,7 @@ function phpbb_show_profile($data, $user_notes_enabled = false, $warn_user_enabl 'L_SEND_EMAIL_USER' => $user->lang('SEND_EMAIL_USER', $username), 'L_CONTACT_USER' => $user->lang('CONTACT_USER', $username), 'L_VIEWING_PROFILE' => $user->lang('VIEWING_PROFILE', $username), - ); + ]); /** * Preparing a user's data before displaying it in profile and memberlist diff --git a/phpBB/includes/mcp/mcp_notes.php b/phpBB/includes/mcp/mcp_notes.php index 4690790dab..5ba6999d2c 100644 --- a/phpBB/includes/mcp/mcp_notes.php +++ b/phpBB/includes/mcp/mcp_notes.php @@ -185,19 +185,6 @@ class mcp_notes trigger_error($msg . '

' . sprintf($user->lang['RETURN_PAGE'], '', '')); } - if (!function_exists('phpbb_get_user_rank')) - { - include($phpbb_root_path . 'includes/functions_display.' . $phpEx); - } - - // Generate the appropriate user information for the user we are looking at - $rank_data = phpbb_get_user_rank($userrow, $userrow['user_posts']); - - /** @var \phpbb\avatar\helper $avatar_helper */ - $avatar_helper = $phpbb_container->get('avatar.helper'); - - $avatar = $avatar_helper->get_user_avatar($userrow); - $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']); $sort_by_text = array('a' => $user->lang['SORT_USERNAME'], 'b' => $user->lang['SORT_DATE'], 'c' => $user->lang['SORT_IP'], 'd' => $user->lang['SORT_ACTION']); $sort_by_sql = array('a' => 'u.username_clean', 'b' => 'l.log_time', 'c' => 'l.log_ip', 'd' => 'l.log_operation'); @@ -235,6 +222,20 @@ class mcp_notes $base_url = $this->u_action . "&$u_sort_param$keywords_param"; $pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, $config['topics_per_page'], $start); + if (!function_exists('phpbb_get_user_rank')) + { + include($phpbb_root_path . 'includes/functions_display.' . $phpEx); + } + + // Generate the appropriate user information for the user we are looking at + $rank_data = phpbb_get_user_rank($userrow, $userrow['user_posts']); + + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar = $avatar_helper->get_user_avatar($userrow); + $template->assign_vars($avatar_helper->get_template_vars($avatar)); + $template->assign_vars(array( 'U_POST_ACTION' => $this->u_action, 'S_CLEAR_ALLOWED' => ($auth->acl_get('a_clearlogs')) ? true : false, @@ -256,13 +257,6 @@ class mcp_notes 'USERNAME' => get_username_string('username', $userrow['user_id'], $userrow['username'], $userrow['user_colour']), 'U_PROFILE' => get_username_string('profile', $userrow['user_id'], $userrow['username'], $userrow['user_colour']), - 'AVATAR_IMG' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $rank_data['img'], 'RANK_TITLE' => $rank_data['title'], )); diff --git a/phpBB/includes/mcp/mcp_warn.php b/phpBB/includes/mcp/mcp_warn.php index 1e146bb004..f22e3f2c5e 100644 --- a/phpBB/includes/mcp/mcp_warn.php +++ b/phpBB/includes/mcp/mcp_warn.php @@ -347,6 +347,7 @@ class mcp_warn $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($user_row); + $template->assign_vars($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( 'U_POST_ACTION' => $this->u_action, @@ -359,13 +360,6 @@ class mcp_warn 'POSTS' => ($user_row['user_posts']) ? $user_row['user_posts'] : 0, 'WARNINGS' => ($user_row['user_warnings']) ? $user_row['user_warnings'] : 0, - 'AVATAR_IMG' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $user_rank_data['img'], 'L_WARNING_POST_DEFAULT' => sprintf($user->lang['WARNING_POST_DEFAULT'], generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&p=$post_id#p$post_id"), @@ -506,6 +500,7 @@ class mcp_warn $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($user_row); + $template->assign_vars($avatar_helper->get_template_vars($avatar)); // OK, they didn't submit a warning so lets build the page for them to do so $template->assign_vars(array( @@ -521,13 +516,6 @@ class mcp_warn 'USERNAME' => get_username_string('username', $user_row['user_id'], $user_row['username'], $user_row['user_colour']), 'U_PROFILE' => get_username_string('profile', $user_row['user_id'], $user_row['username'], $user_row['user_colour']), - 'AVATAR_IMG' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], 'RANK_IMG' => $user_rank_data['img'], 'S_CAN_NOTIFY' => $s_can_notify, diff --git a/phpBB/includes/ucp/ucp_groups.php b/phpBB/includes/ucp/ucp_groups.php index 92c9ee6078..dd14f9072a 100644 --- a/phpBB/includes/ucp/ucp_groups.php +++ b/phpBB/includes/ucp/ucp_groups.php @@ -431,7 +431,11 @@ class ucp_groups $group_name = $group_row['group_name']; $group_type = $group_row['group_type']; - $group_avatar = $group_helper->get_avatar($group_row, 'GROUP_AVATAR', true); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $group_avatar = $avatar_helper->get_group_avatar($group_row, 'GROUP_AVATAR', true); + $template->assign_vars($avatar_helper->get_template_vars($group_avatar)); $template->assign_vars(array( 'GROUP_NAME' => $group_helper->get_name($group_name), @@ -439,15 +443,6 @@ class ucp_groups 'GROUP_COLOUR' => (isset($group_row['group_colour'])) ? $group_row['group_colour'] : '', 'GROUP_DESC_DISP' => generate_text_for_display($group_row['group_desc'], $group_row['group_desc_uid'], $group_row['group_desc_bitfield'], $group_row['group_desc_options']), 'GROUP_TYPE' => $group_row['group_type'], - - 'AVATAR' => empty($group_avatar['html']) ? '' : $group_avatar['html'], - 'AVATAR_IMAGE' => empty($group_avatar['html']) ? '' : $group_avatar['html'], - 'AVATAR_LAZY' => $group_avatar['lazy'], - 'AVATAR_SRC' => $group_avatar['src'], - 'AVATAR_TITLE' => $group_avatar['title'], - 'AVATAR_TYPE' => $group_avatar['type'], - 'AVATAR_WIDTH' => $group_avatar['width'], - 'AVATAR_HEIGHT' => $group_avatar['height'], )); } diff --git a/phpBB/includes/ucp/ucp_pm_viewmessage.php b/phpBB/includes/ucp/ucp_pm_viewmessage.php index d1c5b16b4c..b2f0bc5344 100644 --- a/phpBB/includes/ucp/ucp_pm_viewmessage.php +++ b/phpBB/includes/ucp/ucp_pm_viewmessage.php @@ -221,13 +221,6 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) 'RANK_TITLE' => $user_info['rank_title'], 'RANK_IMG' => $user_info['rank_image'], - 'AUTHOR_AVATAR' => !empty($user_info['avatar']) ? $user_info['avatar']['html'] : '', - 'AUTHOR_AVATAR_LAZY' => !empty($user_info['avatar']) ? $user_info['avatar']['lazy'] : false, - 'AUTHOR_AVATAR_SOURCE' => !empty($user_info['avatar']) ? $user_info['avatar']['src'] : '', - 'AUTHOR_AVATAR_TITLE' => !empty($user_info['avatar']) ? $user_info['avatar']['title'] : '', - 'AUTHOR_AVATAR_TYPE' => !empty($user_info['avatar']) ? $user_info['avatar']['type'] : '', - 'AUTHOR_AVATAR_WIDTH' => !empty($user_info['avatar']) ? $user_info['avatar']['width'] : 0, - 'AUTHOR_AVATAR_HEIGHT' => !empty($user_info['avatar']) ? $user_info['avatar']['height'] : 0, 'AUTHOR_JOINED' => $user->format_date($user_info['user_regdate']), 'AUTHOR_POSTS' => (int) $user_info['user_posts'], 'U_AUTHOR_POSTS' => ($config['load_search'] && $auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$author_id&sr=posts") : '', @@ -279,6 +272,15 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) 'U_FORWARD_PM' => ($config['forward_pm'] && $auth->acl_get('u_sendpm') && $auth->acl_get('u_pm_forward')) ? "$url&mode=compose&action=forward&f=$folder_id&p=" . $message_row['msg_id'] : '', ); + if (!empty($user_info['avatar'])) + { + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $avatar_data = $avatar_helper->get_template_vars($user_info['avatar'], 'AUTHOR_'); + $msg_data = array_merge($msg_data, $avatar_data); + } + /** * Modify pm and sender data before it is assigned to the template * diff --git a/phpBB/includes/ucp/ucp_profile.php b/phpBB/includes/ucp/ucp_profile.php index 2394cc5667..51ab5eddac 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -761,16 +761,10 @@ class ucp_profile $avatar_helper = $phpbb_container->get('avatar.helper'); $avatar = $avatar_helper->get_user_avatar($user->data, 'USER_AVATAR', true); + $template->assign_vars($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( - 'ERROR' => (count($error)) ? implode('
', $error) : '', - 'AVATAR' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SOURCE' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], + 'ERROR' => !empty($error) ? implode('
', $error) : '', 'S_FORM_ENCTYPE' => ' enctype="multipart/form-data"', diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index b5122b93ab..3140ba22b3 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -1290,8 +1290,6 @@ switch ($mode) break; } - $avatar = $group_helper->get_avatar($group_row); - // ... same for group rank $group_rank_data = array( 'title' => null, @@ -1331,6 +1329,12 @@ switch ($mode) 'U_BREADCRUMB' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=group&g=$group_id"), )); + /** @var \phpbb\avatar\helper $avatar_helper */ + $avatar_helper = $phpbb_container->get('avatar.helper'); + + $group_avatar = $avatar_helper->get_group_avatar($group_row); + $template->assign_vars($avatar_helper->get_template_vars($group_avatar)); + $template->assign_vars(array( 'GROUP_DESC' => generate_text_for_display($group_row['group_desc'], $group_row['group_desc_uid'], $group_row['group_desc_bitfield'], $group_row['group_desc_options']), 'GROUP_NAME' => $group_helper->get_name($group_row['group_name']), @@ -1338,14 +1342,6 @@ switch ($mode) 'GROUP_TYPE' => $user->lang['GROUP_IS_' . $group_row['l_group_type']], 'GROUP_RANK' => $group_rank_data['title'], - 'AVATAR_IMG' => $avatar['html'], - 'AVATAR_LAZY' => $avatar['lazy'], - 'AVATAR_SRC' => $avatar['src'], - 'AVATAR_TITLE' => $avatar['title'], - 'AVATAR_TYPE' => $avatar['type'], - 'AVATAR_WIDTH' => $avatar['width'], - 'AVATAR_HEIGHT' => $avatar['height'], - 'RANK_IMG' => $group_rank_data['img'], 'RANK_IMG_SRC' => $group_rank_data['img_src'], diff --git a/phpBB/phpbb/avatar/helper.php b/phpBB/phpbb/avatar/helper.php index 2780fe3c57..a0f9b6e99a 100644 --- a/phpBB/phpbb/avatar/helper.php +++ b/phpBB/phpbb/avatar/helper.php @@ -13,43 +13,55 @@ namespace phpbb\avatar; +use phpbb\avatar\driver\driver_interface; +use phpbb\config\config; +use phpbb\event\dispatcher; +use phpbb\language\language; +use phpbb\path_helper; +use phpbb\user; + +/** + * Avatar helper object. + * + * Generates avatars and their variables for display. + */ class helper { - /** @var \phpbb\config\config */ + /** @var config */ protected $config; - /** @var \phpbb\event\dispatcher */ + /** @var dispatcher */ protected $dispatcher; - /** @var \phpbb\language\language */ + /** @var language */ protected $language; - /** @var \phpbb\avatar\manager */ + /** @var manager */ protected $manager; - /** @var \phpbb\path_helper */ + /** @var path_helper */ protected $path_helper; - /** @var \phpbb\user */ + /** @var user */ protected $user; /** * Constructor. * - * @param \phpbb\config\config $config Config object - * @param \phpbb\event\dispatcher $dispatcher Event dispatcher object - * @param \phpbb\language\language $language Language object - * @param \phpbb\avatar\manager $manager Avatar manager object - * @param \phpbb\path_helper $path_helper Path helper object - * @param \phpbb\user $user User object + * @param config $config Config object + * @param dispatcher $dispatcher Event dispatcher object + * @param language $language Language object + * @param manager $manager Avatar manager object + * @param path_helper $path_helper Path helper object + * @param user $user User object */ public function __construct( - \phpbb\config\config $config, - \phpbb\event\dispatcher $dispatcher, - \phpbb\language\language $language, + config $config, + dispatcher $dispatcher, + language $language, manager $manager, - \phpbb\path_helper $path_helper, - \phpbb\user $user + path_helper $path_helper, + user $user ) { $this->config = $config; @@ -60,6 +72,32 @@ class helper $this->user = $user; } + /** + * Get an avatar's template variables. + * + * @param array $avatar The avatar's data + * @param string $prefix The variables' prefix + * @return array The avatar's template variables + */ + public function get_template_vars(array $avatar, string $prefix = ''): array + { + $prefix = $prefix && substr($prefix, -1) !== '_' ? "{$prefix}_" : $prefix; + + return [ + "{$prefix}AVATAR" => $avatar, + + "{$prefix}AVATAR_SOURCE" => $avatar['src'], + "{$prefix}AVATAR_TITLE" => $avatar['title'], + "{$prefix}AVATAR_TYPE" => $avatar['type'], + + "{$prefix}AVATAR_WIDTH" => $avatar['width'], + "{$prefix}AVATAR_HEIGHT" => $avatar['height'], + + "{$prefix}AVATAR_LAZY" => $avatar['lazy'], + "{$prefix}AVATAR_HTML" => $avatar['html'], + ]; + } + /** * Get user avatar data. * @@ -69,9 +107,9 @@ class helper * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not * @return array The avatar data array */ - public function get_user_avatar(array $row, $title = 'USER_AVATAR', $ignore_config = false, $lazy = false) + public function get_user_avatar(array $row, string $title = 'USER_AVATAR', bool $ignore_config = false, bool $lazy = false): array { - $row = $this->manager->clean_row($row, 'user'); + $row = manager::clean_row($row, 'user'); return $this->get_avatar($row, $title, $ignore_config, $lazy); } @@ -85,9 +123,9 @@ class helper * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not * @return array The avatar data array */ - public function get_group_avatar(array $row, $title = 'GROUP_AVATAR', $ignore_config = false, $lazy = false) + public function get_group_avatar(array $row, string $title = 'GROUP_AVATAR', bool $ignore_config = false, bool $lazy = false): array { - $row = $this->manager->clean_row($row, 'group'); + $row = manager::clean_row($row, 'group'); return $this->get_avatar($row, $title, $ignore_config, $lazy); } @@ -101,7 +139,7 @@ class helper * @param bool $lazy Indicator whether the avatar should be lazy loaded (requires JS) or not * @return array The avatar data array */ - public function get_avatar(array $row, $title, $ignore_config = false, $lazy = false) + public function get_avatar(array $row, string $title, bool $ignore_config = false, bool $lazy = false): array { if (!$this->config['allow_avatar'] && !$ignore_config) { @@ -126,7 +164,7 @@ class helper 'html' => '', ]; - /** @var \phpbb\avatar\driver\driver_interface $driver */ + /** @var driver_interface $driver */ $driver = $this->manager->get_driver($row['avatar_type'], !$ignore_config); if ($driver !== null) @@ -182,6 +220,24 @@ class helper return $data; } + /** + * Get the "no avatar" source string. + * + * @return string The "no avatar" source string + */ + public function get_no_avatar_source(): string + { + /** + * We need to correct the phpBB root path in case this is called from a controller, + * because the web path will be incorrect otherwise. + */ + $board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH; + $web_path = $board_url ? generate_board_url() . '/' : $this->path_helper->get_web_root_path(); + $style_path = rawurlencode($this->user->style['style_path']); + + return "{$web_path}styles/{$style_path}/theme/images/no_avatar.gif"; + } + /** * Get an avatar's HTML element. * @@ -193,21 +249,11 @@ class helper * @param array $data The avatar data array * @return string The avatar's HTML element */ - protected function get_avatar_html(array $data) + private function get_avatar_html(array $data): string { if ($data['lazy']) { - /** - * We need to correct the phpBB root path in case this is called from a controller, - * because the web path will be incorrect otherwise. - */ - $board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH; - $web_path = $board_url ? generate_board_url() . '/' : $this->path_helper->get_web_root_path(); - $style_path = rawurlencode($this->user->style['style_path']); - - $theme = "{$web_path}styles/{$style_path}/theme"; - - $data['src'] = $theme . '/images/no_avatar.gif" data-src="' . $data['src']; + $data['src'] = $this->get_no_avatar_source() . ' data-src="' . $data['src']; } $src = ' src="' . $data['src'] . '"'; diff --git a/phpBB/phpbb/notification/type/base.php b/phpBB/phpbb/notification/type/base.php index e59c5b9638..5c1a6137ec 100644 --- a/phpBB/phpbb/notification/type/base.php +++ b/phpBB/phpbb/notification/type/base.php @@ -297,14 +297,16 @@ abstract class base implements \phpbb\notification\type\type_interface 'TIME' => $this->user->format_date($this->notification_time), 'UNREAD' => !$this->notification_read, - 'AVATAR' => $avatar ? $avatar['html'] : '', - 'AVATAR_LAZY' => $avatar ? $avatar['lazy'] : true, - 'AVATAR_SRC' => $avatar ? $avatar['src'] : '', + 'AVATAR_SOURCE' => $avatar ? $avatar['src'] : '', 'AVATAR_TITLE' => $avatar ? $avatar['title'] : '', 'AVATAR_TYPE' => $avatar ? $avatar['type'] : '', + 'AVATAR_WIDTH' => $avatar ? $avatar['width'] : 0, 'AVATAR_HEIGHT' => $avatar ? $avatar['height'] : 0, + 'AVATAR_HTML' => $avatar ? $avatar['html'] : '', + 'AVATAR_LAZY' => $avatar ? $avatar['lazy'] : true, + 'U_MARK_READ' => (!$this->notification_read) ? $u_mark_read : '', ]; } diff --git a/phpBB/styles/prosilver/template/mcp_notes_user.html b/phpBB/styles/prosilver/template/mcp_notes_user.html index 448021eb8f..83c3485356 100644 --- a/phpBB/styles/prosilver/template/mcp_notes_user.html +++ b/phpBB/styles/prosilver/template/mcp_notes_user.html @@ -11,7 +11,7 @@
-
{AVATAR_IMG}
+
{AVATAR_HTML}
diff --git a/phpBB/styles/prosilver/template/mcp_warn_post.html b/phpBB/styles/prosilver/template/mcp_warn_post.html index 11395eea59..0e37e6bd9a 100644 --- a/phpBB/styles/prosilver/template/mcp_warn_post.html +++ b/phpBB/styles/prosilver/template/mcp_warn_post.html @@ -11,7 +11,7 @@
-
{AVATAR_IMG}
+
{AVATAR_HTML}
diff --git a/phpBB/styles/prosilver/template/mcp_warn_user.html b/phpBB/styles/prosilver/template/mcp_warn_user.html index 022763ba82..b1c24c18e9 100644 --- a/phpBB/styles/prosilver/template/mcp_warn_user.html +++ b/phpBB/styles/prosilver/template/mcp_warn_user.html @@ -11,7 +11,7 @@
-
{AVATAR_IMG}
+
{AVATAR_HTML}
diff --git a/phpBB/styles/prosilver/template/memberlist_body.html b/phpBB/styles/prosilver/template/memberlist_body.html index 551c168d1e..402917befe 100644 --- a/phpBB/styles/prosilver/template/memberlist_body.html +++ b/phpBB/styles/prosilver/template/memberlist_body.html @@ -26,7 +26,7 @@ {% EVENT memberlist_body_group_desc_after %}

- {AVATAR_IMG} + {AVATAR_HTML} {% EVENT memberlist_body_group_rank_before %} {% if RANK_IMG %}{{ RANK_IMG }}{% endif %} {% if GROUP_RANK %} diff --git a/phpBB/styles/prosilver/template/memberlist_view.html b/phpBB/styles/prosilver/template/memberlist_view.html index 69988e57f2..c5d2794ce4 100644 --- a/phpBB/styles/prosilver/template/memberlist_view.html +++ b/phpBB/styles/prosilver/template/memberlist_view.html @@ -8,9 +8,9 @@

- +
-
{AVATAR_IMG}
+
{AVATAR_HTML}
{RANK_TITLE}
{RANK_IMG}
@@ -29,7 +29,7 @@ [ {L_USER_BAN} ] [ {L_USE_PERMISSIONS} ] - +
{L_RANK}{L_COLON}
{RANK_TITLE}
 {L_RANK}{L_COLON}
{RANK_IMG}
diff --git a/phpBB/styles/prosilver/template/navbar_header.html b/phpBB/styles/prosilver/template/navbar_header.html index 88807f9c16..eaf61f2518 100644 --- a/phpBB/styles/prosilver/template/navbar_header.html +++ b/phpBB/styles/prosilver/template/navbar_header.html @@ -106,11 +106,11 @@ {% if S_REGISTERED_USER %} {% EVENT navbar_header_user_profile_prepend %} -
  • +
  • {% EVENT navbar_header_username_prepend %}