<?php

/*
 * @name        JMY CMS
 * @link        https://jmy.su/
 * @copyright   Copyright (C) 2012-2025 JMY LTD
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      Orlov Alexsey
 */

if (!defined('ACCESS')) {
    header('Location:/');
    exit;
}
global $db, $core, $cache;
switch ($url[1] ?? null) {
    case 'bclicks':
        $id = $url[2] ?? '';
        $rows = $core->row_data('banners', 'id ="' . $id . '"');
        $ips = $core->row_data('banners_ips', 'bid ="' . $id . '"');
        $now = time();
        $period = 86400;//24 часа в секундах
        $ip = $_SERVER['REMOTE_ADDR'] ?? getenv('HTTP_X_FORWARDED_FOR');
        $http_referer = $_SERVER['HTTP_REFERER'] ?? '';
        start_end('banners', $rows['id'], $rows['startdate'], $rows['enddate'], $rows['active']);
        clicks_views('banners', $rows['id'], $rows['max_views'], $rows['views'], $rows['max_clicks'], $rows['clicks']);
        if ($now - $ips['time'] < $period) {
            getOneClicks($id, 'banners', 'clicks');
            $core->upd_data('banners_ips', 'clicks = clicks+1,  redir ="' . filter($http_referer) . '"', 'bid ="' . $id . '"');
            if ($rows['allow_views'] == 1) {
                getOneClicks($id, 'banners', 'views');
            } elseif ($rows['allow_views'] == 2 && $ips['ip'] == $ip && $ips['uid'] == $core->auth->user_id) {
                getOneClicks($id, 'banners', 'views');
            }
        } else {
            if ($rows['allow_views'] == 1) {
                getOneClicks($id, 'banners', 'views');
            } elseif ($rows['allow_views'] == 2 && $ips['ip'] == $ip && $ips['uid'] == $core->auth->user_id) {
                getOneClicks($id, 'banners', 'views');
            }
            getOneClicks($id, 'banners', 'clicks');
            $core->ins_data('banners_ips', 'time,ip,redir,uid,bid,clicks', '"' . time() . '","' . filter($ip) . '","' . filter($http_referer) . '","' . $core->auth->user_id . '","' . $id . '", 1');
        }
        if ($rows['urlsb']) {
            header("refresh: 0; url=" . $rows['urlsb'] . "");
        } else {
            $location = $core->auth->isAdmin && $rows['active'] == 1 ? ADMIN . '/banners/edit/' . $rows['id'] : '/';
            location($location);
        }
        break;
    case 'uniqclicks':
        $id = $url[2] ?? '';
        $rows = $core->row_data('banners', 'id ="' . $id . '"');
        $ips = $core->row_data('banners_ips', 'bid ="' . $id . '"');
        $now = time();
        $period = 86400;//24 часа в секундах
        $ip = $_SERVER['REMOTE_ADDR'] ?? getenv('HTTP_X_FORWARDED_FOR');
        $http_referer = $_SERVER['HTTP_REFERER'] ?? '';
        clicks_views('banners', $rows['id'], $rows['max_views'], $rows['views'], $rows['max_clicks'], $rows['clicks']);
        if ($rows['allow_views'] == 1) getOneClicks($id, 'banners', 'views');
        if (isset($core->auth->user_id) && isset($ips['ip']) && $ips['ip'] == $ip && $ips['uid'] == $core->auth->user_id && $now - $ips['time'] < $period) {
            getOneClicks($id, 'banners_ips', 'clicks');
        } else {
            getOneClicks($id, 'banners', 'clicks');
            $core->ins_data('banners_ips', 'time,ip,redir,uid,bid,clicks', '"' . time() . '","' . filter($ip) . '","' . filter($http_referer) . '","' . $core->auth->user_id . '","' . $id . '", 1');
            if ($rows['allow_views'] == 2) getOneClicks($id, 'banners', 'views');
        }
        if ($rows['urlsb']) {
            echo "<meta http-equiv=\"refresh\" HTTP/1.1 301 Moved Permanently>
            <meta http-equiv=\"refresh\" Refresh: 10; url=" . $rows['urlsb'] . "/>
            <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
            <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ru\" lang=\"ru\"><head><title>Redirecting...</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><script type=\"text/javascript\">
            function countdownRedirect(url, msg){var TARG_ID = \"COUNTDOWN_REDIRECT\";var DEF_MSG = \"Redirecting...\";if( ! msg ){msg = DEF_MSG;}if( ! url ){throw new Error('You didn\'t include the \"url\" parameter');}var e = document.getElementById(TARG_ID);if( ! e ){throw new Error('\"COUNTDOWN_REDIRECT\" element id not found');}var cTicks = parseInt(e.innerHTML);var timer = setInterval(function(){if( cTicks ){e.innerHTML = --cTicks;}
            else{clearInterval(timer);document.body.innerHTML = msg;location = url;}}, 1000);}</script></head><body onload='countdownRedirect(\"" . $rows['urlsb'] . "\", \"Redirecting...\")'><div class=\"bord\"><span class=\"counter\" id=\"COUNTDOWN_REDIRECT\"></span></div></body></html>";
            exit();
        } else {
            $location = $core->auth->isAdmin && $rows['active'] == 1 ? ADMIN . '/banners/edit/' . $rows['id'] : '/';
            location($location);
        }
        break;
}
