diff --git a/templates/home.twig b/templates/home.twig
index 57d31eb..bae169a 100644
--- a/templates/home.twig
+++ b/templates/home.twig
@@ -9,7 +9,7 @@
Info:
- Service is Up. Hosts check is done every hour.
+ Hosts check is done every hour.
Supported commands:
/api/<command>/[query]
+
+ - command - required field, possible values:
+
+ - all returns all alive domains b32 and last seen timestamp
+ - status returns base64, base32 and last seen timestamp for requested domain
+
+
+ - query - domain for status query
+
+
+
Terms of use:
@@ -76,7 +91,7 @@
- Domains with offensive content will be hidden from the list (currently hidden {% if all %}{% endif %}{{ blackcnt }} domain(s){% if all %}{% endif %})
- they can be found using the search and jump links
- - adding
?all
to page address (like /alive?all
) will show them
+ - adding ?all to page address (like /alive?all) will show them
- such domains won't be exported to subscriptions
- if you disagree with some eepsite's content - we won't do anything, only specific categories might be hidden
diff --git a/views/api.php b/views/api.php
index da637c6..74e6a4c 100644
--- a/views/api.php
+++ b/views/api.php
@@ -3,6 +3,7 @@
require_once __DIR__ . '/../config.php';
$all = false;
+$data = [];
$pdo = (new App\DB($options))->pdo;
@@ -11,20 +12,39 @@ header('Content-Type: application/json');
if (isset($_GET["all"]))
$all = true;
-if(!$command || !in_array($command, ['all', 'status']))
- exit(json_encode(["status" => "error", "message" => "Incorrect request"]));
-
-if($command == "all") {
- $STH = $pdo->query ("SELECT `base32`, UNIX_TIMESTAMP(`last_seen`) as `last_seen` FROM `hosts` " .
- "WHERE `approved` = 1 AND `disabled` = 0" .
- ($all ? " " : " AND `blacklisted` = 0 "));
- $STH->setFetchMode(PDO::FETCH_ASSOC);
- $rows = $STH->fetchAll();
-
- $data = [];
- foreach($rows as $row) {
- $data[$row["base32"] . ".b32.i2p"] = $row["last_seen"];
- }
-
- exit(json_encode($data, JSON_PRETTY_PRINT));
+switch ($command) {
+ case "all":
+ $STH = $pdo->query ("SELECT `base32`, UNIX_TIMESTAMP(`last_seen`) as `last_seen` FROM `hosts` " .
+ "WHERE `approved` = 1 AND `disabled` = 0" .
+ ($all ? " " : " AND `blacklisted` = 0 "));
+ $STH->setFetchMode(PDO::FETCH_ASSOC);
+ $rows = $STH->fetchAll();
+
+ foreach($rows as $row) {
+ $data[$row["base32"] . ".b32.i2p"] = $row["last_seen"];
+ }
+ break;
+ case "status":
+ if (!$query || empty($query)) {
+ $data = array_merge($data, ["status" => "error", "message" => "Empty query"]);
+ break;
+ }
+ $q = htmlspecialchars($query);
+
+ $STH = $pdo->query("SELECT `host` AS `hostname`, `base64`, `base32`, UNIX_TIMESTAMP(`last_seen`) as `last_seen` FROM `hosts` WHERE `host` = '" . $q . "' LIMIT 1");
+ $row = $STH->fetch(PDO::FETCH_ASSOC);
+
+ if (empty($row)) {
+ $data = array_merge($data, ["status" => "error", "message" => "Not found"]);
+ break;
+ } else {
+ if ($row["last_seen"] == "0000-00-00 00:00:00") $row["last_seen"] = "Never";
+ $row["base32"] .= ".b32.i2p";
+ $data = array_merge($data, $row);
+ }
+ break;
+ default:
+ $data = array_merge($data, ["status" => "error", "message" => "Incorrect request"]);
}
+
+exit(json_encode($data, JSON_PRETTY_PRINT));