<?

if (!isset($user) && !isset($_GET['id_user'])){
header("Location: /modules/photo/?".SID);
exit;
} 

if (isset($user) && isset($_GET['id_user']))$check_foto=mysql_fetch_assoc(mysql_query("SELECT `id_gallery`,`id`,`id_user`FROM `gallery_foto` WHERE `id_gallery` = '".abs(intval($_GET['id_gallery']))."' AND `id` = '".abs(intval($_GET['id_foto']))."' AND `id_user` = '".abs(intval($_GET['id_user']))."' LIMIT 1")); 

//------------------------------- 
$ank=get_user($check_foto['id_user']); 
$gallery['id']=$check_foto['id_gallery']; 
$foto['id']=$check_foto['id']; 
if ($ank['id']=='0'){
header("Location: /modules/photo/?".SID);
exit;
} 

//Бан пользователя 
if (mysql_result(query("SELECT COUNT(*) FROM `ban` WHERE `razdel` = 'foto' AND `id_user` = '$user[id]' AND (`time` > '$time' OR `view` = '0' OR `navsegda` = '1')"), 0) != 0){
header('Location: /user/ban.php?'.SID);
exit();
} 
$gallery = mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1")); 
$foto = mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery_foto` WHERE `id` = '$foto[id]'  LIMIT 1"));

# Закладки
# Добавляем в закладки
if (isset($_GET['fav']) && $_GET['fav'] == 1){ 
if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `bookmarks` WHERE `id_user` = '" . $user['id'] . "' AND `id_object` = '" . $foto['id'] . "' AND `type`='foto' LIMIT 1"),0) == 0){
mysql_query("INSERT INTO `bookmarks` (`type`,`id_object`, `id_user`, `time`) VALUES ('foto','$foto[id]', '$user[id]', '$time')");
$_SESSION['message'] = 'Публикация сохранена'; 
header("Location: /modules/photo/$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
exit;
}
}
# Удаляем из закладок
if (isset($_GET['fav']) && $_GET['fav'] == 2){ 
if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `bookmarks` WHERE `id_user` = '" . $user['id'] . "' AND `id_object` = '" . $foto['id'] . "' `type`='foto' LIMIT 1"),0) == 0){
mysql_query("DELETE FROM `bookmarks` WHERE `id_user` = '$user[id]' AND  `id_object` = '$foto[id]' AND `type`='foto'");
$_SESSION['message'] = 'Удаленно из сохраненок'; 
header("Location: /modules/photo/$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
exit;
}
}
# Закладки end

$IS = GetImageSize(H.'/files/gallery/foto/'.$foto['id'].'.'.$foto['ras']);
printf("", $IS[0], $IS[1]);
$w = $IS[0];
$h = $IS[1];

if ((user_access('foto_foto_edit')) || (isset($user) && $ank['id'] == $user['id']))
include 'inc/gallery_show_foto_act.php';

/*------------очищаем счетчик этого обсуждения-------------*/
if (isset($user))
{
    query("UPDATE `discussions` SET `count` = '0' WHERE `id_user` = '$user[id]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1");
    query("UPDATE `notification` SET `read` = '1' WHERE `type` = 'foto_komm' AND `id_user` = '$user[id]' AND `id_object` = '$foto[id]'");
}
/*---------------------------------------------------------*/

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

if(!isset($err)){
//Уведомления об ответах
if (isset($ank_reply['id'])){
$notifiacation = mysql_fetch_assoc(query("SELECT * FROM `notification_set` WHERE `id_user` = '" . $ank_reply['id'] . "' LIMIT 1"));
if ($notifiacation['komm'] == 1 && $ank_reply['id'] != $user['id'])
query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `type`, `time`) VALUES ('$user[id]', '$ank_reply[id]', '$foto[id]', 'foto_komm', '$time')");
}

// Обсуждения
//Отправляем друзьям
$q = query("SELECT * FROM `frends` WHERE `user` = '".$gallery['id_user']."' AND `i` = '1'");
while ($f = mysql_fetch_array($q)){
$a = get_user($f['frend']);
$discSet = mysql_fetch_array(query("SELECT * FROM `discussions_set` WHERE `id_user` = '".$a['id']."' LIMIT 1")); // Общая настройка обсуждений
if ($f['disc_foto'] == 1 && $discSet['disc_foto'] == 1){
if (mysql_result(query("SELECT COUNT(*) FROM `discussions` WHERE `id_user` = '$a[id]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1"),0)==0){
if ($a['id'] != $user['id'] || $a['id'] != $foto['id_user'])
query("INSERT INTO `discussions` (`id_user`, `avtor`, `type`, `time`, `id_sim`, `count`) values('$a[id]', '$gallery[id_user]', 'foto', '$time', '$foto[id]', '1')"); 
}else{
$disc = mysql_fetch_array(query("SELECT * FROM `discussions` WHERE `id_user` = '$a[id]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1"));
if ($gallery['id_user'] != $user['id'] || $a['id'] != $foto['id_user'])
query("UPDATE `discussions` SET `count` = '" . ($disc['count'] + 1) . "', `time` = '$time' WHERE `id_user` = '$a[id]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1");
}
}
}

//Отправляем автору
if (mysql_result(query("SELECT COUNT(*) FROM `discussions` WHERE `id_user` = '$gallery[id_user]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1"),0)==0){
if ($gallery['id_user'] != $user['id'])
query("INSERT INTO `discussions` (`id_user`, `avtor`, `type`, `time`, `id_sim`, `count`) values('$gallery[id_user]', '$gallery[id_user]', 'foto', '$time', '$foto[id]', '1')");
}else{
$disc2 = mysql_fetch_array(query("SELECT * FROM `discussions` WHERE `id_user` = '$gallery[id_user]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1"));
if ($gallery['id_user'] != $user['id'])
query("UPDATE `discussions` SET `count` = '".($disc2['count']+1)."', `time` = '$time' WHERE `id_user` = '$gallery[id_user]' AND `type` = 'foto' AND `id_sim` = '$foto[id]' LIMIT 1");
}
query("INSERT INTO `gallery_komm` (`id_foto`, `id_user`, `time`, `msg`) values('$foto[id]', '$user[id]', '$time', '".my_esc($msg)."')");
$_SESSION['message'] = 'Сообщение добавлено';

# Отправляем уведомление автору что ему прокоментировали фотографию
if (isset($user) && $ank['id']!=$user['id']){
mysql_query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `all_type`, `type`, `time`) VALUES ('$user[id]', '$ank[id]', '$foto[id]', '$foto[id_gallery]', 'photo_kom', '$time')");
}

header("Location: ?page=".intval($_GET['page']));
exit;
}
}

if ((user_access('foto_komm_del') || $ank['id'] == $user['id']) && isset($_GET['delete']) && mysql_result(query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id`='".intval($_GET['delete'])."' AND `id_foto`='$foto[id]' LIMIT 1"),0)!=0){
query("DELETE FROM `gallery_komm` WHERE `id`='".intval($_GET['delete'])."' LIMIT 1");
admin_log('Фотогалерея','Фотографии', "Удаление комментария к фото [url=/id$ank[id]]" . user::nick($ank['id'], 0) . "[/url]");
$_SESSION['message'] = 'Комментарий удален';
header("Location: ?page=".intval($_GET['page']));
exit;
}

