<?php

include_once '../sys/inc/start.php';
if (!isset($_GET['f']) || !isset($_GET['down'])) {
    include_once H . 'sys/inc/compress.php';
}
include_once H . 'sys/inc/sess.php';
include_once H . 'sys/inc/settings.php';
include_once H . 'sys/inc/db_connect.php';
include_once H . 'sys/inc/ipua.php';
include_once H . 'sys/inc/fnc.php';
include_once H . 'sys/inc/loads.php';
include_once H . 'sys/inc/user.php';

$set['title'] = lang('Загрузки');

$_SESSION['page'] = 1;

if (isset($_GET['d']) && esc($_GET['d']) != NULL) {
    $l = preg_replace("#\.{2,}#", NULL, esc(urldecode($_GET['d'])));
    $l = preg_replace("#\./|/\.#", NULL, $l);
    $l = preg_replace("#(/){1,}#", "/", $l);
    $l = preg_replace("#(^(/){1,})|((/){1,}$)#", "", $l);
    $dir_loads = "../sys/loads/files/$l";
    $dir_scr = "../sys/loads/scr/$l";
}
if (!isset($dir_loads) || !is_dir($dir_loads)) {
    $dir_loads = '../sys/loads/files';
    $l = NULL;
}

if (!isset($dir_scr) || !is_dir($dir_scr)) {
    $dir_scr = '../sys/loads/scr/';
}

if (isset($_GET['f']) && is_file($dir_loads . '/' . preg_replace("#.*/#", '', urldecode($_GET['f']))) && isset($_GET['komm'])) {
    $file = preg_replace("#.*/#", NULL, urldecode($_GET['f'])); // получение имени файла
    $name = preg_replace('#\.[^\.]*$#i', NULL, $file); // имя файла без расширения
    $ras = strtolower(preg_replace('#^.*\.#i', NULL, $file)); // расширение (в нижнем регистре)
    $size = filesize($dir_loads . '/' . $file);
    if (is_file($dir_loads . '/' . $file . '.name')) {
        $name2 = trim(esc(file_get_contents($dir_loads . '/' . $file . '.name')));
    } elseif (function_exists('iconv')) {
        $name2 = iconv('windows-1251', 'utf-8', $name);
    }
    if (is_file($dir_loads . '/' . $file . '.99.99.name')) {
        $name3 = trim(esc(file_get_contents($dir_loads . '/' . $file . '.99.99.name')));
    } elseif (function_exists('iconv')) {
        $name3 = iconv('windows-1251', 'utf-8');
    } else {
        $name2 = $name;
    }
    include 'inc/komm.php';
    exit;
} elseif (isset($_GET['f']) && is_file($dir_loads . '/' . preg_replace("#.*/#", '', urldecode($_GET['f']))) && isset($_GET['down'])) {
    include_once H . 'sys/inc/downloadfile.php';
    $file = preg_replace("#.*/#", NULL, urldecode($_GET['f'])); // получение имени файла

    $name = preg_replace('#\.[^\.]*$#i', NULL, $file); // имя файла без расширения
    $ras = strtolower(preg_replace('#^.*\.#i', NULL, $file)); // расширение (в нижнем регистре)

    $l2 = $l;
    if (function_exists('iconv')) {
        $l2 = iconv('windows-1251', 'utf-8', $l2);
    }

    $jfile = preg_replace('#\.jad$#', '.jar', $file);

    $size = filesize($dir_loads . '/' . $jfile);
    if (function_exists('iconv'))
        $jfile = iconv('windows-1251', 'utf-8', $jfile);

    $path = (function_exists('iconv')) ? iconv('windows-1251', 'utf-8', $l) : $l;
    $path = '/' . preg_replace('#^/+|/+$#', null, $path) . '/';
    $loads = $db->get_row($db->query("SELECT * FROM `loads_list` WHERE `name` = '$jfile' AND `size` = '$size' AND `path` = '" . $db->safesql($path) . "' LIMIT 1"));

    if (!isset($set['downloads_select']) || $set['downloads_select'] == '0') {
        $db->query("UPDATE `loads_list` SET `loads` = '" . ($loads['loads'] + 1) . "' WHERE `name` = '$jfile' AND `size` = '$size' AND `path` = '" . $db->safesql($path) . "' LIMIT 1");
        DownloadFile($dir_loads . '/' . $file, basename($dir_loads . '/' . $file), ras_to_mime($ras));
    } elseif (isset($user) && $set['downloads_select'] == '1') {
        $db->query("UPDATE `loads_list` SET `loads` = '" . ($loads['loads'] + 1) . "' WHERE `name` = '$jfile' AND `size` = '$size' AND `path` = '" . $db->safesql($path) . "' LIMIT 1");
        DownloadFile($dir_loads . '/' . $file, basename($dir_loads . '/' . $file), ras_to_mime($ras));
    } elseif ($user['money'] >= 100 && $set['downloads_select'] == '2') {
        $db->query("UPDATE `loads_list` SET `loads` = '" . ($loads['loads'] + 1) . "' WHERE `name` = '$jfile' AND `size` = '$size' AND `path` = '" . $db->safesql($path) . "' LIMIT 1");
        DownloadFile($dir_loads . '/' . $file, basename($dir_loads . '/' . $file), ras_to_mime($ras));
    }

    exit;
} elseif (isset($_GET['f']) && is_file($dir_loads . '/' . preg_replace("#.*/#", '', urldecode($_GET['f'])))) {
    $file = preg_replace("#.*/#", NULL, urldecode($_GET['f'])); // получение имени файла
    $name = preg_replace('#\.[^\.]*$#i', NULL, $file); // имя файла без расширения
    $ras = strtolower(preg_replace('#^.*\.#i', NULL, $file)); // расширение (в нижнем регистре)
    $size = filesize($dir_loads . '/' . $file);

    if (is_file($dir_loads . '/' . $file . '.name')) {
        $name2 = trim(esc(file_get_contents($dir_loads . '/' . $file . '.name')));
    } elseif (function_exists('iconv')) {
        $name2 = iconv('windows-1251', 'utf-8', $name);
    }
    if (is_file($dir_loads . '/' . $file . '.99.99.name')) {
        $name3 = trim(esc(file_get_contents($dir_loads . '/' . $file . '.99.99.name')));
    } elseif (function_exists('iconv')) {
        $name3 = iconv('windows-1251', 'utf-8');
    } else {
        $name2 = $name;
    }

    $set['title'] = "Загрузки - $name2 скачать";
    include_once H . 'sys/inc/thead.php';
    include 'inc/admin_act_file.php';
    include 'inc/ff_rating.php';

    err();
    aut();
    $tmp_sess = null;

    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 />";
    }

    if (is_file("inc/file/$ras.php")) {
        include "inc/file/$ras.php";
    } else {
        include_once 'inc/file.php';
    }
    if (isset($_GET['page']) && (is_numeric($_GET['page']) || $_GET['page'] == 'end')) {
        $_SESSION['page'] = $_GET['page'];
    } else {
        $_SESSION['page'] = 1;
    }

    include 'inc/admin_form_file.php';
    $doc->Link('list-group-item-null', "?" . url("d=$l&amp;page=$_SESSION[page]") . "", 'arrow-left', 'Вернуться');

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

include_once 'inc/dir.php';
?>