Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tester.php
1<?php
9
16
25
26Loc::loadMessages(__FILE__);
27
28class Tester
29{
30 public const MAX_LAST_CODES = 6;
31 public const MAX_SEND_CODES = 15;
32
34 protected $message;
35
37 protected static $userOptionLastCodesName = 'last_codes';
38
39
45 public function __construct(Adapter $message)
46 {
47 return $this->message = $message;
48 }
49
55 public function isSupport()
56 {
57 $isSupport = in_array(
58 $this->message->getCode(),
59 array(
64 //Message::CODE_WEB_HOOK,
65 )
66 );
67
68 if ($isSupport)
69 {
70 $isSupport = $this->getRecipientType() !== null;
71 }
72
73 return $isSupport;
74 }
75
81 public function getRecipientType()
82 {
83 static $type = false;
84 if ($type === false)
85 {
86 $types = $this->message->getSupportedRecipientTypes();
87
88 $type = current($types);
89 $type = $type ?: null;
90 }
91
92 return $type;
93 }
94
100 protected function getDefaultCode()
101 {
102 $code = null;
103 switch ($this->getRecipientType())
104 {
105 case Recipient\Type::EMAIL:
106 if (!is_object($GLOBALS['USER']))
107 {
108 return null;
109 }
110
111 $code = $GLOBALS['USER']->getEmail();
112 break;
113 case Recipient\Type::PHONE:
114 if (!is_object($GLOBALS['USER']))
115 {
116 return null;
117 }
118
119 $u = UserTable::getRowById($GLOBALS['USER']->getID());
120 $code = $u['PERSONAL_MOBILE'] ?: $u['WORK_PHONE'] ?: $u['PERSONAL_PHONE'] ?: null;
121 break;
122 }
123
124 return Recipient\Normalizer::normalize($code, $this->getRecipientType());
125 }
126
127
131 protected function getUserOptionLastCodesName()
132 {
133 return self::$userOptionLastCodesName . '_' . Recipient\Type::getCode($this->getRecipientType());
134 }
135
141 protected function getEmailToMeList()
142 {
143 $addressToList = [];
144 $email = Option::get('sender', 'address_send_to_me');
145 if(!empty($email))
146 {
147 $addressToList = explode(',', $email);
148 $addressToList = array_unique($addressToList);
149 \TrimArr($addressToList, true);
150 }
151
152 return $addressToList;
153 }
154
160 public function getLastCodes()
161 {
162 $codes = \CUserOptions::getOption('sender', $this->getUserOptionLastCodesName(), array());
163 $codes = is_array($codes) ? $codes : array();
164 $codes = $this->prepareCodes($codes);
165 $code = $this->getDefaultCode();
166 if ($code && !in_array($code, $codes))
167 {
168 $codes = $this->cutCodes($codes, true);
169 $codes[] = $code;
170 }
171
172 $codes = $this->prepareCodes(
173 array_merge($codes, $this->getEmailToMeList()),
174 false
175 );
176
177 return $codes;
178 }
179
186 protected function setLastCodes(array $list)
187 {
188 \CUserOptions::setOption(
189 'sender',
191 $this->prepareCodes($list)
192 );
193 }
194
201 protected function addLastCode($code)
202 {
203 $code = Recipient\Normalizer::normalize((string) $code, $this->getRecipientType());
204 if (!$code)
205 {
206 return false;
207 }
208
209 $this->setLastCodes(array_merge(array($code), $this->getLastCodes()));
210
211 return true;
212 }
213
221 protected function cutCodes(array $codes, $isRemoveLast = false)
222 {
223 $length = (int) Option::get('sender', 'max_last_codes', 0);
224 $length = $length > 0 ? $length : self::MAX_LAST_CODES;
225 if ($isRemoveLast)
226 {
227 $length -= 1;
228 }
229 return array_slice($codes, 0, $length);
230 }
231
239 protected function prepareCodes(array $codes, $doCut = true)
240 {
241 $result = array();
242 foreach ($codes as $code)
243 {
244 $code = Recipient\Normalizer::normalize((string) $code, $this->getRecipientType());
245 if (!$code)
246 {
247 continue;
248 }
249
250 $result[] = $code;
251 }
252
253
254 $result = array_unique($result);
255 if ($doCut)
256 {
257 $result = $this->cutCodes($result);
258 }
259
260 return $result;
261 }
262
270 public function send(array $codes, array $parameters)
271 {
272 $result = new Result();
273 if (!$this->isSupport())
274 {
275 $result->addError(new Error("Testing not supported."));
276 return $result;
277 }
278
279 // agreement accept check
280 if(!Security\User::current()->isAgreementAccepted())
281 {
282 $result->addError(new Error(Security\Agreement::getErrorText(), 'NEED_ACCEPT_AGREEMENT'));
283 return $result;
284 }
285
286 $campaignId = isset($parameters['CAMPAIGN_ID']) ? $parameters['CAMPAIGN_ID'] : Entity\Campaign::getDefaultId(SITE_ID);
287 $name = isset($parameters['NAME']) ? $parameters['NAME'] : null;
288 $name = $name ?: $GLOBALS['USER']->getFirstName();
289 $userId = isset($parameters['USER_ID']) ? $parameters['USER_ID'] : null;
290 $userId = $userId ?: $GLOBALS['USER']->getID();
291 $fields = isset($parameters['FIELDS']) ? $parameters['FIELDS'] : array();
292
293 $this->message->getTransport()->start();
294
295 $count = 0;
296 foreach ($codes as $code)
297 {
298 if (self::MAX_SEND_CODES && $count++ >= self::MAX_SEND_CODES)
299 {
300 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_MAX_COUNT', ['%count%' => self::MAX_SEND_CODES])));
301 return $result;
302 }
303
304 if ($this->message->getTransport()->isLimitsExceeded($this->message))
305 {
306 $limiter = $this->message->getTransport()->getExceededLimiter();
307
308 // special message for portal verification
309 if (($limiter instanceof CountLimiter) && $limiter->getName() === 'portal_verify')
310 {
311 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_LIMIT_PORTAL_VERIFY_FAILED', array('%name%' => $code))));
312 return $result;
313 }
314
315 if (!($limiter instanceof TimeLimiter))
316 {
317 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_LIMIT_EXCEEDED', array('%name%' => $code))));
318 return $result;
319 }
320 }
321
322 if (Integration\Bitrix24\Service::isCloud())
323 {
324 if ($this->message->getCode() === Message\IBase::CODE_MAIL) {
325 $verifyLimit = Integration\Bitrix24\Limitation\PortalVerifyLimit::instance();
326 if ($verifyLimit->getCurrent() >= $verifyLimit->getLimit())
327 {
328 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_LIMIT_PORTAL_VERIFY_FAILED', array('%name%' => $code))));
329 return $result;
330 }
331 }
332
333 $testerDailyLimit = Integration\Bitrix24\Limitation\TesterDailyLimit::instance();
334 if ($testerDailyLimit->getCurrent() >= $testerDailyLimit->getLimit())
335 {
336 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_LIMIT_EXCEEDED', array('%name%' => $code))));
337 return $result;
338 }
339 }
340
341 $type = Recipient\Type::detect($code);
342 if ($type)
343 {
344 $code = Recipient\Normalizer::normalize($code, $type);
345 }
346 if (!$type || !$code)
347 {
348 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_WRONG_RECIPIENT', array('%name%' => $code))));
349 continue;
350 }
351
352 $recipient = array(
353 'ID' => 0,
354 'CAMPAIGN_ID' => $campaignId,
355 'CONTACT_CODE' => $code,
356 'CONTACT_TYPE' => $type,
357 'NAME' => $name,
358 'USER_ID' => $userId,
359 'FIELDS' => $fields,
360 );
361
362 Posting\Sender::applyRecipientToMessage($this->message, $recipient, true);
363 try
364 {
365 $sendResult = $this->message->send();
366 if (!$sendResult && $result->isSuccess())
367 {
368 $to = $this->message->getRecipientCode();
369 $result->addError(new Error(Loc::getMessage('SENDER_MESSAGE_TESTER_ERROR_SENT', array('%name%' => $to))));
370 }
371
372 if ($sendResult)
373 {
374 $this->addLastCode($code);
375 if (Integration\Bitrix24\Service::isCloud() && $this->message->getCode() === Message\iBase::CODE_MAIL)
376 {
377 Integration\Bitrix24\Limitation\DailyLimit::increment();
378 Integration\Bitrix24\Limitation\TesterDailyLimit::increment();
379 }
380 }
381 }
382 catch(SystemException $e)
383 {
384 $result->addError(new Error($e->getMessage()));
385 break;
386 }
387 }
388 $this->message->getTransport()->end();
389
390 return $result;
391 }
392}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct(Adapter $message)
Definition tester.php:45
send(array $codes, array $parameters)
Definition tester.php:270
prepareCodes(array $codes, $doCut=true)
Definition tester.php:239
cutCodes(array $codes, $isRemoveLast=false)
Definition tester.php:221
$GLOBALS['____1444769544']
Definition license.php:1