<?
if (!isset($user) && !isset($_GET['id_user'])){header("Location: /foto/?".SID);exit;}
if (isset($user))$ank['id']=$user['id'];
if (isset($_GET['id_user']))$ank['id']=intval($_GET['id_user']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"),0)==0){header("Location: /foto/?".SID);exit;}
$ank=mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $ank[id] LIMIT 1"));

$gallery['id']=intval($_GET['id_gallery']);

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1"),0)==0){header("Location: /foto/$ank[id]/?".SID);exit;}
$gallery=mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1"));

$foto['id']=intval($_GET['id_foto']);

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id` = '$foto[id]' LIMIT 1"),0)==0){header("Location: /foto/$ank[id]/$gallery[id]/?".SID);exit;}
$foto=mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery_foto` WHERE `id` = '$foto[id]'  LIMIT 1"));

$set['title']=$gallery['name'].' - Комментарии'; // заголовок страницы

include_once '../sys/inc/thead.php';
title();
include_once '../moduls/vip.inc.php';
aut();




if (isset($_POST['msg']) && isset($user))
{
$msg=$_POST['msg'];
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);

$mat=antimat($msg);
if ($mat)$err[]='В тексте сообщения обнаружен мат: '.$mat;

if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' AND `id_user` = '$user[id]' AND `msg` = '".mysql_escape_string($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}
elseif(!isset($err)){

if ($ank['id']!=$user['id'])mysql_query("INSERT INTO `lenta` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ank[id]', '[b]$user[nick][/b] оставил комментарий к вашему фото [url=/foto/$ank[id]/$gallery[id]/komm/$foto[id]/][img]http://$_SERVER[HTTP_HOST]/foto/foto48/$foto[id].$foto[ras][/img][/url]', '$time')");

mysql_query("INSERT INTO `gallery_komm` (`id_foto`, `id_user`, `time`, `msg`) values('$foto[id]', '$user[id]', '$time', '".my_esc($msg)."')");
if($arrs['time']>=$time)	{
 mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+5)."' WHERE `id` = '$user[id]' LIMIT 1");
}else{
mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+1)."' WHERE `id` = '$user[id]' LIMIT 1");
}
mysql_query("UPDATE `user` SET `foto_komm_post` = '".($user['foto_komm_post']+1)."' WHERE `id` = '$user[id]' LIMIT 1");

$q = mysql_query("SELECT * FROM `frends` WHERE `user` = '$user[id]' AND `lenta_forum` = '1' AND `i` = '1'");
while ($f = mysql_fetch_array($q))
{
$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$f[frend]' LIMIT 1"));

$msg_obsuzh="Добавил [url=/foto/$ank[id]/$gallery[id]/komm/$foto[id]/]комментарий[/url] к фотографии  пользователя [url=/info.php?id=$ank[id]/][b]$ank[nick][/b][/url]\n [url=/foto/$ank[id]/$gallery[id]/$foto[id]/][img]http://$_SERVER[HTTP_HOST]/foto/foto48/$foto[id].$foto[ras][/img][/url]\n";

mysql_query("INSERT INTO `obsuzh` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$a[id]', '$msg_obsuzh', '$time')"); 
}
msg('Комментарий добавлен!');
}
}
if(isset($user) && $user['level']>0&& isset($_GET['delete']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id`='".intval($_GET['delete'])."' AND `id_foto`='$foto[id]' LIMIT 1"),0)!=0)
{
mysql_query("DELETE FROM `gallery_komm` WHERE `id`='".intval($_GET['delete'])."' LIMIT 1");
msg('Комментарий успешно удален');
}


err();
echo "<div class='pa'>\n";
echo "<img src='/style/b.gif' alt='' class='icon'/> <a href='/foto/$ank[id]/$gallery[id]/$foto[id]/'>К фотографии</a> ($foto[name])\n";
echo "</div>\n";	

echo "<table class='post'>\n";
echo '<td class="faz">';
echo "<a href='/foto/$ank[id]/$gallery[id]/$foto[id]/'><img class='show_foto' src='/foto/foto640/$foto[id].$foto[ras]' alt='$foto[name]'  align='left' style='margin-right:3px' width='48' height='48' alt=''  /></a>\n";

echo "".online($ank['id'])." <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\"><b>$ank[nick]</b></span></a><br/>\n";
if ($foto['opis']!=null)
echo "<img src='/icons/administrative-docs.png' alt='' /> \n";
if ($foto['opis']!=null)echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($foto['opis']))))))))."<br />\n";
else 
echo "<img src='/icons/administrative-docs.png' alt='' /> <font color='#8e908e'>Описание отсутствует</font><br/>\n";

echo "<img src='/icons/process.png' alt='' /> Комментариев <font color='red'>".mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '$foto[id]'"),0)."</font>\n";
echo '</td></tr>';
echo "</table>\n";
if (isset($user))
{
echo "<form method='post' name='message' action='?$passgen'>\n";
if ($set['web'] && is_file(H.'style/themes/'.$set['set_them'].'/altername_post_form.php'))
include_once H.'style/themes/'.$set['set_them'].'/altername_post_form.php';
else
include_once '../panel_bb/bb.php';
echo "<textarea name=\"msg\"></textarea><br />\n";
echo "<input value=\"Отправить\" type=\"submit\" /> <a href='/foto/$ank[id]/$gallery[id]/komm/$foto[id]/?page=end'>В начало</a>\n";
echo "</form>\n";
}
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '$foto[id]'"),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 "<div class='titr'>";
echo "Покуда комментариев нету. Оставь первый!!!<br/>\n";

echo '</div>';
}
$q=mysql_query("SELECT * FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_assoc($q))
{

$ank2=mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '$post[id_user]' LIMIT 1"));
echo "   <tr>\n";
if($num==1){
echo "  <td class='pa'>\n";
$num=0;
}else{
echo "  <td class='faz'>\n";
$num=1;}


echo "".online($ank2['id'])." <a href='/id$ank2[id]'><b>".GradientText("$ank2[nick]", "$ank2[ncolor]", "$ank2[ncolor2]")."</b></a> \n";
echo "<br/>\n";
echo "<span style=\"color:$ank2[mcolor]\">\n";
echo output_text($post['msg'])." \n";
echo "</span>\n";
if (user_access('guest_delete'))echo " [<a href='?delete=$post[id]' title='Удалить комментарий'>x</a>]";

echo "<div style='text-align: right;'><font color='#afb0a3'>".vremja($post['time'])."</font> <img src='/icons/full-time.png'></div>\n";

}
echo "</table>\n";


if ($k_page>1)str('?',$k_page,$page); // Вывод страниц



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