482 lines
20 KiB
PHP
482 lines
20 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Класс работы с баннерами
|
||
*
|
||
* @package AVE.cms
|
||
* @subpackage module_Banner
|
||
* @filesource
|
||
*/
|
||
class ModuleBanner {
|
||
|
||
/**
|
||
* СВОЙСТВА
|
||
*/
|
||
|
||
var $_limit = 15;
|
||
var $_allowed_files =
|
||
array(
|
||
'image/jpg',
|
||
'image/jpeg',
|
||
'image/pjpeg',
|
||
'image/x-png',
|
||
'image/png',
|
||
'image/gif',
|
||
'image/webp'
|
||
);
|
||
|
||
function displayBanner($id) {
|
||
global $AVE_DB, $AVE_Template;
|
||
|
||
mt_rand();
|
||
|
||
$cur_hour = (int)date('G');
|
||
// Сохраняем ваше оригинальное условие времени полностью
|
||
$and_time = "AND ((start_hour = '0' AND end_hour = '0') OR (start_hour <= '$cur_hour' AND end_hour > '$cur_hour') OR (start_hour > end_hour AND (start_hour BETWEEN start_hour AND '$cur_hour' OR end_hour BETWEEN '$cur_hour' AND end_hour)))";
|
||
$and_category = (!empty($id) && is_numeric($id)) ? "AND category_id = '" . (int)$id . "'" : '';
|
||
|
||
$num_rows = $AVE_DB->Query("
|
||
SELECT id
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE is_active = '1'
|
||
AND (max_clicks = '0' OR (clicks < max_clicks AND max_clicks != '0'))
|
||
AND (max_views = '0' OR (views < max_views AND max_views != '0'))
|
||
" . $and_time . "
|
||
" . $and_category . "
|
||
")->NumRows();
|
||
|
||
// Ваша логика выбора порога веса
|
||
$weight_limit = ($num_rows) ? rand(1, 3) : 3;
|
||
|
||
$sql = $AVE_DB->Query("
|
||
SELECT
|
||
id,
|
||
banner_file,
|
||
target,
|
||
banner_name,
|
||
image_alt,
|
||
width,
|
||
height
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE is_active = '1'
|
||
AND (max_clicks = '0' OR (clicks < max_clicks AND max_clicks != '0'))
|
||
AND (max_views = '0' OR (views < max_views AND max_views != '0'))
|
||
" . $and_time . "
|
||
" . $and_category . "
|
||
AND weight <= '" . (int)$weight_limit . "'
|
||
");
|
||
$num = $sql->NumRows();
|
||
|
||
if ($num == 0) return;
|
||
|
||
$target_index = ($num == 1) ? 0 : rand(0, $num - 1);
|
||
|
||
$sql->DataSeek($target_index);
|
||
$banner = $sql->FetchAssocArray();
|
||
|
||
if(!empty($banner['banner_file']))
|
||
{
|
||
// Передаем переменные в Smarty
|
||
$AVE_Template->assign([
|
||
'banner' => $banner,
|
||
'mod_path' => BANNER_DIR,
|
||
'abs_path' => ABS_PATH
|
||
]);
|
||
|
||
// Выводим через шаблон (путь подставьте свой, обычно так)
|
||
$AVE_Template->display(BASE_DIR . '/modules/' . BANNER_DIR . '/templates/banner_viewer.tpl');
|
||
|
||
if(!empty($banner['id']))
|
||
{
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banners
|
||
SET views = views + 1
|
||
WHERE id = '" . (int)$banner['id'] . "'
|
||
");
|
||
}
|
||
}
|
||
}
|
||
|
||
function fetch_addclick($id) {
|
||
global $AVE_DB;
|
||
|
||
// Используем ?? '', чтобы не было Warning, если action не передан
|
||
$action = $_REQUEST['action'] ?? '';
|
||
|
||
switch($action) {
|
||
case '':
|
||
case 'addclick':
|
||
$sql = $AVE_DB->Query("
|
||
SELECT banner_url
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE id = '" . (int)$id . "'
|
||
LIMIT 1
|
||
");
|
||
|
||
$banner_url = $sql->GetCell();
|
||
|
||
if(!empty($banner_url)) {
|
||
// Сначала обновляем счетчик кликов
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banners
|
||
SET clicks = clicks + 1
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
|
||
// Затем перенаправляем пользователя
|
||
header('Location: ' . $banner_url);
|
||
} else {
|
||
// Если URL пустой или баннер не найден, возвращаем на главную
|
||
header('Location: index.php');
|
||
}
|
||
|
||
exit;
|
||
break;
|
||
}
|
||
}
|
||
|
||
function showBanners($tpl_dir) {
|
||
global $AVE_DB, $AVE_Template;
|
||
|
||
$limit = (int)$this->_limit;
|
||
$sql = $AVE_DB->Query("SELECT id FROM " . PREFIX . "_module_banners");
|
||
$num = $sql->NumRows();
|
||
|
||
$seiten = ceil($num / $limit);
|
||
$start = (int)(get_current_page() * $limit - $limit);
|
||
if ($start < 0) $start = 0;
|
||
|
||
$items = array();
|
||
$sql = $AVE_DB->Query("
|
||
SELECT *
|
||
FROM " . PREFIX . "_module_banners
|
||
LIMIT " . (int)$start . "," . (int)$limit . "
|
||
");
|
||
while($row = $sql->FetchRow()) {
|
||
array_push($items, $row);
|
||
}
|
||
|
||
if($num > $limit)
|
||
{
|
||
$page_nav = ' <a class="pnav" href="index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=1&cp=' . SESSION . '&page={s}">{t}</a> ';
|
||
$page_nav = get_pagination($seiten, 'page', $page_nav);
|
||
$AVE_Template->assign('page_nav', $page_nav);
|
||
}
|
||
|
||
$AVE_Template->assign('items', $items);
|
||
$AVE_Template->assign('mod_path', BANNER_DIR);
|
||
$AVE_Template->assign('kategs', $this->_showCategories());
|
||
$AVE_Template->assign('content', $AVE_Template->fetch($tpl_dir . 'banners.tpl'));
|
||
}
|
||
|
||
function editBanner($tpl_dir,$id) {
|
||
global $AVE_DB, $AVE_Template;
|
||
|
||
$sql = $AVE_DB->Query("
|
||
SELECT *
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
$row = $sql->FetchRow();
|
||
|
||
if (stristr(($row->banner_file),'.swf') === false) $row->swf = false; else $row->swf = true;
|
||
|
||
if(@!is_writeable(BASE_DIR . '/modules/' . BANNER_DIR . '/files/')) {
|
||
$AVE_Template->assign('folder_protected', 1);
|
||
}
|
||
|
||
$AVE_Template->assign('item', $row);
|
||
$AVE_Template->assign('mod_path', BANNER_DIR);
|
||
$AVE_Template->assign('formaction', 'index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=quicksave&cp=' . SESSION . '&id=' . (int)$id . '&pop=1');
|
||
$AVE_Template->assign('kategs', $this->_showCategories());
|
||
$AVE_Template->assign('content', $AVE_Template->fetch($tpl_dir . 'form.tpl'));
|
||
}
|
||
|
||
function deleteBanner($id) {
|
||
global $AVE_DB;
|
||
|
||
$sql = $AVE_DB->Query("
|
||
SELECT
|
||
banner_file,
|
||
banner_name
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
$row = $sql->FetchRow();
|
||
|
||
@unlink(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $row->banner_file);
|
||
$AVE_DB->Query("
|
||
DELETE
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
|
||
reportLog($_SESSION['user_name'] . ' - удалил баннер (' . $row->banner_name . ')', 2, 2);
|
||
|
||
header('Location:index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=1&cp=' . SESSION);
|
||
exit;
|
||
}
|
||
|
||
function quickSave($id) {
|
||
global $AVE_DB, $config_vars;
|
||
|
||
if(!empty($_POST['del'])) {
|
||
$sql = $AVE_DB->Query("
|
||
SELECT banner_file
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
$row = $sql->FetchRow();
|
||
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banners
|
||
SET banner_file = ''
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
|
||
@unlink(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $row->banner_file);
|
||
}
|
||
|
||
if(!empty($_POST['banner_name'])) {
|
||
$d_name = strtolower($_FILES['New']['name']);
|
||
$d_name = str_replace(' ','', $d_name);
|
||
$d_tmp = $_FILES['New']['tmp_name'];
|
||
|
||
if(!empty($_FILES['New']['type'])) {
|
||
if(in_array($_FILES['New']['type'], $this->_allowed_files)) {
|
||
$d_name = preg_replace('/[^ ._a-z0-9-]/', '_', $d_name);
|
||
if(file_exists(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name)) $d_name = $this->_getRandomPrefix() . '__' . $d_name;
|
||
|
||
if(@move_uploaded_file($d_tmp, BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name)) {
|
||
@chmod(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name, 0777);
|
||
echo "<script>alert('" . $config_vars['BANNER_IS_UPLOADED'] . ': ' . $d_name . "');</script>";
|
||
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banners
|
||
SET banner_file = '" . addslashes($d_name) . "'
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
|
||
reportLog($_SESSION['user_name'] . ' - заменил изображение баннера на (' . $d_name . ')', 2, 2);
|
||
|
||
} else {
|
||
echo "<script>alert('" . $config_vars['BANNER_NO_UPLOADED'] . ': ' . $d_name . "');</script>";
|
||
}
|
||
|
||
} else {
|
||
echo "<script>alert('" . $config_vars['BANNER_WRONG_TYPE'] . ': ' . $d_name . "');</script>";
|
||
}
|
||
}
|
||
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banners
|
||
SET
|
||
banner_name = '" . addslashes($_REQUEST['banner_name']) . "',
|
||
banner_url = '" . addslashes($_REQUEST['banner_url']) . "',
|
||
weight = '" . (int)$_REQUEST['weight'] . "',
|
||
views = '" . (int)$_REQUEST['views'] . "',
|
||
clicks = '" . (int)$_REQUEST['clicks'] . "',
|
||
image_alt = '" . addslashes($_REQUEST['image_alt']) . "',
|
||
category_id = '" . (int)$_REQUEST['category_id'] . "',
|
||
max_clicks = '" . (int)$_REQUEST['max_clicks'] . "',
|
||
max_views = '" . (int)$_REQUEST['max_views'] . "',
|
||
start_hour = '" . (int)$_REQUEST['start_hour'] . "',
|
||
end_hour = '" . (int)$_REQUEST['end_hour'] . "',
|
||
is_active = '" . (int)$_REQUEST['is_active'] . "',
|
||
target = '" . addslashes($_REQUEST['target']) . "',
|
||
width = '" . (int)$_REQUEST['width'] . "',
|
||
height = '" . (int)$_REQUEST['height'] . "'
|
||
WHERE
|
||
id = '" . (int)$id . "'
|
||
");
|
||
reportLog($_SESSION['user_name'] . ' - изменил параметры баннера (' . stripslashes($_REQUEST['banner_name']) . ')', 2, 2);
|
||
}
|
||
|
||
header('Location:index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=1&cp=' . SESSION);
|
||
exit;
|
||
}
|
||
|
||
function newBanner($tpl_dir) {
|
||
global $AVE_DB, $AVE_Template, $config_vars;
|
||
|
||
// Определяем под-действие (создание или сохранение)
|
||
$sub = $_REQUEST['sub'] ?? '';
|
||
|
||
switch($sub) {
|
||
case '':
|
||
if(!@is_writeable(BASE_DIR . '/modules/' . BANNER_DIR . '/files/')) {
|
||
$AVE_Template->assign('folder_protected', 1);
|
||
}
|
||
|
||
// Инициализируем пустой объект для Smarty, чтобы PHP 8.4 не ругался на отсутствие свойств
|
||
$item = new stdClass();
|
||
$item->id = 0;
|
||
$item->banner_name = '';
|
||
$item->category_id = 0;
|
||
$item->is_active = 1;
|
||
$item->target = '_blank';
|
||
$item->banner_file = '';
|
||
$item->banner_url = 'https://';
|
||
$item->image_alt = '';
|
||
$item->weight = 1;
|
||
$item->max_clicks = 0;
|
||
$item->max_views = 0;
|
||
$item->start_hour = 0;
|
||
$item->end_hour = 24;
|
||
$item->width = 0;
|
||
$item->height = 0;
|
||
|
||
$AVE_Template->assign('item', $item);
|
||
$AVE_Template->assign('mod_path', BANNER_DIR);
|
||
$AVE_Template->assign('kategs', $this->_showCategories());
|
||
$AVE_Template->assign('formaction', 'index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=newbanner&sub=save&cp=' . SESSION . '&pop=1');
|
||
|
||
// Рендерим шаблон формы
|
||
$AVE_Template->assign('content', $AVE_Template->fetch($tpl_dir . 'form.tpl'));
|
||
break;
|
||
|
||
case 'save':
|
||
if(!empty($_POST['banner_name'])) {
|
||
$file = '';
|
||
|
||
// Проверяем, был ли загружен файл
|
||
if(!empty($_FILES['New']['name']) && $_FILES['New']['error'] == UPLOAD_ERR_OK) {
|
||
$d_name = strtolower($_FILES['New']['name']);
|
||
$d_name = str_replace(' ', '', $d_name);
|
||
$d_tmp = $_FILES['New']['tmp_name'];
|
||
|
||
if(in_array($_FILES['New']['type'], $this->_allowed_files)) {
|
||
$d_name = preg_replace('/[^ ._a-z0-9-]/', '_', $d_name);
|
||
|
||
if(file_exists(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name)) {
|
||
$d_name = $this->_getRandomPrefix() . '__' . $d_name;
|
||
}
|
||
|
||
if(@move_uploaded_file($d_tmp, BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name)) {
|
||
@chmod(BASE_DIR . '/modules/' . BANNER_DIR . '/files/' . $d_name, 0777);
|
||
echo "<script>alert('" . ($config_vars['BANNER_IS_UPLOADED'] ?? 'Uploaded') . ': ' . $d_name . "');</script>";
|
||
reportLog($_SESSION['user_name'] . ' - добавил изображение баннера (' . $d_name . ')', 2, 2);
|
||
$file = $d_name;
|
||
} else {
|
||
echo "<script>alert('" . ($config_vars['BANNER_NO_UPLOADED'] ?? 'Error') . ': ' . $d_name . "');</script>";
|
||
}
|
||
} else {
|
||
echo "<script>alert('" . ($config_vars['BANNER_WRONG_TYPE'] ?? 'Wrong Type') . ': ' . $d_name . "');</script>";
|
||
}
|
||
}
|
||
|
||
// Выполняем запрос к БД с очисткой данных
|
||
$AVE_DB->Query("
|
||
INSERT
|
||
INTO " . PREFIX . "_module_banners
|
||
SET
|
||
category_id = '" . (int)($_REQUEST['category_id'] ?? 0) . "',
|
||
banner_file = '" . addslashes($file) . "',
|
||
banner_url = '" . addslashes($_REQUEST['banner_url'] ?? '') . "',
|
||
weight = '" . (int)($_REQUEST['weight'] ?? 1) . "',
|
||
banner_name = '" . addslashes($_REQUEST['banner_name'] ?? '') . "',
|
||
image_alt = '" . addslashes($_REQUEST['image_alt'] ?? '') . "',
|
||
max_clicks = '" . (int)($_REQUEST['max_clicks'] ?? 0) . "',
|
||
max_views = '" . (int)($_REQUEST['max_views'] ?? 0) . "',
|
||
start_hour = '" . (int)($_REQUEST['start_hour'] ?? 0) . "',
|
||
end_hour = '" . (int)($_REQUEST['end_hour'] ?? 24) . "',
|
||
is_active = '" . (int)($_REQUEST['is_active'] ?? 1) . "',
|
||
target = '" . addslashes($_REQUEST['target'] ?? '_blank') . "',
|
||
width = '" . (int)($_REQUEST['width'] ?? 0) . "',
|
||
height = '" . (int)($_REQUEST['height'] ?? 0) . "'
|
||
");
|
||
|
||
reportLog($_SESSION['user_name'] . ' - добавил новый баннер (' . stripslashes($_REQUEST['banner_name']) . ')', 2, 2);
|
||
}
|
||
|
||
header('Location:index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=1&cp=' . SESSION);
|
||
exit;
|
||
}
|
||
}
|
||
|
||
function bannerCategories($tpl_dir) {
|
||
global $AVE_DB, $AVE_Template;
|
||
|
||
switch($_REQUEST['sub']) {
|
||
case '' :
|
||
$items = array();
|
||
$sql = $AVE_DB->Query("SELECT * FROM " . PREFIX . "_module_banner_categories");
|
||
while($row = $sql->FetchRow()) {
|
||
array_push($items, $row);
|
||
}
|
||
$AVE_Template->assign('items', $items);
|
||
$AVE_Template->assign('mod_path', BANNER_DIR);
|
||
$AVE_Template->assign('kategs', $this->_showCategories());
|
||
$AVE_Template->assign('content', $AVE_Template->fetch($tpl_dir . 'kategs.tpl'));
|
||
break;
|
||
|
||
case 'save' :
|
||
if (isset($_POST['category_name'])) {
|
||
foreach($_POST['category_name'] as $id => $kateg) {
|
||
if(!empty($kateg)) {
|
||
$AVE_DB->Query("
|
||
UPDATE " . PREFIX . "_module_banner_categories
|
||
SET category_name = '" . addslashes($kateg) . "'
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
}
|
||
}
|
||
}
|
||
|
||
if (isset($_POST['del'])) {
|
||
foreach($_POST['del'] as $id => $kateg) {
|
||
$AVE_DB->Query("
|
||
DELETE
|
||
FROM " . PREFIX . "_module_banners
|
||
WHERE category_id = '" . (int)$id . "'
|
||
");
|
||
$AVE_DB->Query("
|
||
DELETE
|
||
FROM " . PREFIX . "_module_banner_categories
|
||
WHERE id = '" . (int)$id . "'
|
||
");
|
||
|
||
reportLog($_SESSION['user_name'] . ' - удалил категорию баннеров (' . (int)$id . ')', 2, 2);
|
||
}
|
||
}
|
||
|
||
header('Location:index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=kategs&cp=' . SESSION);
|
||
break;
|
||
|
||
case 'new' :
|
||
if(!empty($_REQUEST['category_name'])) {
|
||
$sql = $AVE_DB->Query("
|
||
INSERT
|
||
INTO " . PREFIX . "_module_banner_categories
|
||
SET category_name = '" . addslashes($_REQUEST['category_name']) . "'
|
||
");
|
||
|
||
reportLog($_SESSION['user_name'] . ' - добавил новую категорию (' . stripslashes($_REQUEST['category_name']) . ')', 2, 2);
|
||
}
|
||
|
||
header('Location:index.php?do=modules&action=modedit&mod=' . BANNER_DIR . '&moduleaction=kategs&cp=' . SESSION);
|
||
break;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* ВНУТРЕННИЕ МЕТОДЫ
|
||
*/
|
||
|
||
function _getRandomPrefix() {
|
||
return rand(1000, 99999);
|
||
}
|
||
|
||
function _showCategories() {
|
||
global $AVE_DB;
|
||
|
||
$categories = array();
|
||
$sql = $AVE_DB->Query("SELECT * FROM " . PREFIX . "_module_banner_categories");
|
||
while($row = $sql->FetchRow()) {
|
||
array_push($categories, $row);
|
||
}
|
||
|
||
return $categories;
|
||
}
|
||
}
|
||
?>
|