<?php
/*~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!		!!! Разработка сайтов и отдельных модулей на PHP !!!
!	* - Автор скрипата   :  lugaro
!	* - E-mail           :  lugaro@bk.ru
!	* - Сайт             :  wapNEW.ru
!	* - ICQ              :  2000882
~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!*/
/*Просмотр фото*/
$tpl->assign_file('mod', 'tpl/inc/file.tpl');
$id = (int)$_GET['id'];
$tpl->assign('linc', 'mod=file&amp;id=' . $id);
if (!empty($_POST['pas'])) $_SESSION['AlbomPas' . $id] = $_POST['pas'];
$file = $db->assoc($db->query("SELECT *  FROM `foto_file` WHERE `id` = '$id'"));
if (empty($file['name'])) header('Location: ?');
// Проверяем является ли юзер другом автора
$friends = true;
if ($file['close'] == 1) {
    if (($file['id_user'] == $user_id || $moder)) {
        $friends = true;
    } elseif ($db->rows($db->query("SELECT * FROM `friends` WHERE `user`='" . $file['id_user'] . "' AND `to` = '" . $user_id . "' AND `preg`='1'"))) {
        $friends = true;
    } else {
        $friends = false;
    }
}

$file['time'] = date("d.m.Y / H:i", $file['time']);
$file['color'] = $file['ocenka'] < 0 ? '#FF5353' : '#00FF80';
if (empty($file['op'])) $file['op'] = 'нет';
if ($file['close'] == 2) {
    $file['vid'] = 'Только автору фото';
} elseif ($file['close'] == 1) {
    $file['vid'] = 'Только друзьям';
} else {
    $file['vid'] = 'Всем';
}
$file['size'] = formatsize(filesize('files/' . $file['id_dir'] . '/' . $file['id_albom'] . '/' . $id . '.' . $file['ext']));
$tpl->assign($file);

if ($file['pas'] != $_SESSION['AlbomPas' . $id] && ($file['id_user'] != $user_id && !$moder)) {
    $tpl->parse('index.file.pas');
} elseif (!$friends) {
    $tpl->parse('index.file.to_friends');
} elseif ($file['close'] == 2 && ($file['id_user'] != $user_id && !$moder)) {
    $tpl->parse('index.file.to_avtor');
} else {
    if (($file['id_user'] == $user_id) || $moder) $tpl->parsemore('index.file.info.admin,index.file.info.ren');
    if ($user_id) $tpl->parse('index.file.info.ocenka');
    $tpl->parse('index.file.info');
}
// Настройки предпросмотра
if (isset($_GET['prev'])) $_SESSION['AlbomPrev'] = $_GET['prev'];
// Голосоывание
if (isset($_GET['ocenka'])) {
    if ($_COOKIE['AlbomGolos' . $id] == 1) {
        $tpl->parse('index.file.ocenka_error');
    } else {
        $ocenka = $_GET['ocenka'] == 'good' ? '+1' : '-1';
        $db->query("UPDATE `foto_file` SET `ocenka` = (ocenka)$ocenka WHERE `id` = '$id'");
        setcookie('AlbomGolos' . $id, 1, time() + 3600 * 24 * 365);
        header('location: ?mod=file&id=' . $id . '&ok');
    }
}
// //////////////////////
// Коменты
$com = new comments_class_by_lugaro('fotoalbom', $id);
$all = $com->count_post_razdel();
if ($user_id) {
    // Запись коментов
    if (!empty($_POST['msg_comments'])) {
        $com->insert_db($login , $user_id, xss($_POST['msg_comments']));
        $db->query("UPDATE `foto_file` SET `count_com` = (count_com)+1 WHERE `id` = '$id'");

        header('Location: index.php?mod=file&id=' . $id);
    }
    // Форма
    $tpl->parse('index.file.comments.form');
} else {
    $tpl->parse('index.file.comments.vhod');
}
// Удаление
if ($moder) {
    if (isset($_GET['del'])) {
        $com->del($_GET['del']);
        $db->query("UPDATE `foto_file` SET `count_com` = (count_com)-1 WHERE `id` = '$id'");
        header('Location: index.php?mod=file&id=' . $id);
    }
}

if ($all) {
    $n = new navigator($all, $konf_albom['navi'], 'index.php?mod=file&amp;id=' . $id . '&amp;');
    $n->no_button = true;
    $q = $db->query("SELECT
			comments_class.*, comments_class.id AS c_id, comments_class.time,
			users.name, users.lastdate, users.sex, users.status, users.id
			FROM comments_class
			LEFT JOIN users ON users.id = comments_class.id_user  WHERE comments_class.razdel  = '" . $com->razdel . "'  AND `comments_class`.`key` = '" . $com->key . "'  ORDER BY `time` DESC LIMIT " . $n->start() . ", 5");

    while (false != ($res = $db->assoc($q))) {
        $i++;
        $res['msg'] = msg($res['msg']);
        $ass = array('page' => $n->page,
            'c_id' => $res['c_id'],
            'list' => (($i % 2) ? 1 : 2),
            'block' => show_user($res, 1, 0, '(' . date("d.m.y / H:i", $res['time'] + $set_user['sdvig'] * 3600) . ')', $res['msg'])
            );
        $tpl->assign($ass);
        if ($moder) $tpl->parse('index.file.comments.list_com.del');
        $tpl->parse('index.file.comments.list_com');
    }
    $tpl->assign('navi', $n->navi());
}
$tpl->parse('index.file.comments');
if (isset($_GET['ok'])) $tpl->parse('index.file.ocenka_ok');
$tpl->parse('index.file');

?>