<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

if($user['orden']==''){
    header('Location: /main');
    exit;
}

$orden = mysql_fetch_assoc(mysql_query("SELECT * FROM `orden` WHERE `lider`='$user[orden]'"));

switch($sd){
default:

echo "<b>$orden[name]</b> ";

if($orden['lider']==$uid || $user['orden_title']=='2'){
    echo "[<a href='/orden/admin'>управление</a>]";
}
@$ordenexp=round((($orden['exp']-$ol3)/($op3-$ol3))*100);


echo '<center><font size=1 color=grey>Опыт</font></center><div class="status_style status_big" style="background-color:#24404A">
<div class="status_style status_big" style="width:'.$ordenexp.'%; background-color:#086D8F;"></div>
</div><center><font size=1 color=grey>До '.$nolvl=$orden['level']+'1'.''.$nolvl.' уровня: '.$ordenexp.'% ('.number_format($orden['exp']).' / '.number_format($op3).')</font></center><br>';

$allpers = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `orden`='$orden[lider]'"));
echo "<small>Уровень: $orden[level]<br>
<a href='/orden/list'>Состав: </a>$allpers/$orden[max_user]<br>
Казна: <img src='../img/silver.png'> ".number_format($orden['silver'])." <img src='../img/almaz.png'> <font color=violet>".number_format($orden['almaz'])."</font><br><br>";


echo "<a href='/orden/topup'>Пополнить казну</a><br>
<a href='/orden/log'>История действий</a><br>";

// чат

echo "</div><div class='last_news'>";

$num = 10; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `chat_orden` WHERE `orden`='$orden[id]'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num; 


$tnick = mysql_real_escape_string(htmlspecialchars($_GET['nick']));
if($tnick!==''){$tonick="$tnick,";}else{$tonick="";}

$gupdate = mysql_real_escape_string(htmlspecialchars($_GET['update']));

if($gupdate){
    header('Location: /orden');
    exit;
}


echo "<form action='/orden/send' method='post'>
<textarea name='text' class='enter'>$tonick </textarea><br>
<input type='submit' value='Написать' class='enter'></form> <a href='/orden/?update'>Обновить</a><hr>";

$q = mysql_query("SELECT * FROM `chat_orden` WHERE `orden`='$orden[id]' ORDER BY `id` DESC LIMIT $start,$num"); 
$e = mysql_num_rows($q);
if($e=='0'){
    echo "Чат пуст.";
    include ('tmp/foot.php');
    exit;
}


While($w = mysql_fetch_assoc($q)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    
    $obr = explode(",", $w['text']);
    if($obr['0']==$log){$text="<u><i>$w[text]</i></u>";}else{$text=$w['text'];};
    
    echo "<a href='/userid$w[user]'>$us[login]</a>:<br><font color=$us[color_chat]>$text</font><br><small>[<a href='/orden/?nick=$us[login]'>Ответить</a>]</small><div class='linem'></div>";
    
    
}


if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 

echo "</div>";

break;

case 'setst':
if($user['orden_title']=='4' or $user['orden_title']=='5'){
	header('Location: /orden/structure');
	exit;	
}




break;



case 'admin':

if($orden['lider']!==$uid and $user['orden_title']!=='2'){
    header('Location: /orden');
    exit;
}


echo "<a href='/orden/topuplog'>Казна</a><br>";


echo "<hr>← <a href='/orden'>назад</a>";

break;


case 'invite':
if($user['orden_title']=='4' or $user['orden_title']=='5'){
	header('Location: /orden');
	exit;	
}

$kolu = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `orden`='$orden[lider]'"));

if($kolu>=$orden['max_user']){
    header('Location: /orden');
    exit;
}

$q = mysql_query("SELECT * FROM `users` WHERE `id`='$id'");
$e = mysql_num_rows($q);
if($e=='0'){
	header('Location: /orden');
	exit;
}
$us = mysql_fetch_assoc($q);

if($us['level']<'8'){
    header("Location: /userid$id");
    exit;
}


if($us['orden']!=='' and $us['storona']!==$orden['storona']){
	header('Location: /orden');
	exit;	
}

mysql_query("INSERT INTO `orden_invite` SET `orden`='$orden[id]',`to`='$us[id]'");

header('Location: /orden');
exit;

break;


case 'topuplog':

if($orden['lider']!==$uid and $user['orden_title']!=='2'){
    header('Location: /orden');
    exit;
}

$num = 20; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `orden_topup` WHERE `orden`='$orden[id]'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num; 




$q = mysql_query("SELECT * FROM `orden_topup` WHERE `orden`='$orden[id]' ORDER BY `id` DESC LIMIT $start,$num"); 
$e = mysql_num_rows($q);
if($e=='0'){
    echo "лог пуст.";
    include ('tmp/foot.php');
    exit;
}


While($w = mysql_fetch_assoc($q)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    
  
    echo "<a href='/userid$w[user]'>$us[login]</a>: положил ";
    if($w['silver']!=='0'){echo "<img src='/img/silver.png'> $w[silver] ";}
    if($w['almaz']!=='0'){echo "<img src='/img/almaz.png'> $w[almaz] ";}
	echo "($w[time])<br>";
	
}

echo "<hr>";

if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 	
	
echo "<hr>← <a href='/orden'>назад</a>";

break;

case 'topup':

if(empty($_POST['submit'])){
	
	echo "<form action='/orden/topup' method='post'>
	<img src='../img/silver.png'>: <input type='text' name='silver' class='enter'><br>
	<img src='../img/almaz.png'>: <input type='text' name='almaz' class='enter'><br>
	<input type='submit' name='submit' value='Пополнить' class='enter'></form>";

}else{
	
	$silver = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_POST['silver'])));
	$almaz = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_POST['almaz'])));
	
	if($silver<'0' or $almaz<'0'){
		header('Location: /orden');
		exit;
	}
	
	if($silver>$user['silver'] or $almaz>$user['almaz']){
		header('Location: /orden');
		exit;
	}
	
	mysql_query("UPDATE `users` SET `silver`=`silver`-'$silver',`almaz`=`almaz`-'$almaz' WHERE `id`='$uid'");
	
	mysql_query("UPDATE `orden` SET `silver`=`silver`+'$silver',`almaz`=`almaz`+'$almaz' WHERE `id`='$orden[id]'");
	
	$kg=date("d.m H:i:s");
	
	mysql_query("INSERT INTO `orden_topup` SET `orden`='$orden[id]',`user`='$uid',`silver`='$silver',`almaz`='$almaz',`time`='$kg'");
	
	header('Location: /orden/topup');
	
	exit;
	
}

