[new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()]]; return ["save" => $filters, "password" => $filters, "logout" => $filters]; } public function saveAction(string $name, string $phone = ""): ?array { $userId = $this->userId(); if ($userId <= 0) { $this->addError(new Error("Not authorized", "auth")); return null; } $name = trim((string)preg_replace('/\s+/u', " ", $name)); if ($name === "" || mb_strlen($name) > self::NAME_MAX) { $this->addError(new Error("Invalid name", "name")); return null; } if (!mb_check_encoding($name, "UTF-8") || preg_match('/[<>\x00-\x1f\x7f]/', $name)) { $this->addError(new Error("Invalid name characters", "name_chars")); return null; } $phone = trim($phone); if ($phone !== "" && !preg_match('/^\+\d{8,15}$/', $phone)) { $this->addError(new Error("Invalid phone", "phone")); return null; } $user = new \CUser(); if (!$user->Update($userId, ["NAME" => $name, "PERSONAL_PHONE" => $phone])) { $this->addError(new Error("Save failed", "failed")); return null; } return ["ok" => true]; } public function passwordAction(string $current, string $password): ?array { $userId = $this->userId(); if ($userId <= 0) { $this->addError(new Error("Not authorized", "auth")); return null; } $session = Application::getInstance()->getSession(); $now = time(); $log = array_values(array_filter((array)($session->get("vino_profile_password_log") ?? []), static fn($at): bool => (int)$at > $now - self::WINDOW_SECONDS)); if (count($log) >= self::MAX_PER_WINDOW) { $this->addError(new Error("Too many attempts", "too_often")); return null; } $log[] = $now; $session->set("vino_profile_password_log", $log); if (!$this->mayTryPassword($userId)) { $this->addError(new Error("Too many attempts", "too_often")); return null; } $row = UserTable::query()->setSelect(["PASSWORD", "EMAIL"])->setFilter(["=ID" => $userId])->enablePrivateFields()->fetch() ?: null; if ($row === null || $current === "" || !Password::equals((string)$row["PASSWORD"], $current)) { $this->addError(new Error("Wrong current password", "current")); return null; } $length = mb_strlen($password); if ($length < self::PASSWORD_MIN || $length > self::PASSWORD_MAX || mb_strtolower($password) === mb_strtolower((string)$row["EMAIL"])) { $this->addError(new Error("Weak password", "password")); return null; } if ($password === $current) { $this->addError(new Error("Same password", "same")); return null; } $user = new \CUser(); if (!$user->Update($userId, ["PASSWORD" => $password, "CONFIRM_PASSWORD" => $password])) { $this->addError(new Error("Password rejected", "policy")); return null; } return ["ok" => true]; } public function logoutAction(string $lang = "en"): array { global $USER; $USER->Logout(); return ["ok" => true, "redirect" => Redirect::home($lang)]; } private function mayTryPassword(int $userId): bool { $cache = Cache::createInstance(); $key = "vino_profile_password_" . $userId; $attempts = []; if ($cache->initCache(self::WINDOW_SECONDS, $key, "/vino/profile")) { $attempts = (array)($cache->getVars()["attempts"] ?? []); } $now = time(); $attempts = array_values(array_filter($attempts, static fn($at): bool => (int)$at > $now - self::WINDOW_SECONDS)); if (count($attempts) >= self::MAX_PER_WINDOW) { return false; } $attempts[] = $now; $cache->clean($key, "/vino/profile"); $cache->startDataCache(self::WINDOW_SECONDS, $key, "/vino/profile"); $cache->endDataCache(["attempts" => $attempts]); return true; } private function userId(): int { return (int)CurrentUser::get()->getId(); } }