1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
service.php
См. документацию.
1<?php
8
9namespace Bitrix\Seo;
10
11use Bitrix\Main\Application;
12use Bitrix\Main\Context;
13use Bitrix\Main\Loader;
14use Bitrix\Main\LoaderException;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Main\SystemException;
17use Bitrix\Main\Web\HttpClient;
18use Bitrix\Main\Web\Json;
19use Bitrix\Seo\Engine\Bitrix;
20use Bitrix\Seo\Retargeting\AdsAudience;
21use Bitrix\Seo\Retargeting\Request;
22
23Loc::loadMessages(__FILE__);
24
25if (!defined("BITRIX_CLOUD_ADV_URL"))
26{
27 $domain = (new \Bitrix\Main\License\UrlProvider())->getTechDomain();
28 $cloudAdvUrl = 'https://cloud-adv.' . $domain;
29
30 define("BITRIX_CLOUD_ADV_URL", $cloudAdvUrl);
31}
32
33if (!defined("SEO_SERVICE_URL"))
34{
35 define('SEO_SERVICE_URL', BITRIX_CLOUD_ADV_URL);
36}
37
39{
40 const SERVICE_URL = SEO_SERVICE_URL;
41 const REGISTER = "/oauth/register/";
42 const AUTHORIZE = "/register/";
43 const REDIRECT_URI = "/bitrix/tools/seo_client.php";
44
47 const SERVICE_AUTH_CACHE_ID = 'seo|service_auth';
48 const SERVICE_AUTH_CACHE_ID_ERROR = 'seo|service_auth_error';
49
50 const CLIENT_LIST_CACHE_TLL = 86400;
51 const CLIENT_LIST_CACHE_ID = 'seo|client_list|2';
52
53 const CLIENT_TYPE_SINGLE = 'S';
56
57 protected static $engine = null;
58 protected static $auth = null;
59 protected static $clientList = null;
60
65 public static function isRegistered()
66 {
67 return static::getEngine() && static::getEngine()->isRegistered();
68 }
69
78 public static function getAuth(string $engineCode)
79 {
80 global $CACHE_MANAGER;
81 if (static::$auth === null)
82 {
83 if ($CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL, static::SERVICE_AUTH_CACHE_ID))
84 {
85 static::$auth = $CACHE_MANAGER->Get(static::SERVICE_AUTH_CACHE_ID);
86 }
87 elseif (!$CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL_ERROR, static::SERVICE_AUTH_CACHE_ID_ERROR))
88 {
89 static::$auth = static::getEngine()?->getInterface()?->getClientInfo();
90 if (!static::$auth)
91 {
92 static::$auth = false;
93 $CACHE_MANAGER->Read(static::SERVICE_AUTH_CACHE_TLL_ERROR, static::SERVICE_AUTH_CACHE_ID_ERROR);
94 $CACHE_MANAGER->Set(static::SERVICE_AUTH_CACHE_ID_ERROR, static::$auth);
95 }
96 else
97 {
98 $CACHE_MANAGER->Set(static::SERVICE_AUTH_CACHE_ID, static::$auth);
99 }
100 }
101 else
102 {
103 static::$auth = false;
104 }
105 }
106
107 return static::$auth["engine"][$engineCode] ?? false;
108 }
109
116 public static function getClientList($engineCode = false, string $type = null)
117 {
118 if (static::$clientList == null)
119 {
120 $cache = Application::getInstance()->getManagedCache();
121 if ($cache->read(static::CLIENT_LIST_CACHE_TLL, static::CLIENT_LIST_CACHE_ID))
122 {
123 static::$clientList = $cache->get(static::CLIENT_LIST_CACHE_ID);
124 static::$clientList = is_array(static::$clientList) ? static::$clientList : [];
125 }
126 else
127 {
128 $request = Request::create($type);
129 $clientDataProvider = static::getEngine($request)?->getInterface();
130 if (!$clientDataProvider)
131 {
132 return [];
133 }
134
135 $result = $clientDataProvider->getClientList();
136 if (!is_array($result)) // backward compatibility
137 {
138 $result = [];
139 $data = $clientDataProvider->getClientInfo();
140 if (is_array($data))
141 {
142 foreach ($data as $code => $client)
143 {
144 $data['proxy_client_type'] = static::CLIENT_TYPE_COMPATIBLE;
145 $data['engine_code'] = $code;
146 $data['proxy_client_id'] = null;
147 $result[] = $data;
148 }
149 }
150 }
151 else
152 {
153 $result = $result['items'];
154 }
155 $cache->set(static::CLIENT_LIST_CACHE_ID, $result);
156 static::$clientList = $result;
157 }
158 }
159 if ($engineCode)
160 {
161 return array_filter(static::$clientList, function ($item) use ($engineCode)
162 {
163 return $item['engine_code'] == $engineCode;
164 });
165 }
166
167 return static::$clientList;
168 }
169
175 public static function clearLocalAuth()
176 {
177 global $CACHE_MANAGER;
178
179 $CACHE_MANAGER->Clean(static::SERVICE_AUTH_CACHE_ID);
180
181 static::$auth = null;
182 }
183
191 public static function clearClientsCache($engine = null, $clientId = null)
192 {
193 $cache = Application::getInstance()->getManagedCache();
194 $cache->Clean(static::CLIENT_LIST_CACHE_ID);
195 $cache->Clean(static::SERVICE_AUTH_CACHE_ID);
196 $cache->Clean(static::SERVICE_AUTH_CACHE_ID_ERROR);
197
198 if ($engine && is_string($engine))
199 {
200 [$group, $type] = explode('.', $engine, 2);
201 if ($group == \Bitrix\Seo\Retargeting\Service::GROUP)
202 {
203 $service = AdsAudience::getService();
204 $service->setClientId($clientId);
205 $account = $service->getAccount($type);
206 if ($account)
207 {
208 $account->clearCache();
209 }
210 }
211 }
212
213 static::$clientList = null;
214 static::$auth = null;
215 }
216
225 public static function clearAuth($engineCode, $localOnly = false)
226 {
227 static::clearClientsCache($engineCode);
228
229 if (!$localOnly)
230 {
231 static::getEngine()?->getInterface()?->clearClientAuth($engineCode);
232 }
233 }
234
242 public static function clearAuthForClient($client, $localOnly = false)
243 {
244 if (!$localOnly)
245 {
246 static::getEngine()?->getInterface()?->clearClientAuth($client['engine_code'], $client['proxy_client_id']);
247 }
248 static::clearClientsCache($client['engine_code'], $client['proxy_client_id']);
249 }
250
257 protected static function setAccessSettings(array $accessParams): void
258 {
259 if (static::isRegistered())
260 {
261 $id = static::getEngine()->getId();
262
263 $result = SearchEngineTable::update($id, [
264 "CLIENT_ID" => $accessParams["client_id"],
265 "CLIENT_SECRET" => $accessParams["client_secret"],
266 "SETTINGS" => "",
267 ]);
268 }
269 else
270 {
271 $result = SearchEngineTable::add([
272 "CODE" => Bitrix::ENGINE_ID,
273 "NAME" => "Bitrix",
274 "ACTIVE" => SearchEngineTable::ACTIVE,
275 "CLIENT_ID" => $accessParams["client_id"],
276 "CLIENT_SECRET" => $accessParams["client_secret"],
277 "REDIRECT_URI" => static::getRedirectUri(),
278 ]);
279 }
280
281 if ($result->isSuccess())
282 {
283 static::clearAuth(Bitrix::ENGINE_ID, true);
284 static::$engine = null;
285 }
286 }
287
292 public static function getEngine(?Request $request = null): ?Bitrix
293 {
294 if (!Loader::includeModule("socialservices"))
295 {
296 return null;
297 }
298
299 if (!static::$engine)
300 {
301 static::$engine = new Bitrix();
302
303 if ($request)
304 {
305 static::$engine->setAuthSettings(['PROXY_URL' => $request->getProxyUrl()]);
306 }
307 }
308
309 return static::$engine;
310 }
311
318 public static function register(string $serviceUrl = ''): void
319 {
320 static::clearClientsCache();
321
322 $httpClient = new HttpClient();
323
324 $queryParams = [
325 "key" => static::getLicense(),
326 "scope" => static::getEngine()?->getInterface()?->getScopeEncode(),
327 "redirect_uri" => static::getRedirectUri(),
328 ];
329
330 $serviceUrl = $serviceUrl ?: static::SERVICE_URL;
331
332 $result = $httpClient->post($serviceUrl . static::REGISTER, $queryParams);
333
334 try
335 {
336 $result = Json::decode($result);
337 }
338 catch (\Exception $e)
339 {
340 $result = [
341 'error' => $e->getCode() . ': ' . $e->getMessage(),
342 ];
343 }
344
345 if (isset($result['error']))
346 {
347 throw new SystemException($result['error']);
348 }
349
350 static::setAccessSettings($result);
351 }
352
357 public static function unregister()
358 {
359 if (static::isRegistered())
360 {
361 $id = static::getEngine()->getId();
362 SearchEngineTable::delete($id);
363 static::clearClientsCache();
364 }
365 }
366
370 public static function getAuthorizeLink()
371 {
372 return static::SERVICE_URL . static::AUTHORIZE;
373 }
374
381 public static function getAuthorizeData($engine, $clientType = false): array
382 {
383 $checkKey = "";
384 $session = Application::getInstance()
385 ->getSession()
386 ;
387
388 if (Loader::includeModule("socialservices") && $session->isAccessible())
389 {
391 }
392
393 $clientType = $clientType ?: Service::CLIENT_TYPE_COMPATIBLE;
394
395 return [
396 "action" => "authorize",
397 "type" => $clientType,
398 "engine" => $engine,
399 "client_id" => static::getEngine()?->getClientId(),
400 "client_secret" => static::getEngine()?->getClientSecret(),
401 "key" => static::getLicense(),
402 "check_key" => urlencode($checkKey),
403 "redirect_uri" => static::getRedirectUri(),
404 ];
405 }
406
410 protected static function getRedirectUri(): string
411 {
412 $request = Context::getCurrent()->getRequest();
413
414 $host = $request->getHttpHost();
415 $port = (int)$request->getServerPort();
416 $host .= ($port && $port !== 80 && $port !== 443) ? ":{$port}" : '';
417
418 $isHttps = $request->isHttps();
419
420 return ($isHttps ? 'https' : 'http') . '://' . $host . static::REDIRECT_URI;
421 }
422
426 protected static function getLicense(): string
427 {
428 return md5(LICENSE_KEY);
429 }
430
436 public static function changeRegisteredDomain(array $domains = []): void
437 {
438 if (!self::isRegistered())
439 {
440 return;
441 }
442 if (!$engine = static::getEngine())
443 {
444 return;
445 }
446
447 $newRedirectUri = static::getRedirectUri();
448 if (!empty($domains))
449 {
450 $newRedirectUri = str_replace($domains['old_domain'], $domains['new_domain'], $newRedirectUri);
451 }
452
453 SearchEngineTable::update($engine->getId(), [
454 'REDIRECT_URI' => $newRedirectUri,
455 ]);
456 }
457}
$type
Определения options.php:106
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static getInstance()
Определения application.php:98
static includeModule($moduleName)
Определения loader.php:67
Определения request.php:10
const ENGINE_ID
Определения bitrix.php:30
const CLIENT_TYPE_COMPATIBLE
Определения service.php:55
static getAuthorizeLink()
Определения service.php:370
static $auth
Определения service.php:58
static clearLocalAuth()
Определения service.php:175
static getAuth(string $engineCode)
Определения service.php:78
static clearAuthForClient($client, $localOnly=false)
Определения service.php:242
static unregister()
Определения service.php:357
const CLIENT_LIST_CACHE_ID
Определения service.php:51
const AUTHORIZE
Определения service.php:42
static isRegistered()
Определения service.php:65
static getClientList($engineCode=false, string $type=null)
Определения service.php:116
const CLIENT_LIST_CACHE_TLL
Определения service.php:50
const SERVICE_AUTH_CACHE_ID
Определения service.php:47
const SERVICE_AUTH_CACHE_TLL_ERROR
Определения service.php:46
static $engine
Определения service.php:57
static $clientList
Определения service.php:59
const SERVICE_AUTH_CACHE_TLL
Определения service.php:45
const REDIRECT_URI
Определения service.php:43
static getLicense()
Определения service.php:426
static changeRegisteredDomain(array $domains=[])
Определения service.php:436
const REGISTER
Определения service.php:41
static clearAuth($engineCode, $localOnly=false)
Определения service.php:225
static getRedirectUri()
Определения service.php:410
const SERVICE_URL
Определения service.php:40
const SERVICE_AUTH_CACHE_ID_ERROR
Определения service.php:48
static clearClientsCache($engine=null, $clientId=null)
Определения service.php:191
static getAuthorizeData($engine, $clientType=false)
Определения service.php:381
static setAccessSettings(array $accessParams)
Определения service.php:257
const CLIENT_TYPE_SINGLE
Определения service.php:53
const CLIENT_TYPE_MULTIPLE
Определения service.php:54
static getEngine(?Request $request=null)
Определения service.php:292
static GetUniqueKey()
Определения authmanager.php:335
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
const LICENSE_KEY($show_sql_stat=='Y')
Определения start.php:84
$host
Определения mysql_to_pgsql.php:32
$service
Определения payment.php:18
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$clientId
Определения seo_client.php:18