<?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;
$query = $core->select_data('banners', 'active = 1');
if ($db->numRows($query) > 0) {
    while ($rows = $db->getRow($query)) {
        $profileUrl = array('/register', '/password', '/login', '/register_social', '/edit', '/friends_list', '/friends_inbox', '/friends_outbox');
        $pmUrl = array('/write', '/list', '/main', '/view');
        $pmUrl = $_SERVER['QUERY_STRING'] == 'url=pm/sent_ok' ? $rows['inprofile'] : $pmUrl;
        $main_news = $_SERVER['QUERY_STRING'] == 'url=news' ? $rows['main'] : ($rows['innews'] ?? '');
        $inprofile = $_SERVER['QUERY_STRING'] == 'url=profile' . in_array($_SERVER['QUERY_STRING'], $profileUrl) || $_SERVER['QUERY_STRING'] == 'url=pm' . in_array($_SERVER['QUERY_STRING'], $pmUrl);
        if ((($rows['tag'] && $rows['active'] == 1) && (($main_news || $inprofile) == 1)) || $rows['enddate'] && time() > $rows['enddate'] || $rows['startdate'] && time() > $rows['startdate']) {
            if ($rows['allow_clicks'] == 1) {
                $code = str_replace(array('\n', '\n', '""', '""', '&lt;', '&gt;', '{%urlb%}', '{%urltitle%}', '{%urlimg%}'), array('<br>', '<br/>', '\p', '\n', '<', '>', '/banners/bclicks/' . $rows['id'], $rows['urltitle'], $rows['urlimg']), $rows['code']);
            } elseif ($rows['allow_clicks'] == 2) {
                $code = str_replace(array('\n', '\n', '""', '""', '&lt;', '&gt;', '{%urlb%}', '{%urltitle%}', '{%urlimg%}'), array('<br>', '<br/>', '\p', '\n', '<', '>', '/banners/uniqclicks/' . $rows['id'], $rows['urltitle'], $rows['urlimg']), $rows['code']);
            } else {
                $code = str_replace(array('\n', '\n', '""', '""', '&lt;', '&gt;', '{%urlb%}', '{%urltitle%}', '{%urlimg%}'), array('<br>', '<br/>', '\p', '\n', '<', '>', $rows['urlsb'], $rows['urltitle'], $rows['urlimg']), $rows['code']);
            }
        } else {
            $code = '';
        }
        if (isset($rows['random']) && $rows['random'] == 1) {
            $codes[] = $code;
            srand((int)(double)microtime() * 1000000);
            $random_codes = rand(0, count($codes) - 1);
            $this->setVar($rows['tag'], $codes[$random_codes]);
        } else {
            $this->setVar($rows['tag'], $code);
        }
    }
}