echo "<hr>← <a href='/orden'>назад</a>";

break;


case 'title':

if($orden['lider']!==$uid and $user['orden_title']=='3' or $user['orden_title']=='4' or $user['orden_title']=='5'){
    header('Location: /orden');
    exit;
}

$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$id'"));

echo "<a href='/userid$us[id]'>$us[login]</a> ";
if($us['orden_title']=='1'){echo "[Глава] ";}
if($us['orden_title']=='2'){echo "[Зам. Главы] ";}
if($us['orden_title']=='3'){echo "[Генерал] ";}
if($us['orden_title']=='4'){echo "[Солдат] ";}
if($us['orden_title']=='5'){echo "[Новобранец] ";}
echo "(ко. ".number_format($us['orden_exp']).") <br>";
		
if(empty($_POST['submit'])){
		
	echo "<form action='/orden/title/$id' method='post'>
	Должность:<br>
	<select name='title' class='enter'>";
    if($user['orden_title']=='1'){
        echo "<option value='2'>Зам.Главы</option>";
    }
	echo "<option value='3'>Генерал</option>
	<option value='4'>Солдат</option>
	<option value='5'>Новобранец</option>
	</select><br>
	<input type='submit' name='submit' value='Назначить' class='enter'></form>";

}else{
	
	$title = (int)mysql_real_escape_string($_POST['title']);
	
	if($title<='1' and $title>='6'){
		header("Location: /orden/title/$id");
		exit;
	}
	$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$id'"));

	if($us['orden_title']=='1' or $us['orden_title']=='2' and $user['orden_title']!=='1'){
		header("Location: /orden/title/$id");
		exit;
	}
	
	mysql_query("UPDATE `users` SET `orden_title`='$title' WHERE `id`='$id' and `orden`='$orden[lider]'");
	
	header("Location: /orden/title/$id");
	exit;
	
}

echo "<hr>← <a href='/orden/list'>назад</a>";

break;

case 'log':


$num = 5; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `orden_log` WHERE `orden`='$orden[id]'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num;

$q = mysql_query("SELECT * FROM `orden_log` WHERE `orden`='$orden[id]' ORDER BY `id` DESC LIMIT $start, $num");
$e = mysql_num_rows($q);
if($e>='1'){
While($w = mysql_fetch_assoc($q)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    
    echo "$us[login]: $w[text]<br>";
    
}

if ($page != 1) $pervpage = '<a href=?sd=comment&id='.$id.'&page=1>««</a> 
                               <a href=?sd=comment&id='.$id.'&page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?sd=comment&id='.$id.'&page='. ($page + 1) .'>»</a> 
                                   <a href=?sd=comment&id='.$id.'&page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?sd=comment&id='.$id.'&page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?sd=comment&id='.$id.'&page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?sd=comment&id='.$id.'&page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?sd=comment&id='.$id.'&page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 
if($e2>='5' or $page!='1'){
    echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage.'<hr>'; 
}
}else{
    echo "История пуста.";
}


