HEX
Server: Apache
System: Linux web5.visualcom.es 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: ensaco.es (10019)
PHP: 8.3.32
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/ensaco.es/httpdocs/wp-content/plugins/itempress/inc/image-proxy.php
<?php

if (!defined('ABSPATH')) {
    exit;
}


add_action('template_redirect', function (): void {
    $image_id = (string) get_query_var('worker_img_id');
    if ($image_id === '') {
        return;
    }

    // 阻止 WP canonical 为 .webp URL 添加尾部斜杠
    remove_action('template_redirect', 'redirect_canonical');

    $image_id = preg_replace('/[^a-zA-Z0-9\-~]/', '', $image_id);


    $req_uri = $_SERVER['REQUEST_URI'] ?? '';
    $size = 1600;
    if (preg_match('#-(\d+)\.webp#', $req_uri, $m)) {
        $size = (int) $m[1];
    } elseif (isset($_GET['size'])) {
        $size = (int) $_GET['size'];
    }
    $size = min(1600, max(100, $size));

    $eBayUrl = "https://i.ebayimg.com/images/g/" . $image_id . "/s-l" . $size . ".jpg";


    $response = wp_remote_get($eBayUrl, [
        'timeout'    => 10,
        'redirection' => 3,
        'headers'    => [
            'User-Agent' => 'Mozilla/5.0 ItemPress Image Proxy',
            'Accept'     => 'image/avif,image/webp,image/apng,image*;q=0.8',
        ],
    ]);

    if (!is_wp_error($response) && (int) wp_remote_retrieve_response_code($response) === 200) {
        $body = wp_remote_retrieve_body($response);
        if ($body !== '') {
            $fmt = false !== strpos($body, "\x89PNG") ? 'image/png' : 'image/jpeg';
            header('Content-Type: ' . $fmt);
            header('Cache-Control: public, max-age=604800');
            header('X-ItemPress-Image-Proxy: 1');
            echo $body;
            exit;
        }
    }


    wp_redirect($eBayUrl, 302);
    exit;
}, 1);