# Ставим лайк
if (isset($_GET['like'])){
$stena=mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery_foto` WHERE `id` = '".intval($_GET['like'])."' LIMIT 1"));
$ank3=get_user($stena['id_user']);
$l=mysql_result(mysql_query("SELECT COUNT(*) FROM `photo_like` WHERE `id_photo` = '$stena[id]'"),0);
if (isset($_GET['like']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `photo_like` WHERE `id_photo` = '$stena[id]' AND `id_user` = '$user[id]' LIMIT 1"),0)==0){
mysql_query("INSERT INTO `photo_like` (`id_user`, `id_photo`) values('$user[id]', '$stena[id]')");

# Отправляем уведомление автору что ему поставили лайк
if (isset($user) && $ank['id']!=$user['id']){
mysql_query("INSERT INTO `notification` (`avtor`, `id_user`, `id_object`, `all_type`, `type`, `time`) VALUES ('$user[id]', '$ank[id]', '$stena[id]', '$foto[id_gallery]', 'like', '$time')");
}
}
}

# Убрать лайк
if (isset($_GET['dlike'])){
$stena=mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery_foto` WHERE `id` = '".intval($_GET['dlike'])."'"));
$ank3=get_user($stena['id_user']);
$l=mysql_result(mysql_query("SELECT COUNT(*) FROM `photo_like` WHERE `id_photo` = '$stena[id]'"),0);
mysql_query("DELETE FROM `photo_like` WHERE `id_user` = '$user[id]' AND `id_photo` = '$stena[id]'");
}

# Заголовок
$set['title'] = 'Фото';
include_once '../../sys/inc/thead.php';
title();
err();
aut();

