Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
google.php
1<?
8namespace Bitrix\Seo\Engine;
9
15
16class Google extends Engine implements IEngine
17{
18 const ENGINE_ID = 'google';
19 const SCOPE_BASE = 'https://www.googleapis.com/auth/webmasters';
20 const SCOPE_USER = 'https://www.googleapis.com/auth/userinfo.profile';
21 const SCOPE_VERIFY = 'https://www.googleapis.com/auth/siteverification.verify_only';
22
23 const SCOPE_FEED_SITES = 'sites';
24 const SCOPE_FEED_CRAWLISSUES = 'crawlissues/';
25 const SCOPE_FEED_MESSAGES = 'messages/';
26
27 const SCOPE_DOMAIN_PROTOCOL = 'http://';
28
29 const QUERY_BASE = 'https://www.googleapis.com/webmasters/v3/';
30
31 const QUERY_USER = 'https://www.googleapis.com/oauth2/v3/userinfo';
32 const QUERY_VERIFY = 'https://www.googleapis.com/siteVerification/v1/webResource?verificationMethod=FILE';
33 const QUERY_VERIFY_TOKEN = 'https://www.googleapis.com/siteVerification/v1/token';
34
35 protected $engineId = 'google';
36 protected $scope = null;
37
38 public function getScope()
39 {
40/*
41 if(!is_array($this->scope))
42 {
43 $arDomains = \CSeoUtils::getDomainsList();
44 $this->scope = array(
45 self::SCOPE_USER,
46 self::SCOPE_BASE,
47 self::SCOPE_VERIFY,
48 );
49
50 foreach ($arDomains as $arDomain)
51 {
52 $this->scope[] = $this->getSiteId($arDomain['DOMAIN'], $arDomain['SITE_ID']);
53 }
54 }
55*/
56
57 return array(
58 self::SCOPE_USER,
59 self::SCOPE_BASE,
60 self::SCOPE_VERIFY,
61 );
62 }
63
64 public function getAuthUrl()
65 {
66 return $this->getInterface()->getAuthUrl($this->engine['REDIRECT_URI']);
67 }
68
69 public function getInterface()
70 {
71 if($this->authInterface === null)
72 {
73 $this->authInterface = new \CGoogleOAuthInterface($this->engine['CLIENT_ID'], $this->engine['CLIENT_SECRET']);
74 $this->authInterface->setScope($this->getScope());
75
76 if($this->engineSettings['AUTH'])
77 {
78 $this->authInterface->setToken($this->engineSettings['AUTH']['access_token']);
79 $this->authInterface->setRefreshToken($this->engineSettings['AUTH']['refresh_token']);
80 $this->authInterface->setAccessTokenExpires($this->engineSettings['AUTH']['expires_in']);
81 }
82 }
83
85 }
86
87 public function setAuthSettings($settings = null)
88 {
89 if($settings === null)
90 {
91 $settings = $this->getInterface();
92 }
93
94 if($settings instanceof \CGoogleOAuthInterface)
95 {
96 $settings = array(
97 'access_token' => $settings->getToken(),
98 'refresh_token' => $settings->getRefreshToken(),
99 'expires_in' => $settings->getAccessTokenExpires()
100 );
101 }
102
103 $this->engineSettings['AUTH'] = $settings;
104 $this->saveSettings();
105 }
106
107 public function checkAuthExpired($bGetNew)
108 {
109 $ob = $this->getInterface();
110 if(!$ob->checkAccessToken())
111 {
112 return $bGetNew ? $this->refreshAuth() : false;
113 }
114 return true;
115 }
116
117 public function refreshAuth()
118 {
119 $ob = $this->getInterface();
120 if($ob->getNewAccessToken())
121 {
122 $this->setAuthSettings();
123 return true;
124 }
125
126 throw new \Exception($ob->getError());
127 }
128
129 public function getAuth($code)
130 {
131 $ob = $this->getInterface();
132 $ob->setCode($code);
133
134 if($ob->getAccessToken($this->engine['REDIRECT_URI']))
135 {
136 unset($this->engineSettings['AUTH_USER']);
137
138 $this->setAuthSettings();
139 return true;
140 }
141
142 throw new \Exception($ob->getError());
143 }
144
145 public function getCurrentUser()
146 {
147 global $APPLICATION;
148
149 if(!isset($this->engineSettings['AUTH_USER']) || !is_array($this->engineSettings['AUTH_USER']))
150 {
151 $queryResult = $this->queryJson(self::QUERY_USER);
152
153 if(!$queryResult)
154 {
155 return false;
156 }
157
158 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
159 {
160 $res = Json::decode($queryResult->getResult());
161 if(is_array($res))
162 {
163 $this->engineSettings['AUTH_USER'] = $APPLICATION->convertCharsetArray($res, 'utf-8', LANG_CHARSET);
164 $this->saveSettings();
165
166 return $this->engineSettings['AUTH_USER'];
167 }
168 }
169
170 throw new \Exception('Query error! '.$queryResult->getStatus().': '.$queryResult->getResult());
171 }
172 else
173 {
174 return $this->engineSettings['AUTH_USER'];
175 }
176 }
177
178 public function getFeeds()
179 {
180 $queryResult = $this->queryJson(self::QUERY_BASE.self::SCOPE_FEED_SITES);
181 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
182 {
183 $result = Json::decode($queryResult->getResult());
184 $response = array();
185 if(is_array($result) && is_array($result['siteEntry']))
186 {
187 foreach($result['siteEntry'] as $key => $siteInfo)
188 {
189 $siteUrlInfo = parse_url($siteInfo['siteUrl']);
190 if($siteUrlInfo)
191 {
192 $errors = array();
193 $hostKey = \CBXPunycode::toASCII($siteUrlInfo["host"], $errors);
194 if(count($errors) > 0)
195 {
196 $hostKey = $siteUrlInfo["host"];
197 }
198
199 $response[$hostKey] = array(
200 'binded' => $siteInfo["permissionLevel"] !== "siteRestrictedUser",
201 'verified' => $siteInfo["permissionLevel"] !== "siteRestrictedUser"
202 && $siteInfo["permissionLevel"] !== "siteUnverifiedUser",
203 );
204 }
205 }
206 }
207
208 return $response;
209 }
210 else
211 {
212 throw new \Exception('Query error! '.$queryResult->getStatus().': '.$queryResult->getResult());
213 }
214 }
215
216 public function addSite($domain, $dir = '/')
217 {
218 $queryResult = $this->queryJson(self::QUERY_BASE.self::SCOPE_FEED_SITES."/".$domain, "PUT");
219
220 if(!$queryResult)
221 {
222 return false;
223 }
224
225 if($queryResult->getStatus() == self::HTTP_STATUS_NO_CONTENT)
226 {
227 return $this->getFeeds();
228 }
229 else
230 {
231 throw new \Exception('Query error! '.$queryResult->getStatus().': '.$queryResult->getResult());
232 }
233 }
234
235 public function verifyGetToken($domain, $dir)
236 {
237 $data = array(
238 "verificationMethod" => "FILE",
239 "site" => array(
240 "identifier" => self::SCOPE_DOMAIN_PROTOCOL.$domain.$dir,
241 "type" => "SITE"
242 )
243 );
244
245 $queryResult = $this->queryJson(
246 static::QUERY_VERIFY_TOKEN,
247 "POST",
248 Json::encode($data)
249 );
250
251 if(!$queryResult)
252 {
253 return false;
254 }
255
256 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
257 {
258 $result = Json::decode($queryResult->getResult());
259 return $result["token"];
260 }
261 else
262 {
263 throw new \Exception('Query error! '.$queryResult->getStatus().': '.$queryResult->getResult());
264 }
265 }
266
267 public function verifySite($domain, $dir)
268 {
269 $data = array(
270 "site" => array(
271 "identifier" => self::SCOPE_DOMAIN_PROTOCOL.$domain.$dir,
272 "type" => "SITE"
273 )
274 );
275
276 $queryResult = $this->queryJson(
277 self::QUERY_VERIFY,
278 "POST",
279 Json::encode($data)
280 );
281
282 if(!$queryResult)
283 {
284 return false;
285 }
286
287 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
288 {
289 return true;
290 }
291 else
292 {
293 throw new \Exception('Query error! '.$queryResult->getStatus().': '.$queryResult->getResult());
294 }
295 }
296
297
298 protected function queryJson($scope, $method = "GET", $data = null, $bSkipRefreshAuth = false)
299 {
300 return $this->query($scope, $method, $data, $bSkipRefreshAuth, 'application/json');
301 }
302
303 protected function query($scope, $method = "GET", $data = null, $bSkipRefreshAuth = false, $contentType = 'application/json')
304 {
305 if($this->engineSettings['AUTH'])
306 {
307 $http = new HttpClient();
308 $http->setHeader("Authorization", 'Bearer '.$this->engineSettings['AUTH']['access_token']);
309
310/*
311 $http->setAdditionalHeaders(
312 array(
313 'Authorization' => ,
314 'GData-Version' => '2'
315 )
316 );
317*/
318
319 switch($method)
320 {
321 case 'GET':
322 $result = $http->get($scope);
323 break;
324 case 'POST':
325 case 'PUT':
326 $http->setHeader("Content-Type", $contentType);
327
328 if(!$data)
329 {
330 $http->setHeader("Content-Length", 0);
331 }
332
333 $result = $http->query($method, $scope, $data);
334
335 break;
336 case 'DELETE':
337
338 break;
339 }
340
341 if($http->getStatus() == 401 && !$bSkipRefreshAuth)
342 {
343 if($this->checkAuthExpired(true))
344 {
345 return $this->query($scope, $method, $data, true, $contentType);
346 }
347 }
348
349 return $http;
350 }
351 }
352}
353?>
checkAuthExpired($bGetNew)
Definition google.php:107
query($scope, $method="GET", $data=null, $bSkipRefreshAuth=false, $contentType='application/json')
Definition google.php:303
setAuthSettings($settings=null)
Definition google.php:87
verifyGetToken($domain, $dir)
Definition google.php:235
verifySite($domain, $dir)
Definition google.php:267
queryJson($scope, $method="GET", $data=null, $bSkipRefreshAuth=false)
Definition google.php:298
addSite($domain, $dir='/')
Definition google.php:216