209 lines
6.6 KiB
PHP
209 lines
6.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Controller;
|
|
|
|
use Bitrix\Main\Application;
|
|
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
|
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
|
use Bitrix\Main\Engine\Controller;
|
|
use Bitrix\Main\Engine\Response\Json;
|
|
use Bitrix\Main\Error;
|
|
use Bitrix\Main\Loader;
|
|
use Bitrix\Main\Mail\Event;
|
|
use Bitrix\Main\Type\DateTime;
|
|
use Jinove\Vinoteca\Altcha;
|
|
use Jinove\Vinoteca\Config;
|
|
use Jinove\Vinoteca\Consent;
|
|
use Jinove\Vinoteca\Controller\Filter\FormGuard;
|
|
use Jinove\Vinoteca\Phone;
|
|
|
|
class Lead extends Controller
|
|
{
|
|
private const QUESTIONS_CODE = "sommelier_questions";
|
|
private const SUBSCRIBERS_CODE = "newsletter_subscribers";
|
|
private const SUBSCRIBE_RETRY_SECONDS = 60;
|
|
|
|
public function configureActions(): array
|
|
{
|
|
return [
|
|
"challenge" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_GET])],
|
|
],
|
|
"question" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard()],
|
|
],
|
|
"subscribe" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard()],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function challengeAction(): ?Json
|
|
{
|
|
$challenge = Altcha::challenge();
|
|
if ($challenge === null) {
|
|
$this->addError(new Error("Challenge is not available", "altcha"));
|
|
|
|
return null;
|
|
}
|
|
$response = new Json($challenge);
|
|
$response->addHeader("Cache-Control", "no-store");
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function questionAction(string $name, string $phone, string $message, string $consent = "", string $website = "", string $altcha = "", string $page = ""): ?array
|
|
{
|
|
if ($website !== "") {
|
|
return ["ok" => true];
|
|
}
|
|
$name = trim((string)preg_replace('/\s+/u', " ", $name));
|
|
$message = trim($message);
|
|
$phone = Phone::format($phone);
|
|
if ($consent === "" || $name === "" || $message === "" || $phone === "" || mb_strlen($name) > 100 || mb_strlen($message) > 2000) {
|
|
$this->addError(new Error("Invalid fields", "fields"));
|
|
|
|
return null;
|
|
}
|
|
$pageUrl = $this->pageUrl($page);
|
|
$consentUrl = $this->siteUrl() . "consent/";
|
|
$sentAt = (string)new DateTime();
|
|
$leadId = $this->save(self::QUESTIONS_CODE, $name . ", " . $phone, $message, [
|
|
"PERSON_NAME" => $name,
|
|
"PHONE" => $phone,
|
|
"LANG" => LANGUAGE_ID,
|
|
"PAGE" => $pageUrl,
|
|
"CONSENT" => "Y",
|
|
"CONSENT_VERSION" => Consent::VERSION,
|
|
"CONSENT_URL" => $consentUrl,
|
|
"SENT_AT" => $sentAt,
|
|
], $sentAt);
|
|
if ($leadId <= 0) {
|
|
$this->addError(new Error("Save failed", "save"));
|
|
|
|
return null;
|
|
}
|
|
Event::send([
|
|
"EVENT_NAME" => "VINO_SOMMELIER_QUESTION",
|
|
"LID" => SITE_ID,
|
|
"C_FIELDS" => [
|
|
"LEAD_ID" => $leadId,
|
|
"NAME" => $name,
|
|
"PHONE" => $phone,
|
|
"MESSAGE" => $message,
|
|
"PAGE" => $pageUrl,
|
|
"SENT_AT" => $sentAt,
|
|
"CONSENT_VERSION" => Consent::VERSION,
|
|
"CONSENT_URL" => $consentUrl,
|
|
],
|
|
]);
|
|
|
|
return ["ok" => true];
|
|
}
|
|
|
|
public function subscribeAction(string $email, string $consent = "", string $website = "", string $altcha = "", string $page = ""): ?array
|
|
{
|
|
if ($website !== "") {
|
|
return ["ok" => true];
|
|
}
|
|
$email = mb_strtolower(trim($email));
|
|
if (!check_email($email) || mb_strlen($email) > 254) {
|
|
$this->addError(new Error("Invalid e-mail", "email"));
|
|
|
|
return null;
|
|
}
|
|
if ($consent === "") {
|
|
$this->addError(new Error("Consent is required", "consent"));
|
|
|
|
return null;
|
|
}
|
|
$session = Application::getInstance()->getSession();
|
|
if (time() - (int)$session->get("vino_subscribe_last") < self::SUBSCRIBE_RETRY_SECONDS) {
|
|
return ["ok" => true];
|
|
}
|
|
$session->set("vino_subscribe_last", time());
|
|
if ($this->exists(self::SUBSCRIBERS_CODE, $email)) {
|
|
return ["ok" => true];
|
|
}
|
|
$sentAt = (string)new DateTime();
|
|
$consentUrl = $this->siteUrl() . "consent/";
|
|
$id = $this->save(self::SUBSCRIBERS_CODE, $email, "", [
|
|
"LANG" => LANGUAGE_ID,
|
|
"PAGE" => $this->pageUrl($page),
|
|
"CONSENT" => "Y",
|
|
"CONSENT_VERSION" => Consent::VERSION,
|
|
"CONSENT_URL" => $consentUrl,
|
|
"SENT_AT" => $sentAt,
|
|
], $sentAt);
|
|
if ($id <= 0) {
|
|
$this->addError(new Error("Save failed", "save"));
|
|
|
|
return null;
|
|
}
|
|
Event::send([
|
|
"EVENT_NAME" => "VINO_SUBSCRIBE",
|
|
"LID" => SITE_ID,
|
|
"C_FIELDS" => [
|
|
"EMAIL" => $email,
|
|
"CONSENT_VERSION" => Consent::VERSION,
|
|
"CONSENT_URL" => $consentUrl,
|
|
],
|
|
]);
|
|
|
|
return ["ok" => true];
|
|
}
|
|
|
|
private function iblockId(string $code): int
|
|
{
|
|
if (!Loader::includeModule("iblock")) {
|
|
return 0;
|
|
}
|
|
$iblock = \CIBlock::GetList([], ["CODE" => $code, "CHECK_PERMISSIONS" => "N"])->Fetch();
|
|
|
|
return $iblock ? (int)$iblock["ID"] : 0;
|
|
}
|
|
|
|
private function exists(string $code, string $name): bool
|
|
{
|
|
$iblockId = $this->iblockId($code);
|
|
if ($iblockId <= 0) {
|
|
return false;
|
|
}
|
|
|
|
return (bool)\CIBlockElement::GetList([], ["IBLOCK_ID" => $iblockId, "=NAME" => $name], false, ["nTopCount" => 1], ["ID"])->Fetch();
|
|
}
|
|
|
|
private function save(string $code, string $name, string $text, array $properties, string $sentAt): int
|
|
{
|
|
$iblockId = $this->iblockId($code);
|
|
if ($iblockId <= 0) {
|
|
return 0;
|
|
}
|
|
$element = new \CIBlockElement();
|
|
|
|
return (int)$element->Add([
|
|
"IBLOCK_ID" => $iblockId,
|
|
"NAME" => $name,
|
|
"ACTIVE" => "Y",
|
|
"DATE_ACTIVE_FROM" => $sentAt,
|
|
"DETAIL_TEXT" => $text,
|
|
"DETAIL_TEXT_TYPE" => "text",
|
|
"PROPERTY_VALUES" => $properties,
|
|
], false, false, false);
|
|
}
|
|
|
|
private function siteUrl(): string
|
|
{
|
|
return Config::host() . SITE_DIR;
|
|
}
|
|
|
|
private function pageUrl(string $page): string
|
|
{
|
|
$path = (string)parse_url($page, PHP_URL_PATH);
|
|
|
|
return Config::host() . ($path !== "" && str_starts_with($path, "/") ? $path : SITE_DIR);
|
|
}
|
|
}
|