Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor avatar handling. #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions controller/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function fetchinfo($type, $fingerprint)
return $this->get_response("Error: No profile data");
}

$avatar = [
Copy link
Author

@deleted-user-1 deleted-user-1 Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defined here in preparation for #50 #53 .

'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'])))
Expand Down Expand Up @@ -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";
Expand Down
128 changes: 38 additions & 90 deletions core/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}