Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
connection.php
1<?
2namespace Bitrix\B24Connector;
3
11
12Loc::loadMessages(__FILE__);
13
19{
20 const DEFAULT_CACHE_TTL = 3600;
21
26 private static function getAppId()
27 {
28 if(!Loader::includeModule('socialservices'))
29 return '';
30
31 if(!self::isLinkedToNet())
32 self::linkToNet();
33
34 $interface = new \CBitrix24NetOAuthInterface();
35 return $interface->getAppID();
36 }
37
45 private static function linkToNet()
46 {
47 if(!Loader::includeModule('socialservices'))
48 return false;
49
50 if(self::isLinkedToNet())
51 return true;
52
53 $result = false;
54 $request = \Bitrix\Main\Context::getCurrent()->getRequest();
55 $host = ($request->isHttps() ? 'https://' : 'http://').$request->getHttpHost();
56 $registerResult = \CSocServBitrix24Net::registerSite($host);
57
58 if(is_array($registerResult) && isset($registerResult["client_id"]) && isset($registerResult["client_secret"]))
59 {
60 Option::set('socialservices', 'bitrix24net_domain', $host);
61 Option::set('socialservices', 'bitrix24net_id', $registerResult["client_id"]);
62 Option::set('socialservices', 'bitrix24net_secret', $registerResult["client_secret"]);
63 $result = true;
64 }
65
66 return $result;
67 }
68
73 private static function isLinkedToNet()
74 {
75 return Option::get('socialservices', 'bitrix24net_id', '') !== '';
76 }
77
84 public static function delete()
85 {
86 $result = new Result();
87
88 if(!Loader::includeModule('socialservices'))
89 {
90 $result->addError(new Error('Module socialservices is not installed'));
91 return $result;
92 }
93
94 if($connection = self::getFields())
95 {
96 $res = ApTable::delete($connection['ID']);
97
98 if(!$res->isSuccess())
99 $result->addErrors($res->getErrors());
100
101 $dbRes = ButtonTable::getList(array(
102 'filter' => array(
103 '=APP_ID' => $connection['ID']
104 )
105 ));
106
107 while($but = $dbRes->fetch())
108 {
109 $res = ButtonTable::delete($but['ID']);
110
111 if(!$res->isSuccess())
112 $result->addErrors($res->getErrors());
113 }
114 }
115
116 return $result;
117 }
118
125 public static function getButtonHtml($title = '')
126 {
127 global $APPLICATION;
128 $onclick = '';
129 $class = 'connector-btn-blue';
130 $href = 'javascript:void(0)';
131 $moduleAccess = $APPLICATION->GetGroupRight('b24connector');
132
133 if($title == '')
134 $title = Loc::getMessage('B24C_CONN_BUTT_CONNECT');
135
136 if(!Loader::includeModule('socialservices') || $moduleAccess <= "R")
137 {
138 $class .= ' connector-btn-blue-disabled';
139 }
140 else
141 {
142 if(!self::isLinkedToNet())
143 self::linkToNet();
144
145 $hosts = self::getHostsList();
146
147 if(!empty($hosts))
148 {
149 $urlTeml = self::getUrl('##HOST##');
150
151 if(!empty($urlTeml))
152 {
153 $onclick = 'BX.B24Connector.showPortalChoosingDialog(\''.\CUtil::JSEscape($urlTeml).'\', '.\CUtil::PhpToJSObject($hosts).');';
154 }
155 else
156 {
157 $onclick = 'alert(\''.Loc::getMessage('B24C_CONN_CONNECT_ERROR').'\');';
158 }
159 }
160 else
161 {
162 $href = self::getUrlNet();
163 }
164 }
165
166 $result = '<a href="'.htmlspecialcharsbx($href).'"'.
167 ($onclick <> '' ? ' onclick="'.$onclick.'"' : '').
168 ' class="'.$class.'" >'.
169 $title.'</a>';
170
171 return $result;
172 }
173
180 public static function getOptionButtonHtml($title)
181 {
182 $onclick = '';
183 $disabled = false;
184
185 if(!\Bitrix\Main\Loader::includeModule('socialservices'))
186 {
187 $disabled = true;
188 }
189 else
190 {
191 if(!self::isLinkedToNet())
192 self::linkToNet();
193
194 $hosts = self::getHostsList();
195
196 if(!empty($hosts))
197 {
198 $urlTeml = self::getUrl('##HOST##');
199
200 if(!empty($urlTeml))
201 {
202 $onclick = 'BX.B24Connector.showPortalChoosingDialog(\''.\CUtil::JSEscape($urlTeml).'\', '.\CUtil::PhpToJSObject($hosts).');';
203 }
204 else
205 {
206 $onclick = 'alert(\''.\CUtil::JSEscape(Loc::getMessage('B24C_CONN_CONNECT_ERROR')).'\');';
207 }
208 }
209 else
210 {
211 $onclick = 'window.location.href="'.\CUtil::JSEscape(self::getUrlNet()).'"';
212 }
213 }
214
215 return '<input type="button" onclick="'.htmlspecialcharsbx($onclick).'" value="'.$title.'"'.($disabled ? ' disabled' : '').'>';
216 }
217
222 public static function getFields()
223 {
224 static $result = null;
225
226 if($result === null)
227 {
228 $result = array();
229
230 if(Loader::includeModule('socialservices'))
231 $result = ApTable::getConnection();
232 }
233
234 return is_array($result) ? $result : array();
235 }
236
241 public static function getDomain()
242 {
243 $fields = self::getFields();
244 return !empty($fields['DOMAIN']) ? $fields['DOMAIN'] : '';
245 }
246
251 public static function isExist()
252 {
253 $fields = self::getFields();
254 return !empty($fields);
255 }
256
262 public static function isRestAvailable(): bool
263 {
264 static $result = null;
265
266 if ($result === null)
267 {
268 if (!Loader::includeModule('socialservices'))
269 {
270 $result = false;
271 }
272 elseif (!$client = ApClient::init())
273 {
274 $result = false;
275 }
276 else
277 {
278 $cacheId = 'b24connector_rest_status';
279 $cached = Cache::remember($cacheId, self::DEFAULT_CACHE_TTL, function () use ($client)
280 {
281 $response = $client->call('app.info');
282 return (isset($response['result']) && empty($response['error']));
283 });
284 $result = (bool)$cached;
285 }
286 }
287
288 return $result;
289 }
290
296 private static function getUrl($host)
297 {
298 global $APPLICATION;
299
300 if($host == '')
301 return '';
302
303 if(!Loader::includeModule("socialservices"))
304 return '';
305
306 $result = '';
307 $appId = self::getAppID();
308
309 if($appId <> '')
310 {
311 $result = $host.'apconnect/?client_id='.urlencode($appId).'&preset=ap&state='.urlencode(http_build_query(array(
312 'check_key' => \CSocServAuthManager::GetUniqueKey(),
313 'admin' => 1,
314 'backurl' => $APPLICATION->GetCurPageParam(),
315 )));
316 }
317
318 return $result;
319 }
320
325 private static function getUrlNet()
326 {
327 if(!Loader::includeModule("socialservices"))
328 return '';
329
330 global $APPLICATION;
331 $appId = self::getAppID();
332 $result = '';
333
334 if($appId <> '')
335 {
336 $result = \CBitrix24NetOAuthInterface::NET_URL.'/oauth/select/?preset=ap&client_id='.urlencode($appId).'&state='.urlencode(http_build_query(array(
337 'check_key' => \CSocServAuthManager::GetUniqueKey(),
338 'admin' => 1,
339 'backurl' => $APPLICATION->GetCurPageParam('', array('apaction', 'apID')),
340 )));
341 }
342
343 return $result;
344 }
345
350 private static function getHostsList()
351 {
352 if(!Loader::includeModule('socialservices'))
353 return array();
354
355 $result = array();
356 $query = \CBitrix24NetTransport::init();
357
358 if($query)
359 $result = $query->call('admin.profile.list', array());
360
361 return !empty($result['result']) && is_array($result['result']) ? $result['result'] : array();
362 }
363
367 public static function getOpenLinesConfigUrl()
368 {
369 $res = self::getDataFromRest('imopenlines.config.path.get', array('result'));
370
371 if(is_array($res) && !empty($res['SERVER_ADDRESS']) && !empty($res['PUBLIC_PATH']))
372 {
373 $result = $res['SERVER_ADDRESS'].$res['PUBLIC_PATH'];
374 }
375 else
376 {
377 $domain = self::getDomain();
378
379 if($domain == '')
380 return '';
381
382 $result = 'https://'.htmlspecialcharsbx($domain).'/settings/openlines/'; //default for b24 cloud
383 }
384
385 return $result;
386 }
387
391 public static function getTelephonyConfigUrl()
392 {
393 return self::getDataFromRest('voximplant.url.get', array('result', 'lines'), '/telephony/lines.php');
394 }
395
399 public static function getWebformConfigUrl()
400 {
401 return self::getDataFromRest('crm.webform.configuration.get', array('result', 'URL'), '/crm/webform/');
402 }
403
407 public static function getWidgetsConfigUrl()
408 {
409 return self::getDataFromRest('crm.sitebutton.configuration.get', array('result', 'URL'), '/crm/button/');
410 }
411
412 private static function getDataFromRest($method, $pathToData, $defaultPath = '')
413 {
414 if(!Loader::includeModule('socialservices'))
415 return '';
416
417 $result = '';
418
419 if($client = ApClient::init())
420 {
421 $result = $client->call($method);
422
423 if(is_array($result))
424 {
425 foreach($pathToData as $idx)
426 {
427 if(!empty($result[$idx]))
428 {
429 $result = $result[$idx];
430 }
431 else
432 {
433 $result = '';
434 break;
435 }
436 }
437 }
438 }
439
440 if(is_array($result))
441 return $result;
442
443 if($result == '')
444 {
445 $domain = self::getDomain();
446
447 if($domain == '')
448 return '';
449
450 $result = 'https://'.htmlspecialcharsbx($domain).$defaultPath; //default for b24 cloud
451 }
452
453 return $result;
454 }
455}
static getButtonHtml($title='')
static get($moduleId, $name, $default="", $siteId=false)
Definition option.php:30
static set($moduleId, $name, $value="", $siteId="")
Definition option.php:253
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29