<?php
/**
* @author: Egor Smolyakov, 2012.
* @mod: Admin Ulty, 2015, december.
*/
defined('ROOT') or define('ROOT', str_replace('\\', '/', realpath(dirname (__FILE__))));
require_once ROOT . '/system/init.php'; // инициализация ядра
$id = abs(intval($_GET['id']));

$refer_er = $_GET['ref'];
function go($href) {
return '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Переход в анкету</title>
<meta http-equiv="refresh" content="0; url='.$href.'"/><script language="JavaScript">
window.location.href = "'.$href.'"
</script>
</head><body><a href="'.$href.'"><b>Переход на знакомства</b></a></body></html>';
}
ignore_user_abort();
if ($data['id'] == '1') {
$set['link_om'] = 'http://bymas.ru';
$set['link_comp'] = 'http://bymas.ru';
$set['link_bws'] = 'http://bymas.ru';

}
$set['link_other'] = 'http://bymas.ru';
if (rand(0, 7) == '2') {
 echo go($set['link_other']);
 exit;
}
if (!ctype_digit($_GET['id'])) // Если $id не цифра
{
echo go($set['link_bws']);
exit;
}


if (isset($_GET['id'])) {
  $queryread = DB :: run() -> query("SELECT * FROM `users` WHERE `id`=? LIMIT 1;", array(intval($_GET['id'])));
  $data = $queryread -> fetch();

  if ($data > 0) {
  if($data['banned']) {  
header('Location: '.$HOST);
  exit;
  }
  $sites = DB :: run() -> querySingle("SELECT COUNT(*) FROM `sites` WHERE `user_id`= ? AND `active` = '1' LIMIT 1;", array($data['id']));
  if($sites > 0) {

  $click = DB :: run() -> querySingle("SELECT COUNT(*) FROM `click_db` WHERE `user_id`= ? AND `ip` = ? AND `browser` = ? AND `date` = ? LIMIT 1;", array($data['id'], $ip, $useragent, date('d.m.Y')));
  $clickip = DB :: run() -> querySingle("SELECT COUNT(*) FROM `click_db` WHERE `user_id`= ? AND `ip` = ? AND `date` = ? LIMIT 1;", array($data['id'], $ip, date('d.m.Y')));

  if($click == 0 && $clickip == 0) {
  if($user['id'] == $data['id']) {
  $uid_click = 1;
  }else{
  $uid_click = 0;
  }
  $q = DB :: run() -> queryFetch("SELECT o.id_oper, o.oper, o.cena_rub, o.rusname FROM ips i
LEFT JOIN operators o ON i.id_oper=o.id_oper
WHERE INET_ATON('".$ip."') BETWEEN i.astart AND i.aend LIMIT 1");

if(check_smartphone()) {
if(!$q) {
$oper = 'Другой';
$user_plus = 0.1;
$link_go = $set['link_other'];
} else {
if($q['id_oper'] == 66) {
$link_go = $set['link_om']; 
$user_plus = ($q['cena_rub']/1000);
}else {
$link_go = $set['link_bws'];
$user_plus = ($q['cena_rub']/1000);
}
$oper = $q['oper'].' ('.$q['rusname'].')';
}
}else{
 $user_plus = 0.1;
$link_go = $set['link_comp'];
$oper = 'Компютеры';
}
if ($_GET['type'] == 'ClickUnder') {
$user_plus = $user_plus/100*70;
$_GET['type'] = 'ClickUnder';
}
if ($data['id'] == '1') {
$link_go = 'http://bymas.ru';
}
if ($_GET['md5'] !== md5($id.date("dmyh", time()).'Ulty')) {
echo go('http://bymas.ru');
echo "<font color=\"red\">MD5 da xatolik. md5 to'g'ri kelmayapti</font>";
exit;
}
$referer = $_GET['ref'];
if (!$referer) {
echo go('http://bymas.ru');
echo "Referer yo'q pul to'lanmaydi!";
exit;
}
function ulty($str)
    {
        $replace = array(
            'A' => 'a',
            'B' => 'b',
            'V' => 'v',
            'G' => 'g',
            'D' => 'd',
            'E' => 'z',
            'Z' => 'z',
            'I' => 'i',
            'J' => 'j',
            'K' => 'k',
            'L' => 'l',
            'M' => 'm',
            'N' => 'n',
            'O' => 'o',
            'P' => 'p',
            'R' => 'r',
            'S' => 's',
            'T' => 't',
            'U' => 'u',
            'F' => 'f',
            'H' => 'x',
            'C' => 'c',
            'W' => 'w',
            'Q' => 'q',
            'Y' => 'y',
            'X' => 'x'
        );
        return strtr($str, $replace);
    }
$ref = parse_url(ulty($referer));
$queryread = DB :: run() -> query("SELECT * FROM `sites` WHERE `active` = '1' AND `user_id`=?;", array(intval($data['id'])));
while ($site = $queryread -> fetch()) {
$url = parse_url(ulty($site['link']));

if ($ref['host'] == $url['host']) $update_ok = 'ok';
}
if ($update_ok != 'ok')  echo go('http://bymas.ru');
if ($update_ok == 'ok') {
if($data['ref_id'] != 0) {
$rubprc = ($user_plus/100) * $set['ref_prc'];
DB :: run() -> query("UPDATE `users` SET `rub` =`rub` + ? WHERE `id` = ?;", 
 array($rubprc, $data['ref_id']));
}
  DB :: run() -> query("INSERT INTO `click_db` SET `s_click`=?,`referer` = ?,`user_plus` = ?,`operator_name` = ?,`user_id`= ? , `ip` = ? , `browser` = ? , `date` = ?, `time_click` = ?, `type` = ?;", 
  array($uid_click,$referer, $user_plus, $oper, $data['id'] ,$ip, $useragent, date('d.m.Y'), TIME, $_GET['type']));
  DB :: run() -> query("UPDATE `users` SET `rub` =`rub` + ?, `click` =`click` + 1 WHERE `id` = ?;", 
  array($user_plus, $data['id']));
DB :: run() -> query ("UPDATE `counter` SET `click` = `click`+1, `rub` = `rub` + ".$user_plus." WHERE `type` = '".$_GET['type']."';");
  }
  echo go($link_go);
  }else{
  echo go($set['link_bws']);
  }
  }else{
  echo go($set['link_bws']);
  }
  } else {
   echo go($set['link_bws']);
  }
}else{
   echo go($set['link_bws']);
exit;
}
?>