<?php

if (dir_name($dir_loads) != NULL) {
    $set['title'] = lang('Загрузки') . ' - ' . dir_name($dir_loads);
} else {
    $set['title'] = lang('Загрузки');
}
include_once H . 'sys/inc/thead.php';

include 'inc/admin_act.php';
err();
aut();

$_SESSION['sort'] = 0;
if (isset($_GET['sort'])) {
    $_SESSION['sort'] = $_GET['sort'];
}
if ($_SESSION['sort'] == "") {
    $_SESSION['sort'] = 0;
}
if ($_SESSION['sort'] == 1) {
    $order = "loads";
} elseif ($_SESSION['sort'] == 2) {
    $order = "rating";
} elseif ($_SESSION['sort'] == 4) {
    $order = "size";
} elseif ($_SESSION['sort'] == 3) {
    $order = "name";
} else {
    $order = "time";
}
$dirlist_t = NULL;
$opendir = opendir($dir_loads);

while ($readdir = readdir($opendir)) {
    $dirlist = scandir($dir_loads);
}

$offsetarray = 0;

foreach ($dirlist as $readdir) {
    if (!preg_match("#^\.|\.name$|\.99.99.name|\.99.99.jad$|\.opis$|\.php$|\.sql$|\.dirname$#i", $readdir) && !preg_match("#\.1.JPG$|\.1.PNG$|\.1.GIF$|\.1.JPEG$|\.2.JPG$|\.2.PNG$|\.2.GIF$|\.2.JPEG$|\.3.JPG$|\.3.PNG$|\.3.GIF$|\.3.JPEG$#", $readdir)) {
        $offsetarray++;
        if (is_file($dir_loads . '/' . $readdir)) {
            $filesqlinfo = $db->get_row($db->query("SELECT * FROM `loads_list` WHERE(`name`='$readdir' && `path`='/" . $l . "/' && `size`='" . filesize($dir_loads . '/' . $readdir) . "') LIMIT 1"));
        }
        $dirlist_t[] = is_dir($dir_loads . '/' . $readdir) ? array('dir' => 1, 'name' => '000' . $readdir . '', 'size' => 1000 * 1024 * 1024 * 1024 * 1024, 'loads' => '999999', 'rating' => '999999', 'time' => '9999999999') : array('dir' => 0, 'name' => $readdir, 'size' => $filesqlinfo['size'], 'loads' => $filesqlinfo['loads'], 'rating' => $filesqlinfo['rating'], 'time' => $filesqlinfo['time']);
    }
}

function cmp_name($a2, $b2) {
    return ($a2['name'] >= $b2['name']) ? 1 : 0;
}

function cmp_dir($a2, $b2) {
    return ($a2['dir'] < $b2['dir']) ? 1 : 0;
}

function cmp_size($a2, $b2) {
    return ($a2['size'] <= $b2['size']) ? 1 : 0;
}

function cmp_loads($a2, $b2) {
    return ($a2['loads'] <= $b2['loads']) ? 1 : 0;
}

function cmp_rating($a2, $b2) {
    return ($a2['rating'] <= $b2['rating']) ? 1 : 0;
}

function cmp_time($a2, $b2) {
    return ($a2['time'] <= $b2['time']) ? 1 : 0;
}

if (count($dirlist_t) != 0) {
    usort($dirlist_t, "cmp_" . $order . "");
}

$k_post = count($dirlist_t);
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];
$end = $set['p_str'] * $page;

if ($l != NULL) {
    #Навигация
    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='?'>" . lang('Загрузки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo rupath($l, '../sys/loads/files');
    echo "</small></div><br />";
} else {
    #Навигация
    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 lang('Загрузки');
    echo "</small></div><br />";
}

echo "<div class='list-group-item-null list-group-item-grey'><ul class='nav nav-pills'>";
if ($_SESSION['sort'] == 0) {
    echo "<li class='nav-item'><a class='nav-link active'>" . lang('Дата') . "</a></li>";
} else {
    echo "<li class='nav-item'><a class='nav-link' href='/loads/?d=$l&amp;page=" . @$_GET['page'] . "&amp;sort=0'>" . lang('Дата') . "</a></li>";
}
if ($_SESSION['sort'] == 3) {
    echo "<li class='nav-item'><a class='nav-link active'>" . lang('Имя') . "</a></li>";
} else {
    echo "<li class='nav-item'><a class='nav-link' href='/loads/?d=$l&amp;page=" . @$_GET['page'] . "&amp;sort=3'>" . lang('Имя') . "</a></li>";
}
if ($_SESSION['sort'] == 2) {
    echo "<li class='nav-item'><a class='nav-link active'>" . lang('Рейтинг') . "</a></li>";
} else {
    echo "<li class='nav-item'><a class='nav-link' href='/loads/?d=$l&amp;page=" . @$_GET['page'] . "&amp;sort=2'>" . lang('Рейтинг') . "</a></li>";
}
echo "</ul></div>";

