flash.moe/src/SSHKeys.php
2022-02-16 20:49:49 +00:00

48 lines
1.4 KiB
PHP

<?php
namespace Makai;
use Index\Data\DbType;
use Index\Data\IDbConnection;
use Index\Data\IDbResult;
class SSHKeys {
private const QUERY = 'SELECT `key_id`, `key_level`, `key_algo`, `key_body`, `key_comment`, UNIX_TIMESTAMP(`key_created`), UNIX_TIMESTAMP(`key_deprecated`) FROM `fm_public_keys` WHERE `key_level` >= ?';
private IDbConnection $conn;
public function __construct(IDbConnection $conn) {
$this->conn = $conn;
}
public function getKeys(int $minLevel, bool $includeDeprecated = false): array {
$query = self::QUERY;
if(!$includeDeprecated)
$query .= ' AND `key_deprecated` IS NULL';
$query .= ' ORDER BY `key_level` DESC, `key_id`';
$stmt = $this->conn->prepare($query);
$stmt->addParameter(1, $minLevel, DbType::INTEGER);
$stmt->execute();
$result = $stmt->getResult();
$objs = [];
while($result->next())
$objs[] = self::createObject($result);
return $objs;
}
private static function createObject(IDbResult $result): SSHKeyInfo {
return new SSHKeyInfo(
$result->getString(0),
$result->getInteger(1),
$result->getString(2),
$result->getString(3),
$result->getAString(4),
$result->getInteger(5),
$result->isNull(6) ? null : $result->getInteger(6),
);
}
}