<?php
define('BASE_DIR', $_SERVER['DOCUMENT_ROOT']);
define('TIME', time());
require_once(BASE_DIR.'/inc/init.php');
if (isset($_GET['type']) && isset($_GET['id']) && isset($_GET['token'])) {
$id = clear($_GET['id'], 'int');
$type = clear($_GET['type'], 'int');
$token = clear($_GET['token']);
switch ($type) {
case '1':
$query = $mysqli->query("SELECT `time_add`, `user_blocks`, `url_site`, `stats` FROM `".PREFIX."_links` WHERE `id` = '".$id."' LIMIT 1");
if ($query->num_rows) {
$res = $query->fetch_assoc();
$stats = json_decode($res['stats'], true);
$n = 0;
$d = '';
$curr_date = date("Y-m-d", TIME);
while($d != date("Y-m-d", strtotime($res['time_add']))) {
$d = date("Y-m-d", TIME - ($n * 86400));
if (!isset($stats[$d])) {
$stats[$d] = 0;
}
if ($d == $curr_date) {
$stats[$d] += 1;
}
$n++;
}
$result = $mysqli->query("SELECT * FROM `".PREFIX."_links` WHERE `id` = '".$id."' LIMIT 1");
$row1 = $result->fetch_assoc();
$user = $row1["user_id"];
$mysqli->query("UPDATE `".PREFIX."_users` SET `click_links` = `click_links` + '1' WHERE id = '".$user."'");
$mysqli->query("UPDATE `".PREFIX."_links` SET `stats` = '".json_encode($stats)."' WHERE id = '".$id."'");
$ip = clear($_SERVER['REMOTE_ADDR']);
$query = $mysqli->query("SELECT `id` FROM `".PREFIX."_blocks_views` WHERE `time_add` >= CURDATE() and `ident` = '".$id."' and `ip` = '".$ip."' LIMIT 1");
if ($query->num_rows) {
$res_view = $query->fetch_assoc();
$mysqli->query("UPDATE `".PREFIX."_blocks_views` SET `view` = `view` + 1 WHERE `ploshadka` = '".$id."' and `ip` = '".$ip."' and `ident` = '".$user."'");
@$stats[date("Y-m-d", TIME)]['view'] += 1;
} else {
@$stats[date("Y-m-d", TIME)]['user'] += 1;
@$stats[date("Y-m-d", TIME)]['view'] += 1;
$mysqli->query("INSERT INTO `".PREFIX."_blocks_views` (`ident`,`time_add`,`ip`,`view`,`ploshadka`) VALUES ('".$user."',NOW(),'".$ip."','1','".$id."')");
}
exit(header('Location: '.$res['url_site']));
}
break;
case '2':
$query = $mysqli->query("SELECT `time_add`, `user_blocks`, `url_site`, `stats` FROM `".PREFIX."_banners` WHERE `id` = '".$id."' LIMIT 1");
if ($query->num_rows) {
$res = $query->fetch_assoc();
$stats = json_decode($res['stats'], true);
$n = 0;
$d = '';
$curr_date = date("Y-m-d", TIME);
while($d != date("Y-m-d", strtotime($res['time_add']))) {
$d = date("Y-m-d", TIME - ($n * 86400));
if (!isset($stats[$d])) {
$stats[$d] = 0;
}
if ($d == $curr_date) {
$stats[$d] += 1;
}
$n++;
}
$result = $mysqli->query("SELECT * FROM `".PREFIX."_banners` WHERE `id` = '".$id."' LIMIT 1");
$row1 = $result->fetch_assoc();
$user = $row1["user_id"];
$mysqli->query("UPDATE `".PREFIX."_users` SET `click_banners` = `click_banners` + '1' WHERE id = '".$user."'");
$mysqli->query("UPDATE `".PREFIX."_banners` SET `stats` = '".json_encode($stats)."' WHERE id = '".$id."'");
$ip = clear($_SERVER['REMOTE_ADDR']);
$query = $mysqli->query("SELECT `id` FROM `".PREFIX."_blocks_views` WHERE `time_add` >= CURDATE() and `ident` = '".$id."' and `ip` = '".$ip."' LIMIT 1");
if ($query->num_rows) {
$res_view = $query->fetch_assoc();
$mysqli->query("UPDATE `".PREFIX."_blocks_views` SET `view` = `view` + 1 WHERE `ploshadka` = '".$id."' and `ip` = '".$ip."' and `ident` = '".$user."'");
@$stats[date("Y-m-d", TIME)]['view'] += 1;
} else {
@$stats[date("Y-m-d", TIME)]['user'] += 1;
@$stats[date("Y-m-d", TIME)]['view'] += 1;
$mysqli->query("INSERT INTO `".PREFIX."_blocks_views` (`ident`,`time_add`,`ip`,`view`,`ploshadka`) VALUES ('".$user."',NOW(),'".$ip."','1','".$id."')");
}
exit(header('Location: '.$res['url_site']));
}
break;
default:
break;
}
}
header('Location: '.SITE_URL);