Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
authadapter.php
1<?php
2
4
10use Bitrix\Seo\Service as SeoService;
11
13{
15 protected $service;
16
18 protected $type;
19
20 /* @var \CSocServOAuthTransport|\CFacebookInterface */
21 protected $transport;
22
24 protected $data;
25
27 protected $parameters = ['URL_PARAMETERS' => []];
28
29 public function __construct($type)
30 {
31 $this->type = $type;
32
33 if($type === \Bitrix\Seo\Retargeting\Service::TYPE_YANDEX)
34 {
35 $this->parameters['URL_PARAMETERS']['force_confirm'] = 'yes';
36 }
37 }
38
43 public static function create($type, IService $service = null)
44 {
45 if (!Loader::includeModule('socialservices'))
46 {
47 throw new SystemException('Module "socialservices" not installed.');
48 }
49 $instance = new static($type);
50 if ($service)
51 {
52 $instance->setService($service);
53 }
54
55 return $instance;
56 }
57
62 public function setService(IService $service)
63 {
64 $this->service = $service;
65
66 return $this;
67 }
68
73 public function setParameters(array $parameters = [])
74 {
75 $this->parameters = $parameters + $this->parameters;
76
77 return $this;
78 }
79
80 public function getAuthUrl()
81 {
83 {
85 }
86
87 $authorizeData = SeoService::getAuthorizeData(
88 $this->getEngineCode(),
90 );
91
92 if (!empty($this->parameters['URL_PARAMETERS']))
93 {
94 $authorizeData['urlParameters'] = $this->parameters['URL_PARAMETERS'];
95 }
96
98
99 return $uri->addParams($authorizeData)->getLocator();
100 }
101
102 protected function getAuthData($isUseCache = true)
103 {
104 return ($this->canUseMultipleClients()
105 ? $this->getAuthDataMultiple()
106 : $this->getAuthDataSingle($isUseCache))
107 ;
108 }
109
110 protected function getAuthDataMultiple()
111 {
112 return $this->getClientById($this->getClientId());
113 }
114
115 protected function getAuthDataSingle($isUseCache = true)
116 {
117 if (!$isUseCache || !$this->data || count($this->data) == 0)
118 {
119 $this->data = SeoService::getAuth($this->getEngineCode());
120 }
121
122 return $this->data;
123 }
124
129 public function removeAuth()
130 {
131 $this->data = array();
132
133 if ($existedAuthData = $this->getAuthData(false))
134 {
136 $existedAuthData['proxy_client_id'],
137 $existedAuthData['engine_code']
138 );
139 $this->canUseMultipleClients()
140 ? SeoService::clearAuthForClient($existedAuthData)
142
143 }
144 }
145
149 protected function getEngineCode()
150 {
151 if ($this->service)
152 {
153 return $this->service::getEngineCode($this->type);
154 }
155
156 return Service::getEngineCode($this->type);
157 }
158
162 public function getType()
163 {
164 return $this->type;
165 }
166
167 public function getToken()
168 {
169 return is_array($data = $this->getAuthData(false)) ? $data['access_token'] : null;
170 }
171
175 public function hasAuth()
176 {
177 return $this->canUseMultipleClients()
178 ? count($this->getAuthorizedClientsList()) > 0
179 : $this->getToken() <> "";
180 }
181
185 public function canUseMultipleClients()
186 {
187 if (!$this->service)
188 {
190 }
191
192 if ($this->service instanceof IMultiClientService)
193 {
194 return $this->service::canUseMultipleClients();
195 }
196
197 return false;
198 }
199
204 public function getClientList()
205 {
206 return $this->canUseMultipleClients() ? SeoService::getClientList($this->getEngineCode()) : [];
207 }
208
209 public function getClientById($clientId)
210 {
211 $clients = $this->getClientList();
212 foreach ($clients as $client)
213 {
214 if ($client['proxy_client_id'] == $clientId)
215 {
216 return $client;
217 }
218 }
219 return null;
220 }
221
226 public function getAuthorizedClientsList()
227 {
228 return array_filter(
229 $this->getClientList(),
230 static function ($item) : bool {
231 return $item['access_token'] <> '';
232 }
233 );
234 }
235
236 public function getClientId()
237 {
238 if (!$this->canUseMultipleClients())
239 {
240 return null;
241 }
242 $clientId = $this->service->getClientId();
243 if ($clientId)
244 {
245 $client = $this->getClientById($clientId);
246 if ($client['engine_code'] == $this->getEngineCode())
247 {
248 return $clientId;
249 }
250 return null;
251 }
252
253 // try to guess account id from accounts list:
254 $clients = $this->getClientList();
255 foreach ($clients as $client)
256 {
257 if ($client['proxy_client_type'] == SeoService::CLIENT_TYPE_COMPATIBLE)
258 {
259 return $client['proxy_client_id'];
260 }
261 }
262 return null;
263 }
264}
static create($type, IService $service=null)
setParameters(array $parameters=[])
const CLIENT_TYPE_COMPATIBLE
Definition service.php:49
static getAuthorizeLink()
Definition service.php:333
static getAuth(string $engineCode)
Definition service.php:72
static clearAuthForClient($client, $localOnly=false)
Definition service.php:229
static isRegistered()
Definition service.php:59
static register()
Definition service.php:293
static getClientList($engineCode=false)
Definition service.php:115
static clearAuth($engineCode, $localOnly=false)
Definition service.php:212
static getAuthorizeData($engine, $clientType=false)
Definition service.php:344
const CLIENT_TYPE_SINGLE
Definition service.php:47
const CLIENT_TYPE_MULTIPLE
Definition service.php:48