<?php

require_once('conf/dbc.php');
require_once('conf/session_start.php');
require_once('conf/ban.php');
$page_title = 'Отряд';
require_once('conf/head.php');
if ((isset($_SESSION['id'])) and (isset($_SESSION['nick'])))  {
$user_id = abs(intval($_SESSION['id']));
////////////////////////////////////Записали локацию
  $query = "Select  location from users where id = '$user_id'  limit 1";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД6');
  $row = mysqli_fetch_array($result);
  $location = $row['location'];
  if ($location == 'company') {
    }
else {
$query_loc = "update users set location = 'company' where id = '$user_id' limit 1";
$result_loc = mysqli_query($dbc, $query_loc) or die ('Ошибка передачи запроса к БД');
}
  $show_ad = 1;
  require_once('conf/top.php');
$user_id = abs(intval($_SESSION['id']));
}
$usg3=$dbc->query("SELECT * FROM `users` WHERE`id`='$user_id'")->fetch_assoc();
if($usg3['clan_rang']=='9' and $usg3['clan']==$_GET['company_id'] ){
$fn=$_FILES['ava']['name'];
//$fnr=end(explode('.',$fn));
if($fnr=='jpg' or $fnr=='JPG' or $fnr=='GIF' or $fnr=='gif' or $fnr=='png' or $fnr=='PNG'){

if($usg3['money']<10000){die('Нехватает Rub');}
if(copy($_FILES['ava']['tmp_name'],'img/clan_av/'.md5($fn).'.png')){
$dbc->query("UPDATE `clans` SET`ava`='".md5($fn).".png'WHERE`clan_id`='".$_GET['company_id']."'")or die('SQL ERROR');
$rt=$dbc->query("SELECT `money`FROM`users`WHERE`id`='".$_SESSION['id']."'")->fetch_assoc();
$rsm=$rt['money']-10000;
$dbc->query("UPDATE`users`SET`money`='$rsm' WHERE`id`='".$_SESSION['id']."'");
echo'Аватар успешно загружен';}else{
echo'Ошибка загрузки';}
}else{
if(isset($fn)){
echo'Неверный формат изображения!';}
}

}
$clan_id = abs(intval($_GET['company_id']));
if (empty($clan_id)) {
require_once('conf/notclan.php'); 
}
else {
  $query_out = "Select clan, clan_rang, nick from users where id = '$user_id'";
  $result_out = mysqli_query($dbc, $query_out) or die ('Ошибка передачи запроса к БД');
  $row_out = mysqli_fetch_array($result_out);
  $clan_out = $row_out['clan'];
  $rang_out = $row_out['clan_rang'];
  $nick_out = $row_out['nick'];
$rule = filter($_GET['rule']);
  if (!empty($rule)) {
    if ($clan_id <> $clan_out or $rang_out < 6) {
	?>
    <script type="text/javascript">
    document.location.href = "company.php?company_id=<?php echo "$clan_id";?>";
    </script>
    <?php
	exit();
	}
  }
  $query = "Select * from clans where clan_id = '$clan_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $row = mysqli_fetch_array($result);
  if ($row == 0) {
    require_once('conf/notclan.php'); 
  }
  else {
    $name = $row['name'];
	$gruppa = $row['gruppa'];
	$clan_opit = $row['clan_opit'];
          $slava = $row['slava'];
	$mentor = $row['mentor'];
echo '<center><div class="name2">Отряд: ';
	  if ($row['gruppa'] == 'svoboda') {?><img src="img/ico/svobodaon.png" width="12" height="12"/><?php }        
	  if ($row['gruppa'] == 'dolg') {?><img src="img/ico/dolgon.png" width="12" height="12"/><?php }
	  if ($row['gruppa'] == 'naemniki') {?><img src="img/ico/odinochkion.png" width="12" height="12"/><?php } 
	  echo " $name"; ?></center></div>
 
    <?php
	$total = $row['people']; 
	?>
	
	<?php
	///////////////////////Объява=============
	if (!empty($show_ad) and empty($read_ad)) {
  $query_ad = "select ad_nick, ad from clans where clan_id = '$clan_ad'";
  $result_ad = mysqli_query($dbc, $query_ad) or die ('Ошибка передачи запроса к БД');
  $row_ad = mysqli_fetch_array($result_ad);
  $ad = $row_ad['ad'];
  $ad_nick = $row_ad['ad_nick'];
  if (!empty($ad) and $clan_out == $clan_id) {
  ?>
  <?php }?>
  <?php
}
///////////////////////Объява=============КОНЕЦ


	///////////////////////////////Ошибка
$err = filter($_GET['err']);
	  if (!empty($err)) {
	  ?>
	  <?php
	    if ($err = 'out') { 
echo "В клане никого не должно быть.";}
	  ?>
	  </div>
	  <?php
	  }
	  //////////////////////////Конец ошибки
	  
	  ////////////Объявление.
	 if (!empty($rule)) {
	 if(isset($_POST['addad'])) {
	   $add_err = 0;
$ad = filter($_POST['ad']);
	   $ad = mysqli_real_escape_string($dbc, trim($ad));	
	   $ad_num = strlen($ad);
if ($ad_num>250) {
	     $add_err = 1;
?>
Длина объявления должна быть не больше 250 символов
<?php
	   }
	   if ($ad_num == 0 and $add_err == 0) {
	     $query_num = "update clans set ad = '$ad' where clan_id = '$clan_id'";
		 $result_num = mysqli_query($dbc, $query_num) or die ('Ошибка передачи запроса к БД');
		 $query_read = "update users set read_ad = '0' where clan = '$clan_id'";
		 $result_read = mysqli_query($dbc, $query_read) or die ('Ошибка передачи запроса к БД');
		 ?>
         <script type="text/javascript">
         document.location.href = "company.php?company_id=<?php echo "$clan_id";?>&rule=1";
         </script>
         <?php
	     exit();
	   }
	   if ($ad_num <> 0 and $add_err == 0) {
	     $query_num = "update clans set ad = '$ad', ad_nick = '$nick_out' where clan_id = '$clan_id'";
		 $result_num = mysqli_query($dbc, $query_num) or die ('Ошибка передачи запроса к БД');
		 $query_read = "update users set read_ad = '1' where clan = '$clan_id'";
		 $result_read = mysqli_query($dbc, $query_read) or die ('Ошибка передачи запроса к БД');
		 ?>
         <center><script type="text/javascript">
         document.location.href = "company.php?company_id=<?php echo "$clan_id";?>&rule=1";
         </script><p><center>
         <?php
	     exit();
	   }
	 }
$adbot=filter($_POST['ad']);
	 ?> 
	 <div style="background-color: #1E1E1E;">
<p style="border-top:1px solid #444e4f;"></p>
<center><b>Новое объявление:</b></center>
<p style="border-top:1px solid #444e4f;"></p>
</div>
	 <form enctype="multipart/form-data" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
	 <input type="text" class="input" style="width: 80%;" name="ad" value="<?php echo "$adbot"; ?>" />
     <input type="submit" style="width:35px;" class="input" value="+" name="addad"/>
     </form>
	 <?php 
	 }
	 //////////////////////////Конец обьявы
	?>
	<?php
    $query_lvl = "Select lvl, opit from clan_opit order by lvl desc";
	  $result_lvl = mysqli_query($dbc, $query_lvl) or die ('Ошибка передачи запроса к БД');
	  $row_lvl = mysqli_fetch_array($result_lvl);
	  $big_next_lvl = $row_lvl['opit'];
	  $lvl=$row_lvl['lvl'];
	  while (($clan_opit/1000)< $row_lvl['opit']) {
	  $next_lvl = $row_lvl['opit'];
	  $lvl=($lvl-1);
	  $row_lvl = mysqli_fetch_array($result_lvl);
	  }
	  if ($next_lvl == 0) {
	    $next_lvl = "$big_next_lvl" ;
	  } 
	$next_lvl = ($next_lvl/100);
	$clan_opit = ($clan_opit/100000);
	$clan_opit = round($clan_opit,2);
	$query_c = "Select * from users where clan = '$clan_id' and last_active > NOW() - (60*5)";
    $result_c = mysqli_query($dbc, $query_c) or die ('Ошибка передачи запроса к БД');
    $row_c = mysqli_num_rows($result_c);
header('location: clan.php?id='.$clan_id.'');
echo '
<div style="background-color: #0b0b0b;"><style="border-top:1px solid #000000; background-color:#000000;">

 ';
if(strlen($row['ava'])>4){
echo'<center><img src="/img/clan_av/'.$row['ava'].'" width="260" height="210" /></center>';}
elseif ($row['gruppa'] == 'svoboda') {?><center><img src="img/ico/svobodaclan.png" width="210" height="160"/></center><?php }
elseif ($row['gruppa'] == 'dolg') {?><center><img src="img/ico/dolgclan.png" width="210" height="160"/></center><?php }
elseif ($row['gruppa'] == 'naemniki') {?><center><img src="img/ico/odinochkiclan.png" width="210" height="160"/></center><?php 
}
echo '</div>';
?><div style="border-left:1px solid #444e4f;border-right:1px solid #444e4f;">

<div style="background-color: #1E1E1E;">
<p class="podmenu" style="border-top:1px solid #444e4f; background-color:#1c252f;"></p>
<img src="img/ico/lvl.gif" width="12" height="12" alt="н"/><span class="darkkhaki">Уровень: </span><span class="white"><?php echo "$lvl"?></span>
</p><img src="img/ico/slava.png" width="12" height="12" alt="н"/><span class="darkkhaki">Слава: </span><span class="white"><?php echo "$slava"?></span>


</p>
<img src="img/ico/star.png" width="12" height="12"/><span class="darkkhaki">Опыт: </span><span class="white"><?php echo "$clan_opit"; ?>к / <?php echo "$next_lvl";?></span>

<?php
echo '<div class="pt">';
	  if ($row['gruppa'] == 'svoboda') {?> <img src="img/ico/svobodaon.png" width="12" height="12"/><span class="darkkhaki">Группировка: </span><span class="white">Свобода</p></span><?php }
	  if ($row['gruppa'] == 'dolg') {?><img src="img/ico/dolgon.png" width="12" height="12"/><span class="darkkhaki">Группировка: </span><span class="white">Долг</p></span><?php }
	  if ($row['gruppa'] == 'naemniki') {?> <img src="img/ico/odinochkion.png" width="12" height="12"/><span class="darkkhaki">Группировка: </span><span class="white">Одиночки</p></span><?php }
echo '</div><p class="podmenu" style="border-top:1px solid #444e4f; background-color:#1c252f;"></p>';

if ($clan_out == $clan_id) {?>
<div class="link3">
<a class="link3" href="mentor.php?company_id=<?php echo "$clan_id";?>"> <img src="img/ico/dos.png" width="12" height="12"/>Бонус зоны (<?php echo "$mentor" ?>ур.)</a>
</div><?php }
}
}
?>
<div class="link3">
<?php

 ?>
