22if(!defined(
"BITRIX_CLOUD_ADV_URL"))
24 define(
"BITRIX_CLOUD_ADV_URL",
'https://cloud-adv.bitrix.info');
27if(!defined(
"SEO_SERVICE_URL"))
29 define(
'SEO_SERVICE_URL', BITRIX_CLOUD_ADV_URL);
61 return static::getEngine() ? static::getEngine()->isRegistered() :
false;
72 public static function getAuth(
string $engineCode)
74 global $CACHE_MANAGER;
75 if (static::$auth ===
null)
77 if ($CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL, static::SERVICE_AUTH_CACHE_ID))
79 static::$auth = $CACHE_MANAGER->Get(static::SERVICE_AUTH_CACHE_ID);
81 elseif (!$CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL_ERROR, static::SERVICE_AUTH_CACHE_ID_ERROR))
83 static::$auth = static::getEngine()->getInterface()->getClientInfo();
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);
92 $CACHE_MANAGER->Set(static::SERVICE_AUTH_CACHE_ID, static::$auth);
97 static::$auth =
false;
103 return static::$auth[
"engine"][$engineCode];
117 if( static::$clientList ==
null)
120 if ($cache->read(static::CLIENT_LIST_CACHE_TLL, static::CLIENT_LIST_CACHE_ID))
122 static::$clientList = $cache->get(static::CLIENT_LIST_CACHE_ID);
123 static::$clientList = is_array(static::$clientList) ? static::$clientList : [];
127 $clientDataProvider = static::getEngine()->getInterface();
128 $result = $clientDataProvider->getClientList();
129 if (!is_array($result))
132 $data = $clientDataProvider->getClientInfo();
135 foreach ($data as $code => $client)
137 $data[
'proxy_client_type'] = static::CLIENT_TYPE_COMPATIBLE;
138 $data[
'engine_code'] = $code;
139 $data[
'proxy_client_id'] =
null;
146 $result = $result[
'items'];
148 $cache->set(static::CLIENT_LIST_CACHE_ID, $result);
149 static::$clientList = $result;
154 return array_filter(static::$clientList,
function ($item) use ($engineCode) {
155 return $item[
'engine_code'] == $engineCode;
158 return static::$clientList;
168 global $CACHE_MANAGER;
170 $CACHE_MANAGER->Clean(static::SERVICE_AUTH_CACHE_ID);
172 static::$auth =
null;
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);
189 [$group, $type] = explode(
'.',
$engine, 2);
191 if ($group == \
Bitrix\Seo\Retargeting\Service::GROUP)
193 $service = AdsAudience::getService();
194 $service->setClientId($clientId);
195 $account = $service->getAccount($type);
197 $account->clearCache();
200 static::$clientList =
null;
201 static::$auth =
null;
212 public static function clearAuth($engineCode, $localOnly =
false)
214 static::clearClientsCache($engineCode);
218 static::getEngine()->getInterface()->clearClientAuth($engineCode);
233 static::getEngine()->getInterface()->clearClientAuth($client[
'engine_code'], $client[
'proxy_client_id']);
235 static::clearClientsCache($client[
'engine_code'], $client[
'proxy_client_id']);
246 if(static::isRegistered())
248 $id = static::getEngine()->getId();
250 $result = SearchEngineTable::update($id, array(
251 "CLIENT_ID" => $accessParams[
"client_id"],
252 "CLIENT_SECRET" => $accessParams[
"client_secret"],
258 $result = SearchEngineTable::add(array(
259 "CODE" => Bitrix::ENGINE_ID,
261 "ACTIVE" => SearchEngineTable::ACTIVE,
262 "CLIENT_ID" => $accessParams[
"client_id"],
263 "CLIENT_SECRET" => $accessParams[
"client_secret"],
264 "REDIRECT_URI" => static::getRedirectUri(),
268 if($result->isSuccess())
270 static::clearAuth(Bitrix::ENGINE_ID,
true);
271 static::$engine =
null;
282 static::$engine =
new Bitrix();
285 return static::$engine;
293 public static function register()
295 static::clearClientsCache();
300 "key" => static::getLicense(),
301 "scope" => static::getEngine()->getInterface()->getScopeEncode(),
302 "redirect_uri" => static::getRedirectUri(),
305 $result = $httpClient->post(static::SERVICE_URL.static::REGISTER, $queryParams);
306 $result = Json::decode($result);
313 static::setAccessSettings($result);
322 if(static::isRegistered())
324 $id = static::getEngine()->getId();
325 SearchEngineTable::delete($id);
326 static::clearClientsCache();
335 return static::SERVICE_URL.static::AUTHORIZE;
349 $checkKey = \CSocServAuthManager::GetUniqueKey();
352 $clientType = $clientType ?: Service::CLIENT_TYPE_COMPATIBLE;
355 "action" =>
"authorize",
356 "type" => $clientType,
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(),
371 $request = Context::getCurrent()->getRequest();
373 $host = $request->getHttpHost();
374 $port = (int)$request->getServerPort();
375 $host .= ($port && $port !== 80 && $port !== 443) ?
":{$port}" :
'';
377 $isHttps = $request->isHttps();
379 return ($isHttps ?
'https' :
'http').
'://'.$host.static::REDIRECT_URI;
387 return md5(LICENSE_KEY);
397 if (!self::isRegistered())
401 if(!
$engine = static::getEngine())
406 $newRedirectUri = static::getRedirectUri();
409 $newRedirectUri = str_replace($domains[
'old_domain'], $domains[
'new_domain'], $newRedirectUri);
412 SearchEngineTable::update(
$engine->getId(), [
413 'REDIRECT_URI' => $newRedirectUri
static includeModule($moduleName)
static loadMessages($file)
const CLIENT_TYPE_COMPATIBLE
static getAuthorizeLink()
static getAuth(string $engineCode)
static clearAuthForClient($client, $localOnly=false)
const CLIENT_LIST_CACHE_ID
const CLIENT_LIST_CACHE_TLL
const SERVICE_AUTH_CACHE_ID
const SERVICE_AUTH_CACHE_TLL_ERROR
const SERVICE_AUTH_CACHE_TLL
static changeRegisteredDomain(array $domains=[])
static getClientList($engineCode=false)
static clearAuth($engineCode, $localOnly=false)
const SERVICE_AUTH_CACHE_ID_ERROR
static clearClientsCache($engine=null, $clientId=null)
static getAuthorizeData($engine, $clientType=false)
static setAccessSettings(array $accessParams)
const CLIENT_TYPE_MULTIPLE