Skip to content

Commit

Permalink
chore: merge from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
samerton committed Mar 9, 2024
2 parents 2b7bd29 + 0c80570 commit d0f5d28
Show file tree
Hide file tree
Showing 220 changed files with 2,815 additions and 2,081 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
operating-system: ["ubuntu-latest"]
php-versions: ["7.4", "8.0", "8.1", "8.2"]
php-versions: ["7.4", "8.0", "8.1", "8.2", "8.3"]
steps:
- uses: actions/checkout@v2

Expand All @@ -23,16 +23,6 @@ jobs:
- name: Execute PHPStan
run: vendor/bin/phpstan --configuration=dev/phpstan.neon

code-style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: PHP Code Style (phpcs)
uses: chindit/actions-phpcs@master
with:
cli: -q --standard=dev/phpcs.xml

unused-language-term-check:
runs-on: ubuntu-latest
steps:
Expand Down
13 changes: 13 additions & 0 deletions .styleci.yml
@@ -0,0 +1,13 @@
risky: false
version: 7.4
preset: recommended
monolithic: true
finder:
name: "*.php"
disabled:
- align_double_arrow
- phpdoc_no_package
- concat_without_spaces
- include
- die_to_exit
- phpdoc_separation
2 changes: 1 addition & 1 deletion 403.php
Expand Up @@ -33,7 +33,7 @@
'HOME' => $language->get('errors', '403_home'),
'LOGIN' => $language->get('general', 'sign_in'),
'LOGIN_LINK' => URL::build('/login'),
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : '')
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : ''),
]
);

Expand Down
2 changes: 1 addition & 1 deletion 404.php
Expand Up @@ -31,7 +31,7 @@
'BACK' => $language->get('errors', '404_back'),
'HOME' => $language->get('errors', '404_home'),
'ERROR' => $language->get('errors', '404_error'),
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : '')
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : ''),
]
);

Expand Down
5 changes: 2 additions & 3 deletions core/avatar/face.php
Expand Up @@ -17,8 +17,8 @@
$view = isset($_GET['v']) ? $_GET['v'][0] : 'f';
$view = in_array($view, ['f', 'l', 'r', 'b']) ? $view : 'f';

