<?php

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

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'loads', 'user') as $inc) {
    require_once "../sys/inc/$inc.php";
}

$set['title'] = lang('Загрузки') . ' - ' . lang('Поиск файлов'); // заголовок страницы
include_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='/loads/'>" . lang('Загрузки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Поиск');
echo "</small></div><br />";

$search = NULL;
if (isset($_SESSION['search'])) {
    $search = $_SESSION['search'];
}
if (isset($_POST['search'])) {
    $search = $_POST['search'];
}
$_SESSION['search'] = $search;

$search = preg_replace("#( ){2,}#", " ", $search);
$search = preg_replace("#^( ){1,}|( ){1,}$#", "", $search);

if (isset($_GET['go']) && $search != NULL) {
    $search_a = explode(' ', $search);

    for ($i = 0; $i < count($search_a); $i++) {
        $search_a2[$i] = '<span class="search_c">' . stripcslashes(htmlspecialchars($search_a[$i])) . '</span>';
        $search_a[$i] = stripcslashes(htmlspecialchars($search_a[$i]));
    }

    $q_search = str_replace('%', '', $search);
    $q_search = str_replace(' ', '%', $q_search);
    $q = $db->super_query("SELECT COUNT(*) as count FROM loads_list WHERE `name` like '%" . $db->safesql($q_search) . "%'");
    $k_post = $q['count'];
    $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();
    }

    $q = $db->query("SELECT * FROM `loads_list` WHERE `name` like '%" . $db->safesql($q_search) . "%' ORDER BY `loads` DESC LIMIT $start, $set[p_str]");
    $i = 0;

    while ($post = $db->get_row($q)) {
        $l = $post['path'];
        if (function_exists('iconv')) {
            $l = iconv('utf-8', 'windows-1251', $l);
        }
        $dir_loads = "../sys/loads/files/$l";


        $dirlist[$i] = $post['name'];
        if (function_exists('iconv')) {
            $dirlist[$i] = iconv('utf-8', 'windows-1251', $dirlist[$i]);
        }

        if (!is_file($dir_loads . '/' . $dirlist[$i])) {
            continue;
        }


        $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;
        }
        $size = filesize($dir_loads . '/' . $dirlist[$i]);

        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        include H . 'loads/inc/icon48.php';
        echo "</td><td class='null'>";
        echo "<a href='index.php?" . url("d=$l&amp;scr=1&amp;f=$dirlist[$i]&amp;page=$page") . "'>$name2.$ras</a>" . file_new($dir_loads . '/' . $dirlist[$i]) . "\n";

        echo "<br />\n";
        echo "Путь: " . rupath($l, '../sys/loads/files') . "<br />\n";
        include 'inc/opis.php';
        echo "</td></tr></table>\n";
        echo "<div class='hr'></div>";

        $i++;
    }

    if ($k_page > 1) {
        str("search.php?go&amp;", $k_page, $page);
    }
}

echo "<form method=\"post\" action=\"search.php?go\" class=\"list-group-item-null list-group-item-grey\">\n";
$search = stripcslashes(htmlspecialchars($search));
echo "Что будем искать?";
echo "<input class='form-control' required type='text' name='search' maxlength='64' value='$search' /><br />";
echo "<button class='btn btn-success'><span class='fa fa-search fa-fw'></span> Искать</button>";
echo "</form>\n";

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