<?
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';


$set['title']='Форум - поиск';
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации


$search=NULL;
if (isset($_SESSION['search']))$search=$_SESSION['search'];
if (isset($_POST['search']))$search=$_POST['search'];


if ($search==NULL)
unset($_SESSION['search']);
else
$_SESSION['search']=$search;

$search=ereg_replace("( ){2,}"," ",$search);
$search=ereg_replace("^( ){1,}|( ){1,}$","",$search);



if (isset($_GET['go']) && $search!=NULL)
{
$search_a=explode(' ', $search);

for($i=0;$i<count($search_a);$i++)
{
$search_a[$i]='#'.preg_quote(esc(trim(stripcslashes(htmlspecialchars($search_a[$i]))))).'#uim';
}

$adm_add=NULL;

if (!isset($user) || $user['level']==0){
$q222=mysql_query("SELECT * FROM `forum_f` WHERE `adm` = '1'");
while ($adm_f = mysql_fetch_array($q222))
{
$adm_add.="`id_forum` <> '$adm_f[id]' AND ";

}
}

$q_search=str_replace('%','',$search);
$q_search=str_replace(' ','%',$q_search);
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_p` WHERE$adm_add `msg` like '%".mysql_escape_string($q_search)."%'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
echo "<table class='post'>\n";
if ($k_post==0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "По вашему запросу ничего не найдено\n";
echo "  </td>\n";
echo "   </tr>\n";
}
$q=mysql_query("SELECT * FROM `forum_p` WHERE$adm_add `msg` like '%".mysql_escape_string($q_search)."%' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_array($q))
{
$forum=mysql_fetch_array(mysql_query("SELECT * FROM `forum_f` WHERE `id` = '$post[id_forum]' LIMIT 1"));
$razdel=mysql_fetch_array(mysql_query("SELECT * FROM `forum_r` WHERE `id` = '$post[id_razdel]' LIMIT 1"));
$them=mysql_fetch_array(mysql_query("SELECT * FROM `forum_t` WHERE `id` = '$post[id_them]' LIMIT 1"));
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
echo "   <tr>\n";


if ($set['set_show_icon']==2){
echo "  <td class='icon48' rowspan='2'>\n";
avatar($ank['id']);
echo "  </td>\n";
}
elseif ($set['set_show_icon']==1)
{
echo "  <td class='icon14'>\n";
echo "<img src='/style/themes/$set[set_them]/user/$ank[pol].png' alt='' />";
echo "  </td>\n";
}

echo "  <td class='p_t'>\n";
echo "<a href='/info.php?id=$ank[id]' title='Анкета $ank[nick]'>$ank[nick]</a> (".vremja($post['time']).")\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
if ($set['set_show_icon']==1)echo "  <td class='p_m' colspan='2'>\n"; else echo "  <td class='p_m'>\n";
echo "<a href='/forum/$forum[id]/'>$forum[name]</a> &gt; <a href='/forum/$forum[id]/$razdel[id]/'>$razdel[name]</a> &gt; <a href='/forum/$forum[id]/$razdel[id]/$them[id]/mark/".stripcslashes(htmlspecialchars($search))."'>$them[name]</a><br />\n";
//echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($post['msg']))))))))."<br />\n";
echo esc(preg_replace($search_a, '<span class="search_cit">\\0</span>', trim(br(bbcode(stripcslashes(htmlspecialchars($post['msg'])))))))."<br />\n";
echo "  </td>\n";
echo "   </tr>\n";
}
echo "</table>\n";

if ($k_page>1)str("search.php?go&amp;",$k_page,$page); // Вывод страниц
}
else
echo "Введите текст, который Вы хотите найти<br />\n";




echo "<form method=\"post\" action=\"search.php?go\">\n";
$search=stripcslashes(htmlspecialchars(esc($search)));
echo "<input type=\"text\" name=\"search\" maxlength=\"64\" value=\"$search\" />\n";
echo "<input type=\"submit\" value=\"Поиск\" />\n";
echo "</form>\n";



echo "<div class=\"menu\">\n";
echo "&laquo;<a href=\"index.php\" title=\"Вернуться к подфорумам\">Форум</a>\n";
echo "</div>\n";

include_once '../sys/inc/tfoot.php';
?>
