diff options
Diffstat (limited to 'lib/admin.php')
-rw-r--r-- | lib/admin.php | 82 |
1 files changed, 39 insertions, 43 deletions
diff --git a/lib/admin.php b/lib/admin.php index 91aa620..116ee3c 100644 --- a/lib/admin.php +++ b/lib/admin.php @@ -1,55 +1,51 @@ <?php -class admin { +require_once 'lib/stored_config.php'; - const SESSION_TIMEOUT = 86400 * 14; - const COOKIE_NAME = 'admin_key'; +const ADMIN_SESSION_TIMEOUT = 86400 * 14; +const ADMIN_COOKIE_NAME = 'admin_key'; - protected static ?bool $isAdmin = null; - - public static function isAdmin(): bool { - if (is_null(self::$isAdmin)) - self::$isAdmin = self::_verifyKey(); - return self::$isAdmin; - } - - protected static function _verifyKey(): bool { - if (isset($_COOKIE[self::COOKIE_NAME])) { - $cookie = (string)$_COOKIE[self::COOKIE_NAME]; - if ($cookie !== self::getKey()) - self::unsetCookie(); - return true; - } - return false; - } +function is_admin(): bool { + static $is_admin = null; + if (is_null($is_admin)) + $is_admin = _admin_verify_key(); + return $is_admin; +} - public static function checkPassword(string $pwd): bool { - return salt_password($pwd) === config::get('admin_pwd'); +function _admin_verify_key(): bool { + if (isset($_COOKIE[ADMIN_COOKIE_NAME])) { + $cookie = (string)$_COOKIE[ADMIN_COOKIE_NAME]; + if ($cookie !== _admin_get_key()) + admin_unset_cookie(); + return true; } + return false; +} - protected static function getKey(): string { - global $config; - $admin_pwd_hash = config::get('admin_pwd'); - return salt_password("$admin_pwd_hash|{$_SERVER['REMOTE_ADDR']}"); - } +function admin_check_password(string $pwd): bool { + return salt_password($pwd) === scGet('admin_pwd'); +} - public static function setCookie(): void { - global $config; - $key = self::getKey(); - setcookie(self::COOKIE_NAME, $key, time() + self::SESSION_TIMEOUT, '/', $config['cookie_host']); - } +function _admin_get_key(): string { + $admin_pwd_hash = scGet('admin_pwd'); + return salt_password("$admin_pwd_hash|{$_SERVER['REMOTE_ADDR']}"); +} - public static function unsetCookie(): void { - global $config; - setcookie(self::COOKIE_NAME, '', 1, '/', $config['cookie_host']); - } +function admin_set_cookie(): void { + global $config; + $key = _admin_get_key(); + setcookie(ADMIN_COOKIE_NAME, $key, time() + ADMIN_SESSION_TIMEOUT, '/', $config['cookie_host']); +} - public static function logAuth(): void { - getDb()->insert('admin_log', [ - 'ts' => time(), - 'ip' => ip2ulong($_SERVER['REMOTE_ADDR']), - 'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '', - ]); - } +function admin_unset_cookie(): void { + global $config; + setcookie(ADMIN_COOKIE_NAME, '', 1, '/', $config['cookie_host']); +} +function admin_log_auth(): void { + DB()->insert('admin_log', [ + 'ts' => time(), + 'ip' => ip2ulong($_SERVER['REMOTE_ADDR']), + 'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '', + ]); } |