File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Models/PasswordResetToken.php
Back
<?php /** * PasswordResetToken Model * Handles one-time secure password reset tokens */ require_once __DIR__ . '/../Core/Model.php'; class PasswordResetToken extends Model { protected string $table = 'password_reset_tokens'; public function createToken(int $userId, int $lifetimeSeconds = 3600): string { // Invalidate old active tokens for this user $this->query("UPDATE `password_reset_tokens` SET `used_at` = NOW() WHERE `user_id` = :user_id AND `used_at` IS NULL", [':user_id' => $userId]); $rawToken = bin2hex(random_bytes(32)); $tokenHash = hash('sha256', $rawToken); $expiresAt = date('Y-m-d H:i:s', time() + $lifetimeSeconds); $this->insert([ 'user_id' => $userId, 'token_hash' => $tokenHash, 'expires_at' => $expiresAt, 'used_at' => null, 'ip_address' => Security::getClientIp(), 'created_at' => date('Y-m-d H:i:s') ]); return $rawToken; } public function verifyToken(string $rawToken): ?array { if (empty($rawToken)) { return null; } $tokenHash = hash('sha256', $rawToken); $sql = "SELECT prt.*, u.status as user_status, u.email, u.username FROM `password_reset_tokens` prt JOIN `users` u ON prt.user_id = u.id WHERE prt.token_hash = :token_hash AND prt.expires_at > NOW() AND prt.used_at IS NULL AND u.status = 'active' AND u.deleted_at IS NULL LIMIT 1"; return $this->fetchOne($sql, [':token_hash' => $tokenHash]); } public function markAsUsed(int $tokenId): void { $this->query("UPDATE `password_reset_tokens` SET `used_at` = NOW() WHERE `id` = :id", [':id' => $tokenId]); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.44 |
proxy
|
phpinfo
|
Settings