52 private static $verificationTypes =
array(
'DNS',
'HTML_FILE',
'META_TAG',
'WHOIS',
'TXT_FILE');
56 private $userId = NULL;
57 private $hostIds =
array();
61 parent::__construct();
64 if (isset($this->engineSettings[
'AUTH_USER'][
'id']))
65 $this->userId = $this->engineSettings[
'AUTH_USER'][
'id'];
79 $url = self::API_BASE_URL;
84 $url .=
'hosts/' . $hostId .
'/';
103 return $this->engineSettings[
'AUTH'];
112 private function getHostId($domain)
115 if (isset($this->hostIds[$domain]) && !empty($this->hostIds[$domain]))
116 return $this->hostIds[$domain];
121 return $hosts[$domain][
'host_id'];
126 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
127 $queryResult = $this->
query($serviceUrl,
'GET');
129 if ($queryResult && $queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
131 $resultConverted =
array();
132 $result = Json::decode($queryResult->getResult());
136 if(array_key_exists(
"main_mirror",
$host) && is_array(
$host[
"main_mirror"]) && !empty(
$host[
"main_mirror"]))
141 $hostUrl = str_replace(
array(
'http://',
'https://'),
'',
$host[
'ascii_host_url']);
142 $hostUrl = rtrim($hostUrl,
'/');
143 $resultConverted[$hostUrl] =
$host;
146 if (
$host[
'verified'])
147 $resultConverted[$hostUrl][
'verification'] = self::VERIFIED_STATE_VERIFIED;
149 $this->hostIds[$hostUrl] =
$host[
'host_id'];
153 $this->processHosts();
155 return $resultConverted;
172 $result += $this->getSiteInfoGeneral($domain);
173 $result += $this->getSiteInfoStats($domain);
178 private function getSiteInfoGeneral($domain)
180 $domain = mb_strtolower($domain);
181 $hostId = $this->getHostId($domain);
183 $serviceUrl = $this->getServiceUrl($this->userId, $hostId);
184 $queryResult = $this->
query($serviceUrl,
'GET');
186 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
187 return Json::decode($queryResult->getResult());
192 private function getSiteInfoStats($domain)
194 $domain = mb_strtolower($domain);
195 $hostId = $this->getHostId($domain);
197 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_SUMMARY_URL);
198 $queryResult = $this->
query($serviceUrl,
'GET');
200 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
201 return Json::decode($queryResult->getResult());
216 $domain = mb_strtolower($domain);
217 $hostId = $this->getHostId($domain);
221 "order_by" =>
"TOTAL_SHOWS",
222 "query_indicator" =>
"TOTAL_SHOWS",
225 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_POPULAR_URL,
$params);
227 $serviceUrl .=
'&query_indicator=TOTAL_CLICKS';
228 $serviceUrl .=
'&query_indicator=AVG_SHOW_POSITION';
229 $serviceUrl .=
'&query_indicator=AVG_CLICK_POSITION';
231 $queryResult = $this->
query($serviceUrl,
'GET');
232 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
233 $queriesShows = Json::decode($queryResult->getResult());
241 foreach($queriesShows[
'queries'] as
$key =>
$query)
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),
250 $totalShows +=
$query[
'indicators'][
'TOTAL_SHOWS'];
251 $totalClicks +=
$query[
'indicators'][
'TOTAL_CLICKS'];
256 'DATE_FROM' => $queriesShows[
'date_from'],
257 'DATE_TO' => $queriesShows[
'date_to'],
258 'TOTAL_SHOWS' => $totalShows,
259 'TOTAL_CLICKS' => $totalClicks,
263 private function processHosts()
267 foreach($existedDomains as $domain)
269 $domain[
'DOMAIN'] = mb_strtolower($domain[
'DOMAIN']);
271 if(isset($this->hostIds[$domain[
'DOMAIN']]))
273 if(!is_array($this->engineSettings[
'SITES']))
274 $this->engineSettings[
'SITES'] =
array();
276 $this->engineSettings[
'SITES'][$domain[
'DOMAIN']] = $this->hostIds[$domain[
'DOMAIN']];
285 $domain = mb_strtolower($domain);
286 $hostId = $this->getHostId($domain);
289 $limit = self::ORIGINAL_TEXT_SHOW_COUNT;
292 'quota_remainder' => 0,
294 'original_texts' =>
array(),
303 $stepResult = $this->getOriginalTextsRecursive($hostId,
$params);
304 $result[
'count'] = $stepResult[
'count'];
305 $result[
'quota_remainder'] = $stepResult[
'quota_remainder'];
307 $result[
'original_texts'] = array_merge(
$result[
'original_texts'], $stepResult[
'original_texts']);
326 private function getOriginalTextsRecursive($hostId,
$params)
328 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL,
$params);
329 $queryResult = $this->
query($serviceUrl,
'GET',
$params);
331 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
332 return Json::decode($queryResult->getResult());
348 $domain = mb_strtolower($domain);
349 $hostId = $this->getHostId($domain);
354 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_ORIGINAL_TEXTS_URL);
355 $queryResult = $this->
query($serviceUrl,
'POST',
$data);
357 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <>
'')
358 return $queryResult->getResult();
373 $domain = mb_strtolower($domain);
374 $queryDomain = Context::getCurrent()->getRequest()->isHttps() ?
'https://' . $domain : $domain;
379 $serviceUrl = $this->getServiceUrl($this->userId, NULL, self::API_HOSTS_URL);
380 $queryResult = $this->
query($serviceUrl,
'POST',
$data);
382 if ($queryResult->getStatus() == self::HTTP_STATUS_CREATED && $queryResult->getResult() <>
'')
383 return array($domain =>
true);
397 $domain = mb_strtolower($domain);
398 $hostId = $this->getHostId($domain);
400 $serviceUrl = $this->getServiceUrl($this->userId, $hostId, self::API_VERIFICATION_URL);
401 $queryResult = $this->
query($serviceUrl,
'GET');
403 if ($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
405 $result = Json::decode($queryResult->getResult());
406 if (
$result[
'verification_state'] != self::VERIFIED_STATE_VERIFIED)
407 return $result[
'verification_uin'];
419 if (!in_array($verType, self::$verificationTypes))
420 return array(
'error' =>
array(
'message' =>
'incorrect verification type'));
422 $domain = mb_strtolower($domain);
423 $hostId = $this->getHostId($domain);
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() <>
'')
429 $result = Json::decode($queryResult->getResult());
450 if ($this->engineSettings[
'AUTH'])
452 $http = new \CHTTP();
453 $http->setAdditionalHeaders(
455 'Authorization' =>
'OAuth ' . $this->engineSettings[
'AUTH'][
'access_token'],
458 $http->setFollowRedirect(
false);
476 if ($http->status == 401 && !$skipRefreshAuth)
499 if ($this->engineSettings[
'AUTH'])
502 $http->setHeader(
'Authorization',
'OAuth ' . $this->engineSettings[
'AUTH'][
'access_token']);
503 $http->setRedirect(
false);
510 $http->setHeader(
'Content-type',
'application/json');
511 $http->post($scope,
$data);
517 if ($http->getStatus() == 401 && !$skipRefreshAuth)