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/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 9c0ba58a19..b8fc7fa755 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -124,6 +124,7 @@ services: class: phpbb\group\helper arguments: - '@auth' + - '@avatar.helper' - '@cache' - '@config' - '@language' 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/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..b24014cb39 100644 --- a/phpBB/includes/acp/acp_groups.php +++ b/phpBB/includes/acp/acp_groups.php @@ -721,8 +721,6 @@ class acp_groups } } - $avatar = phpbb_get_group_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,10 +775,7 @@ 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_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..4a041886b7 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1968,18 +1968,21 @@ 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($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( - 'S_AVATAR' => true, - 'ERROR' => (!empty($error)) ? implode('
', $error) : '', - 'AVATAR' => (empty($avatar) ? '' : $avatar), + '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 db6704ce86..0f3ec942be 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,12 @@ 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); + $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( 'SITENAME' => $config['sitename'], @@ -3875,8 +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' => phpbb_get_user_avatar($user->data), '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..cec42ddfd9 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,8 +1653,14 @@ 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); + $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']), @@ -1669,7 +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' => phpbb_get_user_avatar($data), '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'], @@ -1691,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 47dc97cc8b..5ba6999d2c 100644 --- a/phpBB/includes/mcp/mcp_notes.php +++ b/phpBB/includes/mcp/mcp_notes.php @@ -185,15 +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']); - $avatar_img = phpbb_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'); @@ -231,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, @@ -252,10 +257,8 @@ 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, '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..f22e3f2c5e 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,12 @@ 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($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( 'U_POST_ACTION' => $this->u_action, @@ -355,7 +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_img, '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 +374,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 +495,12 @@ 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); + $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( @@ -507,7 +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_img, '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..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']; - $avatar = phpbb_get_group_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,11 +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($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'] : '', )); } diff --git a/phpBB/includes/ucp/ucp_pm_viewmessage.php b/phpBB/includes/ucp/ucp_pm_viewmessage.php index 6657aa4fd2..b2f0bc5344 100644 --- a/phpBB/includes/ucp/ucp_pm_viewmessage.php +++ b/phpBB/includes/ucp/ucp_pm_viewmessage.php @@ -221,7 +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' => (isset($user_info['avatar'])) ? $user_info['avatar'] : '', '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") : '', @@ -273,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 * @@ -409,7 +417,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 +457,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..51ab5eddac 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -757,11 +757,14 @@ 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($avatar_helper->get_template_vars($avatar)); $template->assign_vars(array( - 'ERROR' => (count($error)) ? implode('
', $error) : '', - 'AVATAR' => $avatar, + 'ERROR' => !empty($error) ? implode('
', $error) : '', 'S_FORM_ENCTYPE' => ' enctype="multipart/form-data"', diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index fb4f82f38c..3140ba22b3 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -1290,8 +1290,6 @@ switch ($mode) break; } - $avatar_img = phpbb_get_group_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,7 +1342,6 @@ switch ($mode) 'GROUP_TYPE' => $user->lang['GROUP_IS_' . $group_row['l_group_type']], 'GROUP_RANK' => $group_rank_data['title'], - 'AVATAR_IMG' => $avatar_img, '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 new file mode 100644 index 0000000000..a0f9b6e99a --- /dev/null +++ b/phpBB/phpbb/avatar/helper.php @@ -0,0 +1,267 @@ + + * @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; + +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 config */ + protected $config; + + /** @var dispatcher */ + protected $dispatcher; + + /** @var language */ + protected $language; + + /** @var manager */ + protected $manager; + + /** @var path_helper */ + protected $path_helper; + + /** @var user */ + protected $user; + + /** + * Constructor. + * + * @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( + config $config, + dispatcher $dispatcher, + language $language, + manager $manager, + path_helper $path_helper, + user $user + ) + { + $this->config = $config; + $this->dispatcher = $dispatcher; + $this->language = $language; + $this->manager = $manager; + $this->path_helper = $path_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. + * + * @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, string $title = 'USER_AVATAR', bool $ignore_config = false, bool $lazy = false): array + { + $row = 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, string $title = 'GROUP_AVATAR', bool $ignore_config = false, bool $lazy = false): array + { + $row = 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, string $title, bool $ignore_config = false, bool $lazy = false): array + { + if (!$this->config['allow_avatar'] && !$ignore_config) + { + return [ + 'html' => '', + 'lazy' => false, + 'src' => '', + 'title' => '', + 'type' => '', + 'width' => 0, + 'height' => 0, + ]; + } + + $data = [ + 'src' => $row['avatar'], + 'width' => $row['avatar_width'], + 'height' => $row['avatar_height'], + 'title' => $this->language->lang($title), + 'lazy' => $lazy, + 'type' => '', + 'html' => '', + ]; + + /** @var 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 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. + * + * Created for Backwards Compatibility (BC). + * Styles should generate their own HTML element instead. + * + * @deprecated 4.1.0 After admin style is reworked aswell + * + * @param array $data The avatar data array + * @return string The avatar's HTML element + */ + private function get_avatar_html(array $data): string + { + if ($data['lazy']) + { + $data['src'] = $this->get_no_avatar_source() . ' 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 ''; + } +} 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 d99d3b89cb..46f3650898 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,19 @@ 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_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 : '', - ); + ]; } /** @@ -327,11 +339,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/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 %}