Simplify PDO use

This commit is contained in:
Miraty 2022-12-13 17:38:54 +01:00
parent 1c193cd59d
commit ffd7e283a1
6 changed files with 22 additions and 45 deletions

View file

@ -24,7 +24,6 @@ nav ul li ul li::before {
}
h1 {
text-align: center;
font-size: 1.5rem;
display: inline;
}

View file

@ -45,12 +45,8 @@ function outdatedPasswordHash($id) {
}
function changePassword($id, $password) {
$stmt = DB->prepare('UPDATE users SET password = :password WHERE id = :id');
$stmt->bindValue(':id', $id);
$stmt->bindValue(':password', hashPassword($password));
$stmt->execute();
DB->prepare('UPDATE users SET password = :password WHERE id = :id')
->execute([':password' => hashPassword($password), ':id' => $id]);
}
function logout() {
@ -85,11 +81,12 @@ function rateLimitAccount($requestedTokens) {
$tokens -= $requestedTokens;
// Update
$stmt = DB->prepare('UPDATE users SET bucket_tokens = :bucket_tokens, bucket_last_update = :bucket_last_update WHERE id = :id');
$stmt->bindValue(':id', $_SESSION['id']);
$stmt->bindValue(':bucket_tokens', $tokens);
$stmt->bindValue(':bucket_last_update', time());
$stmt->execute();
DB->prepare('UPDATE users SET bucket_tokens = :bucket_tokens, bucket_last_update = :bucket_last_update WHERE id = :id')
->execute([
':bucket_tokens' => $tokens,
':bucket_last_update' => time(),
':id' => $_SESSION['id']
]);
}
function rateLimitInstance($requestedTokens) {
@ -106,11 +103,9 @@ function rateLimitInstance($requestedTokens) {
$tokens -= $requestedTokens;
// Update
$stmt = DB->prepare("UPDATE params SET value = :bucket_tokens WHERE name = 'instance_bucket_tokens';");
$stmt->bindValue(':bucket_tokens', $tokens);
$stmt->execute();
DB->prepare("UPDATE params SET value = :bucket_tokens WHERE name = 'instance_bucket_tokens';")
->execute([':bucket_tokens' => $tokens]);
$stmt = DB->prepare("UPDATE params SET value = :bucket_last_update WHERE name = 'instance_bucket_last_update';");
$stmt->bindValue(':bucket_last_update', time());
$stmt->execute();
DB->prepare("UPDATE params SET value = :bucket_last_update WHERE name = 'instance_bucket_last_update';")
->execute([':bucket_last_update' => time()]);
}

View file

@ -53,12 +53,8 @@ function insert($table, $values) {
}
$query .= ')';
$stmt = DB->prepare($query);
foreach ($values as $key => $val)
$stmt->bindValue(":$key", $val);
$stmt->execute();
DB->prepare($query)
->execute($values);
}
function query($action, $table, $conditions = [], $column = NULL) {
@ -78,15 +74,9 @@ function query($action, $table, $conditions = [], $column = NULL) {
}
$stmt = DB->prepare($query);
$stmt->execute($conditions);
foreach ($conditions as $key => $val)
$stmt->bindValue(":$key", $val);
$stmt->execute();
if (isset($column))
return array_column($stmt->fetchAll(PDO::FETCH_ASSOC), $column);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
return array_column($stmt->fetchAll(PDO::FETCH_ASSOC), $column);
}
function displayIndex() { ?>

View file

@ -12,9 +12,8 @@ if (processForm()) {
query('delete', 'approval-keys', ['key' => $_POST['key']]);
$stmt = DB->prepare('UPDATE users SET type = "approved" WHERE id = :id');
$stmt->bindValue(':id', $_SESSION['id']);
$stmt->execute();
DB->prepare('UPDATE users SET type = "approved" WHERE id = :id')
->execute([':id' => $_SESSION['id']]);
$_SESSION['type'] = 'approved';

View file

@ -8,12 +8,8 @@ if (processForm()) {
if (usernameExists($username) !== false)
output(403, 'Ce nom de compte est déjà utilisé.');
$stmt = DB->prepare('UPDATE users SET username = :username WHERE id = :id');
$stmt->bindValue(':id', $_SESSION['id']);
$stmt->bindValue(':username', $username);
$stmt->execute();
DB->prepare('UPDATE users SET username = :username WHERE id = :id')
->execute([':username' => $username, ':id' => $_SESSION['id']]);
$_SESSION['display-username'] = htmlspecialchars($_POST['new-username']);

View file

@ -15,10 +15,8 @@ if (processForm()) {
checkAuthToken($matches['salt'], $matches['hash']);
$stmt = DB->prepare('UPDATE registry SET username = :username WHERE domain = :domain');
$stmt->bindValue(':username', $_SESSION['id']);
$stmt->bindValue(':domain', $domain);
$stmt->execute();
DB->prepare('UPDATE registry SET username = :username WHERE domain = :domain')
->execute([':username' => $_SESSION['id'], ':domain' => $domain]);
knotcZoneExec(CONF['reg']['registry'], [
$domain,