Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sender.php
1<?php
3
5
10final class Sender
11{
12 protected const URL = 'https://util.1c-bitrix.ru/analytics.php';
13
15 private $type;
16
18 private $data;
19
24 public function __construct(string $type, array $data)
25 {
26 $this->type = $type;
27 $this->data = $data;
28 }
29
33 public function send(): bool
34 {
35 if ($this->data)
36 {
37 $postData = $this->getCommonData();
38 $postData['content'] = $this->data;
39 $postData['bx_hash'] = self::signRequest($postData);
40 $postData = Main\Web\Json::encode($postData);
41
42 $httpClient = new Main\Web\HttpClient();
43 $response = $httpClient->post(self::URL, $postData);
44 if (!$response || $httpClient->getStatus() !== 200)
45 {
46 return false;
47 }
48
49 try
50 {
51 $response = Main\Web\Json::decode($response);
52 if ($response['result'] !== 'ok')
53 {
54 return false;
55 }
56 }
57 catch (Main\ArgumentException $ex)
58 {
59 return false;
60 }
61 }
62
63 return true;
64 }
65
69 private function getCommonData(): array
70 {
71 $isB24 = self::isB24();
72 $data = [
73 'type' => $isB24 ? 'b24' : 'self_hosted',
74 'date_time' => (new Main\Type\DateTime())->format('Y-m-d H:i:s'),
75 'transaction_type' => $this->type,
76 'host_name' => self::getHostName(),
77 ];
78
79 if($isB24)
80 {
81 $data['tariff'] = \CBitrix24::getLicensePrefix();
82 }
83 else
84 {
85 $data['license_key'] = Main\Analytics\Counter::getAccountId();
86 }
87
88 return $data;
89 }
90
95 private static function signRequest(array $request): string
96 {
97 $requestHash = md5(serialize($request));
98
99 if (Main\Loader::includeModule('bitrix24'))
100 {
101 return \CBitrix24::RequestSign($requestHash);
102 }
103
104 $privateKey = Main\Analytics\Counter::getPrivateKey();
105 return md5($requestHash.$privateKey);
106 }
107
111 private static function getHostName(): string
112 {
113 if (self::isB24())
114 {
115 $hostName = BX24_HOST_NAME;
116 }
117 else
118 {
119 $hostName = Main\Config\Option::get('main', 'server_name');
120 if (!$hostName)
121 {
122 $hostName = (defined('SITE_SERVER_NAME') && !empty(SITE_SERVER_NAME)) ? SITE_SERVER_NAME : '';
123 }
124
125 if (!$hostName)
126 {
127 $request = Main\Context::getCurrent()->getRequest();
128 $hostName = $request->getHttpHost();
129 }
130 }
131
132 return (string)$hostName;
133 }
134
138 private static function isB24(): bool
139 {
140 return Main\Loader::includeModule('bitrix24');
141 }
142}
__construct(string $type, array $data)
Definition sender.php:24