From 58694d6f31dad913b83784ad19e0810810f7737f Mon Sep 17 00:00:00 2001 From: matjaeck <59416324+matjaeck@users.noreply.github.com> Date: Sun, 10 Jan 2021 22:15:44 +0100 Subject: [PATCH] Refactor avatar handling. PhpBB's driver->get_data() method does not expose absolute URLs and we need special handling for every avatar type due to each using a different URL pattern. We therefore have to hardcode the avatar type name and path fragments and depend on internals with which we will need to maintain compatibility in case of future upstream changes. Using our own implementation with less overhead does not introduce further costs in this case. --- controller/main.php | 14 +++-- core/core.php | 128 +++++++++++++------------------------------- 2 files changed, 48 insertions(+), 94 deletions(-) diff --git a/controller/main.php b/controller/main.php index 17546a7..fc9feba 100644 --- a/controller/main.php +++ b/controller/main.php @@ -63,6 +63,12 @@ public function fetchinfo($type, $fingerprint) return $this->get_response("Error: No profile data"); } + $avatar = [ + 'src' => $this->core->get_avatar_url($data['user_avatar'], $data['user_avatar_type'], $data['user_avatar_width'], $data['user_avatar_height']), + 'width' => $data['user_avatar_width'], + 'height' => $data['user_avatar_height'] + ]; + // Retrieve badge data $sql = $this->core->get_ubadge_sql_by_key($fingerprint); if (!($result = $this->db->sql_query_limit($sql, $this->config['max_profile_badges']))) @@ -92,11 +98,11 @@ public function fetchinfo($type, $fingerprint) $yaml .= "\tProfileName: " . $data['username'] . "\n"; $yaml .= "\tProfileRank: Registered User\n"; $yaml .= "\tAvatar:\n"; - if ($avatar_data = $this->core->get_avatar_data($data)) + if ($avatar['src']) { - $yaml .= "\t\tSrc: " . $avatar_data['src'] . "\n"; - $yaml .= "\t\tWidth: " . $avatar_data['width'] . "\n"; - $yaml .= "\t\tHeight:" . $avatar_data['height'] . "\n"; + $yaml .= "\t\tSrc: " . $avatar['src'] . "\n"; + $yaml .= "\t\tWidth: " . $avatar['width'] . "\n"; + $yaml .= "\t\tHeight:" . $avatar['height'] . "\n"; } $yaml .= "\tBadges:\n"; diff --git a/core/core.php b/core/core.php index 663f173..0e1857e 100644 --- a/core/core.php +++ b/core/core.php @@ -187,102 +187,50 @@ public function validate_badge_order($user_id) } /** - * Gets the url to the user avatar. - * Returns false on failure or if there is no avatar. + * Returns the absolute url to profile images (avatars) or an empty string if not found. * - * @param array $user_data Avatar data from the user table - * @return array|boolean + * @param string $user_avatar Filename or e-mail address + * @param string|int $avatar_type Internal "driver" name or legacy constants with integer values. + * @param int $width + * @param int $height + * @return string */ - public function get_avatar_data($user_data) + public function get_avatar_url($user_avatar, $avatar_type, $width, $height) { - if (!$this->config['allow_avatar']) - { - return false; - } - - $row = [ - 'avatar' => $user_data['user_avatar'], - 'avatar_width' => $user_data['user_avatar_width'], - 'avatar_height' => $user_data['user_avatar_height'], - ]; + // Avatar not allowed or not set by user + if (!$this->config['allow_avatar'] || !$user_avatar) + return ""; - $driver = $this->avatar_manager->get_driver($user_data['user_avatar_type']); - - if (!$driver) + switch ($avatar_type) { - return false; - } - - $avatar_data = $driver->get_data($row); - - if ($user_data['user_avatar_type'] === 'avatar.driver.gravatar') - { - $avatar_data['src'] = $this->get_gravatar_url($row); - } - else if ($user_data['user_avatar_type'] === 'avatar.driver.upload') - { - $avatar_data['src'] = $this->get_upload_avatar_url($user_data['user_avatar']); - } - else if ($user_data['user_avatar_type'] === 'avatar.driver.local') - { - $avatar_data['src'] = $this->get_local_avatar_url($user_data['user_avatar']); - } - - return $avatar_data; - } - - /** - * Gets the full URL for a user uploaded avatar. - * - * @param $user_avatar User avatar data - * @return string Avatar URL - */ - protected function get_upload_avatar_url($user_avatar) - { - return generate_board_url() . '/download/file.php' . '?avatar=' . $user_avatar; - } - - /** - * Gets the full URL for a gallery avatar. - * - * @param $user_avatar User avatar data - * @return string Avatar URL - */ - protected function get_local_avatar_url($user_avatar) - { - return generate_board_url() . '/' . $this->config['avatar_gallery_path'] . '/' . $user_avatar; - } - - /** - * Gets the URL for a gravatar. - * Essentially a copy of the protected method form avatar.driver.gravatar - * - * @param $row User data - * @return string Gravatar URL - */ - protected function get_gravatar_url($row) - { - global $phpbb_dispatcher; - - $url = 'https://secure.gravatar.com/avatar/'; - $url .= md5(strtolower(trim($row['avatar']))); + // 1 + case AVATAR_UPLOAD: + case 'avatar.driver.upload': + return generate_board_url() . '/download/file.php' . '?avatar=' . $user_avatar; + + // 2 + case AVATAR_REMOTE: + case 'avatar.driver.remote': + return $user_avatar; + + // 3 + case AVATAR_GALLERY: + case 'avatar.driver.local': + return generate_board_url() . '/' . $this->config['avatar_gallery_path'] . '/' . $user_avatar; + + // No legacy value + case 'avatar.driver.gravatar': + { + $url = 'https://secure.gravatar.com/avatar/' . md5(strtolower(trim($user_avatar))); + if ($width || $height) + $url .= '?s=' . max($width, $height); + + return $url; + } - if ($row['avatar_width'] || $row['avatar_height']) - { - $url .= '?s=' . max($row['avatar_width'], $row['avatar_height']); + // Invalid data + default: + return ""; } - - /** - * Modify gravatar url - * - * @event core.get_gravatar_url_after - * @var string row User data or group data - * @var string url Gravatar URL - * @since 3.1.7-RC1 - */ - $vars = array('row', 'url'); - extract($phpbb_dispatcher->trigger_event('core.get_gravatar_url_after', compact($vars))); - - return $url; } }