<?


include 'connect.php';
if(!$u['id']){header ("Location: aut.php");exit;}
if(ban($u['id'])) header ("Location: page.php");

$my_msg = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_messages` WHERE `user` = '".$u['id']."'"), 0);


switch($_GET['act'])
{
//Редактирование ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'edit':
if (isset($_GET['id'])) {
$id=num($_GET['id']);
if($u['admin']==1 || $u['admin']==2)
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' LIMIT 1");
else
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' AND `user` = '".$u['id']."' LIMIT 1");
   if (mysql_num_rows($req)) {
   $microblog = mysql_fetch_assoc($req);
   $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$microblog['user']."' LIMIT 1"));
   }
}
if(empty($microblog)){header ("Location: index.php");exit;}

if (isset($_POST['submit']) && $_POST['message']) {
      $message = check(substr($_POST['message'], 0, 500));
      mysql_query("UPDATE `microblog_messages` SET
      `message` = '$message'
      WHERE `id` = '$id'
      ");
      header ("Location: microblog.php?id=".$user['id']."");
      exit;
      } else {
	  include 'head.php';
      echo '<div class="title"><img src= "img/microblog.png"> Микроблоги - Редактирование записи</div>';
      echo '<div class="subtitle">';
	     if($microblog['user'] == $u['id']) {echo '<a href = "microblog.php">Мой микроблог</a> ('.$my_msg.')';}
	     else {$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_messages` WHERE `user` = '".$user['id']."'"), 0);
	        echo '<a href = "microblog.php?id='.$user['id'].'">Микроблог '.login($microblog['user']).'</a> ('.$count.')';}
      echo '</div>';
      echo '<div class="main_menu">';
      $text = checkout($microblog['message']);
      echo '<form action="microblog.php?act=edit&amp;id=' . $id . '" method="post">';
      echo '<textarea cols="20" rows="3" name="message">'.$text.'</textarea><br/>';
      echo '<input type="submit" name="submit" class="btn" value="Сохранить" class="submit white"/>';
      echo '</form></div>';
      echo '</div>';
      include 'foot.php';
	  }
break;
//Новый статус ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'new':
if (isset($_POST['submit']) and $_POST['message']) {

$message = check(substr($_POST['message'], 0, 500));

   //Проверяем на флуд
   $flood = mysql_query("SELECT `time` FROM `microblog_messages` WHERE `user` = '".$u['id']."' AND `time` > '" . (time() - $system['microblog_antiflud']) . "'");
   if (mysql_num_rows($flood)) {header ("Location: microblog.php?act=new");exit;}
   
mysql_query("UPDATE `microblog_messages` SET `main` = '0' WHERE `user` = '".$u['id']."'");

// Вставляем сообщение в базу
mysql_query("INSERT INTO `microblog_messages` SET
`user` = '".$u['id']."',
`message` = '$message',
`time` = '".time()."',
`main` = '1'
");

header ("Location: microblog.php");
exit;

} else {
include 'head.php';
echo '<div class="title"><img src= "img/microblog.png"> Микроблоги - Новая запись</div>';
echo '<div class="subtitle">';
echo '<a href = "microblog.php">Мой микроблог </a> ('.$my_msg.')';
echo '</div>';
echo '<div class="main_menu">';
echo '<form action="microblog.php?act=new" method="post">';
echo 'Сообщение:<br/><textarea cols="20" rows="3" name="message"></textarea><br/>';
echo '<input type="submit" name="submit" class="btn" value="Добавить" class="submit white"/>';
echo '</form></div>';
echo '</div>';
include 'foot.php';
}
break;

//Убрать ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'close':

if (isset($_GET['id'])) {
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' AND `user` = '".$u['id']."' LIMIT 1");
   if (mysql_num_rows($req)) {
   $microblog = mysql_fetch_assoc($req);
   mysql_query("UPDATE `microblog_messages` SET `main` = '0' WHERE `id` = '".$microblog['id']."'");
   header ("Location: page.php");
   }
}
if(empty($microblog)){header ("Location: index.php");exit;}
break;


//Просмотр ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'view':
if (isset($_GET['id'])) {
$id=num($_GET['id']);
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' LIMIT 1");
   if (mysql_num_rows($req)) {
   $microblog = mysql_fetch_assoc($req);
   $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$microblog['user']."' LIMIT 1"));
   }
}
if(empty($microblog)){header ("Location: index.php");exit;}


