"", "ru" => "/ru", "pl" => "/pl"]; public function configureActions(): array { return [ "add" => [ "prefilters" => [new Authentication(), new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_STARTED, self::MIN_FILL_MS)], ], ]; } public function addAction( int $productId, int $rating, string $text, string $consent = "", string $website = "", int $started = 0, string $altcha = "", string $lang = "en", string $page = "" ): ?array { if ($website !== "" || FormGuard::startedTooEarly($started, self::MIN_FILL_MS)) { return ["ok" => true]; } $user = CurrentUser::get(); $author = trim((string)$user->getFullName()); if ($author === "") { $author = trim((string)$user->getLogin()); } $author = mb_substr($author, 0, 60); if (!mb_check_encoding($author, "UTF-8") || !mb_check_encoding($text, "UTF-8")) { $this->addError(new Error("Wrong encoding", "text")); return null; } $author = trim((string)preg_replace('/\s+/u', " ", $author)); $text = trim($text); $lang = isset(self::LANGS[$lang]) ? $lang : "en"; if ($rating < 1 || $rating > 5) { $this->addError(new Error("Rating is required", "rating")); } if (mb_strlen($text) < 10 || mb_strlen($text) > 2000) { $this->addError(new Error("Text is required", "text")); } if ($consent !== "1") { $this->addError(new Error("Consent is required", "consent")); } if ($this->getErrors()) { return null; } $session = Application::getInstance()->getSession(); if (time() - (int)($session->get("vino_review_last") ?? 0) < self::RETRY_SECONDS) { $this->addError(new Error("Too often", "too_often")); return null; } if (!Loader::includeModule("iblock")) { $this->addError(new Error("Iblock module is required", "internal")); return null; } $product = ElementTable::getRow([ "select" => ["ID", "NAME"], "filter" => ["=ID" => $productId, "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"], ]); if ($product === null) { $this->addError(new Error("Wine not found", "not_found")); return null; } $pagePath = (string)parse_url($page, PHP_URL_PATH); $element = new \CIBlockElement(); $id = $element->Add([ "IBLOCK_ID" => ReviewStats::iblockId(ReviewStats::REVIEWS_CODE), "ACTIVE" => "N", "NAME" => mb_substr($author . ": " . $product["NAME"], 0, 255), "ACTIVE_FROM" => ConvertTimeStamp(time(), "FULL"), "PREVIEW_TEXT" => $text, "PREVIEW_TEXT_TYPE" => "text", "PROPERTY_VALUES" => [ "PRODUCT" => (int)$product["ID"], "RATING" => $rating, "AUTHOR" => $author, "USER_ID" => (int)$user->getId(), "LANG" => $lang, "PAGE" => self::SITE_URL . ($pagePath !== "" ? $pagePath : "/"), "CONSENT" => "Y", "CONSENT_VERSION" => Consent::VERSION, "CONSENT_URL" => self::SITE_URL . self::LANGS[$lang] . "/consent/", ], ]); if (!$id) { $this->addError(new Error("Review was not saved", "internal")); return null; } $session->set("vino_review_last", time()); return ["ok" => true]; } }