<?


include 'connect.php';

//Только для админов
if($u['admin'] == 1){

switch($_GET['act'])
{
//Пользователи ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'users':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка / Пользователи</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

//Сортировка
echo '<div class="subtitle">';
echo ' Сортировка: <br/>';
$sort = $_GET['sort'];

//Привлекательность
if($sort == 'likes') {
$sortq = '`likes`'; 
echo '<a href = "adminka.php?act=users&sort=rating">Популярность</a> | <b>Привлекательность</b> | <a href = "adminka.php?act=users&sort=money">Богатство</a>';
}
//Богатство
if($sort == 'money') {
$sortq = '`money`'; 
echo '<a href = "adminka.php?act=users&sort=rating">Популярность</a> | <a href = "adminka.php?act=users&sort=likes">Привлекательность</a> | <b>Богатство</b>';
}
//Популярность
if($sort == 'rating' || empty($sortq)) {
$sort = 'rating';
$sortq = '`rating`'; 
echo '<b>Популярность</b> | <a href = "adminka.php?act=users&sort=likes">Привлекательность</a> | <a href = "adminka.php?act=users&sort=money">Богатство</a>';
}
echo '</div>';

echo '<div class="main_menu">';
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `users`"), 0);
if($all > 0){
   $total = intval(($all - 1) / 25) + 1;
   $page = num($_GET['page']);
   if(empty($page) or $page < 0) $page = 1; 
   if($page > $total) $page = $total;
   $start = $page * 25 - 25;
   $result = mysql_query("SELECT * FROM `users` $onlineq ORDER BY $sortq DESC LIMIT $start, 25");
      while($user = mysql_fetch_assoc($result)){
	  //Вывод
	  echo $i % 2 ? '<div class="block2">' : '<div class="block1">';
	  echo ''.ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.online($user['online']).'';
	  if($sort == 'likes') echo ' <img src = "img/heart.gif"> '.$user['likes'].'';
	  if($sort == 'money') echo ' <img src = "img/money.gif"> '.$user['money'].'';
	  if($sort == 'rating') echo ' <img src = "img/rating.gif"> '.$user['rating'].'';
	  echo '<hr><div class = "act">';
	  echo ' <a href = "adminka.php?act=edit_users&id='.$user['id'].'"><img src="img/edit.gif"></a> | ';
	  echo ' <a href = "adminka.php?act=ban_users&id='.$user['id'].'"><img src="img/ban.gif"></a> | ';
	  echo ' <a href = "adminka.php?act=delete_users&id='.$user['id'].'"><img src="img/delete.gif"></a>';
	  echo '</div>';
	  echo '<hr>';
      echo '</div>';
	  $i++;
	  }
	  navigation($page, $total,'adminka.php?act=users&sort='.$sort.'');
}else{
echo 'Пользователей не найдено!';
}
echo '</div>';
include 'foot.php';

break;

//Пользователи ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'edit_users':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка / Редактирование пользователя</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

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

//Если id получен 
if(isset($_GET['id'])){
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
   if (mysql_num_rows($req)) {
   $user = mysql_fetch_assoc($req);
   }
}

//Если никто не выбрался
if(empty($user['id'])){
   $req = mysql_query("SELECT * FROM `users` WHERE `id` = '".$u['id']."' LIMIT 1");
   $user = mysql_fetch_assoc($req);
}

//Если данные получены
if (isset($_POST['submit'])) {
$login = check(substr($_POST['login'], 0, 25));
$rating = num($_POST['rating']);
$money = num($_POST['money']);
$password = check(substr($_POST['password'], 0, 25));
$sex = $_POST['sex'];
if ($sex != 'zh' && $sex != 'm') $sex = 'm';
$admin = num($_POST['admin']);
$anketa_name = check(substr($_POST['anketa_name'], 0, 50));
$anketa_surname = check(substr($_POST['anketa_surname'], 0, 50));
$anketa_bday = num($_POST['anketa_bday']);
   if($anketa_bday > 31) $anketa_bday = '';
$anketa_bmonth = num($_POST['anketa_bmonth']);
   if($anketa_bmonth > 12) $anketa_bmonth = '';
$anketa_byear = num($_POST['anketa_byear']);
   if($anketa_byear < 1920 or $anketa_byear > 1999) $anketa_byear = '';
$anketa_city = check(substr($_POST['anketa_city'], 0, 100));
$anketa_mphone = check(substr($_POST['anketa_mphone'], 0, 50));
$anketa_icq = check(substr($_POST['anketa_icq'], 0, 50));
$anketa_site = check(substr($_POST['anketa_site'], 0, 50));
$anketa_activity = check(substr($_POST['anketa_activity'], 0, 1000));
$anketa_interests = check(substr($_POST['anketa_interests'], 0, 1000));
$anketa_about = check(substr($_POST['anketa_about'], 0, 1000));
mysql_query("UPDATE `users` SET
`login` = '$login',
`pass` = '".md5(md5($password))."',
`password` = '$password',
`sex` = '$sex',
`admin` = '$admin',
`rating` = '$rating',
`money` = '$money',
`anketa_name` = '$anketa_name',
`anketa_surname` = '$anketa_surname',
`anketa_bday` = '$anketa_bday',
`anketa_bmonth` = '$anketa_bmonth',
`anketa_byear` = '$anketa_byear',
`anketa_city` = '$anketa_city',
`anketa_mphone` = '$anketa_mphone',
`anketa_icq` = '$anketa_icq',
`anketa_site` = '$anketa_site',
`anketa_activity` = '$anketa_activity',
`anketa_interests` = '$anketa_interests',
`anketa_about` = '$anketa_about'
WHERE `id` = '".$user['id']."'
");
echo '<div class = "action">Изменения сохранены!</div><hr>';

$req = mysql_query("SELECT * FROM `users` WHERE `id` = '".$user['id']."' LIMIT 1");
$user = mysql_fetch_assoc($req);
}

$anketa_name = checkout($user['anketa_name']);
$rating = checkout($user['rating']);
$money = checkout($user['money']);
$anketa_surname = checkout($user['anketa_surname']);
$anketa_bday = $user['anketa_bday'];
$anketa_bmonth = $user['anketa_bmonth'];
$anketa_byear = $user['anketa_byear'];
$anketa_city = checkout($user['anketa_city']);
$anketa_mphone = checkout($user['anketa_mphone']);
$anketa_icq = checkout($user['anketa_icq']);
$anketa_site = checkout($user['anketa_site']);
$anketa_activity = checkout($user['anketa_activity']);
$anketa_interests = checkout($user['anketa_interests']);
$anketa_about = checkout($user['anketa_about']);

echo 'Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> (ID: '.$user['id'].') '.online($user['online']).'<hr>';
echo ava($user['id'],50); echo '<hr>';
echo '<form action="adminka.php?act=edit_users&id='.$user['id'].'" method=post>';
echo 'Логин: <br/><input type=text name=login maxlength=50 value='.$user["login"].'><hr>';
echo '
Должность: <br/><select name="admin">
<option value="0">Пользователь</option>
<option value="2" ' . ($user['admin'] == '2' ? ' selected="selected"' : '') . '>Модератор</option>
<option value="1" ' . ($user['admin'] == '1' ? ' selected="selected"' : '') . '>Администратор</option>
</select><hr>';
echo '<img src="img/rating.gif"> <input type=text name=rating size = "2" maxlength=50 value='.$rating.'>';
echo '<img src="img/money.gif"> <input type=text name=money size = "2" maxlength=50 value='.$money.'><hr>';
echo 'Пароль: <br/><input type=text name=password maxlength=50 value='.$user["password"].'><hr>';
echo '
Пол: <br/><select name="sex">
<option value="0">- Не выбран -</option>
<option value="zh" ' . ($user['sex'] == 'zh' ? ' selected="selected"' : '') . '>Женский</option>
<option value="m" ' . ($user['sex'] == 'm' ? ' selected="selected"' : '') . '>Мужской</option>
</select><hr>';
echo 'Имя: <br/><input type=text name=anketa_name maxlength=50 value='.$anketa_name.'><hr>';
echo 'Фамилия: <br/><input type=text name=anketa_surname maxlength=50 value='.$anketa_surname.'><hr>';
echo 'День рождения:<br/>
<input type=text name=anketa_bday size = "2" maxlength=50 value='.$anketa_bday.'>
<input type=text name=anketa_bmonth size = "2" maxlength=50 value='.$anketa_bmonth.'>
<input type=text name=anketa_byear size = "2" maxlength=50 value='.$anketa_byear.'><hr>';
echo 'Город: <br/><input type=text name=anketa_city maxlength=100 value='.$anketa_city.'><hr>';
echo 'Моб. телефон: <br/><input type=text name=anketa_mphone maxlength=50 value='.$anketa_mphone.'><hr>';
echo 'ICQ: <br/><input type=text name=anketa_icq maxlength=50 value='.$anketa_icq.'><hr>';
echo 'Веб-сайт: <br/><input type=text name=anketa_site maxlength=50 value='.$anketa_site.'><hr>';
echo '<b> Личное</b><hr>';
echo 'Деятельность: <br/><textarea cols="17" rows="2" name="anketa_activity">'.$anketa_activity.'</textarea><hr>';
echo 'Интересы: <br/><textarea cols="17" rows="2" name="anketa_interests">'.$anketa_interests.'</textarea><hr>';
echo 'О себе: <br/><textarea cols="17" rows="2" name="anketa_about">'.$anketa_about.'</textarea><hr>';
echo '
<input type=submit name="submit" value="Сохранить" class="submit white"/></form>';
echo '</div>';
include 'foot.php';

break;

//Блокировка пользователя ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'ban_users':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка / Блокировка пользователя</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

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

//Если id получен 
if(isset($_GET['id'])){
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
if (mysql_num_rows($req))$user = mysql_fetch_assoc($req); else header ("Location: index.php");
} else {header ("Location: index.php");}
   //Нельзя банить админа и себя
   if($user['admin'] == 1 || $user['id'] == $u['id']) {
   echo 'Запрещено!:)';
   } else {
      //Проверяем не забанен ли пользователь
      if(ban($user['id'])) {
	  echo 'Пользователь уже забанен!';
	  } else {
	     if (isset($_POST['submit'])) {
		 $time = num($_POST['time']);
		 $timet = num($_POST['timet']);
		 if($timet == 1) $time = $time*60;
		 if($timet == 2) $time = $time*60*60;
		 if($timet == 3) $time = $time*60*60*24;
		 $message = check(substr($_POST['message'], 0, 1000));
         mysql_query("INSERT INTO `users_ban` SET
         `user` = '".$user['id']."',
         `message` = '$message',
		 `admin` = '".$u['id']."',
         `time` = '".time()."',
		 `time_ban` = '".(time() + $time)."'
         ");
		 echo '<div class = "action">Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> заблокирован!</div><hr>';
		 } else {
	     echo 'Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> (ID: '.$user['id'].') '.online($user['online']).'<hr>';
         echo '<form action="adminka.php?act=ban_users&id='.$user['id'].'" method=post>';
	     echo 'Время: <input type=text name=time size="2" maxlength=7 value=60>
         <select name="timet">
         <option value="1">минут</option>
         <option value="2">часов</option>
         <option value="3">дней</option>
         </select><hr>';
		 echo 'Сообщение:<br/><textarea cols="17" rows="2" name="message"></textarea><hr>';
	     echo '<input type=submit name="submit" value="Заблокировать" class="submit white"/></form>';
         }
	  }
   }

echo '</div>';
include 'foot.php';

break;
//Блокировка пользователя ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'delban_users':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка /разблокировка пользователя</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

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

//Если id получен 
if(isset($_GET['id'])){
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
if (mysql_num_rows($req))$user = mysql_fetch_assoc($req); else header ("Location: index.php");
} else {header ("Location: index.php");}
   //Нельзя банить админа и себя
   if($user['admin'] == 1 || $user['id'] == $u['id']) {
   echo 'Запрещено!:)';
   } else {
      //Проверяем не забанен ли пользователь
      if(!ban($user['id'])) {
	  echo 'Пользователь уже розбанен!';
	  } else {
	     if (isset($_POST['submit'])) {
		 $message = check(substr($_POST['message'], 0, 1000));
mysql_query("DELETE FROM `users_ban` WHERE  `user` = '".$user['id']."'");
mysql_query("DELETE FROM `users_ban` WHERE `message` = '$message'");
mysql_query("DELETE FROM `users_ban` WHERE `admin` = '".$u['id']."'");
mysql_query("DELETE FROM `users_ban` WHERE `time` = '".time()."'");
mysql_query("DELETE FROM `users_ban` WHERE `time_ban` = '".(time() + $time)."'");
		 echo '<div class = "action">Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> теперь розблокирован!</div><hr>';
		 } else {
	     echo 'Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> (ID: '.$user['id'].') '.online($user['online']).'<hr>';
echo "<div class='title'>";
echo "Вы уверены что хотите разблокировать этого пользователя?</br>
Помните админы и модераторы все записывается в админский-журнал и если мы узнаем что вы розблокировали не того будет очень плохо...</div>";         
echo '<form action="adminka.php?act=delban_users&id='.$user['id'].'" method=post>';
	     echo '<input type=submit name="submit" value="Разблокировать" class="submit white"/></form>';
         }
	  }
   }

echo '</div>';
include 'foot.php';

break;
//Удаление пользователя ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'delete_users':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка / Удаление пользователя</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

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

//Если id получен 
if(isset($_GET['id'])){
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
   if (mysql_num_rows($req)) {
   $user = mysql_fetch_assoc($req);
   }
}

//Если выбрался пользователь и не сам себя
if($user['id'] && $user['id'] != $u['id']){
         if (isset($_POST['submit'])) {
		 //Беседка
		 mysql_query("DELETE FROM `arbour` WHERE `user` = '".$user['id']."'");
		 //--------------- Друзья ---------------
		 mysql_query("DELETE FROM `friends` WHERE `user` = '".$user['id']."' or `friend` = '".$user['id']."'");
		 //Обновляем счетчики тем кому он кинул заявку
		 $res_friends =mysql_query("SELECT `friend` FROM `friends_adding` WHERE `user` = '".$user['id']."'");
		 while ($row = mysql_fetch_assoc($res_friends)) {
         mysql_query("UPDATE `users` SET `count_friends` = `count_friends`-1 WHERE `id` = '".$row['friend']."'");
		 }
		 mysql_query("DELETE FROM `friends_adding` WHERE `user` = '".$user['id']."' or `friend` = '".$user['id']."'");
		 
		 //Журнал
		 mysql_query("DELETE FROM `jurnal` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `jurnal` WHERE `outuser` = '".$user['id']."'");
		 //--------------- Почта ---------------
		 //Удаляем из базы сообщения
		 mysql_query("DELETE FROM `mail_messages` WHERE `outuser` = '".$user['id']."' or `inuser` = '".$user['id']."'");
		 //Обновляем счетчики тем кто имеет его в своих диалогах
		 $res_mail =mysql_query("SELECT `user` FROM `mail_contacts` WHERE `contact` = '".$user['id']."'");
		 while ($row = mysql_fetch_assoc($res_mail)) {
		 $mn = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_messages` WHERE `user` = '".$row['user']."' and `inuser` = '".$row['user']."' and `new` = '1'"), 0);
         mysql_query("UPDATE `users` SET `count_mail` = '$mn' WHERE `id` = '".$row['user']."'");
		 }
		 //Удаляем из базы контакты
		 mysql_query("DELETE FROM `mail_contacts` WHERE `user` = '".$user['id']."' or `contact` = '".$user['id']."'");
		 
		 //--------------- Из спамов ---------------
		 mysql_query("DELETE FROM `spam` WHERE `user` = '".$user['id']."' or `spamer` = '".$user['id']."'");
		 //--------------- Баны ---------------
		 mysql_query("DELETE FROM `users_ban` WHERE `user` = '".$user['id']."'");
		 //Привлекательность
		 mysql_query("DELETE FROM `users_likes` WHERE `user` = '".$user['id']."'");
	     $res0 =mysql_query("SELECT `user` FROM `users_likes` WHERE `outuser` = '".$user['id']."'");
		 while ($row = mysql_fetch_assoc($res0)) {
		 mysql_query("UPDATE `users` SET `likes` = `likes`-1 WHERE `id` = '".$row['user']."'");
		 }
		 mysql_query("DELETE FROM `users_likes` WHERE `outuser` = '".$user['id']."'");
		 
		 $users_photo_like =mysql_query("SELECT `photo` FROM `photos_like` WHERE `user` = '".$user['id']."'");
		 while ($row = mysql_fetch_assoc($users_photo_like)) {
		 mysql_query("UPDATE `users_photo` SET `rating` = `rating`-1 WHERE `id` = '".$row['photo']."'");
		 }
		 //Фотография со страницы
		 mysql_query("DELETE FROM `users_photo` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `users_photo_comm` WHERE `user` = '".$user['id']."'");
		 $users_photo_like =mysql_query("SELECT `photo` FROM `users_photo_like` WHERE `user` = '".$user['id']."'");
		 while ($row = mysql_fetch_assoc($users_photo_like)) {
		 mysql_query("UPDATE `users_photo` SET `rating` = `rating`-1 WHERE `id` = '".$row['photo']."'");
		 }
		 mysql_query("DELETE FROM `users_photo_like` WHERE `user` = '".$user['id']."'");
		 //Фотографии
		    
			$photos_like =mysql_query("SELECT `photo` FROM `photos_like` WHERE `user` = '".$user['id']."'");
		    while ($row = mysql_fetch_assoc($photos_like)) {
		    mysql_query("UPDATE `photos_photo` SET `rating` = `rating`-1 WHERE `id` = '".$row['photo']."'");
		    }
			//Выбираем альбомы
		    $res1 = mysql_query("SELECT `id` FROM `photos_albums` WHERE `user` = '".$user['id']."'");
		    while ($album = mysql_fetch_assoc($res1)) {
			   //Выбираем фотки в альбомах
			   $res2 =mysql_query("SELECT `id` FROM `photos_photo` WHERE `album` = '".$album['id']."'");
		       while ($photo = mysql_fetch_assoc($res2)) {
			   mysql_query("DELETE FROM `photos_photo` WHERE `id` = '".$photo['id']."'");
	           mysql_query("DELETE FROM `photos_comm` WHERE `photo` = '".$photo['id']."'");
	           mysql_query("DELETE FROM `photos_like` WHERE `photo` = '".$photo['id']."'");
			   }
		    }
		    mysql_query("DELETE FROM `photos_albums` WHERE `user` = '".$user['id']."'");
			
		 //Из пользователей
		 mysql_query("DELETE FROM `users` WHERE `id` = '".$user['id']."'");
		 //Из стен
		 mysql_query("DELETE FROM `wall` WHERE `user` = '".$user['id']."'");
		 //Из чата
		 mysql_query("DELETE FROM `chat_messages` WHERE `user` = '".$user['id']."'");
		 //Из форума
		 mysql_query("DELETE FROM `forum_themes` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `forum_messages` WHERE `user` = '".$user['id']."'");
		 
		 //Подарки #########################
         mysql_query("DELETE FROM `gifts_users` WHERE `user` = '".$user['id']."' OR `outuser` = '".$user['id']."'");
		 
		 //Дуэли #########################
		 mysql_query("DELETE FROM `duels_add` WHERE `user1` = '".$user['id']."' OR `user2` = '".$user['id']."'");
		 $duels_r = mysql_query("SELECT `id` FROM `duels_votes` WHERE `user1` = '".$user['id']."' OR `user2` = '".$user['id']."'");
		 while ($duels = mysql_fetch_assoc($duels_r)) {
		 mysql_query("DELETE FROM `duels_votes` WHERE `duel` = '".$duels['id']."'");
		 }
		 mysql_query("DELETE FROM `duels_end` WHERE `user1` = '".$user['id']."' OR `user2` = '".$user['id']."'");
		 $duels_r = mysql_query("SELECT `id` FROM `duels_list` WHERE `user1` = '".$user['id']."' OR `user2` = '".$user['id']."'");
		 while ($duels = mysql_fetch_assoc($duels_r)) {
		 mysql_query("DELETE FROM `duels_votes` WHERE `duel` = '".$duels['id']."'");
		 }
		 mysql_query("DELETE FROM `duels_list` WHERE `user1` = '".$user['id']."' OR `user2` = '".$user['id']."'");
		 mysql_query("UPDATE `duels_votes` SET `user` = '0' WHERE `user` = '".$user['id']."'");
		 
		 //Гаремы #########################
		 mysql_query("DELETE FROM `garems_list` WHERE `id` = '".$user['id']."'");
		 mysql_query("DELETE FROM `garems_users` WHERE `id` = '".$user['id']."'");
		 
		 //Дневники #########################
		 mysql_query("DELETE FROM `diary_messages` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `diary_like` WHERE `diary` = '".$user['id']."'");
		 mysql_query("DELETE FROM `diary_view` WHERE `diary` = '".$user['id']."'");
	     mysql_query("DELETE FROM `diary_comm` WHERE `diary` = '".$user['id']."'");
		 $diary_r = mysql_query("SELECT `diary` FROM `diary_like` WHERE `user` = '".$user['id']."'");
		 while ($diary = mysql_fetch_assoc($diary_r)) {
		 mysql_query("UPDATE `diary_messages` SET `rating` = `rating`-1 WHERE `id` = '".$diary['diary']."'");
		 }
		 $diary_r = mysql_query("SELECT `diary` FROM `diary_view` WHERE `user` = '".$user['id']."'");
		 while ($diary = mysql_fetch_assoc($diary_r)) {
		 mysql_query("UPDATE `diary_messages` SET `view` = `view`-1 WHERE `id` = '".$diary['diary']."'");
		 }
		 mysql_query("DELETE FROM `diary_like` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `diary_view` WHERE `user` = '".$user['id']."'");
	     mysql_query("DELETE FROM `diary_comm` WHERE `user` = '".$user['id']."'");
		 
		 //Микроблок #########################
		 mysql_query("DELETE FROM `microblog_messages` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `microblog_like` WHERE `microblog` = '".$user['id']."'");
		 mysql_query("DELETE FROM `microblog_view` WHERE `microblog` = '".$user['id']."'");
	     mysql_query("DELETE FROM `microblog_comm` WHERE `microblog` = '".$user['id']."'");
		 $microblog_r = mysql_query("SELECT `microblog` FROM `microblog_like` WHERE `user` = '".$user['id']."'");
		 while ($microblog = mysql_fetch_assoc($microblog_r)) {
		 mysql_query("UPDATE `microblog_messages` SET `likes` = `likes`-1 WHERE `id` = '".$microblog['microblog']."'");
		 }
		 $microblog_r = mysql_query("SELECT `microblog` FROM `microblog_view` WHERE `user` = '".$user['id']."'");
		 while ($microblog = mysql_fetch_assoc($microblog_r)) {
		 mysql_query("UPDATE `microblog_messages` SET `view` = `view`-1 WHERE `id` = '".$microblog['microblog']."'");
		 }
		 $microblog_r = mysql_query("SELECT `microblog` FROM `microblog_comm` WHERE `user` = '".$user['id']."'");
		 while ($microblog = mysql_fetch_assoc($microblog_r)) {
		 mysql_query("UPDATE `microblog_messages` SET `comm` = `comm`-1 WHERE `id` = '".$microblog['microblog']."'");
		 }
		 mysql_query("DELETE FROM `microblog_like` WHERE `user` = '".$user['id']."'");
		 mysql_query("DELETE FROM `microblog_view` WHERE `user` = '".$user['id']."'");
	     mysql_query("DELETE FROM `microblog_comm` WHERE `user` = '".$user['id']."'");
		 
		 echo '<div class = "action">Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> удален!</div><hr>';
		 } else {
	     echo 'Пользователь: <a href = "page.php?id='.$user['id'].'"><b>'.$user['login'].'</b></a> (ID: '.$user['id'].') '.online($user['online']).'<hr>';
         echo 'Вы подтверждаете удаление?<br/>';
		 echo '<form action="adminka.php?act=delete_users&id='.$user['id'].'" method=post>';
	     echo '<input type=submit name="submit" value="Да" class="submit white"/></form>';
         }
}

echo '</div>';
include 'foot.php';

break;

//Рассылка ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'mail':

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title"><img src= "img/settings.gif"> Админка / Рассылка</div>';

echo '<div class="subtitle">';
echo '<a href = "adminka.php">Главная админки </a>';
echo '</div>';

echo '<div class="main_menu">';
if (isset($_POST['submit']) and isset($_POST['message'])) {
	  $message = check(substr($_POST['message'], 0, 10000));
	  $result = mysql_query("SELECT `id` FROM `users` WHERE `id` <> '".$u['id']."' ");
      while($user = mysql_fetch_assoc($result)){
	  $req = mysql_query("SELECT * FROM `dialogs_contacts` WHERE `user` = '".$user['id']."' and `contact` = '".$u['id']."' LIMIT 1");
      if (mysql_num_rows($req) == 0) {
      mysql_query("INSERT INTO `dialogs_contacts` SET
      `user` = '".$user['id']."',
      `contact` = '".$u['id']."',
	  `time` = '".time()."'
      ");
	  }
	  mysql_query("UPDATE `dialogs_contacts` SET `time` = '".time()."' WHERE (`user` = '".$u['id']."' and `contact` = '".$user['id']."') or (`user` = '".$user['id']."' and `contact` = '".$u['id']."')");
	
	  mysql_query("INSERT INTO `dialogs_messages` SET
      `user` = '".$user['id']."',
      `outuser` = '".$u['id']."',
      `inuser` = '".$user['id']."',
      `message` = '$message',
	  `time` = '".time()."',
	  `new` = '1'
      ");
	  }
      echo '<div class = "action">Сообщение успешно разослано!</div>';
      } else {
	  echo '<form action="adminka.php?act=mail" method="post">';
      echo 'Сообщение:<br/><textarea cols="20" rows="2" name="message"></textarea><br/>';
      echo '<input type="submit" name="submit" value="Отправить" class="submit white"/>';




if (isset($_POST['ok'])){


mysql_query("INSERT INTO `clubwm` (`text`, `sait`) VALUES ('".htmlspecialchars(mysql_real_escape_string($_POST['text']))."', '".htmlspecialchars(mysql_real_escape_string($_POST['sait']))."')");
msg('успешно');}



echo "<form action=\"?\" method=\"post\">";



echo "Текст:<br />\n";
echo "<input type='text' name='text' /><br />\n";
echo "На что заменяем:<br />\n";
echo "<input type='text' name='sait' /><br />\n";
echo "<input name='ok' value=\"Окей\" type=\"submit\" />\n";
echo "</form>\n";






      echo '</form></div>';
      }
echo '</div>';
include 'foot.php';

break;
//Жалобы на спам ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'spam':

//Только для админов

$title = $title.' | Админка';
include 'head.php';
echo '<div class="title">Админка | Жалобы на спам</div>';
echo '<div class="main_menu">';

//Если не наказывать
if(isset($_GET['ok'])){
$ok = num($_GET['ok']);
$req = mysql_query("SELECT * FROM `spam` WHERE `spamer` = '$ok' LIMIT 1");
     if (mysql_num_rows($req)) {
     mysql_query("DELETE FROM `spam` WHERE `spamer` = '$ok'");
	 echo '<div class = "action">Удалено!</div><hr>';
     }
}

//Сообщений на страницу
$count = 10;

$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `spam`"), 0);
if($all > 0){
$total = intval(($all - 1) / $count) + 1;
$page = num($_GET['page']);
     if(empty($page) or $page < 0) $page = 1; 
     if($page > $total) $page = $total;
$start = $page * $count - $count;
$result = mysql_query("SELECT * FROM `spam` ORDER BY `time` DESC LIMIT $start, $count");
     while($msg = mysql_fetch_assoc($result)){
     $user = mysql_fetch_assoc(mysql_query("SELECT `id`,`login`,`sex`,`admin`,`online` FROM `users` WHERE `id` = '".$msg["user"]."'"));
     $spamer = mysql_fetch_assoc(mysql_query("SELECT `id`,`login`,`sex`,`admin`,`online` FROM `users` WHERE `id` = '".$msg["spamer"]."'"));
     $message = checkout($msg['message']);
     //Вывод
     echo '<div class="block">';
     echo '<table><tr><td>';
     ava($user['id'],50);
     echo '</td><td VALIGN=top>';
     echo ''.ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.online($user['online']).''; echo ' ('.vremya($msg['time']).') <a href = "?act=spam&ok='.$spamer['id'].'">Удалить</a>';
     echo '<br>';
     echo 'Пожаловался на спам от пользователя <a href = "page.php?id='.$spamer['id'].'"><b>'.$spamer['login'].'</b></a> за сообщение:<br>';
     echo '<font color = "#000">'.$message.'</font><br>';
     echo '</td></tr></table>';
     echo '</div>';
     }
navigation($page, $total,'?act=spam');
}else{
echo 'Жалоб нет!';
}
echo '</div>';
include 'foot.php';

break;
//Главная админки ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
default:

$title = $title.' | Админка';
include 'head.php';

//Подзаголовок
echo '<div class="title"><img src= "img/settings.gif"> Админка</div>';

echo '<div class="main_menu">';
echo '<img src = "img/users.gif"> <a href = "adminka.php?act=users">Пользователи</a><hr>';
echo '<img src = "img/mail.gif"> <a href = "adminka.php?act=mail">Рассылка сообщений</a><hr>';
echo '<img src = "img/gifts.gif"> <a href = "gifts.php">Подарки</a><hr>';
echo '<img src = "img/smiles.gif"> <a href = "smiles.php">Смайлы</a><hr>';
echo '<img src = "http://new-i09.spaces.ru//spam.gif"> <a href = "adminka.php?act=spam">Спам</a><hr>';
echo '</div>';











include 'foot.php';
break;
}

} else {
header ("Location: index.php");
}

?>