<?php
date_default_timezone_set('Asia/Shanghai');

$projectRoot = dirname(dirname(__FILE__));
require_once $projectRoot . '/cong.php';
require_once $projectRoot . '/config/ConfigLoader.php';
require_once $projectRoot . '/lib/CloakHttpHelpers.php';

$config_name = isset($_GET['c']) ? strip_tags($_GET['c']) : 'index';
ConfigLoader::loadByName($config_name, $projectRoot);

if (!defined('CHECK_KEY')) {
    header('HTTP/1.1 500 Internal Server Error');
    header('Content-Type: text/plain; charset=utf-8');
    echo '// fingerprint script: CHECK_KEY is not configured';
    exit;
}

header('Content-Type: application/javascript; charset=utf-8');

$lang = strtoupper(CLOAK_ZH_ON) == 'ON' ? true : false;
$client = strtoupper(CLOAK_MOBILE_ON) == 'ON' ? true : false;
$os = strtoupper(CLOAK_OS_ON) == 'ON' ? true : false;
$is_virtual = intval(IS_VIRTUAL) == 0 ? false : IS_VIRTUAL;
$imodel = intval(IPHONE_MODEL) == 0 ? false : IPHONE_MODEL;

$cloak_key = CHECK_KEY;
$campagin_id = COSTM_IP_SCORE;
$time = time();
$ckey = sha1($campagin_id . sha1($time . $cloak_key));
$fp_js_cookie_opts = cloak_fingerprint_js_cookie_options();

readfile(__DIR__ . '/cloakjs');
echo "\n";
?>

var cresult = true;
var vbrowser = 'pc';
var br = new MobileDetect(window.navigator.userAgent);
var jsSrc = navigator.languages;

var iphone_model = MobileDevice.getModels();

var mreason = "";

if(br.mobile() == null) {
    vbrowser = 'pc';
} else {
    vbrowser = 'mobile';
}

<?php
if($lang == true) {
?>
for (let lan of jsSrc) {
    if(lan.toLowerCase().indexOf('zh')!=-1)
    {
        cresult = false;
        mreason = 1;
    }
}
<?php
}
?>


<?php
if($client == true) {
?>

if(cresult)
{
    if(br.mobile() == null) {
        cresult = false;
        mreason = 2;
    }
}

<?php
}
?>


<?php
if($os == true) {
?>

if(cresult)
{
    
    if(br.os() == 'AndroidOS' || br.os() == 'iOS' || br.os() == 'iPadOS') {
        
    } else {
        cresult = false;
        mreason = 3;
    }
}

<?php
}
?>

<?php
if ($imodel) {
?>

var imodel = <?php echo $imodel; ?>;

if(cresult && br.is('iPhone'))
{
    var getPixels = [];
    if(imodel == 8){
        getPixels = ["375x812", "390x844", "393x852", "402x874", "414x896@3", "414x896@2", "428x926", "430x932", "440x956"];
    }else if(imodel == 10){
        getPixels = ["375x812", "390x844", "393x852", "402x874", "414x896@3", "414x896@2", "428x926", "430x932", "440x956"];
    }else if (imodel == 11){
        getPixels = ["390x844", "393x852", "402x874", "428x926", "430x932", "440x956"];
    }else if (imodel == 12){
        getPixels = ["390x844", "393x852", "402x874", "428x926", "430x932", "440x956"];
    }

    const dpr = window.devicePixelRatio
    const screenResolution = `${window.screen.width}x${window.screen.height}`;

    if(screenResolution == '414x896') {
       var ppixel = screenResolution + '@' + dpr;
    } else {
       var ppixel = screenResolution;
    }
    
    if(iphone_model != "") {
       if (getPixels.includes(ppixel)) {
       
       } else {
            cresult = false
            mreason = 4;
       }
    } else {
       cresult = false
       mreason = 11;
    }
}

<?php
}
?>

<?php if ($is_virtual): ?>
<?php require __DIR__ . '/../resources/virtual_device_detect.inc.php'; ?>
var virtual = <?php echo (int) $is_virtual; ?>;
<?php endif; ?>

function cloakFinishFingerprint() {
    var fpCookieOpts = <?php echo json_encode($fp_js_cookie_opts, JSON_UNESCAPED_UNICODE); ?>;
    var fpCookies = (typeof Cookies !== 'undefined' && Cookies.withAttributes)
        ? Cookies.withAttributes(fpCookieOpts)
        : Cookies;

    fpCookies.set('ctime', '<?php echo $time; ?>');
    fpCookies.set('cyyek', '<?php echo $ckey; ?>');
    fpCookies.set('mmr', String(mreason));
    fpCookies.set('vbrowser', vbrowser);
    fpCookies.set('imodel', iphone_model || '');

    if(cresult) {
        fpCookies.set('cl', 'oklot');
    } else {
        fpCookies.set('cl', 'uklot');
    }

    if (typeof window.__cloakFingerprintDone === 'function') {
        window.__cloakFingerprintDone();
    }
}

<?php if ($is_virtual): ?>
if (cresult && virtual > 0 && window.CloakVirtualDetect) {
    CloakVirtualDetect.run(virtual, {
        isMobile: br.mobile() != null,
        ua: navigator.userAgent,
        platform: navigator.platform || ''
    }).then(function(res) {
        if (res && res.blocked) {
            cresult = false;
            mreason = res.mmr || 12;
        }
        cloakFinishFingerprint();
    }).catch(function() {
        cloakFinishFingerprint();
    });
} else {
    cloakFinishFingerprint();
}
<?php else: ?>
cloakFinishFingerprint();
<?php endif; ?>
