($page - 1) * $pagesize, "end" => (($page - 1) * $pagesize) + ($pagesize - 1) ]; } function getFormattedArguments($config) { $args = explode(" ", read_stdin()); if (!isset($args) || empty($args)) { throw new Exception("No arguments found"); } // Loop the raw arguments and create our formated argument array $formatArgs = []; $formatArgs["search"] = []; foreach ($args as $arg) { // Switch to check for special arguments // output should exit but incase it errors we double break switch ($arg) { case "--version": case "-v": // Flow through output(getVersion($config)); break 2; case "--help": case "-h": // Flow through output(getHelp($config)); break 2; } // Check to see if we have a page specified if (strpos($arg, "#") === 0) { $formatArgs["page"] = substr($arg, 1); continue; } // If we reach here, then the argument is a search term $formatArgs["search"][] = $arg; } // If we reach this point we must have at least one search term if (empty($formatArgs["search"])) { throw new Exception("Please Enter a search term"); } if (!isset($formatArgs["page"])) { $formatArgs["page"] = 1; } elseif (!is_int((int)$formatArgs["page"]) || $formatArgs["page"] < 1) { throw new Exception("Page number `" . $formatArgs["page"] . "` is not valid"); } return $formatArgs; } try { $config = getConfig(); $content = getJsonFromUrl($config); $arguments = getFormattedArguments($config); // parse the arguments $page = $arguments["page"]; $out = []; // Really slow search $results = []; foreach ($content as $upload) { $match = false; foreach ($arguments["search"] as $arg) { if (!is_null($arg) && stripos($upload->filename, $arg) === false) { $match = false; break; } $match = true; } if ($match) { $results[] = $config["url"] . "/" . rawurlencode($upload->filename); } } if (empty($results)) { output("No results found"); } $count = count($results); $i = 0; $bounds = getPaginationBounds($page, $config["pagesize"]); if ($bounds["start"] > ($count - 1)) { throw new Exception("No results for this page"); } foreach ($results as $result) { if ($i < $bounds["start"] || $i > $bounds["end"]) { $i++; continue; } $out[] = $i + 1 . ": " . $result; $i++; } if ($count > 3) { $out[] = "results (" . $count . ")"; } output($out, " - "); } catch (Exception $e) { echo $e->getMessage() . "\n"; die(); }