Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexbase.php
1<?
8namespace Bitrix\Seo\Engine;
9
14
16{
17 const QUERY_USER = 'https://login.yandex.ru/info';
18
19 protected $engineId = 'yandex_generic';
20
26 public function getAuthUrl()
27 {
28 return $this->getInterface()->getAuthUrl();
29 }
30
36 public function getInterface()
37 {
38 if($this->authInterface === null)
39 {
40 $this->authInterface = new \CYandexOAuthInterface($this->engine['CLIENT_ID'], $this->engine['CLIENT_SECRET']);
41
42 if($this->engineSettings['AUTH'])
43 {
44 $this->authInterface->setToken($this->engineSettings['AUTH']['access_token']);
45 $this->authInterface->setRefreshToken($this->engineSettings['AUTH']['refresh_token']);
46 $this->authInterface->setAccessTokenExpires($this->engineSettings['AUTH']['expires_in']);
47 }
48 }
49
51 }
52
53 public function clearSitesSettings()
54 {
55 unset($this->engineSettings['SITES']);
56 $this->saveSettings();
57 }
58
59 public function setAuthSettings($settings = null)
60 {
61 if($settings === null)
62 {
63 $settings = $this->getInterface();
64 }
65
66 if($settings instanceof \CYandexOAuthInterface)
67 {
68 $settings = array(
69 'access_token' => $settings->getToken(),
70 'refresh_token' => $settings->getRefreshToken(),
71 'expires_in' => $settings->getAccessTokenExpires()
72 );
73 }
74
75 $this->engineSettings['AUTH'] = $settings;
76 $this->saveSettings();
77 }
78
79 public function checkAuthExpired()
80 {
81 $ob = $this->getInterface();
82 return !$ob->checkAccessToken();
83 }
84
85 public function getAuth($code)
86 {
87 $ob = $this->getInterface();
88 $ob->setCode($code);
89
90 if($ob->getAccessToken())
91 {
92 unset($this->engineSettings['AUTH_USER']);
93
94 $this->setAuthSettings();
95 return true;
96 }
97
98 throw new \Exception($ob->getError());
99 }
100
109 public function getCurrentUser()
110 {
111 if(
112 !array_key_exists('AUTH_USER', $this->engineSettings)
113 || !is_array($this->engineSettings['AUTH_USER'])
114 )
115 {
116 $queryResult = self::query(self::QUERY_USER);
117
118 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
119 {
120 $res = Web\Json::decode($queryResult->getResult());
121
122 if(is_array($res))
123 {
124 $this->engineSettings['AUTH_USER'] = $res;
125 $this->saveSettings();
126
127 return $this->engineSettings['AUTH_USER'];
128 }
129 }
130
131 throw new Engine\YandexException($queryResult);
132 }
133 else
134 {
135 return $this->engineSettings['AUTH_USER'];
136 }
137 }
138
150 protected function query($scope, $method = "GET", $data = null, $skipRefreshAuth = false)
151 {
152 if($this->engineSettings['AUTH'])
153 {
154 $http = new Web\HttpClient();
155 $http->setHeader('Authorization', 'OAuth '.$this->engineSettings['AUTH']['access_token']);
156 $http->setRedirect(false);
157
158 switch($method)
159 {
160 case 'GET':
161 $http->get($scope);
162 break;
163 case 'POST':
164 $http->post($scope, $data);
165 break;
166 case 'PUT':
167 $http->query($method, $scope, $data);
168 break;
169 case 'DELETE':
170
171 break;
172 }
173
174 if($http->getStatus() == 401 && !$skipRefreshAuth)
175 {
176 if($this->checkAuthExpired())
177 {
178 $this->query($scope, $method, $data, true);
179 }
180 }
181
182 return $http;
183 }
184 else
185 {
186 throw new SystemException("No Yandex auth data");
187 }
188 }
189
190 protected function prepareQueryResult(array $result)
191 {
192 return Text\Encoding::convertEncodingArray($result, 'utf-8', LANG_CHARSET);
193 }
194}
query($scope, $method="GET", $data=null, $skipRefreshAuth=false)
setAuthSettings($settings=null)
prepareQueryResult(array $result)