1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
yandex.php
См. документацию.
1<?
8
9namespace Bitrix\Seo\Engine;
10
11use Bitrix\Main\Context;
12use Bitrix\Main\Web\HttpClient;
13use Bitrix\Main\Web\Json;
14use Bitrix\Seo\Engine;
15use Bitrix\Seo\IEngine;
16
17class Yandex extends Engine\YandexBase implements IEngine
18{
19 const ENGINE_ID = 'yandex';
20
21 const SERVICE_URL = "https://webmaster.yandex.ru/api/v2";
22 const API_BASE_URL = "https://api.webmaster.yandex.net/v3/user/";
23 const API_HOSTS_URL = "hosts/";
24 const API_SUMMARY_URL = "summary/";
25 const API_SAMPLES_URL = "links/external/samples/";
26 const API_POPULAR_URL = "search-queries/popular/";
27 const API_VERIFICATION_URL = "verification/";
28 const API_ORIGINAL_TEXTS_URL = "original-texts/";
29
30 const HOSTS_SERVICE = "host-list";
31 const HOST_VERIFY = "verify-host";
32 const HOST_INFO = "host-information";
33 const HOST_TOP_QUERIES = "top-queries";
34 const HOST_ORIGINAL_TEXTS = "original-texts";
35 const HOST_INDEXED = "indexed-urls";
36 const HOST_EXCLUDED = "excluded-urls";
37
41
42 const QUERY_USER = 'https://login.yandex.ru/info';
43
44 const VERIFIED_STATE_VERIFIED = "VERIFIED";
45 const VERIFIED_STATE_WAITING = "WAITING";
46 const VERIFIED_STATE_FAILED = "VERIFICATION_FAILED";
47 const VERIFIED_STATE_NEVER_VERIFIED = "NEVER_VERIFIED";
48 const VERIFIED_STATE_IN_PROGRESS = "IN_PROGRESS";
49
50 const INDEXING_STATE_OK = "OK";
51
52 private static $verificationTypes = array('DNS', 'HTML_FILE', 'META_TAG', 'WHOIS', 'TXT_FILE');
53
54 protected $engineId = 'yandex';
55 protected $arServiceList = array();
56 private $userId = NULL;
57 private $hostIds = array();
58
59 public function __construct()
60 {
61 parent::__construct();
62
63// save user ID from auth
64 if (isset($this->engineSettings['AUTH_USER']['id']))
65 $this->userId = $this->engineSettings['AUTH_USER']['id'];
66 }
67
77 private function getServiceUrl($userId = NULL, $hostId = NULL, $service = NULL, $params = NULL)
78 {
79 $url = self::API_BASE_URL;
80
81 if ($userId)
82 $url .= $userId . '/';
83 if ($hostId)
84 $url .= 'hosts/' . $hostId . '/';
85 if ($service)
86 $url .= $service;
87 if ($params)
88 {
89 if (is_array($params))
90 $params = '?' . http_build_query($params);
91 else
92 $params = '?' . str_replace('?', '', $params);
93
94 $url .= $params;
95 }
96
97 return $url;
98 }
99
100 // temporary hack
101 public function getAuthSettings()
102 {
103 return $this->engineSettings['AUTH'];
104 }
105
112 private function getHostId($domain)
113 {
114// get saved host ID
115 if (isset($this->hostIds[$domain]) && !empty($this->hostIds[$domain]))
116 return $this->hostIds[$domain];
117
118// else get host ID from API (host will be saved in local)
119 $hosts = $this->getFeeds();
120
121 return $hosts[$domain]['host_id'];
122 }
123
124 public function getFeeds()
125 {
126 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
127 $queryResult = $this->query($serviceUrl, 'GET');
128
129 if ($queryResult && $queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
130 {
131 $resultConverted = array();
132 $result = Json::decode($queryResult->getResult());
133 foreach ($result['hosts'] as $host)
134 {
135// if set main mirror - we must use them
136 if(array_key_exists("main_mirror", $host) && is_array($host["main_mirror"]) && !empty($host["main_mirror"]))
137 $host = array_merge($host, $host["main_mirror"]);
138
139// ascii_host_url must be equal unicode_host_url for latin URLs.
140// if it cyrillic URL - we need ASCII host.
141 $hostUrl = str_replace(array('http://', 'https://'), '', $host['ascii_host_url']);
142 $hostUrl = rtrim($hostUrl, '/');
143 $resultConverted[$hostUrl] = $host;
144
145// convert verified status in correct format
146 if ($host['verified'])
147 $resultConverted[$hostUrl]['verification'] = self::VERIFIED_STATE_VERIFIED;
148// save hostId in local var
149 $this->hostIds[$hostUrl] = $host['host_id'];
150 }
151
152// save found hosts to table
153 $this->processHosts();
154
155 return $resultConverted;
156 }
157 else
158 {
159 throw new Engine\YandexException($queryResult);
160 }
161 }
162
168 public function getSiteInfo($domain)
169 {
170 $result = array();
171
172 $result += $this->getSiteInfoGeneral($domain);
173 $result += $this->getSiteInfoStats($domain);
174
175 return array($domain => $result);
176 }
177
178 private function getSiteInfoGeneral($domain)
179 {
180 $domain = mb_strtolower($domain);
181 $hostId = $this->getHostId($domain);
182
183 $serviceUrl = $this->getServiceUrl($this->userId, $hostId);
184 $queryResult = $this->query($serviceUrl, 'GET');
185
186 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
187 return Json::decode($queryResult->getResult());
188 else
189 throw new Engine\YandexException($queryResult);
190 }
191
192 private function getSiteInfoStats($domain)
193 {
194 $domain = mb_strtolower($domain);
195 $hostId = $this->getHostId($domain);
196
197 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_SUMMARY_URL);
198 $queryResult = $this->query($serviceUrl, 'GET');
199
200 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
201 return Json::decode($queryResult->getResult());
202 else
203 throw new Engine\YandexException($queryResult);
204 }
205
206// todo: we can add info about external links like a popular queries
207
214 public function getSiteInfoQueries($domain)
215 {
216 $domain = mb_strtolower($domain);
217 $hostId = $this->getHostId($domain);
218
219// get TOTAL_SHOWS
220 $params = array(
221 "order_by" => "TOTAL_SHOWS",
222 "query_indicator" => "TOTAL_SHOWS",
223 );
224
225 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_POPULAR_URL, $params);
226// dirt hack - our construcotr not understand multiply params
227 $serviceUrl .= '&query_indicator=TOTAL_CLICKS';
228 $serviceUrl .= '&query_indicator=AVG_SHOW_POSITION';
229 $serviceUrl .= '&query_indicator=AVG_CLICK_POSITION';
230
231 $queryResult = $this->query($serviceUrl, 'GET');
232 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
233 $queriesShows = Json::decode($queryResult->getResult());
234 else
235 throw new Engine\YandexException($queryResult);
236
237// format out array
238 $result = array();
239 $totalShows = 0;
240 $totalClicks = 0;
241 foreach($queriesShows['queries'] as $key => $query)
242 {
243 $result[$key] = array(
244 'TEXT' => $query['query_text'],
245 'TOTAL_SHOWS' => $query['indicators']['TOTAL_SHOWS'],
246 'TOTAL_CLICKS' => $query['indicators']['TOTAL_CLICKS'],
247 'AVG_SHOW_POSITION' => is_null($query['indicators']['AVG_SHOW_POSITION']) ? '' :round($query['indicators']['AVG_SHOW_POSITION'], 1),
248 'AVG_CLICK_POSITION' => is_null($query['indicators']['AVG_CLICK_POSITION']) ? '' :round($query['indicators']['AVG_CLICK_POSITION'], 1),
249 );
250 $totalShows += $query['indicators']['TOTAL_SHOWS'];
251 $totalClicks += $query['indicators']['TOTAL_CLICKS'];
252 }
253
254 return array(
255 'QUERIES' => $result,
256 'DATE_FROM' => $queriesShows['date_from'],
257 'DATE_TO' => $queriesShows['date_to'],
258 'TOTAL_SHOWS' => $totalShows,
259 'TOTAL_CLICKS' => $totalClicks,
260 );
261 }
262
263 private function processHosts()
264 {
265 $existedDomains = \CSeoUtils::getDomainsList();
266
267 foreach($existedDomains as $domain)
268 {
269 $domain['DOMAIN'] = mb_strtolower($domain['DOMAIN']);
270
271 if(isset($this->hostIds[$domain['DOMAIN']]))
272 {
273 if(!is_array($this->engineSettings['SITES']))
274 $this->engineSettings['SITES'] = array();
275
276 $this->engineSettings['SITES'][$domain['DOMAIN']] = $this->hostIds[$domain['DOMAIN']];
277 }
278 }
279
280 $this->saveSettings();
281 }
282
283 public function getOriginalTexts($domain)
284 {
285 $domain = mb_strtolower($domain);
286 $hostId = $this->getHostId($domain);
287
288 $counter = 0;
289 $limit = self::ORIGINAL_TEXT_SHOW_COUNT;
290 $result = array(
291 'count' => 0,
292 'quota_remainder' => 0,
293 'can-add' =>false,
294 'original_texts' => array(),
295 );
296
297// recursive collect text ehilw not catch limit
298 while($counter < $limit)
299 {
300// default limit 10, may set other value
301 $params = array('offset' => $counter);
302
303 $stepResult = $this->getOriginalTextsRecursive($hostId, $params);
304 $result['count'] = $stepResult['count'];
305 $result['quota_remainder'] = $stepResult['quota_remainder'];
306 $result['can-add'] = intval($result['quota_remainder']) > 0;
307 $result['original_texts'] = array_merge($result['original_texts'], $stepResult['original_texts']);
308 $counter += count($stepResult['original_texts']);
309
310// if catch last text - exit
311 if($counter >= $result['count'])
312 break;
313 }
314
315 return $result;
316 }
317
326 private function getOriginalTextsRecursive($hostId, $params)
327 {
328 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL, $params);
329 $queryResult = $this->query($serviceUrl, 'GET', $params);
330
331 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
332 return Json::decode($queryResult->getResult());
333 else
334 throw new Engine\YandexException($queryResult);
335 }
336
337
346 public function addOriginalText($text, $domain)
347 {
348 $domain = mb_strtolower($domain);
349 $hostId = $this->getHostId($domain);
350
351// create JSON data in correct format
352 $data = array("content" => $text);
353 $data = Json::encode($data);
354 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL);
355 $queryResult = $this->query($serviceUrl, 'POST', $data);
356
357 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <> '')
358 return $queryResult->getResult();
359 else
360 throw new Engine\YandexException($queryResult);
361 }
362
363
371 public function addSite($domain)
372 {
373 $domain = mb_strtolower($domain);
374 $queryDomain = Context::getCurrent()->getRequest()->isHttps() ? 'https://' . $domain : $domain;
375
376// create JSON data in correct format
377 $data = array("host_url" => $queryDomain);
378 $data = Json::encode($data);
379 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
380 $queryResult = $this->query($serviceUrl, 'POST', $data);
381
382 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <> '')
383 return array($domain => true);
384 else
385 throw new Engine\YandexException($queryResult);
386 }
387
388
395 public function getVerifySiteUin($domain)
396 {
397 $domain = mb_strtolower($domain);
398 $hostId = $this->getHostId($domain);
399
400 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_VERIFICATION_URL);
401 $queryResult = $this->query($serviceUrl, 'GET');
402
403 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
404 {
405 $result = Json::decode($queryResult->getResult());
406 if ($result['verification_state'] != self::VERIFIED_STATE_VERIFIED)
407 return $result['verification_uin'];
408 else
409 return false; //already verify
410 }
411 else
412 {
413 throw new Engine\YandexException($queryResult);
414 }
415 }
416
417 public function verifySite($domain, $verType = 'HTML_FILE')
418 {
419 if (!in_array($verType, self::$verificationTypes))
420 return array('error' => array('message' => 'incorrect verification type'));
421
422 $domain = mb_strtolower($domain);
423 $hostId = $this->getHostId($domain);
424
425 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_VERIFICATION_URL, array('verification_type' => $verType));
426 $queryResult = $this->query($serviceUrl, 'POST');
427 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <> '')
428 {
429 $result = Json::decode($queryResult->getResult());
430
431 return array($domain => array('verification' => $result['verification_state']));
432 }
433 else
434 {
435 throw new Engine\YandexException($queryResult);
436 }
437 }
438
439
448 protected function queryOld($scope, $method = "GET", $data = NULL, $skipRefreshAuth = false)
449 {
450 if ($this->engineSettings['AUTH'])
451 {
452 $http = new \CHTTP();
453 $http->setAdditionalHeaders(
454 array(
455 'Authorization' => 'OAuth ' . $this->engineSettings['AUTH']['access_token'],
456 )
457 );
458 $http->setFollowRedirect(false);
459
460 switch ($method)
461 {
462 case 'GET':
463 $result = $http->get($scope);
464 break;
465 case 'POST':
466 $result = $http->post($scope, $data);
467 break;
468 case 'PUT':
469 $result = $http->httpQuery($method, $scope, $http->prepareData($data));
470 break;
471 case 'DELETE':
472
473 break;
474 }
475
476 if ($http->status == 401 && !$skipRefreshAuth)
477 {
478 if ($this->checkAuthExpired())
479 {
480 $this->queryOld($scope, $method, $data, true);
481 }
482 }
483
484 return $http;
485 }
486 }
487
497 protected function query($scope, $method = "GET", $data = NULL, $skipRefreshAuth = false)
498 {
499 if ($this->engineSettings['AUTH'])
500 {
501 $http = new HttpClient();
502 $http->setHeader('Authorization', 'OAuth ' . $this->engineSettings['AUTH']['access_token']);
503 $http->setRedirect(false);
504 switch ($method)
505 {
506 case 'GET':
507 $http->get($scope);
508 break;
509 case 'POST':
510 $http->setHeader('Content-type', 'application/json');
511 $http->post($scope, $data);
512 break;
513 case 'DELETE':
514 break;
515 }
516
517 if ($http->getStatus() == 401 && !$skipRefreshAuth)
518 {
519 if ($this->checkAuthExpired())
520 {
521 $this->query($scope, $method, $data, true);
522 }
523 }
524
525 return $http;
526 }
527 }
528}
529
530?>
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
__construct()
Определения yandex.php:59
queryOld($scope, $method="GET", $data=NULL, $skipRefreshAuth=false)
Определения yandex.php:448
verifySite($domain, $verType='HTML_FILE')
Определения yandex.php:417
addSite($domain)
Определения yandex.php:371
const HOST_TOP_QUERIES
Определения yandex.php:33
getOriginalTexts($domain)
Определения yandex.php:283
query($scope, $method="GET", $data=NULL, $skipRefreshAuth=false)
Определения yandex.php:497
const VERIFIED_STATE_FAILED
Определения yandex.php:46
getVerifySiteUin($domain)
Определения yandex.php:395
const HOST_INFO
Определения yandex.php:32
const API_HOSTS_URL
Определения yandex.php:23
const API_SUMMARY_URL
Определения yandex.php:24
const API_POPULAR_URL
Определения yandex.php:26
getSiteInfoQueries($domain)
Определения yandex.php:214
const API_SAMPLES_URL
Определения yandex.php:25
const HOSTS_SERVICE
Определения yandex.php:30
const INDEXING_STATE_OK
Определения yandex.php:50
const API_VERIFICATION_URL
Определения yandex.php:27
const VERIFIED_STATE_NEVER_VERIFIED
Определения yandex.php:47
const QUERY_USER
Определения yandex.php:42
const API_BASE_URL
Определения yandex.php:22
const HOST_ORIGINAL_TEXTS
Определения yandex.php:34
$engineId
Определения yandex.php:54
const VERIFIED_STATE_WAITING
Определения yandex.php:45
const API_ORIGINAL_TEXTS_URL
Определения yandex.php:28
getFeeds()
Определения yandex.php:124
const ORIGINAL_TEXT_MIN_LENGTH
Определения yandex.php:38
const HOST_INDEXED
Определения yandex.php:35
const ENGINE_ID
Определения yandex.php:19
const SERVICE_URL
Определения yandex.php:21
getSiteInfo($domain)
Определения yandex.php:168
const HOST_VERIFY
Определения yandex.php:31
const ORIGINAL_TEXT_MAX_LENGTH
Определения yandex.php:39
$arServiceList
Определения yandex.php:55
const HOST_EXCLUDED
Определения yandex.php:36
const VERIFIED_STATE_VERIFIED
Определения yandex.php:44
const VERIFIED_STATE_IN_PROGRESS
Определения yandex.php:48
const ORIGINAL_TEXT_SHOW_COUNT
Определения yandex.php:40
getAuthSettings()
Определения yandex.php:101
addOriginalText($text, $domain)
Определения yandex.php:346
saveSettings()
Определения engine.php:83
static getDomainsList()
Определения seo_utils.php:39
$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
$query
Определения get_search.php:11
Определения iengine.php:11
$host
Определения mysql_to_pgsql.php:32
$service
Определения payment.php:18
$counter
Определения options.php:5
if(empty($signedUserToken)) $key
Определения quickway.php:257
$text
Определения template_pdf.php:79
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$method
Определения index.php:27
$url
Определения iframe.php:7