if ($k_post == 0) {
    $doc->NoResult();
}
for ($i = $start; $i < $end && $i < $k_post; $i++) {
    $dirlist[$i] = $dirlist_t[$i]['name'];

    #Список папок
    if ($dirlist_t[$i]['dir'] == 1) {
        $dirlist[$i] = substr($dirlist[$i], 3, strlen($dirlist[$i]));
        echo "<div class='list-group-item-null'>\n";
        echo "<a href='?d=" . urlencode("$l/$dirlist[$i]") . "'><i class='fa fa-folder fa-fw'></i> <b>" . dir_name($dir_loads . '/' . $dirlist[$i]) . "</b></a> <span class='count'>" . k_files($l . '/' . $dirlist[$i]) . "</span>\n";
        if (access('loads_dir_rename') || access('loads_dir_mesto') || access('loads_dir_delete')) {
            if (access('loads_dir_rename')) {
                echo "<a href=\"?" . url("d=$l/$dirlist[$i]&amp;act=rename&amp;page=$page") . "\" class='hint--top' data-hint='" . lang('Редактировать папку') . "'><i class='fa fa-edit fa-fw'></i></a> \n";
            }
            if (access('loads_dir_delete')) {
                echo "<a href=\"?" . url("d=$l/$dirlist[$i]&amp;act=delete&amp;page=$page") . "\" class='hint--top' data-hint='" . lang('Удалить папку') . "'><i class='fa fa-trash-o'></i></a>\n";
            }
        }
        echo "</div>\n";
        echo "<div class='hr'></div>";
    } else {
        #Список файлов
        $ras = strtolower(preg_replace('#^.*\.#i', NULL, $dirlist[$i]));
        $name = preg_replace('#\.[^\.]*$#i', NULL, $dirlist[$i]);
        if (is_file($dir_loads . '/' . $dirlist[$i] . '.name')) {
            $name2 = trim(esc(file_get_contents($dir_loads . '/' . $dirlist[$i] . '.name')));
        } elseif (function_exists('iconv')) {
            $name2 = iconv('windows-1251', 'utf-8', $name);
        } else {
            $name2 = $name;
        }
        $name2 = htmlspecialchars($name2);
        $size = filesize($dir_loads . '/' . $dirlist[$i]);

        echo "<table class='list-group-item-null'><tr><td class='icon14'>\n";
        if (is_file($dir_loads . '/' . $dirlist[$i] . '.1.GIF')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].1.GIF") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.1.JPG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].1.JPG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.1.PNG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].1.PNG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.1.JPEG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].1.JPEG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } else
        if (is_file($dir_loads . '/' . $dirlist[$i] . '.2.GIF')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].2.GIF") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.2.JPG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].2.JPG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.2.PNG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].2.PNG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.2.JPEG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].2.JPEG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } else
        if (is_file($dir_loads . '/' . $dirlist[$i] . '.3.GIF')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].3.GIF") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.3.JPG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].3.JPG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.3.PNG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].3.PNG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '.3.JPEG')) {
            echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i].3.JPEG") . "&amp;w=59&amp;h=59\" style='width: 60px; heigth: 60px;' alt=\"Скрин...\" /><br />\n";
        } else {
            if (is_file(H . "loads/inc/icon48/$ras.php")) {
                include H . "loads/inc/icon48/$ras.php";
            }
        }

        echo "</td><td class='null'>\n";

        if ($set['echo_rassh'] == 1) {
            $ras2 = ".$ras";
        } else {
            $ras2 = NULL;
        }

        if (access('loads_file_edit') || access('loads_file_delete')) {
            echo "<span style='float: right;'>";
            if (access('loads_file_edit')) {
                echo "<a href=\"?" . url("d=$l&amp;act=prop&amp;f=" . urlencode("$dirlist[$i]") . "&amp;page=$page") . "\" class='hint--top' data-hint='" . lang('Редактировать') . "'><i class='fa fa-edit fa-fw'></i></a> \n";
            }
            if (access('loads_file_delete')) {
                echo "<a href=\"?" . url("d=$l&amp;act=delete&amp;f=" . urlencode("$dirlist[$i]") . "&amp;page=$page") . "\" class='hint--top' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o'></i></a>\n";
            }
            echo "</span>";
        }

        include 'inc/icon14.php';
        echo "<a href=\"?" . url("d=$l&amp;scr=1&amp;f=" . urlencode("$dirlist[$i]") . "&amp;page=$page") . "\">$name2$ras2</a> " . file_new($dir_loads . '/' . $dirlist[$i]) . "<br />\n";

        if (!isset($set['prosm_select']) || $set['prosm_select'] == '0') {
            if (is_file($dir_loads . '/' . $name . '.mp3')) {
                if (IS_WEB) {
                    echo "<span style='float: right;'>";
                    echo '<object id="audioplayer56" type="application/x-shockwave-flash" data="/sys/uppod-audio.swf" width="20" height="20"><param name="bgcolor" value="#000000" /><param name="allowScriptAccess" value="always" /><param name="movie" value="/sys/uppod-audio.swf" /><param name="flashvars" value="comment=dfgh&amp;st=/loads/st/audio145-1103.txt&amp;file=/loads/down/' . $l . '/' . $dirlist[$i] . '" /></object>';
                    echo "</span>";
                }
            }
        }

        if (!isset($set['skrin_select']) || $set['skrin_select'] == '0') {
            if (!isset($set['opis_select']) || $set['opis_select'] == '0') {
                if (is_file($dir_loads . '/' . $dirlist[$i] . '.opis')) {
                    $tmp = toOutput(file_get_contents($dir_loads . '/' . $dirlist[$i] . '.opis'));
                    $tmp = text($tmp);
                    echo "<small style='color: grey;'>$tmp...</small><br />\n";
                }
            }
            if (!isset($set['opis_select']) || $set['opis_select'] == '1') {
                $q = $db->super_query("SELECT `opis` as opis FROM `loads_list` WHERE `name` = '$dirlist[$i]' AND `path` = '" . $db->safesql($path) . "'");
                $opis = $q['opis'];
                if ($opis != NULL) {
                    echo text($opis) . "<br />\n";
                }
                unset($q);
            }
            if ($set['echo_rassh'] == 1) {
                $ras2 = ".$ras";
            } else {
                $ras2 = NULL;
            }
            if (!isset($set['podrob_select']) || $set['podrob_select'] == '0') {
                echo "<table><tr><td rowspan='2'><small style='color: grey;'>\n";
                $q = $db->super_query("SELECT `prosm` as prosm FROM `loads_list` WHERE `name` = '$dirlist[$i]' AND `path` = '" . $db->safesql($path) . "'");
                $prosm = $q['prosm'];
                
                echo "<i class='fa fa-eye fa-fw'></i> " . $prosm . "<br />\n";
                echo "</small></td></tr></table>\n";
                unset($q);
            }
        }

        if (!isset($set['skrin_select']) || $set['skrin_select'] == '1') {
            if (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.1.GIF')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.1.GIF") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.1.JPG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.1.JPG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.1.PNG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.1.PNG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } else
            if (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.2.GIF')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.2.GIF") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.2.JPG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.2.JPG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.2.PNG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.2.PNG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } else
            if (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.3.GIF')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.3.GIF") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.3.JPG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.3.JPG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } elseif (is_file($dir_loads . '/' . $dirlist[$i] . '' . $file . '.3.PNG')) {
                echo "<img src=\"resize.php?" . url("f=$l/$dirlist[$i]$file.3.PNG") . "&amp;w=59&amp;h=59\" alt=\"Скрин...\" /><br />\n";
            } else
            if (is_file(H . "loads/inc/icon48/$ras.php")) {
                include H . "loads/inc/icon48/$ras.php";
            }
        }

        if (!isset($set['skrin_select']) || $set['skrin_select'] == '2') {
            if (is_file($dir_loads . '/' . $dirlist[$i] . '.opis')) {
                if (!isset($set['opis_select']) || $set['opis_select'] == '0') {
                    $tmp = toOutput(file_get_contents($dir_loads . '/' . $dirlist[$i] . '.opis'));
                    $tmp = text($tmp);
                    echo "<small style='color: grey;'>$tmp...</small><br />\n";
                }
            }
            if (!isset($set['opis_select']) || $set['opis_select'] == '1') {
                $q = $db->super_query("SELECT `opis` as opis FROM `loads_list` WHERE `name` = '$dirlist[$i]' AND `path` = '" . $db->safesql($path) . "'");

                $opis = $q['opis'];
                if ($opis != NULL) {
                    echo text($opis) . "<br />\n";
                }
            }
            echo "\n";
            if (!isset($set['podrob_select']) || $set['podrob_select'] == '0') {
                echo "\n";
                echo "<a href='?d=" . urlencode("$l") . "&amp;scr=1&amp;f=" . urlencode("$dirlist[$i]") . "&amp;page=$page'><b>Подробно</b></a><br />\n";
            }
        }

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

if ($k_page > 1) {
    str("?d=" . urlencode("$l") . "&amp;", $k_page, $page);
}

include 'inc/admin_form.php';

if ($l == NULL) {
    echo "<a href='/loads/top.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-bar-chart fa-fw'></i> Топ 20 файлов</a>\n";
    echo "<a href='/loads/search.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-search fa-fw'></i> Поиск файлов</a>\n";
    echo "<a href='/loads/stats.php' class='list-group-item-null list-group-item-grey'><i class='fa fa-line-chart fa-fw'></i> Статистика</a>\n";
}
include_once '../sys/inc/tfoot.php';
?>