flash.moe/src/SSHKeys/SSHKeys.php

38 lines
1.1 KiB
PHP
Raw Normal View History

2022-02-06 01:36:05 +00:00
<?php
2023-10-12 22:09:04 +00:00
namespace Makai\SSHKeys;
2022-02-06 01:36:05 +00:00
2024-10-21 20:50:01 +00:00
use Index\Db\{DbConnection,DbStatementCache};
2022-02-06 01:36:05 +00:00
class SSHKeys {
2023-10-12 20:00:08 +00:00
private DbStatementCache $cache;
2022-02-06 01:36:05 +00:00
2024-10-21 20:50:01 +00:00
public function __construct(DbConnection $dbConn) {
2023-10-12 20:00:08 +00:00
$this->cache = new DbStatementCache($dbConn);
2022-02-06 01:36:05 +00:00
}
2023-10-12 20:00:08 +00:00
public function getKeys(
?int $minLevel = null,
?bool $deprecated = null,
2024-12-16 19:15:50 +00:00
): iterable {
2023-10-12 20:00:08 +00:00
$hasMinLevel = $minLevel !== null;
$hasDeprecated = $deprecated !== null;
$args = 0;
$query = 'SELECT key_id, key_level, key_algo, key_body, key_comment, UNIX_TIMESTAMP(key_created), UNIX_TIMESTAMP(key_deprecated) FROM fm_public_keys';
if($hasMinLevel) {
++$args;
$query .= ' WHERE key_level >= ?';
}
if($hasDeprecated)
$query .= sprintf(' %s key_deprecated %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deprecated ? 'IS NOT' : 'IS');
$query .= ' ORDER BY key_level DESC, key_id ASC';
$stmt = $this->cache->get($query);
if($hasMinLevel)
$stmt->addParameter(1, $minLevel);
2022-02-06 01:36:05 +00:00
$stmt->execute();
2023-10-12 20:00:08 +00:00
2024-12-16 19:15:50 +00:00
return $stmt->getResult()->getIterator(SSHKeyInfo::fromResult(...));
2022-02-06 01:36:05 +00:00
}
}