Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
0590195880
593 files changed
+27942
No files matched your search
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Controller;
|
||||
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Data\Cache;
|
||||
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
||||
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
||||
use Bitrix\Main\Engine\Controller;
|
||||
use Bitrix\Main\Error;
|
||||
use Bitrix\Main\UserTable;
|
||||
use Jinove\Vinoteca\Controller\Filter\FormGuard;
|
||||
|
||||
class Password extends Controller
|
||||
{
|
||||
private const MAX_PER_WINDOW = 6;
|
||||
private const WINDOW_SECONDS = 600;
|
||||
private const MAIL_PAUSE_SECONDS = 60;
|
||||
private const PASSWORD_MIN = 8;
|
||||
private const PASSWORD_MAX = 200;
|
||||
private const RESPONSE_MS = 600;
|
||||
private const EVENT = "USER_PASS_REQUEST";
|
||||
|
||||
public function configureActions(): array
|
||||
{
|
||||
$filters = ["prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_ELAPSED)]];
|
||||
|
||||
return ["request" => $filters, "reset" => $filters];
|
||||
}
|
||||
|
||||
public function requestAction(string $email, string $website = "", int $elapsed = 0, string $altcha = ""): ?array
|
||||
{
|
||||
$started = microtime(true);
|
||||
if ($website !== "") {
|
||||
return ["ok" => true];
|
||||
}
|
||||
if (!$this->limit("vino_password_request_log")) {
|
||||
return null;
|
||||
}
|
||||
$email = mb_strtolower(trim($email));
|
||||
if (!check_email($email) || mb_strlen($email) > 254) {
|
||||
$this->addError(new Error("Invalid e-mail", "email"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$row = UserTable::getRow([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=EMAIL" => $email, "=ACTIVE" => "Y"],
|
||||
]);
|
||||
if ($row !== null && $this->mayMail((int)$row["ID"])) {
|
||||
\CUser::SendUserInfo((int)$row["ID"], SITE_ID, "", false, self::EVENT);
|
||||
}
|
||||
$left = self::RESPONSE_MS * 1000 - (int)((microtime(true) - $started) * 1000000);
|
||||
if ($left > 0) {
|
||||
usleep($left);
|
||||
}
|
||||
|
||||
return ["ok" => true];
|
||||
}
|
||||
|
||||
public function resetAction(string $login, string $checkword, string $password, string $website = "", int $elapsed = 0, string $altcha = ""): ?array
|
||||
{
|
||||
if ($website !== "") {
|
||||
$this->addError(new Error("Reset failed", "failed"));
|
||||
|
||||
return null;
|
||||
}
|
||||
if (!$this->limit("vino_password_reset_log")) {
|
||||
return null;
|
||||
}
|
||||
$length = mb_strlen($password);
|
||||
if (
|
||||
$login === ""
|
||||
|| mb_strlen($login) > 254
|
||||
|| !preg_match('/^[A-Za-z0-9]{32}$/', $checkword)
|
||||
) {
|
||||
$this->addError(new Error("Invalid link", "link"));
|
||||
|
||||
return null;
|
||||
}
|
||||
if ($length < self::PASSWORD_MIN || $length > self::PASSWORD_MAX || mb_strtolower($password) === mb_strtolower($login)) {
|
||||
$this->addError(new Error("Weak password", "password"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$result = (new \CUser())->ChangePassword($login, $checkword, $password, $password, SITE_ID, "", 0, false);
|
||||
if (($result["TYPE"] ?? "") !== "OK") {
|
||||
$this->addError(new Error("Reset failed", (string)($result["FIELD"] ?? "") === "PASSWORD" ? "password" : "link"));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return ["ok" => true];
|
||||
}
|
||||
|
||||
private function limit(string $sessionKey): bool
|
||||
{
|
||||
$session = Application::getInstance()->getSession();
|
||||
$now = time();
|
||||
$log = array_values(array_filter((array)($session->get($sessionKey) ?? []), 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 false;
|
||||
}
|
||||
$log[] = $now;
|
||||
$session->set($sessionKey, $log);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function mayMail(int $userId): bool
|
||||
{
|
||||
$cache = Cache::createInstance();
|
||||
if ($cache->initCache(self::MAIL_PAUSE_SECONDS, "vino_pass_mail_" . $userId, "/vino/password")) {
|
||||
return false;
|
||||
}
|
||||
$cache->startDataCache();
|
||||
$cache->endDataCache(["sent" => true]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user