<a class="link3" href="forum.php?type=company&company=<?php echo "$clan_id"?>"><img src="img/ico/chat.png" width="12" height="12"/>Форум</a>
</div>
	<?php if ($clan_out == $clan_id) {?>
<div class="link3">
<a class="link3" href="chatcompany.php"><img src="img/ico/m-antispam.png" width="12" height="12"/>Чат отряда</a></div>
<div class="link3">
<a class="link3" href="gerb.php?company_id=<?php echo "$clan_id"; ?>">
<?php
	  if ($row['gruppa'] == 'svoboda') {?><img src="img/ico/svobodaon.png" width="12" height="12"/></span><?php }
	  if ($row['gruppa'] == 'dolg') {?><img src="img/ico/dolgon.png" width="12" height="12"/></span><?php }
	  if ($row['gruppa'] == 'naemniki') {?> <img src="img/ico/odinochkion.png" width="12" height="12"/></span><?php }?> Герб</a>
</div>
      <?php }?>
<div class="link3">
<a class="link3" href="onlinecom.php?company_id=<?php echo "$clan_id";?>"><img src="img/ico/on.png" width="12" height="12"/>Онлайн <?php echo "$row_c"; ?></a></div>
<?php
if($clan_out == $clan_id) {
http://wapstalker.ru/img/ico/forum.png
echo '<div class="link3">
<a class="link3" href="clan_log.php"><img src="img/ico/forum.png" width="12" height="12"/>Журнал отряда</a></div><div class="link3">
<a class="link3" href="cbon.php"><img src="img/ico/dos.png" width="12" height="12"/>Отрядные Постройки</a></div>';
echo '<div style="background-color: #1E1E1E;">';
echo '<p class="podmenu" style="border-top:1px solid #444e4f; background-color:#1c252f;"></p>';
$money = $row['money'];
$habar = $row['habar'];
echo '<div style="background-color: #1E1E1E;">';
echo 'Общая казна отряда RUB: '.$money.'</div>';
echo '<div class="pt">';
echo 'Общая казна отряда Хабар: '.$habar.'</div>';
echo '<div class="foot"><a style="text-decoration:none;" href="clanp.php?act=p"><span class="bonus">[Пополнить]</span></a>';
echo '<p class="podmenu" style="border-top:1px solid #444e4f; background-color:#1c252f;"></p></div></div>';
}
 ?>
