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


$path = '../'; //путь

$textl = 'Форум';
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
include($path . 'files/core.php');
include('head.php');

$code=rand(11111,99999);
$_SESSION['code']=$code;
$id = intval($_GET['id']);

$topic = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_topics` WHERE `id` = '$id'"));
if(isset($topic['id'])){
	echo '<div class="m_bg">'.$topic['title'].'</div>';
	$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_msg` WHERE `id_topic` = '$id'"), 0);
	if($count > 0){
		$pages = ceil($count/10);
		if(isset($_GET['page'])){
			$page = abs(intval($_GET['page']));
		}else{
			$page = 1;
		}
		$from = ($page-1)*10;

//start
function smiles($msg){
$dir = opendir ("../pic/smiles"); 
while ($file = readdir ($dir)) {
if (ereg (".gif$", "$file")){
$file2=str_replace(".gif","",$file);
$msg=str_replace(":$file2",'<img src="../pic/smiles/'.$file.'" alt="">',$msg);
}}
closedir ($dir);
return $msg;  }

function s($msg){
$msg=str_replace("\\r\\n",'<br />',$msg);
return $msg;  }





//end
//bb
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;
}
//
		$result = mysql_query("SELECT * FROM `forum_msg` WHERE `id_topic` = '$id' ORDER BY `time` LIMIT $from, 10");
		while($msg = mysql_fetch_assoc($result)){
			if(!isset($num)) $num = 1;  
			$num++;
////////////////////////////
$laikas = 150;
$dabar = time();
$timeout = $dabar - $laikas;
$asd = mysql_num_rows(mysql_query("SELECT laikas, usr FROM online WHERE laikas > '$timeout' AND usr='$msg[login]'"));




if($asd == 0){
$status = '<b><font color="red">[off]'; }
else
{
$status = '<b><font color="green">[on]'; }
echo"<div class='event'>";
echo $status;
			echo '<a href="../search.php?nick='.$msg['login'].'&amp;go=go">'.$msg['login'].'</a></b></font> <i>'.date('d.m.Y / H:i', $msg['time']).'</i>';
						echo ' [<a href="posting.php?act=answer&amp;id='.$msg['id'].'&amp;page='.$pages.'">отв</a>]';

			if($msg['login'] == $log or $udata['prava'] > 1){
				if($msg['time']+900 > time() or $udata['prava'] > 1){
					echo ' <a href="posting.php?act=msg_edit&amp;id='.$msg['id'].'">ред</a> | ';
				}
			}
			if($udata['prava'] > 1){ echo '<a href="posting.php?act=msg_del&amp;id='.$msg['id'].'">X</a>';}
			echo '<br/>'.smiles(addslashes(AddBB(s(htmlspecialchars($msg['text']))))).'</div>';
			
			
			
		}
	}
	$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_msg` WHERE `id_topic` = '$id'"), 0);
	
	if($udata['prava'] > 1){
		if($topic['closed'] == 0){
			echo '<div class="m_bg"><a href="posting.php?act=topic_close&amp;id='.$id.'">Закр</a>|';
		}
	}
	if($udata['prava'] > 1){
		if($topic['closed'] == 1){
			echo '<div class="m_bg"><a href="posting.php?act=topic_open&amp;id='.$id.'">Откр</a>|';
		}
	}
	if($udata['prava'] > 1){
		if($topic['closed'] == 0 or $udata['prava'] > 1){
			echo '<a href="posting.php?act=topic_edit&amp;id='.$id.'">Ред</a>';
		}
	}
	if($udata['prava'] > 1){
		echo '|<a href="posting.php?act=topic_del&amp;id='.$id.'">Удал</a>';
	}
	if($udata['prava'] > 1){
		echo'</div>';
	}
echo'<div class="m_bg">';
	navig($page, 'topic.php?id='.$id.'&amp;', $pages);
$result = mysql_query("SELECT * FROM `forum_msg` WHERE `id_topic` = '$id' ORDER BY `time` LIMIT 1");
		while($msg = mysql_fetch_assoc($result)){
echo'<br> <a href=index.php>Форум</a> - <a href=index.php?act=cat_view&id='.$msg[id_cat].'>В раздел</a> ';}
echo '</div>';
	if($topic['closed'] == 0){
			echo '<div class="m_bg">Добавить сообщение:<br/>';
			echo '<form action="posting.php?act=msg_add&amp;id='.$topic['id'].'&amp;page='.$page.'" method="post" name="form">';
                                        //  echo'Проверочный код:'.$code.'<br><input type="text" name="code" maxlength="5" size="5"><br>';
			echo '<textarea name="text" rows="3"></textarea><br/>';
			echo '<input name="submit" type="submit" value="Добавить" /></form></div>';
	}else{
		echo '<div class="m_bg">Вы не можете добавить сообщение, т.к. тема закрыта!</div>';
	}
}else{
	echo'Выбранной темы не существует!';
}
	
include('end.php');
?>