auth.php: format

This commit is contained in:
Zankaria 2025-04-23 22:29:23 +02:00
parent 3e3b71211a
commit f7dae74522

View file

@ -10,7 +10,7 @@ use Vichan\Functions\{Hide, Net};
defined('TINYBOARD') or exit; defined('TINYBOARD') or exit;
// create a hash/salt pair for validate logins // create a hash/salt pair for validate logins
function mkhash($username, $password, $salt = false) { function mkhash(string $username, ?string $password, mixed $salt = false): array|string {
global $config; global $config;
if (!$salt) { if (!$salt) {
@ -42,7 +42,7 @@ function mkhash($username, $password, $salt = false) {
} }
} }
function crypt_password($password) { function crypt_password(string $password): array {
global $config; global $config;
// `salt` database field is reused as a version value. We don't want it to be 0. // `salt` database field is reused as a version value. We don't want it to be 0.
$version = $config['password_crypt_version'] ? $config['password_crypt_version'] : 1; $version = $config['password_crypt_version'] ? $config['password_crypt_version'] : 1;
@ -56,8 +56,6 @@ function crypt_password($password) {
} }
function test_password(string $db_hash, string|int $version, string $input_password): bool { function test_password(string $db_hash, string|int $version, string $input_password): bool {
// Version = 0 denotes an old password hashing schema. In the same column, the
// password hash was kept previously
$version = (int)$version; $version = (int)$version;
if ($version < 2) { if ($version < 2) {
$ok = \hash_equals($db_hash, \crypt($input_password, $db_hash)); $ok = \hash_equals($db_hash, \crypt($input_password, $db_hash));
@ -89,23 +87,24 @@ function login(string $username, string $password): array|false {
$query->execute(); $query->execute();
} }
return $mod = array( return $mod = [
'id' => $user['id'], 'id' => $user['id'],
'type' => $user['type'], 'type' => $user['type'],
'username' => $username, 'username' => $username,
'hash' => mkhash($username, $user['password']), 'hash' => mkhash($username, $user['password']),
'boards' => explode(',', $user['boards']) 'boards' => explode(',', $user['boards'])
); ];
} }
} }
return false; return false;
} }
function setCookies() { function setCookies(): void {
global $mod, $config; global $mod, $config;
if (!$mod) if (!$mod) {
error('setCookies() was called for a non-moderator!'); error('setCookies() was called for a non-moderator!');
}
$is_https = Net\is_connection_https(); $is_https = Net\is_connection_https();
@ -118,14 +117,14 @@ function setCookies() {
time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, $is_https, $config['cookies']['httponly']); time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, $is_https, $config['cookies']['httponly']);
} }
function destroyCookies() { function destroyCookies(): void {
global $config; global $config;
$is_https = Net\is_connection_https(); $is_https = Net\is_connection_https();
// Delete the cookies // Delete the cookies
setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, $is_https, true); setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, $is_https, true);
} }
function modLog($action, $_board=null) { function modLog(string $action, ?string $_board = null): void {
global $mod, $board, $config; global $mod, $board, $config;
$query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)"); $query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)");
$query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT); $query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT);
@ -140,16 +139,18 @@ function modLog($action, $_board=null) {
$query->bindValue(':board', null, PDO::PARAM_NULL); $query->bindValue(':board', null, PDO::PARAM_NULL);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if ($config['syslog']) if ($config['syslog']) {
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action); _syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
}
} }
function create_pm_header() { function create_pm_header(): mixed {
global $mod, $config; global $mod, $config;
if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) { if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
if ($header === true) if ($header === true) {
return false; return false;
}
return $header; return $header;
} }
@ -158,26 +159,29 @@ function create_pm_header() {
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT); $query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if ($pm = $query->fetch(PDO::FETCH_ASSOC)) if ($pm = $query->fetch(PDO::FETCH_ASSOC)) {
$header = array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1); $header = [ 'id' => $pm['id'], 'waiting' => $query->rowCount() - 1 ];
else } else {
$header = true; $header = true;
}
if ($config['cache']['enabled']) if ($config['cache']['enabled']) {
cache::set('pm_unread_' . $mod['id'], $header); cache::set('pm_unread_' . $mod['id'], $header);
}
if ($header === true) if ($header === true) {
return false; return false;
}
return $header; return $header;
} }
function make_secure_link_token($uri) { function make_secure_link_token(string $uri): string {
global $mod, $config; global $mod, $config;
return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8); return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
} }
function check_login(Context $ctx, $prompt = false) { function check_login(Context $ctx, bool $prompt = false): void {
global $config, $mod; global $config, $mod;
// Validate session // Validate session
@ -187,7 +191,9 @@ function check_login(Context $ctx, $prompt = false) {
if (count($cookie) != 3) { if (count($cookie) != 3) {
// Malformed cookies // Malformed cookies
destroyCookies(); destroyCookies();
if ($prompt) mod_login($ctx); if ($prompt) {
mod_login($ctx);
}
exit; exit;
} }
@ -200,7 +206,9 @@ function check_login(Context $ctx, $prompt = false) {
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) { if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
// Malformed cookies // Malformed cookies
destroyCookies(); destroyCookies();
if ($prompt) mod_login($ctx); if ($prompt) {
mod_login($ctx);
}
exit; exit;
} }
@ -211,4 +219,4 @@ function check_login(Context $ctx, $prompt = false) {
'boards' => explode(',', $user['boards']) 'boards' => explode(',', $user['boards'])
); );
} }
} }