<?php

/* DCMS Special
 * Дата последнего редактирования 25.09.2016
 * Модифицировал densnet
 * Автор yadalay
 */

$forum = mysql_fetch_object(mysql_query('SELECT `id`, `access`, `name` FROM `forum` WHERE `id` = ' . intval($_GET['forum'])));
$razdel = mysql_fetch_object(mysql_query('SELECT `id`, `id_forum`, `name` FROM `forum_razdels` WHERE `id_forum` = ' . $forum->id . ' AND `id` = ' . intval($_GET['razdel'])));
$theme = mysql_fetch_object(mysql_query('SELECT `id`, `id_razdel`, `name` FROM `forum_themes` WHERE `id_razdel` = ' . $razdel->id . ' AND `id` = ' . intval($_GET['theme'])));

if (!$theme || !$razdel || !$forum || ($forum->access == 1 && $user['group_access'] < 8) || ($forum->access == 2 && $user['group_access'] < 3)) {
    header('Location: ' . DIR_FORUM);
    exit;
} else {
    $set['title'] = lang('Файлы темы') . ' - ' . toOutput($theme->name, 1, 1, 0, 0, 0);

    require_once H . 'sys/inc/thead.php';

    aut();

    #Навигация
    echo "<div class='list-group-item-null list-group-item-grey'><small>";
    echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "'>" . lang('Форум') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "$forum->id/'>" . toOutput(utf8_substr($forum->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/'>" . toOutput(utf8_substr($razdel->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'>" . toOutput(utf8_substr($theme->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Файлы темы');
    echo "</small></div><br />";

    $k_post = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_post_files` WHERE `id_theme` = ' . $theme->id), 0);
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

    if ($k_post == 0) {
        $doc->NoResult();
    }

    $files = mysql_query('SELECT * FROM `forum_post_files` WHERE `id_theme` = ' . $theme->id . ' ORDER BY `id` ASC LIMIT ' . $start . ', ' . $set['p_str']);
    while ($file = mysql_fetch_object($files)) {
        $ras = strtolower(preg_replace('#^.*\.#', NULL, $file->name));

        if ($ras == 'jpg' || $ras == 'jpeg' || $ras == 'gif' || $ras == 'png' || $ras == 'bmp' || $ras == 'ico') {
            $icons = "<i class='fa fa-file-image-o fa-fw'></i>";
        } elseif ($ras == '3gp' || $ras == 'mp4' || $ras == 'avi' || $ras == 'mpeg' || $ras == 'flv' || $ras == 'wmv' || $ras == 'mkv') {
            $icons = "<i class='fa fa-file-video-o fa-fw'></i>";
        } elseif ($ras == 'docx' || $ras == 'doc' || $ras == 'docm' || $ras == 'dotx' || $ras == 'dot' || $ras == 'dotm') {
            $icons = "<i class='fa fa-file-text-o fa-fw'></i>";
        } elseif ($ras == 'mp1' || $ras == 'mp2' || $ras == 'mp3' || $ras == 'wav' || $ras == 'aif' || $ras == 'ape' || $ras == 'flac' || $ras == 'ogg' || $ras == 'asf' || $ras == 'wma') {
            $icons = "<i class='fa fa-file-audio-o fa-fw'></i>";
        } elseif ($ras == 'zip' || $ras == 'rar' || $ras == 'tar' || $ras == '7-zip' || $ras == 'gzip' || $ras == 'jar' || $ras == 'jad' || $ras == 'war' || $ras == 'xar') {
            $icons = "<i class='fa fa-file-archive-o fa-fw'></i>";
        } elseif ($ras == 'txt' || $ras == 'xml') {
            $icons = "<i class='fa fa-file-text-o fa-fw'></i>";
        } elseif ($ras == 'pdf') {
            $icons = "<i class='fa fa-file-text-o fa-fw'></i>";
        } elseif ($ras == 'psd') {
            $icons = "<i class='fa fa-file-image-o fa-fw'></i>";
        } else {
            $icons = "<i class='fa fa-file-o fa-fw'></i>";
        }

        $icon = DIR_FORUM_FILES . $file->name;

        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        echo "<img src='$icon' style='width: 40px;height: 40px;padding:3px;'/>";
        echo "</td><td class='null'>";

        echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id/download_$file->id'>$icons " . toOutput($file->real_name, 1, 1, 0, 0, 0) . "</a> (" . size_file($file->size) . " ";
        echo "<span style='color: #88C057;'><i class='fa fa-download fa-fw'></i> $file->count_downloads чел.</span>)";

        echo "</td></tr></table>";
        echo "<div class='hr'></div>";
    }

    if ($k_page > 1) {
        str(DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '/files/', $k_page, $page);
    }
}
