Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mfmsru.php
1<?php
3
10
14
16
18{
19 public const ID = 'mfmsru';
20
21 public static function isSupported()
22 {
23 return (
25 || Option::get('messageservice', 'mfmsru_enabled', 'N') === 'Y'
26 );
27 }
28
29 public function getId()
30 {
31 return static::ID;
32 }
33
34 public function getName()
35 {
36 return 'mfms.ru';
37 }
38
39 public function getShortName()
40 {
41 return 'mfms.ru';
42 }
43
44 public function getFromList()
45 {
46 $from = $this->getOption('from_list');
47 return is_array($from) ? $from : [];
48 }
49
50 public function isRegistered()
51 {
52 return (
53 $this->getOption('login') !== null
54 &&
55 $this->getOption('password') !== null
56 );
57 }
58
59 public function register(array $fields)
60 {
61 $login = (string) $fields['login'];
62 $password = (string) $fields['password'];
63 $from = (string) $fields['from_list'];
64 $sendUrl = (string) $fields['hpg_send_url'];
65 $statusUrl = (string) $fields['hpg_status_url'];
66
67 $result = new Result();
68 if ($login && $password && $from && $sendUrl && $statusUrl)
69 {
70 $this->setOption('login', $login);
71 $this->setOption('password', $password);
72 $this->setOption('hpg_send_url', $sendUrl);
73 $this->setOption('hpg_status_url', $statusUrl);
74
75 $from = array_map(function($v) {
76 $v = trim($v);
77 return ['id' => $v, 'name' => $v];
78 }, explode(';', $from));
79
80 $this->setOption('from_list', $from);
81 }
82 else
83 {
84 $result->addError(new Error('Empty required fields.'));
85 }
86
87 return $result;
88 }
89
95 public function getOwnerInfo()
96 {
97 return array(
98 'login' => $this->getOption('login'),
99 'fromList' => $this->getFromList(),
100 );
101 }
102
103 public function getExternalManageUrl()
104 {
105 return 'https://mfms.ru';
106 }
107
108 public function sendMessage(array $messageFields)
109 {
110 if (!$this->canUse())
111 {
112 $result = new SendMessage();
113 $result->addError(new Error('Service is unavailable'));
114 return $result;
115 }
116
117 return $this->sendViaHpg($messageFields);
118 }
119
120 public function getMessageStatus(array $messageFields)
121 {
122 if (!$this->canUse())
123 {
124 $result = new MessageStatus();
125 $result->addError(new Error('Service is unavailable'));
126 return $result;
127 }
128
129 return $this->getStatusViaHpg($messageFields);
130 }
131
132 public static function resolveStatus($serviceStatus)
133 {
134 $status = parent::resolveStatus($serviceStatus);
135
136 switch ($serviceStatus)
137 {
138 case 'delayed':
139 case 'enqueued':
140 return MessageService\MessageStatus::ACCEPTED;
141 break;
142 case 'sent':
143 return MessageService\MessageStatus::SENT;
144 break;
145 case 'delivered':
146 return MessageService\MessageStatus::DELIVERED;
147 break;
148 case 'undelivered':
149 return MessageService\MessageStatus::UNDELIVERED;
150 break;
151 case 'cancelled':
152 case 'failed':
153 return MessageService\MessageStatus::FAILED;
154 break;
155 }
156
157 return $status;
158 }
159
160 private function sendViaHpg(array $messageFields)
161 {
162 $result = new SendMessage();
163
164 $params = [
165 'login' => $this->getOption('login'),
166 'pass' => $this->getOption('password'),
167 'subject' => $messageFields['MESSAGE_FROM'],
168 'address' => str_replace('+', '', $messageFields['MESSAGE_TO']),
169 'text' => $this->prepareMessageBodyForSend($messageFields['MESSAGE_BODY']),
170 ];
171
172 $remoteCallResult = $this->touchHpg($this->getOption('hpg_send_url'), $params);
173 $result->setServiceRequest($remoteCallResult->getHttpRequest());
174 $result->setServiceResponse($remoteCallResult->getHttpResponse());
175 if (!$remoteCallResult->isSuccess())
176 {
177 $result->addErrors($remoteCallResult->getErrors());
178 }
179 else
180 {
181 $answer = $remoteCallResult->getData();
182 [$code, $index, $msgId] = $answer;
183
184 if ($msgId)
185 {
186 $result->setExternalId($msgId);
187 $result->setStatus(MessageService\MessageStatus::SENT);
188 }
189 }
190
191 return $result;
192 }
193
194 private function getStatusViaHpg(array $messageFields)
195 {
196 $result = new MessageStatus();
197 $result->setId($messageFields['ID']);
198 $result->setExternalId($messageFields['EXTERNAL_ID']);
199
200 $params = [
201 'login' => $this->getOption('login'),
202 'password' => $this->getOption('password'),
203 'providerId' => [$messageFields['EXTERNAL_ID']],
204 ];
205
206 $remoteCallResult= $this->touchHpg($this->getOption('hpg_status_url'), $params);
207
208 if (!$remoteCallResult->isSuccess())
209 {
210 $result->addErrors($remoteCallResult->getErrors());
211 }
212 else
213 {
214 $answer = $remoteCallResult->getData();
215 [$code, $msgId, $status, $date, $reason] = $answer;
216
217 if ($msgId)
218 {
219 $result->setStatusText($status);
220 $result->setStatusCode(self::resolveStatus($status));
221 }
222 }
223
224 return $result;
225 }
226
227 private function touchHpg($url, array $params): Sender\Result\HttpRequestResult
228 {
229 $result = new Sender\Result\HttpRequestResult();
230 if (!Application::getInstance()->isUtfMode())
231 {
232 $params = \Bitrix\Main\Text\Encoding::convertEncoding($params, SITE_CHARSET, 'UTF-8');
233 }
234
235 $httpClient = new HttpClient(array(
236 "socketTimeout" => $this->socketTimeout,
237 "streamTimeout" => $this->streamTimeout,
238 "waitResponse" => true,
239 ));
240 $httpClient->setHeader('User-Agent', 'Bitrix24');
241 $httpClient->setCharset('UTF-8');
242
243 $answer = '';
244
245 $url .= '?'.http_build_query($params);
246
247 if ($httpClient->query(HttpClient::HTTP_GET, $url))
248 {
249 $answer = $httpClient->getResult();
250 }
251
252 if ($httpClient->getStatus() != '200')
253 {
254 $result->addError(new Error($answer));
255 }
256 else
257 {
258 $status = explode(';', $answer)[0];
259 if ($status !== 'ok')
260 {
261 $result->addError(new Error($status));
262 }
263 }
264
265 $result->setHttpRequest(new MessageService\DTO\Request([
266 'method' => HttpClient::HTTP_GET,
267 'uri' => $url,
268 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
269 ]));
270 $result->setHttpResponse(new MessageService\DTO\Response([
271 'statusCode' => $httpClient->getStatus(),
272 'headers' => $httpClient->getHeaders()->toArray(),
273 'body' => $answer,
274 'error' => Sender\Util::getHttpClientErrorString($httpClient)
275 ]));
276 $result->setData(explode(';', $answer));
277
278 return $result;
279 }
280}
static isModuleInstalled($moduleName)
getOption($optionName, $defaultValue=null)
prepareMessageBodyForSend(string $text)
Definition base.php:176
sendMessage(array $messageFields)
Definition mfmsru.php:108
static resolveStatus($serviceStatus)
Definition mfmsru.php:132
getMessageStatus(array $messageFields)
Definition mfmsru.php:120
static getHttpClientErrorString(HttpClient $httpClient)
Definition util.php:24