55 private static $verificationTypes = array(
'DNS',
'HTML_FILE',
'META_TAG',
'WHOIS',
'TXT_FILE');
59 private $userId = NULL;
60 private $hostIds = array();
64 parent::__construct();
67 if (isset($this->engineSettings[
'AUTH_USER'][
'id']))
68 $this->userId = $this->engineSettings[
'AUTH_USER'][
'id'];
80 private function getServiceUrl($userId = NULL, $hostId = NULL, $service = NULL, $params = NULL)
85 $url .= $userId .
'/';
87 $url .=
'hosts/' . $hostId .
'/';
92 if (is_array($params))
93 $params =
'?' . http_build_query($params);
95 $params =
'?' . str_replace(
'?',
'', $params);
106 return $this->engineSettings[
'AUTH'];
115 private function getHostId($domain)
118 if (isset($this->hostIds[$domain]) && !empty($this->hostIds[$domain]))
119 return $this->hostIds[$domain];
124 return $hosts[$domain][
'host_id'];
129 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
130 $queryResult = $this->
query($serviceUrl,
'GET');
132 if ($queryResult && $queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
134 $resultConverted = array();
135 $result = Json::decode($queryResult->getResult());
136 foreach ($result[
'hosts'] as $host)
139 if(array_key_exists(
"main_mirror", $host) && is_array($host[
"main_mirror"]) && !empty($host[
"main_mirror"]))
140 $host = array_merge($host, $host[
"main_mirror"]);
144 $hostUrl = str_replace(array(
'http://',
'https://'),
'', $host[
'ascii_host_url']);
145 $hostUrl = rtrim($hostUrl,
'/');
146 $resultConverted[$hostUrl] = $host;
149 if ($host[
'verified'])
152 $this->hostIds[$hostUrl] = $host[
'host_id'];
156 $this->processHosts();
158 return $resultConverted;
175 $result += $this->getSiteInfoGeneral($domain);
176 $result += $this->getSiteInfoStats($domain);
178 return array($domain => $result);
181 private function getSiteInfoGeneral($domain)
183 $domain = ToLower($domain);
184 $hostId = $this->getHostId($domain);
186 $serviceUrl = $this->getServiceUrl($this->userId, $hostId);
187 $queryResult = $this->
query($serviceUrl,
'GET');
189 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
190 return Json::decode($queryResult->getResult());
195 private function getSiteInfoStats($domain)
197 $domain = ToLower($domain);
198 $hostId = $this->getHostId($domain);
200 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_SUMMARY_URL);
201 $queryResult = $this->
query($serviceUrl,
'GET');
203 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
204 return Json::decode($queryResult->getResult());
219 $domain = ToLower($domain);
220 $hostId = $this->getHostId($domain);
224 "order_by" =>
"TOTAL_SHOWS",
225 "query_indicator" =>
"TOTAL_SHOWS",
228 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_POPULAR_URL, $params);
230 $serviceUrl .=
'&query_indicator=TOTAL_CLICKS';
231 $serviceUrl .=
'&query_indicator=AVG_SHOW_POSITION';
232 $serviceUrl .=
'&query_indicator=AVG_CLICK_POSITION';
234 $queryResult = $this->
query($serviceUrl,
'GET');
235 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
236 $queriesShows = Json::decode($queryResult->getResult());
244 foreach($queriesShows[
'queries'] as $key => $query)
246 $result[$key] = array(
247 'TEXT' => $query[
'query_text'],
248 'TOTAL_SHOWS' => $query[
'indicators'][
'TOTAL_SHOWS'],
249 'TOTAL_CLICKS' => $query[
'indicators'][
'TOTAL_CLICKS'],
250 'AVG_SHOW_POSITION' => is_null($query[
'indicators'][
'AVG_SHOW_POSITION']) ?
'' :round($query[
'indicators'][
'AVG_SHOW_POSITION'], 1),
251 'AVG_CLICK_POSITION' => is_null($query[
'indicators'][
'AVG_CLICK_POSITION']) ?
'' :round($query[
'indicators'][
'AVG_CLICK_POSITION'], 1),
253 $totalShows += $query[
'indicators'][
'TOTAL_SHOWS'];
254 $totalClicks += $query[
'indicators'][
'TOTAL_CLICKS'];
258 'QUERIES' => $result,
259 'DATE_FROM' => $queriesShows[
'date_from'],
260 'DATE_TO' => $queriesShows[
'date_to'],
261 'TOTAL_SHOWS' => $totalShows,
262 'TOTAL_CLICKS' => $totalClicks,
266 private function processHosts()
268 $existedDomains = \CSeoUtils::getDomainsList();
270 foreach($existedDomains as $domain)
272 $domain[
'DOMAIN'] = ToLower($domain[
'DOMAIN']);
274 if(isset($this->hostIds[$domain[
'DOMAIN']]))
276 if(!is_array($this->engineSettings[
'SITES']))
277 $this->engineSettings[
'SITES'] = array();
279 $this->engineSettings[
'SITES'][$domain[
'DOMAIN']] = $this->hostIds[$domain[
'DOMAIN']];
288 $domain = ToLower($domain);
289 $hostId = $this->getHostId($domain);
295 'quota_remainder' => 0,
297 'original_texts' => array(),
301 while($counter < $limit)
304 $params = array(
'offset' => $counter);
306 $stepResult = $this->getOriginalTextsRecursive($hostId, $params);
307 $result[
'count'] = $stepResult[
'count'];
308 $result[
'quota_remainder'] = $stepResult[
'quota_remainder'];
309 $result[
'can-add'] = intval($result[
'quota_remainder']) > 0;
310 $result[
'original_texts'] = array_merge($result[
'original_texts'], $stepResult[
'original_texts']);
311 $counter += count($stepResult[
'original_texts']);
314 if($counter >= $result[
'count'])
329 private function getOriginalTextsRecursive($hostId, $params)
331 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL, $params);
332 $queryResult = $this->
query($serviceUrl,
'GET', $params);
334 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
335 return Json::decode($queryResult->getResult());
351 $domain = ToLower($domain);
352 $hostId = $this->getHostId($domain);
355 $data = array(
"content" => $text);
356 $data = Json::encode($data);
357 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL);
358 $queryResult = $this->
query($serviceUrl,
'POST', $data);
360 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <>
'')
361 return $queryResult->getResult();
376 $domain = ToLower($domain);
377 $queryDomain =
Context::getCurrent()->getRequest()->isHttps() ?
'https://' . $domain : $domain;
380 $data = array(
"host_url" => $queryDomain);
381 $data = Json::encode($data);
382 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
383 $queryResult = $this->
query($serviceUrl,
'POST', $data);
385 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <>
'')
386 return array($domain =>
true);
400 $domain = ToLower($domain);
401 $hostId = $this->getHostId($domain);
403 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_VERIFICATION_URL);
404 $queryResult = $this->
query($serviceUrl,
'GET');
406 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
408 $result = Json::decode($queryResult->getResult());
409 if ($result[
'verification_state'] != self::VERIFIED_STATE_VERIFIED)
410 return $result[
'verification_uin'];
422 if (!in_array($verType, self::$verificationTypes))
423 return array(
'error' => array(
'message' =>
'incorrect verification type'));
425 $domain = ToLower($domain);
426 $hostId = $this->getHostId($domain);
428 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_VERIFICATION_URL, array(
'verification_type' => $verType));
429 $queryResult = $this->
query($serviceUrl,
'POST');
430 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
432 $result = Json::decode($queryResult->getResult());
434 return array($domain => array(
'verification' => $result[
'verification_state']));
451 protected function queryOld($scope, $method =
"GET", $data = NULL, $skipRefreshAuth =
false)
453 if ($this->engineSettings[
'AUTH'])
455 $http = new \CHTTP();
456 $http->setAdditionalHeaders(
458 'Authorization' =>
'OAuth ' . $this->engineSettings[
'AUTH'][
'access_token'],
461 $http->setFollowRedirect(
false);
466 $result = $http->get($scope);
469 $result = $http->post($scope, $data);
472 $result = $http->httpQuery($method, $scope, $http->prepareData($data));
479 if ($http->status == 401 && !$skipRefreshAuth)
483 $this->
queryOld($scope, $method, $data,
true);
487 $http->result = Text\Encoding::convertEncoding($http->result,
'utf-8', LANG_CHARSET);
502 protected function query($scope, $method =
"GET", $data = NULL, $skipRefreshAuth =
false)
504 if ($this->engineSettings[
'AUTH'])
507 $http->setHeader(
'Authorization',
'OAuth ' . $this->engineSettings[
'AUTH'][
'access_token']);
508 $http->setRedirect(
false);
515 $http->setHeader(
'Content-type',
'application/json');
516 $http->post($scope, $data);
522 if ($http->getStatus() == 401 && !$skipRefreshAuth)
526 $this->
query($scope, $method, $data,
true);