<?php
defined('PROTECTOR') OR die('Ошибка: нет доступа');
 
function my_round($num){
return sprintf('%01.1fk', $num / 1000);
}

if(isset($set['lvl'])){
$mast=$set['lvl'];
}else{
$mast=FALSE;
}

/*
if(isset($new_lvl)){
if($new_lvl=='yes'){
header('Location: '.$_SERVER['SCRIPT_FILENAME'].'');exit;
}
}else{
$new_lvl=FALSE;
}
*/
// Функция времени
function Sec2Time($time){
if(is_numeric($time)){
$value = array("years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,);
if($time >= 31536000){
$value["years"] = floor($time/31536000);
$time = ($time%31536000);
}
if($time >= 86400){
$value["days"] = floor($time/86400);
$time = ($time%86400);
}
if($time >= 3600){
$value["hours"] = floor($time/3600);
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$time = ($time%60);
}
$value["seconds"] = floor($time);
		if($value["seconds"]>0){
	$time5 = $value["seconds"].' сек. ';
	}else{
	$time5='';
	}
	if($value["minutes"]>0){
	$time4 = $value["minutes"].' мин. ';
	}else{
	$time4='';
	}
	if($value["hours"]>0){
	$time3 = $value["hours"].' ч. ';
	}else{
	$time3='';
	}
		if($value["days"]>0){
	$time2 = $value["days"].' дн. ';
	}else{
	$time2='';
	}
	if($value["years"]>0){
	$time1 = $value["years"].' г. ';
	}else{
	$time1='';
	}
return $time1.	$time2.$time3.$time4.$time5;
return (array) $value;
}else{
return (bool) FALSE;
}
}
//---------------------------
$a=isset($a) ? htmlspecialchars($a):null;
$asg=isset($asg) ? htmlspecialchars($asg):null;
$nick=isset($nick) ? htmlspecialchars($nick):null;


$_GET['instalscript']=isset($_GET['instalscript']) ? htmlspecialchars($_GET['instalscript']):null;

$instalscript =$_GET['instalscript'];

$_GET['do']=isset($_GET['do']) ? htmlspecialchars($_GET['do']):null;

$do =$_GET['do'];

$_GET['attack']=isset($_GET['attack']) ? htmlspecialchars($_GET['attack']):null;

$_GET['image']=isset($_GET['image']) ? htmlspecialchars($_GET['image']):null;

$_GET['kolvo']=isset($_GET['kolvo']) ? htmlspecialchars($_GET['kolvo']):null;

$_GET['nick']=isset($_GET['nick']) ? htmlspecialchars($_GET['nick']):null;

$_GET['name']=isset($_GET['name']) ? htmlspecialchars($_GET['name']):null;

$_GET['cena']=isset($_GET['cena']) ? htmlspecialchars($_GET['cena']):null;

$_GET['mana']=isset($_GET['mana']) ? htmlspecialchars($_GET['mana']):null;

$_GET['enter']=isset($_GET['enter']) ? htmlspecialchars($_GET['enter']):null;

$_GET['mod']=isset($_GET['mod']) ? htmlspecialchars($_GET['mod']):null;

$_GET['usr']=isset($_GET['usr']) ? htmlspecialchars($_GET['usr']):null;

$_GET['user']=isset($_GET['user']) ? htmlspecialchars($_GET['user']):null;

$_GET['go']=isset($_GET['go']) ? htmlspecialchars($_GET['go']):null;

$_GET['o']=isset($_GET['o']) ? htmlspecialchars($_GET['o']):null;

$_GET['p']=isset($_GET['p']) ? htmlspecialchars($_GET['p']):null;

$_GET['ok']=isset($_GET['ok']) ? htmlspecialchars($_GET['ok']):null;

$_GET['act']=isset($_GET['act']) ? htmlspecialchars($_GET['act']):null;

$_GET['ref']=isset($_GET['ref']) ? htmlspecialchars($_GET['ref']):null;

$_GET['r']=isset($_GET['r']) ? htmlspecialchars($_GET['r']):null;

$_GET['refer']=isset($_GET['refer']) ? htmlspecialchars($_GET['refer']):null;

$_GET['id']=isset($_GET['id']) ? htmlspecialchars($_GET['id']):null;

$_GET['ip']=isset($_GET['ip']) ? htmlspecialchars($_GET['ip']):null;

$_GET['ids']=isset($_GET['ids']) ? htmlspecialchars($_GET['ids']):null;

$_GET['tip']=isset($_GET['tip']) ? htmlspecialchars($_GET['tip']):null;

$_GET['kom']=isset($_GET['kom']) ? htmlspecialchars($_GET['kom']):null;

$_GET['old']=isset($_GET['old']) ? htmlspecialchars($_GET['old']):null;

$_GET['page']=isset($_GET['page']) ? htmlspecialchars($_GET['page']):null;

$_GET['oponent']=isset($_GET['oponent']) ? htmlspecialchars($_GET['oponent']):null;

$gameout = time() - 1800;//время онлайна игрока
$online = mysql_num_rows(mysql_query("SELECT * FROM `online` WHERE `laikas` > '$gameout'"));
//if($online==0){$online=1;}
//$guest = mysql_num_rows(mysql_query("SELECT * FROM `guest` WHERE `laikas` > '$gameout'"));
//$online=$online+$guest;

date_default_timezone_set('Europe/Moscow');

function zasad(){}
function ryd(){
global $inryd;
if ($inryd=='1'){
header ('Location: rydnik.php?');exit;}}
// Бои
function going(){
global $in_battle,$inpk,$inar,$inzasada;

if ($in_battle=='1'){
header ('Location: battle.php?');exit;}

elseif ($inpk=='1'){
header ('Location: pk.php?');exit;}

elseif ($inar=='1'){
header ('Location: combat.php?');exit;}}

function auto_clean(){
global $log;
$req=mysql_query("SELECT * FROM log WHERE usr = '$log' LIMIT 20");
$avto = mysql_num_rows($req);
if($avto>='20'){
$lim=round($avto/2);
mysql_query("DELETE FROM `log` WHERE `usr` = '$log' LIMIT $lim");
}
}

function del_log($lpl){
global $log;
mysql_query("DELETE FROM `log` WHERE `usr` = '$log' AND `place` = '$lpl'");
}

function log_msg($tlog){
global $log;
$req=mysql_query("SELECT * FROM `log` WHERE `usr` = '$log' AND `place` = '$tlog' AND `timer` >= '".time()."' ORDER BY `id` DESC LIMIT 6");
$avto = mysql_num_rows($req);
if($avto>=1){
echo'<div class="log">';
While($ilok = mysql_fetch_assoc($req))
{
if($ilok['kto']=='system'){
$color='#AAAAAA';
}
if($ilok['kto']=='user'){
$color='#66FF66';
}
if($ilok['kto']=='enemy'){
$color='#FF6633';
}
echo '<small><font color="'.$color.'">'.$ilok['text'].'</font></small><br/>';
}
echo'</div>';
}
}

function log_duel($tlog){
global $log;
$req=mysql_query("SELECT * FROM `duellog` WHERE `opon` = '$log' OR `usr` = '$log' AND `timer` >= '".time()."' ORDER BY `id` DESC LIMIT 6");
$avto = mysql_num_rows($req);
if($avto>=1){
While($ilok = mysql_fetch_assoc($req))
{
if($ilok['usr']=="system"){
$color='#AAAAAA';
}
if($ilok['usr']!=="$log"){
$color='#FF6633';
}
if($ilok['usr']=="$log"){
$color='#CCCCCC';
}
if($ilok['umen']==1 AND $ilok['usr']=="$log"){
$color='#71CC71';
}
if($ilok['umen']==1 AND $ilok['usr']!=="$log"){
$color='#FF6633';
}
if($ilok['uron']>0){
echo '<font color="#7f9fae">'.$ilok['usr'].' </font><font color="'.$color.'">'.$ilok['text'].'</font> <font color="#7f9fae">'.$ilok['opon'].'</font> -'.$ilok['uron'].'<br/>';
}
else{
echo '<font color="#AAAAAA">'.$ilok['text'].'</font><br/>';
}
}
}}

function idlog($idlog){}
// Рефералы
function ref(){
global $log,$udata,$set;
if(!empty($udata['ref'])){
if($udata['lvl']>='10' AND $udata['ref']!='not'){
$req = mysql_query("SELECT `exp`, `money`, `usr` FROM `users` WHERE `id` = '$udata[ref]' LIMIT 1");
$avto = mysql_num_rows($req);
$refec = mysql_fetch_array($req);
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$udata[ref]' LIMIT 1");
$usdata = mysql_fetch_array($req);
$res = mysql_query("SELECT * FROM `set` WHERE `usr` = '$usdata[usr]' LIMIT 1");
$set = mysql_fetch_array($res);
if ($avto == 1) {
$refer = mysql_fetch_array($req);
$refer['almaz']=$usdata['almaz']+100;
$refer['exp']=$usdata['exp']+10000;
$time = date("H:i d.m.y");
$text = "Ваш друг $udata[usr] дошел до 10 уровня, Вы получили 100 золота и 10000 опыта!";
mysql_query("UPDATE `users` SET `almaz` = '$refer[almaz]', `exp`='$refer[exp]' WHERE `id` = '$udata[ref]' LIMIT 1");
mysql_query("UPDATE `set` SET `friend` = '$set[friend]'+'1' WHERE `usr` = '$usdata[usr]' LIMIT 1");
mysql_query("UPDATE `users` SET `ref` = 'not' WHERE `usr` = '$log' LIMIT 1");
mysql_query("INSERT INTO `msg_r` SET `user_from` = 'Журнал', `user_to` = '$refec[usr]', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
}
}
}
}

function vip(){
global $log,$udata,$set;
$sett = mysql_query("SELECT * FROM `set` WHERE `usr` = '$log' LIMIT 1");
$set = mysql_fetch_assoc($sett);
$time = time();
if($set['vip']=='on' AND $time >= $set['viptime']){
mysql_query("UPDATE `set` SET `vip` = 'off' WHERE `usr` = '$log' LIMIT 1");
$time = date("H:i d.m.y");
$text = "VIP-аккаунт закончился!";
mysql_query("INSERT INTO `msg_r` SET `user_from` = 'Журнал', `user_to` = '$log', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
}
}

function domin(){
global $udata;

$dom = mysql_fetch_assoc(mysql_query("SELECT * FROM domination WHERE id = '1'"));
$my=$dom['white'];
$enemy=$dom['black'];
$all=$my+$enemy;
if($my<$enemy){
$lid="<b>у Красных</b>";
}else{
$lid="<b>у Синих</b>";
}
if($my/$all<$enemy/$all){
$domin=round($enemy/$all * 100);
}else{$domin=round($my/$all * 100);
}
if($domin<='15'){
echo'Контроль над тюрягой '.$lid.'';
}elseif($domin<='30'){
echo'Контроль над тюрягой '.$lid.'';}elseif($domin<='45'){
echo'Контроль над тюрягой '.$lid.'';
}elseif($domin<='55'){
echo'Контроль над тюрягой ';}elseif($domin<='70'){
echo'Контроль над тюрягой '.$lid.'';
}elseif($domin<='85'){
echo'Контроль над тюрягой '.$lid.' ';
}elseif($domin<='100'){
echo'Контроль над тюрягой '.$lid.' ';
}
}

// Ауры
function aura(){
global $log,$udata;
$time=time();
$req = mysql_query("SELECT * FROM `aurs` WHERE `actimer`<'$time' AND `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto=='1'){
$req = mysql_query("SELECT * FROM `item_aura` WHERE `usr` = '$log' AND `status`='1' LIMIT 1");
$avto=mysql_num_rows($req);
if($avto=='1'){
$mag = mysql_fetch_array($req);
if($udata['hp']<$mag['hp']){
$hp=$udata['hp'];
}else{
$hp=$udata['hp']-$mag['hp'];
}
if($udata['mp']<$mag['mp']){
$mp=$udata['mp'];
}else{
$mp=$udata['mp']-$mag['mp'];
}
$nlovk=$udata['lovk']-$mag['lovk'];
$nsila=$udata['sila']-$mag['sila'];
$nprotect=$udata['prot']-$mag['protect'];
$nhp=$udata['hpall']-$mag['hp'];
$nmp=$udata['mpall']-$mag['mp'];
$nkrit=$udata['krit']-$mag['krit'];
$nukrit=$udata['ukrit']-$mag['ukrit'];
mysql_query("UPDATE `users` SET
`hp` = '$hp', `mp` = '$mp', `sila` = '$nsila',
`lovk` = '$nlovk',
`prot` = '$nprotect',
`hpall` = '$nhp', `mpall` = '$nmp',
`krit` = '$nkrit', `ukrit` = '$nukrit'
WHERE `usr` = '$log'") OR die (mysql_error());
mysql_query("DELETE FROM `item_aura` WHERE `id` = '$mag[id]' LIMIT 1");
mysql_query("DELETE FROM `aurs` WHERE `usr` = '$log' LIMIT 1");
// Пишем письмо
if($udata['klas']='fighert'){
$avtor="Журнал";
}else{
$avtor="Журнал";
}
$time = date("H:i d.m.y");
$text = 'Закончилось действие Ауры!';
mysql_query("INSERT INTO `msg_r` SET `user_from` = '$avtor', `user_to` = '$log', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
}
}
}
// Чистим Арену
function clear_arena(){}
// Кормим питомца
function pit_eda(){
global $log;
$time=time();
$req = mysql_query("SELECT `dies`,`eda`,`name`,`korm`,`status` FROM `pit` WHERE `usr` = '$log' LIMIT 1") or die(mysql_error());
$avto=mysql_num_rows($req);
if($avto>='1'){
$pit = mysql_fetch_assoc($req);
$proc=(($pit['korm']-$time)/$pit['eda'])*100;
if($proc<=0 and $pit['status']!="die"){
$pit['dies']=$pit['dies']+1;
mysql_query("UPDATE `pit` SET `hp` = '0',`dies`='$pit[dies]',`status`='die' WHERE `usr` = '$log' LIMIT 1");
// Пишем письмо
$time = date("H:i d.m.y");
$text = $pit['name'].' погиб от голода! Вы можете его воскресить в питомнике!';
mysql_query("INSERT INTO `msg_r` SET `user_from` = 'Журнал', `user_to` = '$log', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
}
}
}

function nav2($link, $link_name){
	echo '<br/><a href="'.$link.'">'.$link_name.'</a>';
}
function sec($sec){
$sec = htmlspecialchars(mysql_real_escape_string($sec));
	return $sec;
}

function navig($page, $link, $pages){
	for($i = 1; $i <= $pages; $i++){
		if($i != $page){
			echo '<a href="'.$link.'page='.$i.'">'.$i.'</a> ';
		}		else{
echo '<u>'.$i.'</u> ';
		}
	}
	if($pages>=1){
	echo '<br/>';
	}
}

if(isSet($log)){
$uuu=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `usr` = '$log'"));
$avto=mysql_num_rows(mysql_query("SELECT * FROM `karma` WHERE `usr` = '$log'"));
mysql_query("UPDATE `users` SET `karma` = '$avto' WHERE `usr` = '$log'");
$sup=$uuu['hpall']+$uuu['prot']+$uuu['lovk']+$uuu['sila'];
mysql_query("UPDATE `users` SET `super` = '$sup' WHERE `usr` = '$log'");
}
//@@@@@@ ЗДАНИЯ КЛАНА@@@@@
if(isSet($udata)){
$user_clan = mysql_fetch_array(mysql_query("SELECT * FROM `clan` WHERE `lider` = '".$udata['clan']."' "));
$bhpall = mysql_fetch_array(mysql_query("SELECT * FROM `clan_buildings` WHERE `clan` = '".$user_clan['name']."' AND `tip`='hpall' "));
$bmpall = mysql_fetch_array(mysql_query("SELECT * FROM `clan_buildings` WHERE `clan` = '".$user_clan['name']."' AND `tip`='mpall' "));
$bsila = mysql_fetch_array(mysql_query("SELECT * FROM `clan_buildings` WHERE `clan` = '".$user_clan['name']."' AND `tip`='sila' "));
$bprot = mysql_fetch_array(mysql_query("SELECT * FROM `clan_buildings` WHERE `clan` = '".$user_clan['name']."' AND `tip`='prot' "));
$my_bon_hpall = mysql_fetch_array(mysql_query("SELECT * FROM `clan_user_time` WHERE `user`='".$udata['usr']."' AND `tip`='hpall' "));
$my_bon_mpall = mysql_fetch_array(mysql_query("SELECT * FROM `clan_user_time` WHERE `user`='".$udata['usr']."' AND `tip`='mpall' "));
$my_bon_sila = mysql_fetch_array(mysql_query("SELECT * FROM `clan_user_time` WHERE `user`='".$udata['usr']."' AND `tip`='sila' "));
$my_bon_prot = mysql_fetch_array(mysql_query("SELECT * FROM `clan_user_time` WHERE `user`='".$udata['usr']."' AND `tip`='prot' "));

if($my_bon_hpall!=0 && $my_bon_hpall['time']<time()){
mysql_query("UPDATE `users` SET `hpall`='".($udata['hpall']-$my_bon_hpall['bonus'])."' WHERE `usr` ='".$udata['usr']."'   ");
mysql_query("DELETE FROM `clan_user_time` WHERE `tip`='hpall' AND `time`<'".time()."' AND `user`='".$udata['usr']."' ")or die(mysql_error());
}

if($my_bon_mpall!=0 && $my_bon_mpall['time']<time()){
mysql_query("UPDATE `users` SET `mpall`='".($udata['mpall']-$my_bon_mpall['bonus'])."' WHERE `usr` ='".$udata['usr']."'   "); 
mysql_query("DELETE FROM `clan_user_time` WHERE `tip`='mpall' AND `time`<'".time()."' AND `user`='".$udata['usr']."' ")or die(mysql_error());
}

if($my_bon_sila!=0 && $my_bon_sila['time']<time()){
mysql_query("UPDATE `users` SET `sila`='".($udata['sila']-$my_bon_sila['bonus'])."' WHERE `usr` ='".$udata['usr']."'   "); 
mysql_query("DELETE FROM `clan_user_time` WHERE `tip`='sila' AND `time`<'".time()."' AND `user`='".$udata['usr']."' ")or die(mysql_error());
}

if($my_bon_prot!=0 && $my_bon_prot['time']<time()){
mysql_query("UPDATE `users` SET `prot`='".($udata['prot']-$my_bon_prot['bonus'])."' WHERE `usr` ='".$udata['usr']."'   "); 
mysql_query("DELETE FROM `clan_user_time` WHERE `tip`='prot' AND `time`<'".time()."' AND `user`='".$udata['usr']."' ")or die(mysql_error());
}
}
/*
if(isset($textl) AND $textl=='Клановые Земли'){
mysql_query("UPDATE `metro_bands` SET `vbande`='yes' WHERE `user`='".$udata['usr']."' ");
}else{
mysql_query("UPDATE `metro_bands` SET `vbande`='no' WHERE `user`='".$udata['usr']."' ");
}
if(isset($textl) AND $textl=='Подземелье бой'){
mysql_query("UPDATE `metro_bands` SET `vbande`='yes',`vboi`='yes' WHERE `user`='".$udata['usr']."' ");
}else{
mysql_query("UPDATE `metro_bands` SET `vboi`='no' WHERE `user`='".$udata['usr']."' ");
}
$tsu = mysql_fetch_array(mysql_query("SELECT * FROM `metro_user_time` WHERE `user` = '".$udata['usr']."'  AND `time`<'".time()."'  "));
if($tsu!=0){
mysql_query("DELETE FROM `metro_user_time` WHERE `user`='".$udata['usr']."' AND `time`<'".time()."' ") or die (mysql_error());
}
$moya_banda = mysql_fetch_array(mysql_query("SELECT * FROM `metro_bands` WHERE `user`='".$udata['usr']."' "));
*/
// Навигация
function place_okr(){}
// Навигация
function place_zamok(){}
// Навигация
function place_tower(){}
// Навигация
function place_city(){}
//////////////////////////////////////
function check($msg) {
	if (is_array($msg)) {
		foreach($msg as $key => $val) {
			$msg[$key] = check($val);
		} 
	} else {
		$msg = htmlspecialchars($msg);
		$search = array('|', '\'', '$', '\\', '^', '%', '`', "\0", "\x00", "\x1A", chr(226) . chr(128) . chr(174));
		$replace = array('&#124;', '&#39;', '&#36;', '&#92;', '&#94;', '&#37;', '&#96;', '', '', '', '');

		$msg = str_replace($search, $replace, $msg);
		$msg = stripslashes(trim($msg));
	} 

	return $msg;
} 

function sint($in){
$in=abs(intval($in));
return $in;
}
function userr($id){
$reqi = mysql_query("SELECT lvl,storona,usr FROM `users` WHERE `id` = '$id' LIMIT 1");
$ref = mysql_fetch_assoc($reqi);

$seti = mysql_query("SELECT vip FROM `set` WHERE `usr` = '$ref[usr]' LIMIT 1");
$refi = mysql_fetch_assoc($seti);

// //////////////////////////
$avto = mysql_num_rows($reqi);
if($avto>=1){

if($refi[vip]==on){echo' <img src="pic/vip.gif" alt="#"/>';}else
{echo' <img src="pic/main/'.$ref['storona'].'.png" alt="#"/>';}
echo'<a href="search.php?nick='.$ref[usr].'&go=go"><font color="white">'.$ref[usr].'</font>';
echo'<img src="pic/main/up.png" alt="lvl"/>'.$ref['lvl'].'<font color="white"></a>';
}
}
function cus($id,$idr){
$reqi = mysql_query("SELECT * FROM `catacomb_user` WHERE `id` = '$id' and `id_room` = '$idr' LIMIT 1");
if(mysql_num_rows($reqi)){return 1;}else{return 0;}
}
function cuss($id){
$reqi = mysql_query("SELECT * FROM `catacomb_user` WHERE `id` = '$id' LIMIT 1");
if(mysql_num_rows($reqi)){return 1;}else{return 0;}
}
if(cuss($udata[id]))
{
$reqir = mysql_query("SELECT * FROM `catacomb_user` WHERE `id` = '$udata[id]' LIMIT 1");
$cvvcr = mysql_fetch_assoc($reqir);
$reqi = mysql_query("SELECT * FROM `catacomb_room` WHERE `id` = '$cvvcr[id_room]' LIMIT 1");
$cvvc = mysql_fetch_assoc($reqi);
if($cvvc[level]==0){;if($_SERVER['PHP_SELF']!="/catacomb_room.php"){ header("Location: /catacomb_room.php?id=$cvvc[id]");}}
if($cvvc[level]>0){if($_SERVER['PHP_SELF']!="/catacomb.php"){ header("Location: /catacomb.php?");}}

}


$cset_time=60; //время  старта
$cset_urovni=3; //колличество уровней
$cset_min_user=3; //минимум в комнату
$cset_max_user=5; //максимально в комнату
$catacomb_rock=1; //стоимость пропуска

////////////////////////////////

?>