<?php
require_once('system/func.php');
$title = 'Общий чат';
require_once('system/header.php');
auth(); // Закроем от гостей

mysql_query("DELETE FROM `chat` WHERE `time` < '" . (time() - 3600 * 24) . "'");







if (isset($_GET['text'])) {
    if (isset($_POST['text'])) {
        $err = false;
        $text = text($_POST['text']);
        $ban = mysql_query('SELECT * FROM `ban` WHERE `id_user` = "' . $myID . '" AND `last` > "' . time() . '" ORDER BY `id` DESC LIMIT 1');
        $ban = mysql_fetch_array($ban);
        if (strlen($text) < 2 or strlen($text) > 3000) {
            $err = 'Длина сообщения должна быть в пределах 2 - 3000 символов';
        //}
        //if (isset($ban)) {
            //$err = "На вас наложен, бан осталось " . tl($ban['last'] - time());
        }
        if (!$err) {
            mysql_query("INSERT INTO `chat` SET `id_user` = '$user[id]', `time` = '" . time() . "', `text` = '" . text($text) . "'");

            $_SESSION['msg'] = 'Сообщение отправлено';

            $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '120'"));
            $q = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest_user` WHERE `id_quest` = '$quest[id]' AND `id_user` = '$myID'"));
            if ($q['last'] < time()) {
                mysql_query("update `quest_user` set `koll` = `koll`+'1' where (`id` = '" . $q['id'] . "')");
            }


            header('Location: /46545346465465465456');
            exit();
        } else {
            $_SESSION['msg'] = $err;
            header('Location: /46545346465465465456');
            exit();
        }
    } else {
        $_SESSION['msg'] = 'Введите сообщение';
        header('Location: /46545346465465465456');
        exit();
    }

}
if (isset($_GET['delete_post']) and $user['access'] > 0) {
    mysql_query("DELETE FROM `chat` WHERE `id` = '" . num($_GET['delete_post']) . "'");
    $_SESSION['msg'] = 'Пост удален';
    header('Location: ?');
    exit();
}
echo "<div class='header'>";
if ($user['level'] < 7) {
    echo "В чате можно писать только с " . ico('icons', 'level.png') . " 7 уровня</div>";
} else {

if(isset($_GET['to'])){

$opponent = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = $_GET[to] LIMIT 1"));
?>
 <div id="pokazat"> 
 <a href="#" onClick="document.getElementById('pokazat').style.display='none';document.getElementById('skryt').style.display='';return false;"><img src='/images/smiles/happy.gif'></a>
  </div> 
 <div id="skryt" style="display:none"> 
 <a href="#" onClick="document.getElementById('skryt').style.display='none';document.getElementById('pokazat').style.display='';return false;">Скрыть</a>
 <p>


<form name='form' method='post' action='?text=<?=$_GET[to]?>'><div class='fight'><? echo bbpanel('form', 'text').'';?></div></div>
<?
echo '
<div class="p3">
        <input type="text" class="input_text w97 " id="text" name="text" maxlength="500" value="'.text($opponent['login']).',  " /><br />

      
                    
            <td>
                <span style="min-width: 220px;" >
                    <span class="cb_end">
                        <input type="submit" class="btn_grey" name="submit" value="Отправить"/>
                    </span>
                </span>
            </td>
            
                    <a class="btn_grey" href="/46545346465465465456?'.$LinkKey.'=7854781">
                        <span class="cb_end"><span >
                            Обновить
                        </span></span>
                    </a>
                </div>
            </td>
        </tr></tbody></table>
    </form>';
}else{
    
 ?>
 <div id="pokazat"> 
 <a href="#" onClick="document.getElementById('pokazat').style.display='none';document.getElementById('skryt').style.display='';return false;"><div style='right'><img src='/images/smiles/happy.gif'></a></div>
  </div> 
 <div id="skryt" style="display:none"> 
 <a href="#" onClick="document.getElementById('skryt').style.display='none';document.getElementById('pokazat').style.display='';return false;">Скрыть</a>
 <p>


<form name='form' method='post' action='?text=<?=$_GET[to]?>'><div class='fight'><? echo bbpanel('form', 'text').'';?></div></div></p>
<?
echo '
<div class="p3">
    <form action="?text" method="post">
        <input type="text" class="input_text w97 " id="msg" name="text" maxlength="500" value="" /><br />

        
                    
            <td>
                <span style="min-width: 220px;" >
                    <span class="cb_end">
                        <input type="submit" class="btn_grey" name="submit" value="Отправить"/>
                    </span>
                </span>
            </td>
            
                    <a class="btn_grey" href="/46545346465465465456?'.$LinkKey.'=7854781">
                        <span class="cb_end"><span >
                            Обновить
                        </span></span>
                    </a>
                </div>
            </td>
        </tr></tbody></table>
    </form>';


    }
}

 
echo "</div>";

$online = mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `online` > '" . (time() - 900) . "' AND `fix_mesto` = '$title'"), 0);


 


echo "</div>";

$set['p_str'] = 10;
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `chat`"), 0);
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];
$q = mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT $start, $set[p_str]");
if ($k_post == 0) {
    echo "Сообщений не найдено...";
}
while ($post = mysql_fetch_assoc($q)) {
    echo div('text_p1');


    $ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = $post[id_user] LIMIT 1"));


    echo icons_user($ank[id])." <font color='grey'> <a href='/profile/$ank[id]'> <font color='white'> $ank[login]</font></a><span style='color: red'><span style ='float:right;'>" . tl(time() - $post['time']) . " </span></span> ";
    if ($myID != $ank['id']) {
        echo "<a href='?to=$ank[id]'> " . ico('icons', 'rarrow.png') . " </a>";
    }

    $msg = str_replace(array($user['login'] . ','), "<b><font color='lime'> $user[login]</font>,</b> ", $post['text']);
    echo "<br>" . text_msg($msg) . "";
    if ($user['access'] > 0) {
        echo " <a href='?delete_post=$post[id]'><font color='red'>[ X ]</font></a>";
    }

    echo "</div>";


}


if ($k_post > 10) {
    echo '<div class="wrapper center">';
    str('?', $k_page, $page); //Вывод страниц

    echo "</div>";
}

require_once('system/footer.php');
?>
