diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2024-01-31 06:11:00 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2024-01-31 20:45:40 +0300 |
commit | c0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch) | |
tree | 2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /lib/admin.php | |
parent | 48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff) |
make it simple, but not simpler
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'] ?? '', + ]); } |