$token[0] === $username); } public static function getNewTokenForUser(string $username): string { $tokenBody = self::random32Characters() . str_pad(self::$iterations++, 5, '0'); if (self::$iterations >= 99999) { self::$iterations = 0; } $timestamp = microtime(true) * 10000; self::$tokens[] = [$username, $timestamp, $tokenBody]; return $timestamp . $tokenBody; } public static function getTokenOwnership(string $controlledToken): string | null { for ($i = 0; $i < count(self::$tokens); ++$i) { $token = self::$tokens[$i]; if ($token[1] . $token[2] === $controlledToken) { return $token[0]; } } return null; } public static function deleteOldTokens(): void { for ($i = 0; $i < count(self::$tokens); ++$i) { $token = self::$tokens[$i]; // 1 hour if (time() - ($token[1] / 10000) > 3600) { array_splice(self::$tokens, $i, 1); } } } public static function deleteTokensForUser(string $user): void { for ($i = 0; $i < count(self::$tokens); ++$i) { if (self::$tokens[$i][0] === $user) { array_splice(self::$tokens, $i, 1); } } } }