2022-06-06 17:57:49 +00:00
< ? php
require_once __DIR__ . '/../config.php' ;
$all = false ;
2022-06-12 00:01:02 +00:00
$data = [];
2022-06-06 17:57:49 +00:00
$pdo = ( new App\DB ( $options )) -> pdo ;
header ( 'Content-Type: application/json' );
if ( isset ( $_GET [ " all " ]))
$all = true ;
2022-06-12 00:01:02 +00:00
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 {
2022-06-12 00:05:16 +00:00
if ( ! $row [ " last_seen " ]) $row [ " last_seen " ] = " Never " ;
2022-06-12 00:01:02 +00:00
$row [ " base32 " ] .= " .b32.i2p " ;
$data = array_merge ( $data , $row );
}
break ;
default :
$data = array_merge ( $data , [ " status " => " error " , " message " => " Incorrect request " ]);
2022-06-06 17:57:49 +00:00
}
2022-06-12 00:01:02 +00:00
exit ( json_encode ( $data , JSON_PRETTY_PRINT ));