<?php

require_once('conf/dbc.php');
require_once('conf/session_start.php');
require_once('conf/ban.php');
if ((!isset($_SESSION['id'])) or (!isset($_SESSION['nick'])))  {
  ?>
  <script type="text/javascript">
  document.location.href = "reg.php?err_login=1";
  </script>
  <?php
  exit();
}
$set_id = abs(intval($_GET['id']));
$usst=$dbc->query("SELECT * FROM `users` WHERE`id`='$set_id' LIMIT 1")->fetch_assoc();
$user_id = abs(intval($_SESSION['id']));
$rang_inf = filter($_GET['inf']);
///Если не правильная инф в упро ранга
if ($rang_inf <> 'up' and $rang_inf <> 'down' and $rang_inf <> 'out'  and $rang_inf <> 'lider') {
?>
    <script type="text/javascript">
    document.location.href = "index.php";
    </script>
    <?php
	exit();
}

///////Пустое ли ид над кем управляют
if (empty($set_id))  {
  ?>
  <script type="text/javascript">
  document.location.href = "profile.php?id=<?php echo "$user_id";?>";
  </script>
  <?php
  exit();
}
////////////////////////////////

///////Данные приглашающего
$query = "Select clan_rang, clan from users where id = '$user_id'";
$result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
$row = mysqli_fetch_array($result);
$clan_rang_user = $row['clan_rang'];
$clan_user = $row['clan'];
/////////////////////////////////

///////Проверка на клан
if (empty($clan_user)) {
  ?>
  <script type="text/javascript">
  document.location.href = "profile.php?id=<?php echo "$user_id";?>";
  </script>
  <?php
  exit();
}
////////////////////////////////

///////Проверка на ранг
if ($clan_rang_user < 6) {
  if ($clan_user == 0) {
  ?>
  <script type="text/javascript">
  document.location.href = "profile.php?id=<?php echo "$user_id";?>";
  </script>
  <?php
  exit();
  }
  else {
  ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_id";?>&vid=sost";
  </script>
  <?php
  exit();
  }
}
////////////////////////////////

////////Данные приглашаемого
$query = "Select clan_rang, clan from users where id = '$set_id'";
$result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
$row = mysqli_fetch_array($result);
$clan_set = $row['clan'];
$clan_rang_set = $row['clan_rang'];
/////////////////////////////////


//////В одном ли они клане
if ($clan_set <> $clan_user) {
if ($clan_user == 0) {
  ?>
  <script type="text/javascript">
  document.location.href = "profile.php?id=<?php echo "$user_id";?>";
  </script>
  <?php
  exit();
  }
  else {
  ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_id";?>&vid=sost";
  </script>
  <?php
  exit();
  }
}
//////////////////////////////////

//////Если ранг У типа больше чем у того, кто хочет исправить
if ($clan_rang_set>=$clan_rang_user) {
  if ($clan_user == 0) {
  ?>
  <script type="text/javascript">
  document.location.href = "profile.php?id=<?php echo "$user_id";?>";
  </script>
  <?php
  exit();
  }
  else {
  ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_id";?>&vid=sost";
  </script>
  <?php
  exit();
  }
}
/////////////////////////////////


/////Если нужно повысить
////////////////////////////////////////////////
if ($rang_inf == up) { 
  $clan_rang_set = ($clan_rang_set + 1);
  if ($clan_rang_set == $clan_rang_user) {
   ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_user";?>&vid=sost";
  </script>
  <?php
  exit();
  }
$time=time();
$pdo->exec("INSERT INTO `clan_log` SET `user_id` = '$user_id', `user` = '".$usst['nick']."', `tip`  = '1', `time` = '$time',`clan_id` = '$clan_user'");
  $query = "update users set clan_rang = '$clan_rang_set'  where id = '$set_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
}
////////////////////////////////////////////////
////////////////////////////////////////////////

/////Если нужно понизить
////////////////////////////////////////////////
if ($rang_inf == down) {
  if ($clan_rang_set == 1) {
  ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_user";?>&vid=sost";
  </script>
  <?php
  exit();
  }
$time=time();
$pdo->exec("INSERT INTO `clan_log` SET `user_id` = '$user_id', `user` = '".$usst['nick']."', `tip`  = '2', `time` = '$time',`clan_id` = '$clan_user'");
  $clan_rang_set = ($clan_rang_set - 1);
  $query = "update users set clan_rang = '$clan_rang_set'  where id = '$set_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
}
////////////////////////////////////////////////
////////////////////////////////////////////////

/////Если нужно исключить
////////////////////////////////////////////////
if ($rang_inf == out) {
$time=time();
$pdo->exec("INSERT INTO `clan_log` SET `user_id` = '$user_id', `user` = '".$usst['nick']."', `tip`  = '3', `time` = '$time',`clan_id` = '$clan_user'");
  $query = "select people from clans where clan_id='$clan_user'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $row=mysqli_fetch_array($result);
  $people = $row['people'];
  $people = ($people - 1);
  $query = "update clans set people = '$people' where clan_id = '$clan_user'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $query = "update users set clan = 0, clan_rang = 0, ko=0, mentor_time = NOW() - (60*60*3) where id = '$set_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
  $query = "DELETE FROM  in_clan WHERE id_in = '$set_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');
}
////////////////////////////////////////////////
////////////////////////////////////////////////

/////Передача лидерства
////////////////////////////////////////////////
if ($rang_inf == lider) { 
if ($clan_rang_user >= 9) { 
   if ($clan_rang_set == 1) {
  ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_user";?>&vid=sost";
  </script>
  <?php
  exit();
  }
$clan_rang_set = ($clan_rang_set = 9);
$clan_rang_t = ($clan_rang_t = 8);
$time=time();
$pdo->exec("INSERT INTO `clan_log` SET `user_id` = '$user_id', `user` = '".$usst['nick']."', `tip`  = '7', `time` = '$time',`clan_id` = '$clan_user'");
  $query = "update users set clan_rang = '$clan_rang_set'  where id = '$set_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');

  $query = "update users set clan_rang = '$clan_rang_t'  where id = '$user_id'";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД');

  }
}
////////////////////////////////////////////////
////////////////////////////////////////////////
 ?>
  <script type="text/javascript">
  document.location.href = "clan.php?id=<?php echo "$clan_user";?>&vid=sost";
  </script>
  <?php


mysqli_close($dbc);

?>