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

Json api 2 #53

Open
wants to merge 5 commits 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
4 changes: 2 additions & 2 deletions config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
openra_openrauseraccounts_controller:
path: /openra/{type}/{fingerprint}
defaults: { _controller: openra.openrauseraccounts.controller:fetchinfo }
path: /openra/{type}/{fingerprint}/{format}
defaults: { _controller: openra.openrauseraccounts.controller:fetchinfo, format: MiniYAML }
149 changes: 84 additions & 65 deletions controller/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,96 +38,115 @@ public function __construct(\openra\openrauseraccounts\core\core $core, \phpbb\d
}

/**
* Controller for route /openra/{$type}/{$fingerprint}
* Controller for route /openra/{$type}/{$fingerprint}/{format}
*
* @param string $type, $fingerprint
* @param string $type
* @param string $fingerprint
* @param string $format Response format, default value is 'MiniYAML'
*
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
*/
public function fetchinfo($type, $fingerprint)
public function fetchinfo($type, $fingerprint, $format)
{
// Profile data
$sql = $this->core->get_info_sql($fingerprint);
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$data)
return $this->get_response("Error: No profile data", $format);

$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']
];

// Badge data
$sql = $this->core->get_ubadge_sql_by_key($fingerprint);
$result = $this->db->sql_query_limit($sql, $this->config['max_profile_badges']);
$badges = [];
while ($row = $this->db->sql_fetchrow($result))
{
$badges[] = $row;
}
$this->db->sql_freeresult($result);

// Update last accessed time
$sql = $this->core->get_update_sql($fingerprint);
$result = $this->db->sql_query($sql);

switch ($type)
{
case 'info':
{
// Retrieve profile data
$sql = $this->core->get_info_sql($fingerprint);
if (!($result = $this->db->sql_query($sql)))
{
return $this->get_response("Error: Failed to query profile data");
}
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$data)
{
return $this->get_response("Error: No profile data");
}

// 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'])))
{
return $this->get_response("Error: Failed to query badge data");
}
// Store all the badge data in an array to loop over it later
$badges = array();
while ($row = $this->db->sql_fetchrow($result))
if (strtolower($format) !== "json")
{
$badges[] = $row;
}
$this->db->sql_freeresult($result);

// Update last accessed time
$sql = $this->core->get_update_sql($fingerprint);
if (!($result = $this->db->sql_query($sql)))
{
return $this->get_response("Error: Failed to update last accessed time");
}

$yaml = "Player:\n";
$yaml .= "\tFingerprint: " . $data['fingerprint'] . "\n";
$yaml .= "\tPublicKey: " . base64_encode($data['public_key']) . "\n";
$yaml .= "\tKeyRevoked: " . ($data['revoked'] ? 'true' : 'false') . "\n";
$yaml .= "\tProfileID: " . $data['user_id'] . "\n";
$yaml .= "\tProfileName: " . $data['username'] . "\n";
$yaml .= "\tProfileRank: Registered User\n";
$yaml .= "\tAvatar:\n";
if ($avatar_data = $this->core->get_avatar_data($data))
{
$yaml .= "\t\tSrc: " . $avatar_data['src'] . "\n";
$yaml .= "\t\tWidth: " . $avatar_data['width'] . "\n";
$yaml .= "\t\tHeight:" . $avatar_data['height'] . "\n";
}

$yaml .= "\tBadges:\n";
if ($badges)
{
$i = 0;
foreach ($badges as $badge)
$content = "Player:\n";
$content .= "\tFingerprint: " . $data['fingerprint'] . "\n";
$content .= "\tPublicKey: " . base64_encode($data['public_key']) . "\n";
$content .= "\tKeyRevoked: " . ($data['revoked'] ? 'true' : 'false') . "\n";
$content .= "\tProfileID: " . $data['user_id'] . "\n";
$content .= "\tProfileName: " . $data['username'] . "\n";
$content .= "\tProfileRank: Registered User\n";
$content .= "\tAvatar:\n";
if ($avatar['src'])
{
$content .= "\t\tSrc: " . $avatar['src'] . "\n";
$content .= "\t\tWidth: " . $avatar['width'] . "\n";
$content .= "\t\tHeight:" . $avatar['height'] . "\n";
}

$content .= "\tBadges:\n";
if ($badges)
{
$yaml .= "\t\tBadge@$i:\n";
$yaml .= "\t\t\tLabel: " . $badge['badge_label'] . "\n";
$yaml .= "\t\t\tIcon24: " . $badge['badge_icon_24'] . "\n";
$i++;
$i = 0;
foreach ($badges as $badge)
{
$content .= "\t\tBadge@$i:\n";
$content .= "\t\t\tLabel: " . $badge['badge_label'] . "\n";
$content .= "\t\t\tIcon24: " . $badge['badge_icon_24'] . "\n";
$i++;
}
}
} else {
$content = [
'Player' => [
'Fingerprint' => $data['fingerprint'],
'PublicKey' => base64_encode($data['public_key']),
'KeyRevoked' => ($data['revoked'] ? 'true' : 'false'),
'ProfileID' => $data['user_id'],
'ProfileName' => $data['username'],
'ProfileRank' => 'Registered User',
'Avatar' => $avatar,
'Badges' => $badges,
]
];
}

return $this->get_response($yaml);
return $this->get_response($content, $format);

break;
}

default:
{
return $this->get_response("Error: Unknown route");
return $this->get_response("Error: Unknown route", $format);
}
}
}

public function get_response($content)
public function get_response($content, $format)
{
$response = new Response($content);
$response->headers->set('Content-Type', 'Content-type: text/plain; charset=utf-8');
if (strtolower($format) !== "json")
{
$response = new Response($content);
$response->headers->set('Content-Type', 'Content-type: text/plain; charset=utf-8');
} else {
$response = new Response();
$response->setContent(json_encode($content, JSON_UNESCAPED_SLASHES));
$response->headers->set('Content-Type', 'application/json');
}
return $response;
}
}
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;
}
}