From 75872be417067e4ef1f8d128b96fc338540cda50 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Mon, 7 Dec 2015 09:05:04 +0000 Subject: switch the ifs for version and help so we use the same output method and add a couple of config checks. --- search.php | 142 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 73 insertions(+), 69 deletions(-) (limited to 'search.php') diff --git a/search.php b/search.php index 2c9ddb7..c3e2d6d 100644 --- a/search.php +++ b/search.php @@ -10,7 +10,12 @@ try { if (!$config = parse_ini_file("config.ini")) { throw new Exception('Could not parse ini file'); } - + if (!isset($config["url"])) { + throw new Exception('`url` not found in ini file'); + } + if (!isset($config["command"])) { + throw new Exception('`command` not found in ini file'); + } // hash the password if (isset($config['password'])) { $hash = hash("sha256", $config['password']); @@ -31,86 +36,85 @@ try { $args = explode(" ", $search); $page = 1; - + $out = []; if ($args[0] == "-v" || $args[0] == "--version") { - echo "blaupload-search v" . $version . "\n"; - die(); - } else if ($args[0] == "-h" || $args[0] == "--help") { - echo "Usage: !" . $config["command"] . " [query] #[page] | "; - echo "e.g: !" . $config["command"] . " intense | "; - echo "!" . $config["command"] . " intense marcus | "; - echo "!" . $config["command"] . " intense marcus #2 (returns the second page of results)\n"; - die(); - } + $out[] = "blaupload-search v" . $version; + } elseif ($args[0] == "-h" || $args[0] == "--help") { + $help = "Usage: !" . $config["command"] . " [query] #[page] | "; + $help .= "e.g: !" . $config["command"] . " intense | "; + $help .= "!" . $config["command"] . " intense marcus | "; + $help .= "!" . $config["command"] . " intense marcus #2 (returns the second page of results)"; + $out[] = $help; + } else { - // Check to see if user specified pagination - $pages = array_filter($args, function($elem) { - if (strpos($elem, "#") === 0) { - return true; - } - return false; - }); - // reset the indexes after the array filter - $pages = array_merge($pages); + // Check to see if user specified pagination + $pages = array_filter($args, function($elem) { + if (strpos($elem, "#") === 0) { + return true; + } + return false; + }); + // reset the indexes after the array filter + $pages = array_merge($pages); - if (count($pages) > 1) { - throw new Exception("Too many page choices. Enter only one `#`"); - } + if (count($pages) > 1) { + throw new Exception("Too many page choices. Enter only one `#`"); + } - if (!empty($pages)) { - // Set the page - $page = substr($pages[0], 1); - } - if (!is_int((int)$page) || $page <= 0) { - throw new Exception("Page `" . $page . "` is not valid"); - } + if (!empty($pages)) { + // Set the page + $page = substr($pages[0], 1); + } + if (!is_int((int)$page) || $page <= 0) { + throw new Exception("Page `" . $page . "` is not valid"); + } - // remove the pagination from the search args - $pos = array_search("#" . $page, $args); - if ($pos) { - $args[$pos] = null; - } + // remove the pagination from the search args + $pos = array_search("#" . $page, $args); + if ($pos) { + $args[$pos] = null; + } - // Really slow search - $results = []; - foreach ($output as $upload) { - $match = false; - foreach ($args as $arg) { - if (!is_null($arg) && stripos($upload->filename, $arg) === false) { - $match = false; - break; + // Really slow search + $results = []; + foreach ($output as $upload) { + $match = false; + foreach ($args as $arg) { + if (!is_null($arg) && stripos($upload->filename, $arg) === false) { + $match = false; + break; + } + $match = true; + } + if ($match) { + $results[] = $config["url"] . "/" . rawurlencode($upload->filename); } - $match = true; - } - if ($match) { - $results[] = $config["url"] . "/" . rawurlencode($upload->filename); } - } - // Return results - $out = []; - if (empty($results)) { - echo "No results found\n"; - } else { - $count = count($results); - $i = 0; - - $start = ($page-1)*3; - $end = $start+2; - if ($start > ($count-1)) { - throw new Exception("No results for this page"); - } - foreach($results as $result) { - if ($i < $start || $i > $end) { + // Return results + if (empty($results)) { + echo "No results found\n"; + } else { + $count = count($results); + $i = 0; + + $start = ($page-1)*3; + $end = $start+2; + if ($start > ($count-1)) { + throw new Exception("No results for this page"); + } + foreach($results as $result) { + if ($i < $start || $i > $end) { + $i++; + continue; + } + $out[] = $i+1 . ": " . $result; $i++; - continue; } - $out[] = $i+1 . ": " . $result; - $i++; - } - if ($count > 3) { - $out[] = "results (" . $count . ")"; + if ($count > 3) { + $out[] = "results (" . $count . ")"; + } } } -- cgit v1.2.3