if(isset($_GET['show'])) if($_GET['show']=='comm' || $_GET['show']=='like' || $_GET['show']=='view' ) $show = $_GET['show'];

//Лайк (Мне нрав)
if(isset($_GET['like'])) if($_GET['like']=='yes' || $_GET['like']=='no') $like = $_GET['like'];
if(isset($like)){
   if($like=='yes'){
      $req = mysql_query("SELECT * FROM `microblog_like` WHERE `user` = '".$u['id']."' AND `microblog` = '".$microblog['id']."' LIMIT 1");
      if (!mysql_num_rows($req)) {
	     $microblog['likes']++;
         mysql_query("INSERT INTO `microblog_like` SET
         `user` = '".$u['id']."',
         `time` = '".time()."',
         `microblog` = '".$microblog['id']."'
         ");
         mysql_query("UPDATE `microblog_messages` SET
         `likes` = '".$microblog['likes']."'
         WHERE `id` = '".$microblog['id']."'
         ");
		 //Записываем в журнал
         if($microblog['user'] != $u['id']){
            if($u['sex'] == 'm'){
               $action[0] = 'Отметил';
               $action[1] = 'ему';
            } else {
               $action[0] = 'Отметила';
               $action[1] = 'ей';
            }
            $text = $action[0].' что '.$action[1].' понравилась ваша <a href = "microblog.php?act=view&id='.$microblog['id'].'">запись</a>.';
            mysql_query("INSERT INTO `jurnal` SET
            `user` = '".$user['id']."',
            `outuser` = '".$u['id']."',
            `text` = '$text',
            `time` = '".time()."',
            `new` = '1'
            ");
	     } 
      }
   } else {
      $req = mysql_query("SELECT * FROM `microblog_like` WHERE `user` = '".$u['id']."' AND `microblog` = '".$microblog['id']."' LIMIT 1");
      if (mysql_num_rows($req)) {
	     //Проверяем на флуд
         $flood = mysql_query("SELECT `time` FROM `microblog_like` WHERE `user` = '".$u['id']."' AND `time` > '" . (time() - $system['microblog_like_antiflud']) . "'");
         if (mysql_num_rows($flood)) {
         header ("Location: microblog.php?act=view&id=$id");
         exit;
         }
		 mysql_query("DELETE FROM `microblog_like` WHERE `user` = '".$u['id']."' AND `microblog` = '".$microblog['id']."' LIMIT 1");
		 $microblog['likes']--;
		 mysql_query("UPDATE `microblog_messages` SET
         `likes` = '".$microblog['likes']."'
         WHERE `id` = '".$microblog['id']."'
         ");
	  }
   }
}
//Удаление лайка
if(isset($_GET['del_like'])){
   if($microblog['user']==$u['id']){
      $del_like=num($_GET['del_like']);
      $req = mysql_query("SELECT * FROM `microblog_like` WHERE `user` = '$del_like' AND `microblog` = '".$microblog['id']."' LIMIT 1");
      if (mysql_num_rows($req)) {
	     mysql_query("DELETE FROM `microblog_like` WHERE `user` = '$del_like' AND `microblog` = '".$microblog['id']."' LIMIT 1");
	     $microblog['likes']--;
		 mysql_query("UPDATE `microblog_messages` SET
         `likes` = '".$microblog['likes']."'
         WHERE `id` = '".$microblog['id']."'
         ");
	  }
   }
}

//Просмотры
$req = mysql_query("SELECT * FROM `microblog_view` WHERE `user` = '".$u['id']."' AND `microblog` = '".$microblog['id']."' LIMIT 1");
   if (!mysql_num_rows($req)) {
   mysql_query("INSERT INTO `microblog_view` SET
   `user` = '".$u['id']."',
   `view` = '1',
   `last_time` = '".time()."',
   `time` = '".time()."',
   `microblog` = '".$microblog['id']."'
   ");
   mysql_query("UPDATE `microblog_messages` SET
   `view` = `view`+1
   WHERE `id` = '".$microblog['id']."'
   ");
   $microblog['view']++;
   } else {
   $view = mysql_fetch_assoc($req); 
   mysql_query("UPDATE `microblog_view` SET
   `view` = `view`+1,
   `last_time` = '".time()."'
   WHERE `id` = '".$view['id']."'
   ");
   }

//Добавление комментария
if($_POST['comm_message']){
$message = check(substr($_POST['comm_message'], 0, 500));
   //Проверяем на флуд
   $flood = mysql_query("SELECT `time` FROM `microblog_comm` WHERE `user` = '".$u['id']."' AND `time` > '" . (time() - $system['microblog_comm_antiflud']) . "'");
   if (mysql_num_rows($flood)) {
      header ("Location: microblog.php?act=view&id=$id&show=comm&flud=1");
   exit;
   }
   // Проверка на одинаковые сообщения
   $req = mysql_query("SELECT * FROM `microblog_comm` WHERE `user` = '".$u['id']."' ORDER BY `time` DESC");
   $res = mysql_fetch_array($req);
   if ($res['message'] == $message) {
      header("Location: microblog.php?act=view&id=$id&show=comm");
   exit;
   }
// Вставляем сообщение в базу
mysql_query("INSERT INTO `microblog_comm` SET
`user` = '".$u['id']."',
`message` = '$message',
`time` = '".time()."',
microblog = '$id'
");

mysql_query("UPDATE `microblog_messages` SET `comm` = `comm`+1 WHERE `id` = '".$microblog['id']."'");

header("Location: microblog.php?act=view&id=$id&show=comm");
}


include 'head.php';
   if($microblog['user'] == $u['id']){
   echo '<div class="title"><img src= "img/microblog.png"> Мой микроблог - Просмотр записи</div>';
      }else{
	  echo '<div class="title"><img src= "img/microblog.png"> Микроблог <a href = "page.php?id='.$microblog['user'].'">'.login($microblog['user']).'</a> - Просмотр записи</div>';
	  }
	  
echo '<div class="subtitle">';
if($user['id'] != $u['id']) {
$all_msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `microblog_messages` WHERE `user` = '".$user['id']."'"), 0);
echo '<a href = "microblog.php?id='.$user['id'].'">Все записи '.$user['login'].'</a> ('.$all_msg.')';
} else {
echo '<a href = "microblog.php?id='.$user['id'].'">Все записи</a> ('.$my_msg.')';
}

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

if(($u['admin']==1 || $u['admin']==2) || $microblog['user'] == $u['id'])
echo '<a href = "microblog.php?act=edit&id='.$microblog['id'].'">Ред.</a> |
<a href = "microblog.php?act=delete&id='.$microblog['id'].'">Удалить</a>';

$message = checkout($microblog['message']);

echo '<div class="microblog">'.smile($message).'
<br><small><font color = "#929292">'.vremya($microblog['time']).'</font></small></div>';

$req = mysql_query("SELECT * FROM `microblog_like` WHERE `user` = '".$u['id']."' AND `microblog` = '".$microblog['id']."' LIMIT 1");
   if (!mysql_num_rows($req)) 
      echo '<img src = "img/like.png"> ('.$microblog['likes'].') <a href = "microblog.php?act=view&id='.$microblog['id'].'&like=yes"><font color = "green"><b>+1</b></font></a>'; 
      else 
         echo '<img src = "img/like.png"> ('.$microblog['likes'].') <a href = "microblog.php?act=view&id='.$microblog['id'].'&like=no"><font color = "red"><b>-1</b></font></a>';
   
   //Комментарии, Лайки, Просмотры ##################################################################################
   switch($show)
   {
   //Комментарии ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   case 'comm':
   echo '<div class = "nav"><b>Комм ('.$microblog['comm'].')</b> | <a href = "microblog.php?act=view&id='.$id.'&show=like">Кому нрав ('.$microblog['likes'].')</a> |  <a href = "microblog.php?act=view&id='.$id.'&show=view">Просмотры ('.$microblog['view'].')</a></div>';
   echo '<div class="main_menu">';
   
   echo'<form action="microblog.php?act=view&id='.$id.'" method="post">';
   echo '<textarea cols="20" rows="3" name="comm_message">';
   echo '</textarea><br/>';
   echo "<input type='submit' title='Нажмите для отправки' name='submit' class='btn' value='Написать'  class='submit white'/>
   </form>";
   
   echo '<hr>';
   
   //Удаление
   if(isset($_GET['del'])){
      $del = num($_GET['del']);
	  $req = mysql_query("SELECT * FROM `microblog_comm` WHERE `id` = '$del' LIMIT 1");
      if (mysql_num_rows($req)) {
         $del_comm = mysql_fetch_assoc($req);
	     $del_microblog = mysql_fetch_assoc(mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '".$del_comm['microblog']."'"));
	     if($u['admin'] == 1 || $u['admin'] == 1){
		    mysql_query("DELETE FROM `microblog_comm` WHERE `id` = '$del'");
			mysql_query("UPDATE `microblog_messages` SET `comm` = `comm`-1 WHERE `id` = '".$id."'");
			echo '<div class = "action">Комментарий удален!</div><hr>';
		    }else{ 
			   if($del_comm['user'] == $u['id'] || $del_microblog['user'] == $u['id']){
			      mysql_query("DELETE FROM `microblog_comm` WHERE `id` = '$del'");
				  mysql_query("UPDATE `microblog_messages` SET `comm` = `comm`-1 WHERE `id` = '".$id."'");
				  echo '<div class = "action">Комментарий удален!</div><hr>';
				  }
		    }
	  }
   }

   $all = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_comm` WHERE `microblog` = '".$microblog['id']."'"), 0);
   if($all > 0){
      $total = intval(($all - 1) / 10) + 1;
      $page = num($_GET['page']);
      if(empty($page) or $page < 0) $page = 1; 
      if($page > $total) $page = $total;
      $start = $page * 10 - 10;
      $result = mysql_query("SELECT * FROM `microblog_comm` WHERE `microblog` = '".$microblog['id']."' ORDER BY `time` DESC LIMIT $start, 10");
      while($comm = mysql_fetch_assoc($result)){
         $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$comm["user"]."'"));
         $msg = checkout($comm['message']);
         //Вывод
	     echo '<table><tr>';
	        echo '<td VALIGN=top>';
		       ava($user['id'],35);
	        echo '</td>';
		    
		    echo '<td VALIGN=top>';
		       echo ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.onl($user['online'],$system['online_time']); 
		       echo '<br/><font color = "#000">'.smile($msg).'</font><br/>
			   <small>'.vremya($comm['time']).'</small>';
			   if(($u['admin'] == 1 || $u['admin'] == 2) || ($comm['user'] == $u['id'] || $microblog['user'] == $u['id'])) echo ' | <a href = "microblog.php?act=view&id='.$id.'&show=comm&del='.$comm['id'].'"><img src = "img/delete.gif"></a>';
		    echo '</td>';
	     echo '</tr></table>';
	     echo '<hr>';
      }  
   navigation($page, $total,'microblog.php?act=view&id='.$id.'&show=comm&');
   }else{
   echo '<div class = "center">Комментариев еще нет!</div>';
   }
   echo '</div>';
   break;
   //Лайки ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   case 'like':
   echo '<div class = "nav"><a href = "microblog.php?act=view&id='.$id.'&show=comm">Комм ('.$microblog['comm'].')</a> | <b>Кому нрав ('.$microblog['likes'].')</b> | <a href = "microblog.php?act=view&id='.$id.'&show=view">Просмотры ('.$microblog['view'].')</a></div>';
   echo '<div class="main_menu">';
   $all = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_like` WHERE `microblog` = '".$microblog['id']."'"), 0);
   if($all > 0){
      $total = intval(($all - 1) / 10) + 1;
      $page = num($_GET['page']);
      if(empty($page) or $page < 0) $page = 1; 
      if($page > $total) $page = $total;
      $start = $page * 10 - 10;
      $result = mysql_query("SELECT * FROM `microblog_like` WHERE `microblog` = '".$microblog['id']."' ORDER BY `time` DESC LIMIT $start, 10");
      while($likes = mysql_fetch_assoc($result)){
         $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$likes["user"]."'"));
         //Вывод
	     echo '<table><tr>';
	        echo '<td VALIGN=top>';
		       ava($user['id'],35);
	        echo '</td>';
		    
		    echo '<td VALIGN=top>';
		       echo ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.onl($user['online'],$system['online_time']); 
		       echo '<br/><font color = "#000">'.smile($msg).'</font>';
			   if($likes['user'] == $u['id'])
			      echo '<a href = "microblog.php?act=view&id='.$id.'&show=like&like=no">Убрать</a>';
		          else		
                     if($microblog['user'] == $u['id'])		  
				        echo '<a href = "microblog.php?act=view&id='.$id.'&show=like&del_like='.$user['id'].'">Убрать</a>';
			echo '</td>';
	     echo '</tr></table>';
	     echo '<hr>';
      }  
   navigation($page, $total,'microblog.php?act=view&id='.$id.'&show=like&');
   }else{
   echo '<div class = "center">Запись пока никому не понравилась!</div>';
   }
   echo '</div>';
   break;
   //Просмотры ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   case 'view':
   echo '<div class = "nav"><a href = "microblog.php?act=view&id='.$id.'&show=comm">Комм ('.$microblog['comm'].')</a> | <a href = "microblog.php?act=view&id='.$id.'&show=like">Кому нрав ('.$microblog['likes'].')</a> |  <b>Просмотры ('.$microblog['view'].')</b></div>';
   echo '<div class="main_menu">';
   $all = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_view` WHERE `microblog` = '".$microblog['id']."'"), 0);
   if($all > 0){
      $total = intval(($all - 1) / 10) + 1;
      $page = num($_GET['page']);
      if(empty($page) or $page < 0) $page = 1; 
      if($page > $total) $page = $total;
      $start = $page * 10 - 10;
      $result = mysql_query("SELECT * FROM `microblog_view` WHERE `microblog` = '".$microblog['id']."' ORDER BY `time` DESC LIMIT $start, 10");
      while($view = mysql_fetch_assoc($result)){
         $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$view["user"]."'"));
         //Вывод
	     echo '<table><tr>';
	        echo '<td VALIGN=top>';
		       ava($user['id'],35);
	        echo '</td>';
		    
		    echo '<td VALIGN=top>';
		       echo ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.onl($user['online'],$system['online_time']); 
		       echo '<br/><font color = "#000">'.smile($msg).'</font>';
			   
			   echo '<small>'.vremya($view['last_time']).'</small>';
			echo '</td>';
	     echo '</tr></table>';
	     echo '<hr>';
      }  
   navigation($page, $total,'microblog.php?act=view&id='.$id.'&show=view&');
   }else{
   echo '<div class = "center">Запись пока никто не просматривал!</div>';
   }
   echo '</div>';
   break;
   //По умолчанию ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   default:
   echo '<div class = "nav"><a href = "microblog.php?act=view&id='.$id.'&show=comm">Комм ('.$microblog['comm'].')</a> | <a href = "microblog.php?act=view&id='.$id.'&show=like">Кому нрав ('.$microblog['likes'].')</a> |  <a href = "microblog.php?act=view&id='.$id.'&show=view">Просмотры ('.$microblog['view'].')</a></div>';
   }
   //#################################################################################################################

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

break;

//Удаление ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 'delete':
if (isset($_GET['id'])) {
$id=num($_GET['id']);
if($u['admin']==1 || $u['admin']==2)
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' LIMIT 1");
else
$req = mysql_query("SELECT * FROM `microblog_messages` WHERE `id` = '$id' AND `user` = '".$u['id']."' LIMIT 1");
   if (mysql_num_rows($req)) {
   $microblog = mysql_fetch_assoc($req);
   $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$microblog['user']."' LIMIT 1"));
   }
}
if(empty($microblog)){header ("Location: index.php");exit;}
      
	  include 'head.php';
      echo '<div class="title"><img src= "img/microblog.png"> Микроблоги - Удаление записи</div>';
      echo '<div class="subtitle">';
	  if($microblog['user'] == $u['id']) {echo '<a href = "microblog.php">Мой микроблог</a> ('.$my_msg.')';}
	  else {$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_messages` WHERE `user` = '".$user['id']."'"), 0);
	  echo '<a href = "microblog.php?id='.$user['id'].'">Микроблог '.login($microblog['user']).'</a> ('.$count.')';}
      echo '</div>';
	  echo '<div class="main_menu">';
      if (isset($_POST['submit'])) {
	  mysql_query("DELETE FROM `microblog_messages` WHERE `id` = '".$id."'");
	  mysql_query("DELETE FROM `microblog_like` WHERE `microblog` = '".$id."'");
	  mysql_query("DELETE FROM `microblog_view` WHERE `microblog` = '".$id."'");
	  mysql_query("DELETE FROM `microblog_comm` WHERE `microblog` = '".$id."'");
	  echo '<div class = "action">Запись удалена!</div>';
      } else {
      echo 'Вы уверены, что хотите удалить <a href = "microblog.php?act=view&id='.$microblog['id'].'">эту запись</a> из микроблога?';
      echo '<form action="microblog.php?act=delete&amp;id='.$id.'" method="post">';
      echo '<input type="submit" name="submit" class="btn" value="Да, удалить" class="submit white"/>';
      echo '</form>';
      }
	  echo '</div>';
      include 'foot.php';
break;

//Все записи ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
default:
//Если id получен 
$id=num($_GET['id']); if($id == 0) $id=$u['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");

$title = $title.' | Микроблог';
include 'head.php';
if($user['id'] == $u['id'])$zag = 'Мой микроблог';else $zag = 'Микроблог <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a>';
echo '<div class="title"><img src= "img/microblog.png"> '.$zag.'</div>';
if($user['id'] != $u['id'])echo '<div class="subtitle"><a href = "microblog.php">Мой микроблог</a> ('.$my_msg.')</div>';
else echo '<div class="subtitle"><a href = "microblog.php?act=new">Новая запись</a></div>';
echo '<div class="main_menu">';
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog_messages` WHERE `user` = '".$user['id']."'"), 0);
if($all > 0){
$total = intval(($all - 1) / 10) + 1;
$page = num($_GET['page']);
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total;
$start = $page * 10 - 10;
$result = mysql_query("SELECT * FROM `microblog_messages` WHERE `user` = '".$user['id']."' ORDER BY `time` DESC LIMIT $start, 10");
   while($microblog = mysql_fetch_assoc($result)){
	  $message = checkout($microblog['message']);
	  if (iconv_strlen($message, 'UTF-8') > 25) {
      $message = iconv_substr($message, 0, 25, 'UTF-8');
      $message=''.$message.'...';
      }
	  echo'<img src = "img/microblog_message.png"> <a href="microblog.php?act=view&id='.$microblog['id'].'">
      '.smile($message).'</a>
      <br><small><font color = "#929292">'.vremya($microblog['time']).'</font></small><br>
      <img src = "img/eye.png"> '.$microblog['view'].'
      <img src = "img/like.png"> '.$microblog['likes'].'
      <img src = "img/comm.png"> '.$microblog['comm'].'
      <hr>';
   }
   navigation($page, $total,'diary.php?act=view&id='.$user['id'].'');
}else{
echo '<div class = "center">Записей пока нет!</div>';
}
echo '</div>';
include 'foot.php';
}

?>