<?php
require_once __DIR__ . '/../lib/twig.php';
$utils = new App\Utils;
$result = [];
$q = "";
$a = false;
$all = false;
if (isset($query) & & strlen($query) > 67 || isset($_POST["q"]) & & strlen($_POST["q"]) > 67) {
$result["error"] = "Request is too long, max length is 67 chars";
} else if (isset($query) & & !empty($query) & & strlen($query) < 3 | | isset ( $ _POST [ " q " ] ) & & strlen ( $ _POST [ " q " ] ) < 3 ) {
$result["error"] = "Request is too short, min length is 3 chars";
} else if (isset($query) & & !empty($query)) {
$q = htmlspecialchars($query);
} else if (isset($_POST["q"])) {
$q = htmlspecialchars($_POST["q"]);
}
if (isset($_POST["a"]) || isset($_GET["a"]))
$a = true;
if (isset($_POST["all"]) || isset($_GET["all"]))
$all = true;
if(!empty($q)) {
$pdo = (new App\DB($options))->pdo;
if($a) {
$STH = $pdo->prepare('SELECT `host`, `base64`, `base32`, `last_seen` FROM `hosts` WHERE (`host` LIKE ? OR `base32` LIKE ?) AND `disabled` = 0 LIMIT ' . $options["tableitems"]);
} else {
$STH = $pdo->prepare('SELECT `host`, `base64`, `base32`, `last_seen` FROM `hosts` WHERE (`host` LIKE ? OR `base32` LIKE ?) LIMIT ' . $options["tableitems"]);
}
$STH->execute(['%'.$q.'%', '%'.$q.'%']);
$row = $STH->fetchAll(PDO::FETCH_ASSOC);
if (empty($row))
$result["error"] = "Nothing was found";
else
$result = array_merge($result, $row);
}
$template = $twig->load('search.twig');
echo $template->render(['query' => $q ?: '', 'result' => $result, 'limit' => $options["tableitems"], 'all' => $all]);