# Пользователь + меню
echo '<table class="block3">';
echo '<td class="block_avatar">';
echo ''.avatar($ank['id']).'';
echo '</td>';
echo '<td style="vertical-align: middle;" class="block_info">';
# Меню фото
include 'inc/gallery_show_foto_form.php';
echo '<span class="nick">'.user::nick($ank['id']).'</span>';
echo '<div class="block_msg">';
echo ''. output_text($post['msg']) . '';
echo '</div>';
echo '</td>';
echo '</table>';

# Вывод фотографии
echo '<div class="block2">';
echo '<center><img style="width:80%;" src="/modules/photo/foto/'.$foto[id].''.$foto[ras].'"></center></br>';

echo '<div style="padding: 0 0 15px 0;">';
if ($foto['opis'] != null)
echo output_text($foto['opis']);
echo '</div>';

# Счетчики
$count_kom = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '".$foto[id]."' "),0);
$count_like = mysql_result(mysql_query("SELECT COUNT(*) FROM `photo_like` WHERE `id_photo` = '$foto[id]'"),0);

# Лайки + комментарии
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `photo_like` WHERE `id_user` = '".$user['id']."' AND `id_photo` = '".$foto['id']."' LIMIT 1"),0)==0){
echo '<a href="?like='.$foto['id'].'"><img src="/img/like.png"> <font color="#1f1f1f">'.$count_like.'</font></a>';
}else{
echo '<a href="?dlike='.$foto['id'].'"><img src="/img/like_ok.png"> <font color="#1f1f1f">'.$count_like.'</font></a>';
}

echo '<span style="margin: 0 15px;"><img src="/img/kom.png"> <font color="#1f1f1f">'.$count_kom.'</font></span>';

# Закладки
echo '<span style="float:right;">';
if (mysql_result(query("SELECT COUNT(*) FROM `bookmarks` WHERE `id_user` = '".$user['id']."' AND `id_object` = '" . $foto['id'] . "' AND `type`='foto' LIMIT 1"),0) == 0){
echo '<a href="?fav=1"><img src="/img/fav.png"></a>';
}else{
echo '<a href="?fav=2"><img src="/img/fav_ok.png"></a>';
}
echo '</span>';
# Закладки end

echo '</div>';

$k_post = mysql_result(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'];

$q = query("SELECT * FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' ORDER BY `id` $sort LIMIT $start, $set[p_str]");
    
//Вывод комментариев
while ($post = mysql_fetch_assoc($q)){ 
$ank2 = mysql_fetch_assoc(query("SELECT * FROM `user` WHERE `id` = '$post[id_user]' LIMIT 1"));

echo '<table class="block2">';
echo '<td class="block_avatar">';
//Ответ
if (isset($user) && $user['id'] != $ank2['id']){
echo '<a href="?response=' . $ank2['id'] . '&amp;page=' . $page . '">';
}
echo ''.avatar($ank2['id']).'</a> <span class="avatar_icon">'.online($ank2['id']).'</span>';
echo '</td>';
echo '<td class="block_info">';
//Вывод дополнительного меню
if (user_access('foto_komm_del') || $ank['id'] == $user['id']){
echo '<a class="toggle-menu-act" onclick="UI.toggleMenu(this);"><span></span></a>';
echo '<div class="toggle-menu">';
echo '<div class="toggle-menu-arrow"><span></span></div>';
echo '<ul class="toggle-menu-block">';
echo '<a href="?delete=' . $post['id'] . '&amp;page=' . $page . '">Удалить</a>';
echo '</ul>';
echo '</div>';
}
echo '<span class="nick">'.user::nick($ank2['id']).'</span>';
echo '<span class="time">' . vremja($post['time']) . '</span>';
echo '<div class="block_msg">';
echo ''. output_text($post['msg']) . '';
echo '</div>';
echo '</td>';
echo '</table>';

}

//Страницы
if ($k_page > 1)str('?',$k_page,$page);

# Отправка комментариев
if (isset($user)){
echo '<table class="block4">';
echo '<td style="width:55px;">';
echo ''.avatar($user['id']).'';
echo '</td>';
echo '<td>';
echo '<form method="post" name="message" action="?page=' . $pageEnd . '&amp;' . REPLY . '">';
echo '<textarea class="input3" placeholder="Добавьте комментарий..." name="msg">' . $insert . '</textarea>';
echo '<td style="width:10px;">';
echo '<button class="send" type="submit"><img src="/img/send.png"></button>';
echo '</td>';
echo '</td>';
echo '</table>';
echo '</form>';
echo '</table>';

//Вывод смайлов
include_once H.'modules/smiles/smile.php';

}

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

?>