Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cookies.php
1<?php
3
4use \Bitrix\Landing\Hook;
5use \Bitrix\Landing\Manager;
6use \Bitrix\Landing\Site;
7use \Bitrix\Landing\Internals\BlockTable;
8use \Bitrix\Landing\Internals\HookDataTable;
9use \Bitrix\Main\Loader;
10use \Bitrix\Main\Localization\Loc;
11use \Bitrix\Main\ORM\Data\AddResult;
12use \Bitrix\Main\ORM\Data\UpdateResult;
13use \Bitrix\Main\EventResult;
14use \Bitrix\Main\UserConsent\Consent;
15use \Bitrix\Main\UserConsent\Agreement;
16use \Bitrix\Main\UserConsent\Internals\AgreementTable;
17
18Loc::loadMessages(__FILE__);
19
21{
26 'ga' => ['type' => 'analytic'],// google analytics
27 'gtm' => ['type' => 'analytic'],// google tag manager
28 'ym' => ['type' => 'analytic'],// yandex metrika
29 'fbp' => ['type' => 'analytic'],// facebook pixel
30 'vkp' => ['type' => 'analytic'],// vkontakte pixel
31 'yt' => ['type' => 'technical'],// youtube
32 'gmap' => ['type' => 'technical']// google maps
33 ];
34
38 const USER_CONSENT_CODE = 'landing_cookies';
39
45 public static function getMainAgreement(?int $agreementId = null): ?array
46 {
47 $currentLang = LANGUAGE_ID;
48 $agreementCode = 'landing_cookie_agreement';
49 $fields = [
50 'ID' => 0,
51 'NAME' => Loc::getMessage('LANDING_COOKIES_MAIN_AGREEMENT_TITLE'),
52 'AGREEMENT_TEXT' => Loc::getMessage('LANDING_COOKIES_MAIN_AGREEMENT_TEXT'),
53 'LABEL_TEXT' => Loc::getMessage('LANDING_COOKIES_MAIN_AGREEMENT_LABEL'),
54 ];
55
56 if (!$fields['NAME'])
57 {
58 return null;
59 }
60
61 // current from database (actualize in db)
62 $res = AgreementTable::getList([
63 'select' => [
64 'ID', 'NAME', 'AGREEMENT_TEXT', 'LABEL_TEXT'
65 ],
66 'filter' =>
67 $agreementId
68 ? [
69 'ID' => $agreementId
70 ]
71 : [
72 '=ACTIVE' => 'Y',
73 '=CODE' => $agreementCode,
74 '=LANGUAGE_ID' => $currentLang
75 ]
76 ]);
77 if ($row = $res->fetch())
78 {
79 return $row;
80 }
81 else
82 {
83 $res = AgreementTable::add([
84 'CODE' => $agreementCode,
85 'LANGUAGE_ID' => $currentLang,
86 'TYPE' => Agreement::TYPE_CUSTOM,
87 'NAME' => $fields['NAME'],
88 'AGREEMENT_TEXT' => $fields['AGREEMENT_TEXT'],
89 'LABEL_TEXT' => $fields['LABEL_TEXT'],
90 'IS_AGREEMENT_TEXT_HTML' => 'Y'
91 ]);
92 if ($res->isSuccess())
93 {
94 $fields['ID'] = $res->getId();
95 return $fields;
96 }
97 }
98
99 return null;
100 }
101
107 protected static function getCookieType(string $code): string
108 {
109 if (isset(self::SYSTEM_COOKIES[$code]))
110 {
111 return self::SYSTEM_COOKIES[$code]['type'];
112 }
113 else
114 {
115 return 'other';
116 }
117 }
118
125 public static function getAgreements(int $siteId, bool $viewMode = false): array
126 {
127 $agreements = [];
128
129 if (!$siteId)
130 {
131 return $agreements;
132 }
133
134 //get zone
135 $zone = '';
136 if (Loader::includeModule('bitrix24'))
137 {
138 $zone = \CBitrix24::getPortalZone();
139 }
140 elseif (file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/lang/ru")
141 && !file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/lang/ua"))
142 {
143 $zone = 'ru';
144 }
145
146 // first get system messages
147 foreach (self::SYSTEM_COOKIES as $code => $cookieItem)
148 {
149 if (in_array($code, ['ym', 'vkp']) && !Manager::availableOnlyForZone('ru'))
150 {
151 continue;
152 }
153 if ($code === 'fbp' && $zone === 'ru')
154 {
155 continue;
156 }
157 $codeUp = strtoupper($code);
158 $agreements[$code] = [
159 'ID' => 0,
160 'CODE' => $code,
161 'TYPE' => $cookieItem['type'],
162 'SYSTEM' => 'Y',
163 'ACTIVE' => 'Y',
164 'TITLE' => Loc::getMessage('LANDING_COOKIES_SYS_' . $codeUp . '_TITLE'),
165 '~TITLE' => $viewMode ? '' : Loc::getMessage('LANDING_COOKIES_SYS_' . $codeUp . '_TITLE'),
166 'CONTENT' => Loc::getMessage('LANDING_COOKIES_SYS_' . $codeUp . '_TEXT'),
167 '~CONTENT' => $viewMode ? '' : Loc::getMessage('LANDING_COOKIES_SYS_' . $codeUp . '_TEXT')
168 ];
169 }
170
171 if ($siteId < 0)
172 {
173 return $agreements;
174 }
175
176 // then get custom messages from DB
177 $res = CookiesAgreement::getList([
178 'select' => [
179 'ID', 'ACTIVE', 'CODE', 'TITLE', 'CONTENT'
180 ],
181 'filter' => [
182 'SITE_ID' => $siteId
183 ]
184 ]);
185 while ($row = $res->fetch())
186 {
187 if (isset($agreements[$row['CODE']]))
188 {
189 if (!$row['TITLE'])
190 {
191 unset($row['TITLE']);
192 }
193 $agreements[$row['CODE']] = array_merge(
194 $agreements[$row['CODE']],
195 $row
196 );
197 }
198 else
199 {
200 $row['SYSTEM'] = 'N';
201 $row['TYPE'] = self::getCookieType($row['CODE']);
202 $row['~TITLE'] = $viewMode ? '' : $row['TITLE'];
203 $row['~CONTENT'] = $viewMode ? '' : $row['CONTENT'];
204 $agreements[$row['CODE']] = $row;
205 }
206 }
207
208 if ($viewMode)
209 {
210 $agreements = array_map(function($item)
211 {
212 $parser = new \CTextParser;
213 $item['CONTENT'] = $parser->convertText($item['CONTENT']);
214 $item['TITLE'] = \htmlspecialcharsbx($item['TITLE']);
215 return $item;
216 }, $agreements);
217 }
218
219 return $agreements;
220 }
221
228 public static function addAgreementForSite(int $siteId, array $fields): AddResult
229 {
230 return CookiesAgreement::add([
231 'SITE_ID' => $siteId,
232 'CODE' => $fields['CODE'] ?? null,
233 'ACTIVE' => $fields['ACTIVE'] ?? 'Y',
234 'TITLE' => $fields['TITLE'] ?? null,
235 'CONTENT' => $fields['CONTENT'] ?? null
236 ]);
237 }
238
245 public static function updateAgreementForSite(int $agreementId, array $fields): UpdateResult
246 {
247 return CookiesAgreement::update($agreementId, $fields);
248 }
249
256 public static function removeAgreementsForSite(int $siteId, ?string $code = null): void
257 {
258 $filter = [
259 'SITE_ID' => $siteId
260 ];
261 if ($code)
262 {
263 $filter['=CODE'] = $code;
264 }
265 $res = CookiesAgreement::getList([
266 'select' => [
267 'ID'
268 ],
269 'filter' => $filter
270 ]);
271 while ($row = $res->fetch())
272 {
273 CookiesAgreement::delete($row['ID'])->isSuccess();
274 }
275 }
276
283 public static function acceptAgreement(int $siteId, array $accepted = []): void
284 {
285 $agreementId = \Bitrix\Landing\Hook\Page\Cookies::getAgreementIdBySiteId($siteId);
286 if (!$agreementId)
287 {
288 return;
289 }
290
291 $agreement = self::getMainAgreement($agreementId);
292 if (!$agreement)
293 {
294 return;
295 }
296
297 $consentItems = [];
298 foreach ($accepted as $key)
299 {
300 $consentItems[] = [
301 'VALUE' => $key
302 ];
303 }
304 if (!$consentItems)
305 {
306 return;
307 }
308
310 $agreement['ID'],
311 self::USER_CONSENT_CODE,
312 $siteId,
313 [
314 'URL' => Site::getPublicUrl($siteId),
315 'ITEMS' => $consentItems
316 ]
317 );
318 }
319
326 public static function isSiteIncludesScript(int $siteId): bool
327 {
328 if (!$siteId)
329 {
330 return false;
331 }
332
333 // first check if site includes script
334 $res = HookDataTable::getList([
335 'select' => [
336 'ID'
337 ],
338 'filter' => [
339 '=HOOK' => 'HEADBLOCK',
340 '=CODE' => 'CODE',
341 '=ENTITY_ID' => $siteId,
342 '=ENTITY_TYPE' => Hook::ENTITY_TYPE_SITE,
343 '=PUBLIC' => 'N',
344 'VALUE' => '%<script%'
345 ]
346 ]);
347 if ($res->fetch())
348 {
349 return true;
350 }
351
352 // then check if any page of this site includes script
353 $res = HookDataTable::getList([
354 'select' => [
355 'ID'
356 ],
357 'filter' => [
358 '=HOOK' => 'HEADBLOCK',
359 '=CODE' => 'CODE',
360 '=ENTITY_TYPE' => Hook::ENTITY_TYPE_LANDING,
361 '=LANDING.SITE_ID' => $siteId,
362 '=PUBLIC' => 'N',
363 'VALUE' => '%<script%'
364 ],
365 'runtime' => [
366 new \Bitrix\Main\Entity\ReferenceField(
367 'LANDING',
368 '\Bitrix\Landing\Internals\LandingTable',
369 ['=this.ENTITY_ID' => 'ref.ID']
370 )
371 ]
372 ]);
373 if ($res->fetch())
374 {
375 return true;
376 }
377
378 // then check any blocks on the pages of the site
379 $res = BlockTable::getList([
380 'select' => [
381 'ID'
382 ],
383 'filter' => [
384 '=CODE' => 'html',
385 '=PUBLIC' => 'N',
386 '=ACTIVE' => 'Y',
387 '=LANDING.SITE_ID' => $siteId,
388 'CONTENT' => '%&lt;script%'
389 ],
390 'runtime' => [
391 new \Bitrix\Main\Entity\ReferenceField(
392 'LANDING',
393 '\Bitrix\Landing\Internals\LandingTable',
394 ['=this.LID' => 'ref.ID']
395 )
396 ]
397 ]);
398 if ($res->fetch())
399 {
400 return true;
401 }
402
403 return false;
404 }
405
410 public static function onUserConsentProviderList(): EventResult
411 {
412 $currentSite = [];
413
414 $parameters = [
415 [
416 'CODE' => self::USER_CONSENT_CODE,
417 'NAME' => 'Cookie',
418 'DATA' => function ($id = null) use(&$currentSite)
419 {
420 static $sites = [];
421
422 $id = intval($id);
423 if (!$id)
424 {
425 return null;
426 }
427
428 if (!isset($sites[$id]))
429 {
430 $sites[$id] = Site::getList([
431 'select' => [
432 'ID', 'TITLE'
433 ],
434 'filter' => [
435 'ID' => $id,
436 '=DELETED' => ['Y', 'N']
437 ],
438 'limit' => 1
439 ])->fetch();
440 }
441 if ($sites[$id])
442 {
443 $sites[$id]['URL'] = Site::getPublicUrl($id);
444 $sites[$id]['AGREEMENTS'] = self::getAgreements($id);
445 $currentSite = $sites[$id];
446 }
447 else
448 {
449 return null;
450 }
451
452 return [
453 'NAME' => $sites[$id]['TITLE'],
454 'URL' => $sites[$id]['URL']
455 ];
456 },
457 'ITEMS' => function ($code = null) use(&$currentSite)
458 {
459 if (!$currentSite)
460 {
461 $currentSite = [
462 'AGREEMENTS' => self::getAgreements(-1)
463 ];
464 }
465 return $currentSite['AGREEMENTS'][$code]['TITLE'] ?? $code;
466 },
467 ]
468 ];
469
470 return new EventResult(EventResult::SUCCESS, $parameters, 'landing');
471 }
472}
const ENTITY_TYPE_SITE
Definition hook.php:20
const ENTITY_TYPE_LANDING
Definition hook.php:25
static availableOnlyForZone(string $zone)
Definition manager.php:975
static getMainAgreement(?int $agreementId=null)
Definition cookies.php:45
static removeAgreementsForSite(int $siteId, ?string $code=null)
Definition cookies.php:256
static getAgreements(int $siteId, bool $viewMode=false)
Definition cookies.php:125
static addAgreementForSite(int $siteId, array $fields)
Definition cookies.php:228
static getCookieType(string $code)
Definition cookies.php:107
static isSiteIncludesScript(int $siteId)
Definition cookies.php:326
static updateAgreementForSite(int $agreementId, array $fields)
Definition cookies.php:245
static acceptAgreement(int $siteId, array $accepted=[])
Definition cookies.php:283
static getPublicUrl($id, bool $full=true, bool $hostInclude=true, bool $previewForNotActive=false)
Definition site.php:77
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29