<?php

$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);
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `loads_list` WHERE `name` like '%" . mysql_escape_string($q_search) . "%'"), 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();
    }

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

    while ($post = mysql_fetch_assoc($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' style='margin: 2px;'><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 H . 'loads/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=\"/loads/search.php?go\" class=\"list-group-item-null\">\n";
$search = stripcslashes(htmlspecialchars($search));
echo "Что будем искать?";
echo "<input class='form-control' 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';
