<?php
  include '../../system/core.php';
      $_title = 'Бан-чат';
  include '../../system/header.php';

  if(!$user['id']){
  header('Location: /index.php/?m=guest'); exit;
  }





  if($user['status'] >= 1) {

  
          switch($_GET['m']){
  default:
   echo '<div class="tegi">Бан-чат</div>';
  if(isset($_POST['submit'])){

  $text = TextGuard($_POST['text']);
  if(empty($text)) $err[] = 'Сообщение осталось пустым';
  
  ////////////////////////
   $ncm = mysql_query("SELECT * FROM `polcha` WHERE `who`='".$user['id']."' ORDER BY `time` desc");
  while($ncm2 = mysql_fetch_assoc($ncm)){  
  $ncm_timeout = $ncm2['time'];
  
    if((time()-$ncm_timeout) < 5) $err[] = 'Писать можно только раз в 5 секунд';

  } 
 
    
  $ncm_rep = mysql_fetch_array(mysql_query('select * from `polcha` where `who` = "'.$user['id'].'" and `text` = "'.$text.'"'));
  
  if($ncm_rep != 0) $err[] = 'Вы уже писали подобное сообщение';
  ////////////////////

  if(!$err){

  		mysql_query("INSERT INTO `polcha`(
			`who`,`ho`,`text`,`type`,`time`
			)VALUES(
			'".$user['id']."','','".$text."','0','".time()."'
			)");

  header('Location: /banchat'); exit;
  
  }else{
  echo '<div class="pet"><b>';
  foreach($err as $err_info){
  echo $err_info.'<br>';
  }
  echo '</b></div>';
  }
  
  }
///////////////////////
  echo '<div class="mist">
  <form action="" method="post">
  Сообщение (<a href="/banchat">обн</a>):<br>
  <textarea name="text" style="width:98%;" rows="3"></textarea>
   <input type="submit" value="Написать" name="submit"/> <a href="/smiles.php">Смайлы</a> | <a href="/bbcode.php">BB-коды</a>
  </form></div>';
  ////////////////
  $cop = $user['on_page'];
  $ca = mysql_result(mysql_query("SELECT COUNT(*) FROM `polcha`"),0);
  $cp = ceil($ca/$cop);
  if(isset($_GET['p']) && is_numeric($_GET['p'])){
  $p = $_GET['p'];
  if($_GET['p']>$cp) $p = $cp;
  if($_GET['p']<1)$p = 1;
  }else{
  $p = 1;
  }
  $start = $p*$cop-$cop;

  
  $a_chat = mysql_query("SELECT * FROM `polcha` ORDER BY `id` DESC LIMIT $start,$cop");
  while($_a_chat = mysql_fetch_assoc($a_chat)){
echo '<div class="post1"><table cellpadding="0" cellspacing="4"><tr><td style="width: 40px; vertical-align: top; height: 40px; margin: 0; padding:0; border: 0;" id="mail_avatar">';
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/modules/profile/ava/'.$_a_chat['who'].'.jpg')) {
echo '<img src="/modules/profile/ava/'.$_a_chat['who'].'.jpg" alt="Ava" style="max-width:40px;">';
}
else
{
echo '<img src="/modules/profile/images/noava.png" alt="Ava" style="max-width:40px;">';
}
echo '</td><td style="vertical-align: top;" id="mail_textarea"> '.ustatus($_a_chat['who']).' <a href="/us'.$_a_chat['who'].'">'.uname($_a_chat['who']).'</a>
  '.uaccess($_a_chat['who']).' [<a href="?m=dc&amp;id='.$_a_chat['id'].'">удалить</a>]<br/>';
 echo ''.bbcode(smiles($_a_chat['text'])).'';
  echo '</td></tr></table></div>';
  
  }
 
  

  echo '<div class="levo">Стр. ';
  if($p-2>0) echo '<a href="/banchat/?m=chat&amp;p=1"><<</a> ';
  if($p-1>0) echo '<a href="/banchat/?m=chat&amp;p='.($p-1).'"><</a> ';

  if($p-3>0) echo '<a href="/banchat/??m=chat&amp;p'.($p-3).'">'.($p-3).'</a> | ';
  if($p-2>0) echo '<a href="/banchat/?m=chat&amp;p='.($p-2).'">'.($p-2).'</a> | ';  
  if($p-1>0) echo '<a href="/banchat/?m=chat&amp;p='.($p-1).'">'.($p-1).'</a> | ';
  
  echo '<b>'.$p.'</b>';
  if($p+1<=$cp) echo ' | <a href="/banchat/?m=chat&amp;p='.($p+1).'">'.($p+1).'</a>';
  if($p+2<=$cp) echo ' | <a href="/banchat/?m=chat&amp;p='.($p+2).'">'.($p+2).'</a>';
  if($p+3<=$cp) echo ' | <a href="/banchat/?m=chat&amp;p='.($p+3).'">'.($p+3).'</a>';

  if($p+1<=$cp) echo ' <a href="/banchat/?m=chat&amp;p='.($p+1).'">></a>';
  if($p+2<=$cp) echo ' <a href="/banchat/?m=chat&amp;p='.ceil($ca/$cop).'">>></a>';
  echo '</div>';


  
  break;
  case 'dc':
  $id = TextGuard($_GET['id']);

  if(empty($id) or $id == $user['id']){
  header('Location: /banchat'); exit;
  }


  mysql_query("DELETE FROM `polcha` WHERE `id` = '".$id."'");
 


  header('Location: /banchat'); exit;
  break;

  }
 } else {
echo '<div class="post2">Бан-чат закрыт</div>';
}
    include '../../system/footer.php'
   
   
   
 ?>