<p class="link">

<p class="link3"><img src="img/ico/chel.png" width="12" height="12"/>Сталкеры отряда <?php echo "[ $total ]"; ?></a></p>


	<?php
	if (!empty($_GET['page'])) {
$cur_page = abs(intval($_GET['page']));
	}
	else {
	$cur_page = 1;
	}
	$result_per_page = 10;
	$skip = (($cur_page - 1) * $result_per_page);
	$num_page = ceil($total / $result_per_page);
	if ($num_page > 0) {
	  $query_us = "Select nick ,slava,id, clan_rang, ko, last_active  from users where clan = '$clan_id' order by  clan_rang DESC, ko and last_active > NOW()-(300*5) DESC limit $skip, $result_per_page";

      $result_us = mysqli_query($dbc, $query_us) or die ('Ошибка передачи запроса к БД');
	  while ($row_us = mysqli_fetch_array($result_us)) {
	    $nick = $row_us['nick'];
	    $clan_rang = $row_us['clan_rang'];
	    $id_us = $row_us['id'];
		$ko = $row_us['ko'];
		$ko = ($ko/100000);
		$ko = round($ko,2);
		$last_active = $row_us['last_active'];
$slava = $row_us['slava'];
		$last_active = strtotime("$last_active");
		$now = (date("Y-m-d H:i:s"));
        $now = strtotime("$now");
		$razn_last_act = ($now - $last_active);

echo '<div class="menudiv">';

echo '<a class="k_menu"  style="text-decoration:none;" href="profile.php?id='.$id_us.'">';
if ($row_us['clan_rang'] == '1') {
if ($razn_last_act > 300 ) {?>
<img src="img/rangs/rekryt.png" width="12" height="12" alt="н"/><?php } else {?><img src="img/rangs/rekryton.png" width="12" height="12" alt="н"/><?php }}
	  if ($row_us['clan_rang'] == '2') { if ($razn_last_act > 300 ) {?><img src="img/rangs/ryadovoy.png" width="12" height="12"alt="р"/><?php } else {?><img src="img/rangs/ryadovoyon.png" width="12" height="12"alt="р"/><?php }}
	  if ($row_us['clan_rang'] == '3') { if ($razn_last_act > 300 ) {?><img src="img/rangs/serjant.png" width="12" height="12" alt="с"/><?php } else {?><img src="img/rangs/serjanton.png" width="12" height="12" alt="с"/><?php }}
	  if ($row_us['clan_rang'] == '4') { if ($razn_last_act > 300 ) {?><img src="img/rangs/leitenant.png" width="12" height="12" alt="л"/><?php } else {?><img src="img/rangs/leitenanton.png" width="12" height="12" alt="л"/><?php }}
	  if ($row_us['clan_rang'] == '5') { if ($razn_last_act > 300 ) {?><img src="img/rangs/kapitan.png" width="12" height="12" alt="к"/><?php } else {?><img src="img/rangs/kapitanon.png" width="12" height="12" alt="к"/><?php }}
	  if ($row_us['clan_rang'] == '6') { if ($razn_last_act > 300 ) {?><img src="img/rangs/mayor.png" width="12" height="12" alt="м"/><?php } else {?><img src="img/rangs/mayoron.png" width="12" height="12" alt="м"/><?php }}
	  if ($row_us['clan_rang'] == '7') { if ($razn_last_act > 300 ) {?><img src="img/rangs/polkovnik.png" width="12" height="12" alt="п"/><?php } else {?><img src="img/rangs/polkovnikon.png" width="12" height="12" alt="п"/><?php }}
	  if ($row_us['clan_rang'] == '8') { if ($razn_last_act > 300 ) {?><img src="img/rangs/general.png" width="12" height="12" alt="г"/><?php } else {?><img src="img/rangs/generalon.png" width="12" height="12" alt="г"/><?php }}
if ($row_us['clan_rang'] == '10') { if ($razn_last_act > 300 ) {?><img src="img/rangs/marshal.png" width="12" height="12" alt="л"/><?php } else {?><img src="img/rangs/marshalon.png" width="12" height="12" alt="м"/><?php }}
if ($row_us['clan_rang'] == '9') { if ($razn_last_act>300 ) {?><img src="img/rangs/lider.png" width="12" height="12" alt="п"/><?php } else {?><img src="img/rangs/lideron.png"width="12"height="12"alt="л"/><?php }}
	  ?>
<?php echo "$nick";?><span class="white">  (<?php echo "$ko";?>к)</span></a><?php if (!empty($rule) and $rang_out>$clan_rang) { if ($rang_out>($clan_rang+1)) {?>[<a style="text-decoration:none;" href="rang.php?inf=up&id=<?php echo "$id_us";?>">Повысить</a>]<?php }  if (0<($clan_rang-1)) {?> [<a style="text-decoration:none;" href="rang.php?inf=down&id=<?php echo "$id_us";?>">Понизить</a>]<?php }   if ($rang_out==9) {?> [<a style="text-decoration:none;" href="rang.php?inf=lider&id=<?php echo "$id_us";?>">+Лидер</a>]<?php }?>  [<a style="text-decoration:none;" href="agree.php?inf=outuser&id=<?php echo "$id_us";?>">Исключить</a>]<?php }?>
<?php
	  if ($row_us['clan_rang'] == '1') {?> -Рекрут<?php }
	  if ($row_us['clan_rang'] == '2') {?> -Рядовой<?php }
	  if ($row_us['clan_rang'] == '3') {?> -Сержант<?php }
	  if ($row_us['clan_rang'] == '4') {?> -Лейтенант<?php }
	  if ($row_us['clan_rang'] == '5') {?> -Капитан<?php }
	  if ($row_us['clan_rang'] == '6') {?> -Майор<?php }
	  if ($row_us['clan_rang'] == '7') {?> -Полковник<?php }
	  if ($row_us['clan_rang'] == '8') {?><span class="bonus"> -Генерал</span><?php }
	  if ($row_us['clan_rang'] == '9') {?><span class="yellow"> -Лидер</span><?php } 
	  $rank = $row['clan_rang'];
	  ?> 
   <p class="zx"></p><?php
}
	  }
