<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

$gpage = mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));

switch($sd){
default:

	$num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
	$result = mysql_query("SELECT * FROM `chat`") or die(mysql_error()); 
	$posts = mysql_num_rows($result); 
	$total = intval(($posts - 1) / $num) + 1; 
	$page = intval($page); 
	if(empty($page) or $page < 0) $page = 1; 
    if($page > $total) $page = $total; 
	$start = $page * $num - $num; 

mysql_query("UPDATE `users` SET `lastchat`='$posts' WHERE `id`='$uid'") or die(mysql_error());

$tnick = mysql_real_escape_string(htmlspecialchars($_GET['nick']));
if($tnick!==''){$tonick="$tnick,";}else{$tonick="";}

$gupdate = mysql_real_escape_string(htmlspecialchars($_GET['update']));

if($gupdate){
    header('Location: chat.php');
    exit;
}

echo "<form action='chat.php?sd=send' method='post'>
<textarea name='text' class='enter' rows='4' cols='40'>$tonick </textarea><br>
<input type='submit' value='Написать' class='enter'></form> <a href='chat.php?update'>Обновить</a><hr>";

$q = mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT $start,$num"); 
$e = mysql_num_rows($q);
if($e=='0'){
    echo "Чат пуст.";
    include ('tmp/foot.php');
    exit;
}


While($w = mysql_fetch_assoc($q)){
$time=time();
if (($time-$w['time'])>="86400"){
$w['time']=date("j M Y  в H:i", $w['time']);
$w['time'] = strtr($w['time'], array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
}else{
$timer=$time-$w['time'];
$ost=@date("i",$timer);
$time2=time()-10800;
$timer2=$time2-$w['time'];
$ost2=@date("H",$timer2);
if($ost2>0){
$w['time']=@date("H ч. i мин. s сек. назад",$timer2);
}else{
if($ost>0){
$w['time']=@date("i мин. s сек. назад",$timer);
}else{
$w['time']=@date("s сек. назад",$timer);
}}}
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    
    if($user['access']>='2'){$del="<small>[<a style='color:red' href='chat.php?sd=del&id=$w[id]'>del</a>]</small>";}else{$del="";}
    
    $obr = explode(",", $w['text']);
    if($obr['0']==$log){$text="<u><i>$w[text]</i></u>";}else{$text=$w['text'];};
    
    echo "<a href='userid$w[user]'>$us[login]</a>:<br><font color=$us[color_chat]>$text</font><br><small>[<a href='chat.php?nick=$us[login]'>Ответить</a>]</small> $del<div class='linem'></div>";
    
    
}


	if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
	if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 

break;
 
case 'del':

if($user['access']<'2'){
    header('Location: chat.php');
    exit;
}

$q = mysql_query("SELECT * FROM `chat` WHERE `id`='$id'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: chat.php');
    exit;
}

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

header('Location: chat.php');
exit;

break;
 
case 'send':

if($user['np']=='ok'){
	header('Location: save.php');
	exit;
}
if($user['level']<'3'){
	echo "Писать можно с 3-го уровня.";
	include ('tmp/foot.php');
	exit;	
}

$text = trim(mysql_real_escape_string(htmlspecialchars($_POST['text'])));

$text=str_replace(".h2m.ru","[реклама]",$text);
$text=str_replace(".ru","[реклама]",$text);
$text=str_replace(".su","[реклама]",$text);
$text=str_replace(".tk","[реклама]",$text);
$text=str_replace(".org","[реклама]",$text);
$text=str_replace(".net","[реклама]",$text);
$text=str_replace(".org.ru","[реклама]",$text);
$text=str_replace(".net.ru","[реклама]",$text);
$text=str_replace(".com.ru","[реклама]",$text);
$text=str_replace(".pp.ru","[реклама]",$text);
$text=str_replace(".wup.ru","[реклама]",$text);
$text=str_replace(".gb7.ru","[реклама]",$text);
$text=str_replace(".ts6.ru","[реклама]",$text);
$text=str_replace(".us","[реклама]",$text);
$text=str_replace(".ua","[реклама]",$text);
$text=str_replace(".com.ua","[реклама]",$text);
$text=str_replace(".org.ua","[реклама]",$text);
$text=str_replace(".net.ua","[реклама]",$text);
$text=str_replace(".pp.ua","[реклама]",$text);

if($text==''){
    header('Location: chat.php');
    exit;
}

mysql_query("INSERT INTO `chat` SET `user`='$uid',`text`='$text',`time`='".time()."'");
 
header('Location: chat.php');
exit;
 
break;
 
    
}
include ('tmp/foot.php');
?>