<?php
/**
 * @package     JohnCMS
 * @link        http://johncms.com
 * @copyright   Copyright (C) 2008-2011 JohnCMS Community
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      http://johncms.com/about
 *
 * @var $lng
 */

define('_IN_JOHNCMS', 1);

$headmod = 'load';

require_once '../incfiles/core.php';
require_once 'functions.php';
$cat = isset($_GET['cat']) ? intval($_GET['cat']) : 0;
$sort = isset($_GET['sort']) ? $_GET['sort'] : "";
$tree = array();
$dirid = $cat;

// Access control
$error = '';

if (empty($set['mod_down']) AND $rights < 7) {
    $error = $lng_dl['downloads_closed'];
} elseif (!empty($set['mod_down']) AND $set['mod_down'] == 1 AND !$user_id) {
    $error = $lng['access_guest_forbidden'];
}

if (!empty($error)) {
    require_once('../incfiles/head.php');
    echo '<div class="menu"><p>' . $error . '</p></div>';
    require_once("../incfiles/end.php");
    exit;
}

$do = array('new', 'rat', 'bookmarks');
if (in_array($act, $do)) {
    include_once($act . '.php');
} else {
    while ($dirid != '0' && $dirid != "") {
        $req = mysql_query("SELECT * FROM `downpath` WHERE `id` = '" . $dirid .
            "' LIMIT 1");
        $res = mysql_fetch_array($req);
        $tree[] = '<a href="dir_' . $dirid . '.html" title="' . $res['name'] . '">' . $res['name'] . '</a>';
        $dirid = $res['refid'];
    }
    krsort($tree);
    $cdir = array_pop($tree);
    $valueeq = '';
    foreach ($tree as $valuee) {
        $valueeq = '' . $valueeq . $valuee . ' / ';
    }
    $textl = $lng_dl['downloads'] . ' ' . strip_tags($valueeq . $cdir) . '';
    require_once '../incfiles/head.php';


    if (empty($_GET['cat'])) {
        // Заголовок начальной страницы загрузок
        ?>
        <div class="phdr"><strong><?= $lng_dl['downloads'] ?></strong></div>
        <div class="topmenu">
            <a href="search.php"><?= $lng_dl['search_files'] ?></a>
            <?php if ($user_id): ?>
                | <a href="index.php?act=bookmarks"><?= $lng_dl['bookmarks'] ?></a>
            <?php endif; ?>
        </div>
        <div class="gmenu blockpad">
            <?= functions::image('new.png') ?><a href="index.php?act=new"><?= $lng_dl['last_100_files'] ?></a><br>
            <?= functions::image('rate.gif') ?><a href="top_users.php"><?= $lng_dl['user_rating'] ?></a><br>
            <?= functions::image('rate.gif') ?><a href="top.php"><?= $lng_dl['top_files'] ?></a>
        </div>

    <?php
    } else {
        echo '<div class="phdr"> ';
        foreach ($tree as $value) {
            echo $value;
            if ($value != $cdir)
                echo ' | ';
        }
        echo '<b>' . strip_tags($cdir) . '</b></div>';
    }


    $totalcat = mysql_result(mysql_query("SELECT COUNT(*) FROM `downpath` WHERE `refid` = '" . $cat . "'"), 0);

    if ($totalcat > 0) {
        $zap = mysql_query("SELECT * FROM `downpath` WHERE `refid` = '" . $cat . "' ORDER BY `position` ASC LIMIT " . $start . "," . $kmess);
        $cachetime = time() - $down_setting['cachetime'] * 3600; // Время кэширования

        if (is_file('cache/' . $cat . '.dat') && filemtime('cache/' . $cat . '.dat') > $cachetime) {
            $count_cache = file_get_contents('cache/' . $cat . '.dat');
            $count_cache = unserialize($count_cache);
            $opencache = 'true';
        } else {
            $count_cache = array();
        }
        $i = 0;
        while ($zap2 = mysql_fetch_array($zap)) {
            echo ($i % 2) ? '<div class="menu">' : '<div class="menu">';
            ++$i;
            if (!$count_cache[$zap2['id']]) {
                ////////// счётчики //////////
                $countf = mysql_result(mysql_query("SELECT COUNT(*) FROM `downfiles` WHERE `type` != 1 AND `status` = 1 && `way` LIKE '" . $zap2['way'] . "%' "), 0);
                $old = time() - (3 * 24 * 3600);
                $countnf = mysql_result(mysql_query("SELECT COUNT(*) FROM `downfiles` WHERE `type` != 1 AND `status` = 1 && `time` > '" . $old . "' && `way` LIKE '" . $zap2['way'] . "%' "), 0);

                //$countp = mysql_result(mysql_query("SELECT COUNT(*) FROM `downpath` WHERE `way` LIKE '" . $zap2['way'] ."%';"), 0);
                //$countp--; // Счётчик папок. Раскомментировать если нужен.

                if ($countnf) {
                    $countnf = '/<span class="red">+' . $countnf . '</span>';
                } else {
                    $countnf = '';
                }
                $count_cache[$zap2['id']] = $countf . $countnf; // Сюда вставлять $countp если нужен счётчик папок.
            }
            echo '<img src="img/dir.png" alt="."/> <a href="dir_' . $zap2['id'] . '.html">' . $zap2['name'] . '</a> (' . $count_cache[$zap2['id']] . ')<br/>';
            if ($zap2['desc']) {
                echo '<div class="menu">' . $zap2['desc'] . '</div>';
            }
            echo '</div>';
        }
        
        if ($totalcat > $kmess) {
            echo '<div class="menu">';
            echo '' . functions::display_pagination('index.php?cat=' . $cat . '&amp;', $start, $totalcat, $kmess) . '</div>';
        }

echo '<div class="phdr">' . $lng_dl['all_dirs'] . ': ' . $totalcat . '</div>';
        //// Создаём файл с кэшем если он устарел или его нет ////
        if (!$opencache && $arr = fopen('cache/' . $cat . '.dat', "w")) {
            fwrite($arr, serialize($count_cache));
            fclose($arr);
            //echo 'Кэш обновлен или создан!';
        }

    } else {
        $req = mysql_query("SELECT COUNT(*) FROM `downfiles` WHERE `type` != '1' AND `pathid` = '" . $cat . "' AND `status` = 1");
        $totalfile = mysql_result($req, 0);

        if ($totalfile > 0) {
            // Проверка установки сортировки
            if ($sort) {
                $_SESSION['downsort'] = $sort;
            } else {
                $sort = isset($_SESSION['downsort']) ? $_SESSION['downsort'] : '';
            }
            // Проверка упорядочивания
            if (isset($_GET['orderby'])) {
                $order = $_GET['orderby'] == 'desc' ? 'desc' : 'asc';
                $_SESSION['orderby'] = $order;
            } else {
                $order = (isset($_SESSION['orderby']) && $_SESSION['orderby'] == 'desc') ? 'desc' : 'asc';
            }
            // Ссылки
            

           
            if ($sort != 'time' && $sort != 'count' && $sort != 'rating')
                $sort = 'name';
            $zap = mysql_query("SELECT * FROM `downfiles` WHERE `type` != '1' AND `pathid` = '" . $cat . "' AND `status` = 1 ORDER BY `" . $sort . "` " . $order . " LIMIT " . $start . "," . $kmess);


            // Вывод списка файлов
            $i = 0;
            while ($zap2 = mysql_fetch_array($zap)) {
                echo ($i % 2) ? '<div class="menu">' : '<div class="menu">';
                ++$i;
                $tf = pathinfo($zap2['way'], PATHINFO_EXTENSION); // Тип файла
                if ($tf == 'mp3') {
                    $set_view = array('variant' => 1);
                } elseif ((($tf == 'thm' or $tf == 'nth') && $down_setting['tmini']) || (($tf == '3gp' or $tf == 'mp4' or $tf == 'avi') && $down_setting['vmini']) || ($tf == 'jpg' or $tf == 'png' or $tf == 'jpeg' or $tf == 'gif')) {
                    $set_view = array('link_download' => 1, 'div' => 1);
                } else {
                    $set_view = array('variant'  => 1,
                                      'size'     => 1, 'desc' => 1, 'count' => 1, 'div' => 1,
                                      'comments' => 1, 'add_date' => 1, 'rating' => 1);
                }
                echo f_preview($zap2, $set_view, $tf);
                echo '';
            }


        } else {
            ?>
            <div class="menu">
                <p>
                    <?= $lng['list_empty'] ?>
                </p>
            </div>
        <?php
        }

        if ($totalfile > $kmess) {
            echo '<div class="menu">';
            echo '' . functions::display_pagination('index.php?cat=' . $cat . '&amp;', $start, $totalfile, $kmess) . '';
            echo '</p>';
        }

echo '<div class="phdr">' . $lng_dl['all_files'] . ': ' . $totalfile . '</div>';
        $dost = mysql_query("SELECT * FROM `downpath` WHERE id = '" . $cat . "';");
        $dost = mysql_fetch_array($dost);
        if ($dost['dost'] && $user_id != 0) {
            echo '<div class="menu"><img src="img/upload.png" alt="."/><a href="add_file.php?cat=' . $cat . '">' . $lng_dl['load_your_file'] . '</a></div>';
        }
    }
        echo '</div>';
  /* ?>
    <?php if ($rights == 1 || $rights >= 6): ?>
        <p>
            <?php if ($totalcat > 0): ?>
                <div class="menu"><a href="admin.php?act=import&amp;cat=<?= $cat ?>"><?= $lng_dl['import_file'] ?></a><br>
                <a href="admin.php?act=upload&amp;cat=<?= $cat ?>"><?= $lng_dl['upload_file'] ?></a><br>
            <?php endif; ?>
            <a href="admin.php"><?= $lng_dl['admin_panel'] ?></a>
        </p></div>
    <?php endif; ?>
*/
}
require_once '../incfiles/end.php';