$phpself= filter($_SERVER['PHP_SELF']);
echo '<div class="pt">';
	  if ($cur_page > 1) {
	    echo '<a href="' . "$phpself" . '?page=' . ($cur_page=1) . '&company_id=' .$clan_id . '&rule=' . $rule .  '"><<</a> ';
      }
	  else {
	    echo '<< ';
	  }
	  if ($cur_page > 1) {
             echo '<a href="' . "$phpself" . '?page=' . ($cur_page-1) . '&company_id=' .$clan_id . '&rule=' . $rule . '"><</a> '; 
      }
	  else {
	    echo '<';
	  }
	/////
	  if (($cur_page-3)>0) {
	 $k = ($cur_page-3);
	    ?><a href="<?php echo "$phpself" . '?page=' . ($cur_page-3). '&company_id=' .$clan_id . '&rule=' . $rule ?>"><?php echo "$k";?></a><?php
      }
	 if (($cur_page-2)>0) {
	 $k = ($cur_page-2);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page-2). '&company_id=' .$clan_id . '&rule=' . $rule?>"><?php echo "$k";?></a> <?php
      }
     if (($cur_page-1)>0) {
	 $k = ($cur_page-1);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page-1). '&company_id=' .$clan_id  . '&rule=' . $rule?>"><?php echo "$k";?></a> <?php
      }
	?> <span class="white"><?php echo " $cur_page ";?></span><?php
	 if (($cur_page+1)<=$num_page) {
	 $k = ($cur_page+1);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+1). '&company_id=' .$clan_id . '&rule=' . $rule?>"><?php echo "$k";?></a> <?php
      }
	  	 if (($cur_page+2)<=$num_page) {
	 $k = ($cur_page+2);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+2). '&company_id=' .$clan_id . '&rule=' . $rule ?>"><?php echo "$k";?></a> <?php
      }
	 if (($cur_page+3)<=$num_page) {
	 $k = ($cur_page+3);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+3). '&company_id=' .$clan_id . '&rule=' . $rule ?>"><?php echo "$k";?></a> <?php
      }
	/////
	if ($cur_page < $num_page) {
	  echo '<a href="' . "$phpself" . '?page=' . ($cur_page+1). '&company_id=' .$clan_id  . '&rule=' . $rule . '">></a> ';
    }
	else {
	  echo '>';
	}
	if ($cur_page < $num_page) {
	  echo ' <a href="' . "$phpself" .  '?page=' . $num_page . '&company_id=' .$clan_id . '&rule=' . $rule .  '">>> </a> ';
    }
	else {
	  echo ' >>';
	}
	?>