echo "<hr>← <a href='/orden'>назад</a>";

break;


case 'oust':

if($orden['lider']!==$uid and $user['orden_title']=='3' or $user['orden_title']=='4' or $user['orden_title']=='5'){
    header('Location: /orden');
    exit;
}

$q = mysql_query("SELECT * FROM `users` WHERE `id`='$id' and `orden`='$orden[lider]'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: /orden/list');
    exit;
}
$w = mysql_fetch_assoc($q);

if($w['id']==$orden['lider']){
    header('Location: /orden/list');
    exit;
}

if(isset($_GET['oustyes'])){
    
    mysql_query("UPDATE `users` SET `orden`='',`orden_title`='0',`orden_exp`='0' WHERE `id`='$w[id]'");
    mysql_query("INSERT INTI `orden_log` SET `user`='$uid',`orden`='$orden[id]',`text`='Игрок <a href=userid$w[id]>$w[log]</a> выгнан из ордена игроком $log'");
    
    header('Location: /orden/list');
    exit;
    
}

echo "Выгнать игрока <a href='/userid$w[id]'>$w[login]</a> из ордена?<br>
<a href='/orden/oust/$w[id]?oustyes'>Да</a> | <a href='/orden/list'>Нет</a>";

break;


case 'list':

$num = 10; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `users` WHERE `orden`='$orden[lider]'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num;


$q = mysql_query("SELECT * FROM `users` WHERE `orden`='$orden[lider]' ORDER BY `orden_title` ASC, `orden_exp` DESC LIMIT $start, $num");

While($w = mysql_fetch_assoc($q)){
		
		echo "<small> ";
        if($w['online']>=time()){echo "<font color=lime>•</font>";}else{echo "<font color=red>•</font>";}
        echo " <a href='/userid$w[id]'>$w[login]</a> ";
		if($w['orden_title']=='1'){echo "[Глава] ";}
		if($w['orden_title']=='2'){echo "[Зам. Главы] ";}
		if($w['orden_title']=='3'){echo "[Генерал] ";}
		if($w['orden_title']=='4'){echo "[Солдат] ";}
		if($w['orden_title']=='5'){echo "[Новобранец] ";}
		echo "(ко. ".number_format($w['orden_exp']).") ";
		if($orden['lider']==$uid or $user['orden_title']<='2'){
			echo "[<a href='/orden/title/$w[id]'>звание</a>]";
		}
        if($user['orden_title']<='2'){
            echo " [<a href='/orden/oust/$w[id]'>выгн.</a>]";
        }
		echo "</small><br>";
	
}



if ($page != 1) $pervpage = '<a href=/orden/list/?page=1>««</a> 
                               <a href=/orden/list/?page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=/orden/list/?page='. ($page + 1) .'>»</a> 
                                   <a href=/orden/list/?page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=/orden/list/?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=/orden/list/?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=/orden/list/?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=/orden/list/?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

if($posts>='11' or $page!='1'){
	echo '<hr>';
	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 
}

echo "<hr>← <a href='/orden'>назад</a>";

break;


case 'send':

if($user['np']=='ok'){
	header('Location: save.php');
	exit;
}


$text = trim(mysql_real_escape_string(htmlspecialchars($_POST['text'])));

$text=str_replace(".h2m.ru","[реклама]",$text);
$text=str_replace(".ru","[реклама]",$text);
$text=str_replace(".su","[реклама]",$text);
$text=str_replace(".tk","[реклама]",$text);
$text=str_replace(".org","[реклама]",$text);
$text=str_replace(".net","[реклама]",$text);
$text=str_replace(".org.ru","[реклама]",$text);
$text=str_replace(".net.ru","[реклама]",$text);
$text=str_replace(".com.ru","[реклама]",$text);
$text=str_replace(".pp.ru","[реклама]",$text);
$text=str_replace(".wup.ru","[реклама]",$text);
$text=str_replace(".gb7.ru","[реклама]",$text);
$text=str_replace(".ts6.ru","[реклама]",$text);
$text=str_replace(".us","[реклама]",$text);
$text=str_replace(".ua","[реклама]",$text);
$text=str_replace(".com.ua","[реклама]",$text);
$text=str_replace(".org.ua","[реклама]",$text);
$text=str_replace(".net.ua","[реклама]",$text);
$text=str_replace(".pp.ua","[реклама]",$text);

if($text==''){
    header('Location: /orden');
    exit;
}

mysql_query("INSERT INTO `chat_orden` SET `orden`='$orden[id]',`user`='$uid',`text`='$text',`time`='".time()."'");
 
header('Location: /orden/?page='.$_GET['page'].'');
exit;
 
break;


}
include ('tmp/foot.php');
?>