Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
service.php
1<?php
8namespace Bitrix\Seo;
9
19
20Loc::loadMessages(__FILE__);
21
22if(!defined("BITRIX_CLOUD_ADV_URL"))
23{
24 define("BITRIX_CLOUD_ADV_URL", 'https://cloud-adv.bitrix.info');
25}
26
27if(!defined("SEO_SERVICE_URL"))
28{
29 define('SEO_SERVICE_URL', BITRIX_CLOUD_ADV_URL);
30}
31
33{
34 const SERVICE_URL = SEO_SERVICE_URL;
35 const REGISTER = "/oauth/register/";
36 const AUTHORIZE = "/register/";
37 const REDIRECT_URI = "/bitrix/tools/seo_client.php";
38
41 const SERVICE_AUTH_CACHE_ID = 'seo|service_auth';
42 const SERVICE_AUTH_CACHE_ID_ERROR = 'seo|service_auth_error';
43
44 const CLIENT_LIST_CACHE_TLL = 86400;
45 const CLIENT_LIST_CACHE_ID = 'seo|client_list|2';
46
47 const CLIENT_TYPE_SINGLE = 'S';
50
51 protected static $engine = null;
52 protected static $auth = null;
53 protected static $clientList = null;
54
59 public static function isRegistered()
60 {
61 return static::getEngine() ? static::getEngine()->isRegistered() : false;
62 }
63
72 public static function getAuth(string $engineCode)
73 {
74 global $CACHE_MANAGER;
75 if (static::$auth === null)
76 {
77 if ($CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL, static::SERVICE_AUTH_CACHE_ID))
78 {
79 static::$auth = $CACHE_MANAGER->Get(static::SERVICE_AUTH_CACHE_ID);
80 }
81 elseif (!$CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL_ERROR, static::SERVICE_AUTH_CACHE_ID_ERROR))
82 {
83 static::$auth = static::getEngine()->getInterface()->getClientInfo();
84 if (!static::$auth)
85 {
86 static::$auth = false;
87 $CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL_ERROR, static::SERVICE_AUTH_CACHE_ID_ERROR);
88 $CACHE_MANAGER->Set(static::SERVICE_AUTH_CACHE_ID_ERROR, static::$auth);
89 }
90 else
91 {
92 $CACHE_MANAGER->Set(static::SERVICE_AUTH_CACHE_ID, static::$auth);
93 }
94 }
95 else
96 {
97 static::$auth = false;
98 }
99 }
100
101 if (static::$auth)
102 {
103 return static::$auth["engine"][$engineCode];
104 }
105
106 return false;
107 }
108
115 public static function getClientList($engineCode = false)
116 {
117 if( static::$clientList == null)
118 {
119 $cache = Application::getInstance()->getManagedCache();
120 if ($cache->read(static::CLIENT_LIST_CACHE_TLL, static::CLIENT_LIST_CACHE_ID))
121 {
122 static::$clientList = $cache->get(static::CLIENT_LIST_CACHE_ID);
123 static::$clientList = is_array(static::$clientList) ? static::$clientList : [];
124 }
125 else
126 {
127 $clientDataProvider = static::getEngine()->getInterface();
128 $result = $clientDataProvider->getClientList();
129 if (!is_array($result)) // backward compatibility
130 {
131 $result = [];
132 $data = $clientDataProvider->getClientInfo();
133 if (is_array($data))
134 {
135 foreach ($data as $code => $client)
136 {
137 $data['proxy_client_type'] = static::CLIENT_TYPE_COMPATIBLE;
138 $data['engine_code'] = $code;
139 $data['proxy_client_id'] = null;
140 $result[] = $data;
141 }
142 }
143 }
144 else
145 {
146 $result = $result['items'];
147 }
148 $cache->set(static::CLIENT_LIST_CACHE_ID, $result);
149 static::$clientList = $result;
150 }
151 }
152 if ($engineCode)
153 {
154 return array_filter(static::$clientList, function ($item) use ($engineCode) {
155 return $item['engine_code'] == $engineCode;
156 });
157 }
158 return static::$clientList;
159 }
160
166 public static function clearLocalAuth()
167 {
168 global $CACHE_MANAGER;
169
170 $CACHE_MANAGER->Clean(static::SERVICE_AUTH_CACHE_ID);
171
172 static::$auth = null;
173 }
174
182 public static function clearClientsCache($engine = null, $clientId = null)
183 {
184 $cache = Application::getInstance()->getManagedCache();
185 $cache->Clean(static::CLIENT_LIST_CACHE_ID);
186 $cache->Clean(static::SERVICE_AUTH_CACHE_ID);
187 $cache->Clean(static::SERVICE_AUTH_CACHE_ID_ERROR);
188
189 [$group, $type] = explode('.', $engine, 2);
190
191 if ($group == \Bitrix\Seo\Retargeting\Service::GROUP)
192 {
193 $service = AdsAudience::getService();
194 $service->setClientId($clientId);
195 $account = $service->getAccount($type);
196 if ($account)
197 $account->clearCache();
198 }
199
200 static::$clientList = null;
201 static::$auth = null;
202 }
203
212 public static function clearAuth($engineCode, $localOnly = false)
213 {
214 static::clearClientsCache($engineCode);
215
216 if(!$localOnly)
217 {
218 static::getEngine()->getInterface()->clearClientAuth($engineCode);
219 }
220 }
221
229 public static function clearAuthForClient($client, $localOnly = false)
230 {
231 if(!$localOnly)
232 {
233 static::getEngine()->getInterface()->clearClientAuth($client['engine_code'], $client['proxy_client_id']);
234 }
235 static::clearClientsCache($client['engine_code'], $client['proxy_client_id']);
236 }
237
244 protected static function setAccessSettings(array $accessParams)
245 {
246 if(static::isRegistered())
247 {
248 $id = static::getEngine()->getId();
249
250 $result = SearchEngineTable::update($id, array(
251 "CLIENT_ID" => $accessParams["client_id"],
252 "CLIENT_SECRET" => $accessParams["client_secret"],
253 "SETTINGS" => "",
254 ));
255 }
256 else
257 {
258 $result = SearchEngineTable::add(array(
259 "CODE" => Bitrix::ENGINE_ID,
260 "NAME" => "Bitrix",
261 "ACTIVE" => SearchEngineTable::ACTIVE,
262 "CLIENT_ID" => $accessParams["client_id"],
263 "CLIENT_SECRET" => $accessParams["client_secret"],
264 "REDIRECT_URI" => static::getRedirectUri(),
265 ));
266 }
267
268 if($result->isSuccess())
269 {
270 static::clearAuth(Bitrix::ENGINE_ID, true);
271 static::$engine = null;
272 }
273 }
274
278 public static function getEngine()
279 {
280 if(!static::$engine && Loader::includeModule("socialservices"))
281 {
282 static::$engine = new Bitrix();
283 }
284
285 return static::$engine;
286 }
287
293 public static function register()
294 {
295 static::clearClientsCache();
296
297 $httpClient = new HttpClient();
298
299 $queryParams = [
300 "key" => static::getLicense(),
301 "scope" => static::getEngine()->getInterface()->getScopeEncode(),
302 "redirect_uri" => static::getRedirectUri(),
303 ];
304
305 $result = $httpClient->post(static::SERVICE_URL.static::REGISTER, $queryParams);
306 $result = Json::decode($result);
307
308 if($result["error"])
309 {
310 throw new SystemException($result["error"]);
311 }
312
313 static::setAccessSettings($result);
314 }
315
320 public static function unregister()
321 {
322 if(static::isRegistered())
323 {
324 $id = static::getEngine()->getId();
325 SearchEngineTable::delete($id);
326 static::clearClientsCache();
327 }
328 }
329
333 public static function getAuthorizeLink()
334 {
335 return static::SERVICE_URL.static::AUTHORIZE;
336 }
337
344 public static function getAuthorizeData($engine, $clientType = false): array
345 {
346 $checkKey = "";
347 if(Loader::includeModule("socialservices"))
348 {
349 $checkKey = \CSocServAuthManager::GetUniqueKey();
350 }
351
352 $clientType = $clientType ?: Service::CLIENT_TYPE_COMPATIBLE;
353
354 return [
355 "action" => "authorize",
356 "type" => $clientType,
357 "engine" => $engine,
358 "client_id" => static::getEngine()->getClientId(),
359 "client_secret" => static::getEngine()->getClientSecret(),
360 "key" => static::getLicense(),
361 "check_key" => urlencode($checkKey),
362 "redirect_uri" => static::getRedirectUri(),
363 ];
364 }
365
369 protected static function getRedirectUri(): string
370 {
371 $request = Context::getCurrent()->getRequest();
372
373 $host = $request->getHttpHost();
374 $port = (int)$request->getServerPort();
375 $host .= ($port && $port !== 80 && $port !== 443) ? ":{$port}" : '';
376
377 $isHttps = $request->isHttps();
378
379 return ($isHttps ? 'https' : 'http').'://'.$host.static::REDIRECT_URI;
380 }
381
385 protected static function getLicense(): string
386 {
387 return md5(LICENSE_KEY);
388 }
389
395 public static function changeRegisteredDomain(array $domains = []): void
396 {
397 if (!self::isRegistered())
398 {
399 return;
400 }
401 if(!$engine = static::getEngine())
402 {
403 return;
404 }
405
406 $newRedirectUri = static::getRedirectUri();
407 if(!empty($domains))
408 {
409 $newRedirectUri = str_replace($domains['old_domain'], $domains['new_domain'], $newRedirectUri);
410 }
411
412 SearchEngineTable::update($engine->getId(), [
413 'REDIRECT_URI' => $newRedirectUri
414 ]);
415 }
416}
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
const CLIENT_TYPE_COMPATIBLE
Definition service.php:49
static getAuthorizeLink()
Definition service.php:333
static clearLocalAuth()
Definition service.php:166
static getAuth(string $engineCode)
Definition service.php:72
static clearAuthForClient($client, $localOnly=false)
Definition service.php:229
static unregister()
Definition service.php:320
const CLIENT_LIST_CACHE_ID
Definition service.php:45
static isRegistered()
Definition service.php:59
static getEngine()
Definition service.php:278
const CLIENT_LIST_CACHE_TLL
Definition service.php:44
const SERVICE_AUTH_CACHE_ID
Definition service.php:41
const SERVICE_AUTH_CACHE_TLL_ERROR
Definition service.php:40
const SERVICE_AUTH_CACHE_TLL
Definition service.php:39
static getLicense()
Definition service.php:385
static changeRegisteredDomain(array $domains=[])
Definition service.php:395
static getClientList($engineCode=false)
Definition service.php:115
static clearAuth($engineCode, $localOnly=false)
Definition service.php:212
static getRedirectUri()
Definition service.php:369
const SERVICE_AUTH_CACHE_ID_ERROR
Definition service.php:42
static clearClientsCache($engine=null, $clientId=null)
Definition service.php:182
static getAuthorizeData($engine, $clientType=false)
Definition service.php:344
static setAccessSettings(array $accessParams)
Definition service.php:244
const CLIENT_TYPE_SINGLE
Definition service.php:47
const CLIENT_TYPE_MULTIPLE
Definition service.php:48