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

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

$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;  }

    function gradient($txt, $from=0x00FF00, $to=0x00FFFF)
        {
                // count of all characters
                $strlen = strlen($txt);

                // count of nonblank characters
                $grdlen = $strlen - preg_match_all('/\s/', $txt, $null) - 1;

                // skip a colorizing of whitespace line
                if ( ! $grdlen ) {
                        return $txt;
                }

                // skip a single coloring
                if ( $from == $to ) {
                        return sprintf('<span style="color: #%06x">%s</span>', $from, $txt);
                }

                // represent the start and the end colors as arrays
                $f = array(
                        floor($from / 0x10000),
                        floor(($from & 0xffff) / 0x100),
                        $from & 0xff,
                );
                $t = array(
                        floor($to / 0x10000),
                        floor(($to & 0xffff) / 0x100),
                        $to & 0xff,
                );

                // current value of red, green and blue colors separately
                $r = $f[0];
                $g = $f[1];
                $b = $f[2];

                // deltas per each color
                $rd = ($t[0] - $f[0]) / $grdlen;
                $gd = ($t[1] - $f[1]) / $grdlen;
                $bd = ($t[2] - $f[2]) / $grdlen;

                $result = '';
                for ($i = 0; $i < $strlen; $i++) {
                        // skip colorizing of whitespaces
                        if ( preg_match('/\s/', $txt{$i}) ) {
                                $result .= $txt{$i};
                                continue;
                        }

                        // colorize one character
                        $result .= sprintf('<span style="color: #%02x%02x%02x">%s</span>', $r, $g, $b, $txt{$i});

                        // next color
                        $r += $rd;
                        $g += $gd;
                        $b += $bd;
                }

                return $result;
        }


function AddBB($var) {
$search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[red\](.*?)\[\/red\]/is',
'/\[blue\](.*?)\[\/blue\]/is',
'/\[green\](.*?)\[\/green\]/is',
'/\[yellow\](.*?)\[\/yellow\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is'
);

$replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<font color="red">$1</font>',
'<font color="blue">$1</font>',
'<font color="green">$1</font>',
'<font color="yellow">$1</font>',
'<u>$1</u>',
'<a href="$1">$1</a>',
'<a href="$1">$2</a>'
);
$var = preg_replace ($search, $replace, $var);
return $var;
}
$rand = rand(1000,9999);
///////////////////
echo'<div class="hid" align="left">';
echo "<a href=\"smile.php?\">Смайлы</a><br/>";
if($udata[prava] == 2){echo "<a href=\"md_chat.php?\">Наказания</a><br/>";}
echo "<a href=\"chat.php?r=$rand\">Обновить</a>";
echo "<form action=\"chat.php?mod=writes\" method=\"POST\" name=\"hh\" >";
echo "<input type=\"text\" name=\"zin\" maxlength=\"5000\"/> ";
echo"  <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 komentarai"));
$puslap = floor($viso/10);
$razn = 4;// для Москвы зимой
$times = gmdate("H:i:s",time()+($razn*3600));



echo "<center>= $times =";

echo"</center>";
$asd = mysql_query("SELECT * FROM komentarai 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 = AddBB(smiles($koment));

$req = mysql_query("SELECT * FROM `users` WHERE `usr` = '$log'");
$usdata = mysql_fetch_array($req);
$koment = str_ireplace($usdata[usr], '<font color=#8B814C><b>'.$usdata[usr].'</b></font>', $koment);

$adm = $dsa['prava'];
if($dsa[prava] == '5'){$adm = '<b><font color=red>[SAdm]</font></b>';}
if($dsa[prava] == '4'){$adm = '<b><font color=blue>[Adm]</font></b>';}
if($dsa[prava] == '3'){$adm = '<b><font color=gold>[SM]</font></b>';}
if($dsa[prava] == '2'){$adm = '<b><font color=gold>[M]</font></b>';}
if($dsa[prava] == '0'){$adm = ' ';}
//выделяем

//Анти-матюгальник
str_replace("сука", "OOPS", $koment);
//Анти-матюгальник



$req = mysql_query("SELECT `font` FROM `users` WHERE `usr` = '$nickas'");
$usdata = mysql_fetch_array($req);
if($usdata[font] != grad){
echo "<div class='event'><b>[$time]</b><a href=\"chat.php?nick=$nickas&mod=write\"><font color=$usdata[font]>$nickas</font></a>$adm
<a href=\"search.php?nick=$nickas&go=go\"><img src='pic/inf.png'></a></a>: $koment";}
elseif($usdata[font] == grad){
echo "<div class='event'><b>[$time]</b><a href=\"chat.php?nick=$nickas&mod=write\">";
echo gradient($nickas);
echo"</a>$adm
<a href=\"search.php?nick=$nickas&go=go\"><img src='pic/inf.png'></a></a>: $koment";}
if($udata[prava]>=2){
echo"[<a href=\"chat.php?mod=del_post&p=$dsa[id]\">X</a>]";

}
echo'</div>';
}
echo "</div>";

if ($_GET[page] > 0)
{
echo "<a href=\"chat.php?page=$back\"> < Назад</a> ";
}
echo"";
if($_GET[page] < $puslap || $_GET[page] == "" || $_GET[page] == 0)
{echo " <a href=\"chat.php?page=$next\">Далее ></a>";}
break;

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

case 'write':

echo"<b>Сообщение</b><br/>";
echo "<form action=\"chat.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='pic/feather.png' alt=''><a href=\"chat.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 komentarai WHERE komentaras = '$msg'"));
$b = mysql_fetch_array(mysql_query("SELECT kada FROM komentarai WHERE nick = '$log' ORDER BY kada DESC LIMIT 1"));
$data_kom = strip_tags($b['kada']);
$data = date("y/m/d H:i:s", strtotime("+30 seconds"));
$data_dbr = date("y/m/d H:i:s");
$razn = 4;
$time = gmdate("H:i:s",time()+($razn*3600));
if($data_dbr >= $data_kom && $msg != "")
{
mysql_query("INSERT INTO komentarai SET nick = '$log', komentaras = '$msg', kada = '$data', time = '$time', prava = '$udata[prava]'");
mysql_query("UPDATE users SET fchat = '$udata[fchat]'+'1' WHERE usr = '$log'");
echo'Сообщение успешно добавлено. Через 3 сек. Вы будете перенаправлены в Чат<br/>';
echo'<a href=chat.php>В чат</a>';
echo'<meta http-equiv="refresh" content="3; url=chat.php">';}
else  { echo"Одно сообщение в 30 секунд, пожалуйста<br/>";
echo'<a href=chat.php>В чат</a>';
echo'<meta http-equiv="refresh" content="3; url=chat.php">';}
break;

case 'trinti':

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

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