Files
fitme3070.pt/views/auth/forgot_password.php
T
andreandClaude Opus 5.5 0e8b53f620 feat(audit): platform-tools atualizado (12f26fd) e registo de auditoria ligado
- shared/platform-tools: ea3616a → 12f26fd (27 commits). Traz o módulo de
  audit (f761d99, o pin da frota), «manter sessão iniciada», o registo de
  segurança da frota (o gancho que o login já tinha passa a funcionar) e a
  correção do IP do cliente encontrada nesta auditoria (último X-Forwarded-For
  / X-Real-IP), promovida ao partilhado. client_ip() da app delega nele.
- config/functions_audit.php: audit() sobre platform_audit(); tabela
  audit_log criada no primeiro uso, segredos ocultados, nunca parte a ação.
- Registado: login, falha de login, bloqueio por excesso de tentativas,
  logout, pedidos de recuperação, mudança/definição de palavra-passe,
  registo e criação de utilizadores, convites, perfil (antes/depois), todas
  as definições (db_update_setting, com valor anterior), cada passo da
  lixeira (apagar, restaurar, eliminar de vez) e ações nas mensagens.
- Sistema → Auditoria (views/sysadmin/audit.php), só sysadmin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 07:28:13 +01:00

87 lines
3.4 KiB
PHP

<?php
// "Forgot password" request. Mints a single-use token in `password_resets` and
// emails the reset link through the centralized notify façade.
//
// The response is identical whether or not the address exists — otherwise this
// page would be an account-enumeration oracle.
//
// This is also how anyone carried over from the Andmore ID era sets their
// first local password: those rows have password_hash NULL, and the flow keys
// on the address on file, so only its owner can act on the link.
require __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../../config/functions.php';
if (!empty($_SESSION['user_id'])) {
header('Location: ' . base_url(post_login_destination($_SESSION['user']['role'] ?? '')));
exit;
}
$sent = false;
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_check();
// Tighter than login: minting a reset token costs an email each time.
if (!throttle_check($pdo, 'pwreset', 3, 10)) {
throttle_block('forgot password');
}
throttle_record($pdo, 'pwreset');
$email = trim($_POST['email'] ?? '');
if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = 'Endereço de email inválido.';
} else {
$user = user_find_by_email($pdo, $email);
// Registado exista ou não a conta (sem dizer qual) — pedidos em série
// ao mesmo endereço ficam visíveis.
audit('auth.password_reset_requested', ['target_type' => 'user', 'target_label' => audit_mask_email($email), 'actor' => null]);
if ($user) {
$token = auth_create_reset_token($pdo, (int) $user['id']);
send_password_reset_email(
(string) $user['name'],
(string) $user['email'],
base_url('views/auth/reset_password.php?token=' . $token),
(int) $user['id'],
false // fica na fila: o tempo de resposta não pode denunciar a conta
);
}
// Same message either way.
$sent = true;
}
}
$page_title = 'Recuperar palavra-passe';
include __DIR__ . '/../../includes/auth_header.php';
?>
<p class="auth-sub">Enviamos-lhe um link para definir uma nova palavra-passe.</p>
<?php if ($error): ?>
<div class="alert alert-danger py-2 small"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($sent): ?>
<div class="alert alert-success py-2 small">
Se existir uma conta com esse email, vai receber as instruções
nos próximos minutos. O link é válido durante 2 horas.
</div>
<a href="<?= htmlspecialchars(base_url('views/auth/login.php')) ?>" class="btn btn-outline-secondary w-100">Voltar ao login</a>
<?php else: ?>
<form method="POST" novalidate>
<?= csrf_field() ?>
<div class="mb-4">
<label class="form-label" for="fp-email">Email</label>
<input type="email" id="fp-email" name="email" class="form-control" required autofocus
autocomplete="email" inputmode="email"
value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
</div>
<button type="submit" class="btn btn-primary w-100">Enviar link</button>
</form>
<hr class="my-3">
<p class="text-center small mb-0 auth-foot">
<a href="<?= htmlspecialchars(base_url('views/auth/login.php')) ?>">Voltar ao login</a>
</p>
<?php endif; ?>
<?php include __DIR__ . '/../../includes/auth_footer.php'; ?>