125 lines
4.1 KiB
PHP
125 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Controller;
|
|
|
|
use Bitrix\Main\Application;
|
|
use Bitrix\Main\Config\Option;
|
|
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
|
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
|
use Bitrix\Main\Engine\Controller;
|
|
use Bitrix\Main\Error;
|
|
use Bitrix\Main\Type\DateTime;
|
|
use Bitrix\Main\UserTable;
|
|
use Jinove\Vinoteca\Config;
|
|
use Jinove\Vinoteca\Controller\Filter\FormGuard;
|
|
use Jinove\Vinoteca\Consent;
|
|
use Jinove\Vinoteca\Redirect;
|
|
|
|
class Register extends Controller
|
|
{
|
|
private const MAX_PER_WINDOW = 5;
|
|
private const WINDOW_SECONDS = 600;
|
|
private const PASSWORD_MIN = 8;
|
|
private const PASSWORD_MAX = 200;
|
|
private const NAME_MAX = 50;
|
|
|
|
public function configureActions(): array
|
|
{
|
|
return [
|
|
"create" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_ELAPSED)],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function createAction(
|
|
string $name,
|
|
string $email,
|
|
string $password,
|
|
string $consent = "",
|
|
string $back = "",
|
|
string $lang = "en",
|
|
string $website = "",
|
|
int $elapsed = 0,
|
|
string $altcha = ""
|
|
): ?array {
|
|
$lang = Redirect::language($lang);
|
|
if ($website !== "") {
|
|
return ["ok" => true, "redirect" => Redirect::home($lang)];
|
|
}
|
|
$session = Application::getInstance()->getSession();
|
|
$now = time();
|
|
$log = array_values(array_filter((array)($session->get("vino_register_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_register_log", $log);
|
|
|
|
$name = trim((string)preg_replace('/\s+/u', " ", $name));
|
|
$email = mb_strtolower(trim($email));
|
|
$length = mb_strlen($password);
|
|
if (
|
|
$consent !== "1"
|
|
|| $name === ""
|
|
|| mb_strlen($name) > self::NAME_MAX
|
|
|| !mb_check_encoding($name, "UTF-8")
|
|
|| preg_match('/[<>\x00-\x1f\x7f]/', $name)
|
|
|| !check_email($email)
|
|
|| mb_strlen($email) > 254
|
|
|| $length < self::PASSWORD_MIN
|
|
|| $length > self::PASSWORD_MAX
|
|
|| mb_strtolower($password) === $email
|
|
) {
|
|
$this->addError(new Error("Registration failed", "failed"));
|
|
|
|
return null;
|
|
}
|
|
$exists = UserTable::getRow([
|
|
"select" => ["ID"],
|
|
"filter" => ["LOGIC" => "OR", "=EMAIL" => $email, "=LOGIN" => $email],
|
|
]);
|
|
if ($exists !== null) {
|
|
$this->addError(new Error("Registration failed", "failed"));
|
|
|
|
return null;
|
|
}
|
|
$groups = array_values(array_filter(array_map("intval", explode(",", (string)Option::get("main", "new_user_registration_def_group", "")))));
|
|
$user = new \CUser();
|
|
$id = (int)$user->Add([
|
|
"LOGIN" => $email,
|
|
"EMAIL" => $email,
|
|
"NAME" => $name,
|
|
"PASSWORD" => $password,
|
|
"CONFIRM_PASSWORD" => $password,
|
|
"ACTIVE" => "Y",
|
|
"LID" => SITE_ID,
|
|
"GROUP_ID" => $groups,
|
|
"UF_CONSENT_TIME" => (string)new DateTime(),
|
|
"UF_CONSENT_VERSION" => Consent::VERSION,
|
|
"UF_CONSENT_PAGE" => $this->consentPage(),
|
|
"UF_CONSENT_URL" => Config::host() . SITE_DIR . "consent/",
|
|
]);
|
|
if ($id <= 0) {
|
|
$this->addError(new Error("Registration failed", "failed"));
|
|
|
|
return null;
|
|
}
|
|
global $USER;
|
|
$USER->Authorize($id);
|
|
|
|
return ["ok" => true, "redirect" => Redirect::back($back, $lang)];
|
|
}
|
|
|
|
private function consentPage(): string
|
|
{
|
|
$path = (string)parse_url((string)($this->getRequest()->getHeader("Referer") ?? ""), PHP_URL_PATH);
|
|
|
|
return mb_check_encoding($path, "UTF-8") && !preg_match('/[\x00-\x1f\x7f]/', $path) ? mb_substr($path, 0, 200) : "";
|
|
}
|
|
}
|