function get_skin($user, $cache) {

function get_skin($user, $cache)
{
// Check cache
$cache->setCache('avatarCache_' . $user);
if ($cache->isCached($user)) {
Expand Down Expand Up @@ -49,7 +49,6 @@ function get_skin($user, $cache) {
$output .= 'Ne9AAAAAElFTkSuQmCC';
$output = base64_decode($output);
if ($user != '') {

$json = HttpClient::get('https://sessionserver.mojang.com/session/minecraft/profile/' . $user)->json();

if (isset($json->properties[0]->value)) {
Expand Down
57 changes: 36 additions & 21 deletions core/classes/Avatars/AvatarSource.php
Expand Up @@ -7,8 +7,8 @@
* @version 2.0.0-pr10
* @license MIT
*/
class AvatarSource {

class AvatarSource
{
protected static array $_sources = [];

protected static AvatarSourceBase $_active_source;
Expand All @@ -18,26 +18,28 @@ class AvatarSource {
* Uses active avatar source to get the URL of their Minecraft avatar.
*
* @param string $uuid UUID of avatar to get.
* @param int $size Size in pixels to render avatar at. Default 128
* @param int $size Size in pixels to render avatar at. Default 128
*
* @return string Compiled URL of avatar image.
*/
public static function getAvatarFromUUID(string $uuid, int $size = 128): string {
public static function getAvatarFromUUID(string $uuid, int $size = 128): string
{
return self::getActiveSource()->getAvatar($uuid, self::getDefaultPerspective(), $size);
}

/**
* Get a user's avatar from their raw data object.
* Used by the API for TinyMCE mention avatars to avoid reloading the user from the database.
*
* @param object $data User data to use
* @param bool $allow_gifs Whether to allow GIFs or not ()
* @param int $size Size in pixels to render avatar at. Default 128
* @param bool $full Whether to return the full URL or just the path
* @param object $data User data to use
* @param bool $allow_gifs Whether to allow GIFs or not ()
* @param int $size Size in pixels to render avatar at. Default 128
* @param bool $full Whether to return the full URL or just the path
*
* @return string Full URL of avatar image.
*/
public static function getAvatarFromUserData(object $data, bool $allow_gifs = false, int $size = 128, bool $full = false): string {
public static function getAvatarFromUserData(object $data, bool $allow_gifs = false, int $size = 128, bool $full = false): string
{
// If custom avatars are enabled, first check if they have gravatar enabled, and then fallback to normal image
if (defined('CUSTOM_AVATARS')) {
if ($data->gravatar) {
Expand Down Expand Up @@ -95,10 +97,11 @@ public static function getAvatarFromUserData(object $data, bool $allow_gifs = fa
/**
* Determine if a URL is a valid image URL for avatars.
*
* @param string $url URL to check
* @return bool Whether the URL is a valid image URL
* @param string $url URL to check
* @return bool Whether the URL is a valid image URL
*/
private static function validImageUrl(string $url): bool {
private static function validImageUrl(string $url): bool
{
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']);
$cache->setCache('avatar_validity');

Expand All @@ -107,6 +110,7 @@ private static function validImageUrl(string $url): bool {
}

$is_valid = false;

try {
$response = HttpClient::createClient()->head($url);
$headers = $response->getHeaders();
Expand All @@ -117,6 +121,7 @@ private static function validImageUrl(string $url): bool {
}

$cache->store($url, $is_valid, 3600);

return $is_valid;
}

Expand All @@ -125,7 +130,8 @@ private static function validImageUrl(string $url): bool {
*
* @return AvatarSourceBase The active source.
*/
public static function getActiveSource(): AvatarSourceBase {
public static function getActiveSource(): AvatarSourceBase
{
return self::$_active_source;
}

Expand All @@ -135,7 +141,8 @@ public static function getActiveSource(): AvatarSourceBase {
*
* @param string $name Name of source to set as active.
*/
public static function setActiveSource(string $name): void {
public static function setActiveSource(string $name): void
{
$source = self::getSourceByName($name);
if ($source === null) {
$source = self::getSourceByName('cravatar');
Expand All @@ -149,7 +156,8 @@ public static function setActiveSource(string $name): void {
*
* @return string Perspective.
*/
private static function getDefaultPerspective(): string {
private static function getDefaultPerspective(): string
{
if (defined('DEFAULT_AVATAR_PERSPECTIVE')) {
return DEFAULT_AVATAR_PERSPECTIVE;
}
Expand All @@ -162,7 +170,8 @@ private static function getDefaultPerspective(): string {
*
* @return AvatarSourceBase|null Instance if found, null if not found.
*/
public static function getSourceByName(string $name): ?AvatarSourceBase {
public static function getSourceByName(string $name): ?AvatarSourceBase
{
foreach (self::getAllSources() as $source) {
if (strtolower($source->getName()) == strtolower($name)) {
return $source;
Expand All @@ -177,7 +186,8 @@ public static function getSourceByName(string $name): ?AvatarSourceBase {
*
* @return AvatarSourceBase[]
*/
public static function getAllSources(): iterable {
public static function getAllSources(): iterable
{
return self::$_sources;
}

Expand All @@ -186,10 +196,12 @@ public static function getAllSources(): iterable {
*
* @return string URL with placeholders.
*/
public static function getUrlToFormat(): string {
public static function getUrlToFormat(): string
{
// Default to Cravatar
if (!isset(self::$_active_source)) {
require_once(ROOT_PATH . '/modules/Core/classes/Avatars/CravatarAvatarSource.php');

return (new CravatarAvatarSource())->getUrlToFormat(self::getDefaultPerspective());
}

Expand All @@ -201,7 +213,8 @@ public static function getUrlToFormat(): string {
*
* @param AvatarSourceBase $source Instance of avatar source to register.
*/
public static function registerSource(AvatarSourceBase $source): void {
public static function registerSource(AvatarSourceBase $source): void
{
self::$_sources[] = $source;
}

Expand All @@ -211,7 +224,8 @@ public static function registerSource(AvatarSourceBase $source): void {
*
* @return array<string, string> List of names.
*/
public static function getAllSourceNames(): array {
public static function getAllSourceNames(): array
{
$names = [];

foreach (self::getAllSources() as $source) {
Expand All @@ -227,7 +241,8 @@ public static function getAllSourceNames(): array {
*
* @return array<string, array<string>> Array of source => [] perspectives.
*/
public static function getAllPerspectives(): array {
public static function getAllPerspectives(): array
{
$perspectives = [];

foreach (self::getAllSources() as $source) {
Expand Down
37 changes: 22 additions & 15 deletions core/classes/Avatars/AvatarSourceBase.php
Expand Up @@ -7,8 +7,8 @@
* @version 2.0.0-pr10
* @license MIT
*/
abstract class AvatarSourceBase {

abstract class AvatarSourceBase
{
protected string $_name;

/**
Expand All @@ -27,7 +27,8 @@ abstract class AvatarSourceBase {
*
* @return string Name of this avatar source.
*/
public function getName(): string {
public function getName(): string
{
return $this->_name;
}

Expand All @@ -36,7 +37,8 @@ public function getName(): string {
*
* @return string Base url of this source.
*/
public function getBaseUrl(): string {
public function getBaseUrl(): string
{
return $this->_base_url;
}

Expand All @@ -45,33 +47,36 @@ public function getBaseUrl(): string {
*
* @return array Array of perspective names.
*/
public function getPerspectives(): array {
public function getPerspectives(): array
{
return array_keys($this->_perspectives_map);
}

/**
* Get the URL for this users avatar.
*
* @param string $uuid UUID of avatar to get.
* @param string $uuid UUID of avatar to get.
* @param string $perspective Perspective to render avatar with.
* @param int $size Size in pixels to render avatar at. Default 128
* @param int $size Size in pixels to render avatar at. Default 128
*
* @return string Compiled URL of avatar image.
*/
public function getAvatar(string $uuid, string $perspective, int $size = 128): string {
public function getAvatar(string $uuid, string $perspective, int $size = 128): string
{
return $this->formatUrl($this->getUrlToFormat($perspective), $uuid, $size);
}

/**
* Replace placeholders in raw url with uuid and size of requested avatar.
*
* @param string $url_to_format Raw url to replace placeholders in.
* @param string $uuid uuid (or username, yuck!) of avatar to get.
* @param int $size Size of avatar image in pixels to get.
* @param string $uuid uuid (or username, yuck!) of avatar to get.
* @param int $size Size of avatar image in pixels to get.
*
* @return string Formatted url.
*/
public function formatUrl(string $url_to_format, string $uuid, int $size): string {
public function formatUrl(string $url_to_format, string $uuid, int $size): string
{
return str_replace(
['{identifier}', '{size}'],
[$uuid, $size],
Expand All @@ -82,7 +87,7 @@ public function formatUrl(string $url_to_format, string $uuid, int $size): strin
/**
* Get raw URL with placeholders to format.
* - `{identifier} = UUID / username`
* - `{size} = size in pixels`
* - `{size} = size in pixels`.
*
* @param string $perspective Perspective to use in url.
*
Expand All @@ -93,17 +98,19 @@ abstract public function getUrlToFormat(string $perspective): string;
/**
* Translate NamelessMC perspective name to the relative name for this avatar source.
*
* @param string $perspective NamelessMC perspective name to translate.
* @return string Translated perspective name.
* @param string $perspective NamelessMC perspective name to translate.
* @throws InvalidArgumentException When an invalid perspective is passed.
* @return string Translated perspective name.
*/
public function getRelativePerspective(string $perspective): string {
public function getRelativePerspective(string $perspective): string
{
$perspective = strtolower($perspective);
if (isset($this->_perspectives_map[$perspective])) {
return $this->_perspectives_map[$perspective];
}

$class = static::class;

throw new InvalidArgumentException("Attempted to get invalid perspective of: {$perspective} on {$class}");
}
}

0 comments on commit d0f5d28

Please sign in to comment.