<?
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();




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 `jurnal` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ank[id]', '[img]http://$_SERVER[HTTP_HOST]/style/img/gal.png[/img] [b]$user[nick][/b] оставил комментарий к вашему фото [url=/foto/$ank[id]/$gallery[id]/komm/$foto[id]/]$foto[name][/url]', '$time')");

mysql_query("INSERT INTO `gallery_komm` (`id_foto`, `id_user`, `time`, `msg`) values('$foto[id]', '$user[id]', '$time', '".my_esc($msg)."')");
mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+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('Комментарий к фото принят + 1 Mонета');
}
}
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 "<table class='post'>\n";
echo '<tr><td class="icon14">';
echo "<a href='/foto/$ank[id]/$gallery[id]/$foto[id]/'><img class='show_foto' src='/foto/foto640/$foto[id].$foto[ras]' alt='$foto[name]' width='50' height='50' /></a>\n";
echo '</td><td class="anput">';
echo "".online($ank['id'])." <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br/>\n";
if ($foto['opis']!=null)
echo "<span style=\"color:$ank[mcolor]\">";
echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($foto['opis']))))))))."<br />\n";
echo "</span>\n";
echo "Комментариев <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 "<div class='textmes'>";
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
echo "<a href='/smiles/index.php'><font color='black'>Смайлы</a></font> | <a href='/foto/$ank[id]/$gallery[id]/komm/$foto[id]/?page=end'><font color='black'>В начало</font></a><br />\n<textarea name=\"msg\"></textarea><br />\n";
echo "<input value=\"Добавить комментарий\" type=\"submit\" />\n";
echo "</form>\n";
echo "</div>\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='komm'>";
echo "Фото еще никто не обсуждал,будь первым!!!<br/>\n";
echo "</div>\n";
}

$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='linechat'>\n";
$num=0;
}else{
echo "  <td class='linechat'>\n";
$num=1;}


echo "".online($ank2['id'])." <a href='/info.php?id=$ank2[id]'><span style=\"color:$ank2[ncolor]\">$ank2[nick]</span></a> \n";

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

echo "</span>\n";
echo "<div style='text-align: right;'><font color='#afb0a3'>".vremja($post['time'])."</font></div>\n";
//if (user_access('guest_delete'))
//echo '[<a href="/deletef.php?id='.$post[id].'"><span style="color:red"><b>X</b></span></a>]</span>';
}
echo "</table>\n";


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




echo "<div class='foot'>";
echo "<a href='/foto/$ank[id]/$gallery[id]/$foto[id]/'>К фотографии</a> ($foto[name])\n";
echo "</div>\n";
echo "<div class='foot'>";
echo "<a href='/foto/$ank[id]/$gallery[id]/'>К фотографиям</a> ($ank[nick])\n";
echo "</div>\n";
echo "<div class='foot'>";
echo "<a href='/foto/$ank[id]/'>К фотоальбомам</a> ($ank[nick])\n";
echo "</div>\n";

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