</div>
<?php
if ($clan_out == $clan_id) {
if ($rang_out > 5) {
?>
<div class="menu">
<a class="menu" href="company.php?page=<?php echo "$cur_page"; ?>&company_id=<?php echo "$clan_id"; ?>"> <img src="img/ico/remont.png" width="12" height="12"/> Скрыть управление</a></div>
<?php
}
}
if ($clan_out == $clan_id) {
if ($rang_out > 5 and empty($rule)) {
?>
<div class="menu">
<a class="menu" href="company.php?page=<?php echo "$cur_page"; ?>&company_id=<?php echo "$clan_id";?>&rule=1"> <img src="img/ico/remont.png" width="12" height="12"/> Управление отрядом</a></div>
	  <?php
	  }
	  if ($rang_out < 9) {
	  ?>
</p><a class="menu" href="agree.php?inf=company"> <img src="img/ico/out.png" width="12" height="12"/> Покинуть отряд</a></div>
<?php
} else {
if ($total == 1) {
		?>
</p><a class="menu" href="agree.php?inf=company"> <img src="img/ico/out.png" width="12" height="12"/> Покинуть отряд</a></div>
	<?php
		}
	  }
	}

  


  
require_once('conf/navig.php');
require_once('conf/foot.php');
mysqli_close($dbc);

?>
</body>
</html>