<?php

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

$fp = trim(strtoupper($VARS["fingerprint"]));

$where = [];

if (strlen($fp) == 8) {
    $where = ["fingerprint_tiny" => $fp];
} else if (strlen($fp) == 16) {
    $where = ["fingerprint_short" => $fp];
} else {
    $where = ["fingerprint[~]" => "%$fp"];
}

$results = $database->select("notary_registry", '*', $where);

$out = [];

foreach ($results as $r) {
    $out[] = [
        "fingerprint" => $r["fingerprint"],
        "name" => empty($r["name"]) ? null : $r["name"],
        "email" => empty($r["email"]) ? null : $r["email"],
        "location" => empty($r["location"]) ? null : $r["location"],
        "commissionexpires" => (is_null($r["commissionexpires"]) ? null : strtotime($r["commissionexpires"])),
        "idnumber" => empty($r["idnumber"]) ? null : $r["idnumber"],
        "state" => empty($r["state"]) ? null : $r["state"],
        "publickey" => $r["publickey"]
    ];
}

exitWithJson([
    "status" => "OK",
    "query" => $fp,
    "count" => count($results),
    "results" => $out
]);