<?php
define('PROTECTOR', 1);

$headmod = 'tchat';//фикс. места

$textl='Торговый Чат';
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
include($path.'files/core.php');
include($path.'files/head.php');
include($path.'files/zag.php');

switch($_GET[mod]){

default:

function smiles($string){
$dir = opendir ("pic/smiles"); 
while ($file = readdir ($dir)) {
if (ereg (".gif$", "$file")){
$file2=str_replace(".gif","",$file);
$string=str_replace(":$file2",'<img src="pic/smiles/'.$file.'" alt="">',$string);
}}
closedir ($dir);
return $string;  }
/////////////
$rand = rand(1000,9999);
///////////////////
echo'<div class="hid" align="left">';
echo "<a href=\"smile.php?\">Смайлы</a><br/>";
echo "<a href=\"tchat.php?r=$rand\">Обновить</a>";
echo "<form action=\"tchat.php?mod=writes\" method=\"POST\">";
echo "<input type=\"text\" name=\"zin\" maxlength=\"5000\"/> <input type=\"submit\" value=\"Написать\" class=\"ibutton\"></div>";

if ($_GET[page] == "" || $_GET[page] < 0 || $_GET[page] == "0") 
{
$_GET[page] = 0;
}
$next = $_GET[page] + 1;
$back = $_GET[page] - 1;
$num = $_GET[page] * 10;
if($_GET[page] == "0")
{$i = 1;}
else{$i = ($_GET[page]*10)+1;}
$viso = mysql_num_rows(mysql_query("SELECT komentaras FROM tchat"));
$puslap = floor($viso/10);
$times = date("H:i");
echo "<center>-=$times=-</center>";
$asd = mysql_query("SELECT * FROM tchat ORDER BY id DESC LIMIT $num,10");
echo"<div align='left'>";
while($dsa = mysql_fetch_array($asd))
{
$nickas = strip_tags($dsa['nick']);
$koment = strip_tags($dsa['komentaras']);
$time = strip_tags($dsa['time']);
$koment = smiles($koment);

echo "<b>[$time]<a href=\"tchat.php?nick=$nickas&amp;mod=write\">$nickas</a>
<a href=\"search.php?nick=$nickas&amp;go=go\">[i]</a>:</b> $koment ";
if($udata[prava]>=2){
echo"[<a href=\"tchat.php?mod=del_post&amp;p=$dsa[id]\">X</a>]";
}
echo'<br/>';
}
echo "</div>";
if($udata[prava] >= 2)
{
echo "<a href=\"tchat.php?mod=trinti\">Удалить сообщения</a><br/>";
}
if ($_GET[page] > 0)
{
echo "<a href=\"tchat.php?page=$back\">back</a>";
}
elseif ($_GET[page] == 0)
{
echo "back";
}
echo"|";
if($_GET[page] < $puslap || $_GET[page] == "" || $_GET[page] == 0)
{echo "<a href=\"tchat.php?page=$next\">next</a>";}
else
{echo "next";}
break;

case 'del_post':
if($udata[prava] >= 2)
{
if(empty($_GET[p])){
echo"Не выбран пост!<br/>";
}else{
$asd = mysql_query("SELECT * FROM tchat WHERE id='$_GET[p]' LIMIT 1");
$avto=mysql_num_rows($asd);
if($avto==0){
echo'Нет такого поста!<br/>';
}else{
mysql_query("DELETE FROM `tchat` WHERE id='$_GET[p]' LIMIT 1");
echo'Пост успешно удалён!<br/>';
echo "<a href=\"tchat.php?\">Назад</a>";
}
}
}else{
echo "Ошибка!Доступ закрыт!";
}
break;

case 'write':

echo"<b>Объявление</b><br/>";
echo "<form action=\"tchat.php?mod=writes\" method=\"POST\">";
if (isset($_GET[nick]))
{
$_GET[nick] = htmlspecialchars($_GET[nick]);
echo "<input type=\"text\" name=\"zin\" maxlength=\"250\" value=\"$_GET[nick], \" size=\"10\"/><br/>";
}
else
{
echo "<input type=\"text\" name=\"zin\" maxlength=\"250\" size=\"10\"/><br/>";
}
echo "<input type=\"submit\" value=\"Ok\" class=\"ibutton\"><br/>";
echo "<img src='img/feather.png' alt=''><a href=\"tchat.php?\">Назад</a><br>";
break;

case 'writes':

$msg=$_POST['zin'];
$msg=substr($msg, 0, 512);
$msg=stripslashes(htmlspecialchars($msg));
$msg=str_replace("\r\n","<br />",$msg);
$msg=str_replace("\r","<br />",$msg);
$msg=str_replace("\n","<br />",$msg);
$msg = addslashes($msg);
$msg=preg_replace ("|[\r\n]+|si","",$msg);
$a = mysql_num_rows(mysql_query("SELECT komentaras FROM tchat WHERE komentaras = '$msg'"));
$b = mysql_fetch_array(mysql_query("SELECT kada FROM tchat WHERE nick = '$log' ORDER BY kada DESC LIMIT 1"));
$data_kom = strip_tags($b['kada']);
$data = date("y/m/d H:i:s", strtotime("+900 seconds"));
$data_dbr = date("y/m/d H:i:s");
$time = date("H:i");
if($data_dbr >= $data_kom && $msg != "")
{
mysql_query("INSERT INTO tchat SET nick = '$log', komentaras = '$msg', kada = '$data', time = '$time'");
$rand = rand(1000,9999);
//start
function smiles($string){
$dir = opendir ("pic/smiles"); 
while ($file = readdir ($dir)) {
if (ereg (".gif$", "$file")){
$file2=str_replace(".gif","",$file);
$string=str_replace(":$file2",'<img src="pic/smiles/'.$file.'" alt="">',$string);
}}
closedir ($dir);
return $string;  }
/////////////
$rand = rand(1000,9999);
///////////////////
echo'<div class="hid" align="left">';
echo "<a href=\"smile.php?\">Смайлы</a><br/>";
echo "<a href=\"tchat.php?r=$rand\">Обновить</a>";
echo "<form action=\"tchat.php?mod=writes\" method=\"POST\">";
echo "<input type=\"text\" name=\"zin\" maxlength=\"5000\"/> <input type=\"submit\" value=\"Написать\" class=\"ibutton\"></div>";

if ($_GET[page] == "" || $_GET[page] < 0 || $_GET[page] == "0") 
{
$_GET[page] = 0;
}
$next = $_GET[page] + 1;
$back = $_GET[page] - 1;
$num = $_GET[page] * 10;
if($_GET[page] == "0")
{$i = 1;}
else{$i = ($_GET[page]*10)+1;}
$viso = mysql_num_rows(mysql_query("SELECT komentaras FROM tchat"));
$puslap = floor($viso/10);
$times = date("H:i");
echo "<center>-=$times=-</center>";
$asd = mysql_query("SELECT * FROM tchat ORDER BY id DESC LIMIT $num,10");
echo"<div align='left'>";
while($dsa = mysql_fetch_array($asd))
{
$nickas = strip_tags($dsa['nick']);
$koment = strip_tags($dsa['komentaras']);
$time = strip_tags($dsa['time']);
$koment = smiles($koment);

echo "<b>[$time]<a href=\"tchat.php?nick=$nickas&amp;mod=write\">$nickas</a>
<a href=\"search.php?nick=$nickas&amp;go=go\">[i]</a>:</b> $koment ";
if($udata[prava]>=2){
echo"[<a href=\"tchat.php?mod=del_post&amp;p=$dsa[id]\">X</a>]";
}
echo'<br/>';
}
echo "</div>";
if($udata[prava] >= 2)
{
echo "<a href=\"tchat.php?mod=trinti\">Удалить сообщения</a><br/>";
}
if ($_GET[page] > 0)
{
echo "<a href=\"tchat.php?page=$back\">back</a>";
}
elseif ($_GET[page] == 0)
{
echo "back";
}
echo"|";
if($_GET[page] < $puslap || $_GET[page] == "" || $_GET[page] == 0)
{echo "<a href=\"tchat.php?page=$next\">next</a>";}
else
{echo "next";}
//end
}
elseif($data_dbr < $data_kom)
{
$sec = $data_kom-$data_dbr;
$rand = rand(1000,9999);
echo "Массовое размещение сообщений в торговом чате запрещено. Одно сообщение в 15 минут.<br/>";
echo"<a href=\"tchat.php?r=$rand\">Продолжить</a>";
}
elseif($msg == "")
{
$rand = rand(1000,9999);
echo "Вы не написали сообщение!<br/>";
echo"<a href=\"tchat.php?r=$rand\">Продолжить</a>";
}
else
{
$rand = rand(1000,9999);
echo "Ошибка!<br/>";
echo"<a href=\"tchat.php?r=$rand\">Продолжить</a>";
}
break;

case 'trinti':

if($udata[prava] >= 2)
{
mysql_query("DELETE FROM komentarai");
echo "Все сообщения удалены!";
echo "<br/><a href=\"tchat.php?\">Назад</a><br/></div>";
}
else
{
echo "Ошибка!Доступ закрыт!<br/></div>";
}
break;
}

include($path.'files/down.php');
?>
