Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
consentbuildermail.php
1<?php
2
4
14
16{
17 const CODE = iBase::CODE_MAIL;
18 const REQUIRED_FIELDS = ['RECIPIENT_ID', 'CONTACT_ID', 'CONTACT_CODE', 'SITE_ID'];
19 const CONSENT_EVENT = "SENDER_CONSENT";
20 protected const APPLY = 1;
21 protected const REJECT = 2;
22 protected const URI = "/pub/mail/consent.php";
23
24 protected static function getFilter(): \Closure
25 {
26 return function ($item, $key)
27 {
28 return isset($item, $key) && in_array($key, static::REQUIRED_FIELDS);
29 };
30 }
31
32 protected static function filterFields(array $fieldForConsent): array
33 {
34 return array_filter($fieldForConsent, static::getFilter(), ARRAY_FILTER_USE_BOTH);
35 }
36
37 protected static function checkRequireFields($fields): bool
38 {
39 foreach (static::REQUIRED_FIELDS as $field)
40 {
41 if (!array_key_exists($field, $fields))
42 {
43 return false;
44 }
45 }
46 return true;
47 }
48
52 public function buildMessage(): array
53 {
54 ['CONTACT_CODE' => $code, 'SITE_ID' => $siteId] = $this->fields;
55 $typeId = Recipient\Type::detect($code);
56 $code = Recipient\Normalizer::normalize($code, $typeId);
57 return [
58 "EVENT_NAME" => static::CONSENT_EVENT,
59 "C_FIELDS" => [
60 "EMAIL" => $code,
61 "SENDER_CONSENT_APPLY" => $this->buildLinkApply(),
62 "SENDER_CONSENT_REJECT" => $this->buildLinkReject(),
63 ],
64 "LID" => is_array($siteId) ? implode(",", $siteId) : $siteId,
65 ];
66 }
67
68 protected function buildLinkApply(): string
69 {
70 return static::buildLink([
71 'CODE' => $this->fields['CONTACT_CODE'] ?? '',
72 'CONTACT' => $this->fields['CONTACT_ID'] ?? '',
73 'RECIPIENT' => $this->fields['RECIPIENT_ID'] ?? '',
74 'POSTING' => $this->fields['POSTING_ID'] ?? '',
75 'CONSENT' => $this->fields['CONSENT_ID'] ?? '',
76 ], $this->fields['SITE_ID'] ?? '', static::APPLY);
77 }
78
79 protected function buildLinkReject(): string
80 {
81 return static::buildLink([
82 'CODE' => $this->fields['CONTACT_CODE'] ?? '',
83 'CONTACT' => $this->fields['CONTACT_ID'] ?? '',
84 'RECIPIENT' => $this->fields['RECIPIENT_ID'] ?? '',
85 'POSTING' => $this->fields['POSTING_ID'] ?? '',
86 'CONSENT' => $this->fields['CONSENT_ID'] ?? '',
87 ], $this->fields['SITE_ID'], static::REJECT);
88 }
89
90 protected static function buildQuery(array $fields): string
91 {
92 return http_build_query($fields);
93 }
94
95 protected static function buildLink($fields, $siteId, $type): string
96 {
97 $tag = Consent::encodeTag($fields);
98 $dir = static::getLink($siteId);
99 switch ($type)
100 {
101 case static::APPLY:
102 $result = $dir . '?' . static::buildQuery(['consent' => 'apply', 'type' => static::CODE, 'tag' => $tag]);
103 break;
104 case static::REJECT:
105 $result = $dir . '?' . static::buildQuery(['consent' => 'reject', 'type' => static::CODE, 'tag' => $tag]);
106 break;
107 default:
108 throw new \InvalidArgumentException("Type is out of range");
109 }
110 return $result;
111 }
112
113 protected static function checkUri($siteId): bool
114 {
115 return $siteId && File::isFileExists(SiteTable::getDocumentRoot($siteId) . DIRECTORY_SEPARATOR . static::URI);
116 }
117
118 protected static function getLink($siteId): ?string
119 {
120 $uri = null;
121 if (static::checkUri($siteId))
122 {
123 $uri = static::URI;
124 // exclude com.br & com.de domains
125 if (
126 Service::isCloud()
127 && defined('BX24_HOST_NAME')
128 && !in_array(mb_substr(BX24_HOST_NAME, -7), ['.com.br', '.com.de'])
129 )
130 {
131 $domain = BX24_HOST_NAME;
132 if (!\CBitrix24::isCustomDomain())
133 {
134 $domain = preg_replace('/^([-\.\w]+)\.bitrix24\.([-\.\w]+)/', '$2.$1', $domain);
135 $domain = "mailinternetsub.com/" . $domain;
136 }
137 $uri = "https://$domain$uri";
138 }
139 }
140 return $uri;
141 }
142}