27 const onRead =
'OnMailEventMailRead';
40 public static function getTag($moduleId, $fields)
43 $moduleId = str_replace(
".",
"--", $moduleId);
44 return $moduleId .
"." . base64_encode(json_encode($fields));
55 $data = explode(
".", $tag);
56 $moduleId = str_replace(
"--",
".", $data[0]);
59 return array(
'MODULE_ID' => $moduleId,
'FIELDS' => (array) json_decode(base64_decode(implode(
'.', $data))));
72 $tag = static::getTag($moduleId, $fields);
74 return $signer->
sign($tag, static::SIGN_SALT_ACTION);
90 $unsignedTag = $signer->
unsign($signedTag, static::SIGN_SALT_ACTION);
91 return static::parseTag($unsignedTag);
97 $signer->setKey(self::getSignKey());
98 $unsignedTag = $signer->unsign($signedTag, static::SIGN_SALT_ACTION);
99 return static::parseTag($unsignedTag);
111 public static function getLinkRead($moduleId, $fields, $urlPage =
null)
113 return static::getTaggedLink(
114 static::getTag($moduleId, $fields),
129 public static function getLinkClick($moduleId, $fields, $urlPage =
null)
131 return static::getTaggedLink(
132 static::getTag($moduleId, $fields),
148 public static function getLinkUnsub($moduleId, $fields, $urlPage =
null)
150 return static::getTaggedLink(
151 static::getSignedTag($moduleId, $fields),
169 $uri .=
"/tools/track_mail_$opCode.php";
172 $uri = $uri . (strpos($uri,
"?") ===
false ?
"?" :
"&");
173 $uri .=
'tag=' . urlencode($tag);
187 static $cached = array();
188 foreach ($cached as $cache)
190 if ($cache[0] == $value)
197 $sign = $signer->
getSignature($value, static::SIGN_SALT_ACTION);
199 $cached[] = array($value, $sign);
200 if (count($cached) > 10)
202 array_shift($cached);
220 $result = $signer->
validate($value, $signature, static::SIGN_SALT_ACTION);
229 return self::validateSignWithStoredKey($value, $signature);
235 private static function validateSignWithStoredKey($value, $signature)
240 $key = self::getSignKey();
244 $signer->setKey($key);
247 return $signer->validate($value, $signature, static::SIGN_SALT_ACTION);
249 catch (BadSignatureException $exception)
255 private static function getSignKey()
257 $key = Config\Option::get(
'sender', self::CUSTOM_SIGNER_KEY,
null);
260 $key = Config\Option::get(
'main',
'signer_default_key',
null);
263 Config\Option::set(
'sender', self::CUSTOM_SIGNER_KEY, $key);
278 $subscription = array();
280 if(array_key_exists(
'MODULE_ID', $data))
281 $filter = array($data[
'MODULE_ID']);
285 if(!is_array($data[
'FIELDS']))
return false;
287 $event =
new Main\Event(
"main",
"OnMailEventSubscriptionList", array($data[
'FIELDS']), $filter);
289 foreach ($event->getResults() as $eventResult)
291 if ($eventResult->getType() == EventResult::ERROR)
296 $subscriptionList = $eventResult->getParameters();
297 if($subscriptionList && is_array($subscriptionList[
'LIST']))
299 $subscription = array_merge(
301 array($eventResult->getModuleId() => $subscriptionList[
'LIST'])
306 if (empty($data[
'MODULE_ID']) || $data[
'MODULE_ID'] ===
'main')
308 if (empty($subscription[
'main']))
310 $subscription[
'main'] = [];
312 $subscription[
'main'] = array_merge(
313 $subscription[
'main'],
318 if(array_key_exists(
'MODULE_ID', $data))
319 $subscription = $subscription[$data[
'MODULE_ID']];
321 return $subscription;
332 if(!is_array($data[
'FIELDS']))
return false;
334 $event =
new Main\Event(
"main",
"OnMailEventSubscriptionEnable", array($data[
'FIELDS']), array($data[
'MODULE_ID']));
336 foreach ($event->getResults() as $eventResult)
338 if ($eventResult->getType() == EventResult::ERROR)
355 if(!is_array($data[
'FIELDS']))
return false;
357 $event =
new Main\Event(
"main",
"OnMailEventSubscriptionDisable", array($data[
'FIELDS']), array($data[
'MODULE_ID']));
359 foreach ($event->getResults() as $eventResult)
361 if ($eventResult->getType() == EventResult::ERROR)
367 if (!empty($data[
'MODULE_ID']) && $data[
'MODULE_ID'] ===
'main')
381 public static function click(array $data)
383 if (Main\
Config\Option::get(
'main',
'track_outgoing_emails_click',
'Y') !=
'Y')
388 if(array_key_exists(
'MODULE_ID', $data))
389 $filter = array($data[
'MODULE_ID']);
393 $event =
new Main\Event(
"main",
"OnMailEventMailClick", array($data[
'FIELDS']), $filter);
395 foreach ($event->getResults() as $eventResult)
397 if ($eventResult->getType() == EventResult::ERROR)
413 $request = Main\Context::getCurrent()->getRequest();
414 $url = $request->get(
'url');
415 $sign = $request->get(
'sign');
416 $tag = $request->get(
'tag');
422 $tag = static::parseTag($tag);
423 $tag[
'FIELDS'][
'IP'] = $request->getRemoteAddress();
424 $tag[
'FIELDS'][
'URL'] = $url;
433 $isValidate = static::validateSign($url, $sign);
434 $skipSecCheck = ($sign && $url && $isValidate);
438 LocalRedirect($url, $skipSecCheck);
442 ShowError(
'Failed to verify the security of the url address');
453 $request = Main\Context::getCurrent()->getRequest();
454 $tag = $request->get(
'tag');
462 $data = static::parseTag($tag);
463 $data[
'FIELDS'][
'IP'] = $request->getRemoteAddress();
464 return static::read($data);
478 public static function read(array $data)
480 if (Main\
Config\Option::get(
'main',
'track_outgoing_emails_read',
'Y') !=
'Y')
485 if(array_key_exists(
'MODULE_ID', $data))
486 $filter = array($data[
'MODULE_ID']);
490 $event =
new Main\Event(
"main",
"OnMailEventMailRead", array($data[
'FIELDS']), $filter);
492 foreach ($event->getResults() as $eventResult)
494 if ($eventResult->getType() == EventResult::ERROR)
511 if($callbackResult->getModuleId())
513 $filter = [$callbackResult->getModuleId()];
520 $event =
new Main\Event(
"main", self::onChangeStatus, [$callbackResult], $filter);
522 foreach ($event->getResults() as $eventResult)
524 if ($eventResult->getType() == EventResult::ERROR)
static onMailEventSubscriptionList(array $data)
static onMailEventSubscriptionDisable(array $data)
static changeStatus(Callback\Result $callbackResult)
static getTag($moduleId, $fields)
static getLinkClick($moduleId, $fields, $urlPage=null)
static getLinkUnsub($moduleId, $fields, $urlPage=null)
static click(array $data)
static clickFromRequest()
static getSignedTag($moduleId, $fields)
static unsubscribe($data)
static getLinkRead($moduleId, $fields, $urlPage=null)
static parseSignedTag($signedTag)
static getTaggedLink($tag, $opCode, $uri=null)
static getSubscriptionList($data)
static validateSign($value, $signature)
validate($value, $signature, $salt=null)
getSignature($value, $salt=null)
unsign($signedValue, $salt=null)