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';
29 const QUERY_BASE =
'https://www.googleapis.com/webmasters/v3/';
31 const QUERY_USER =
'https://www.googleapis.com/oauth2/v3/userinfo';
32 const QUERY_VERIFY =
'https://www.googleapis.com/siteVerification/v1/webResource?verificationMethod=FILE';
66 return $this->
getInterface()->getAuthUrl($this->engine[
'REDIRECT_URI']);
71 if($this->authInterface ===
null)
73 $this->authInterface = new \CGoogleOAuthInterface($this->engine[
'CLIENT_ID'], $this->engine[
'CLIENT_SECRET']);
74 $this->authInterface->setScope($this->
getScope());
76 if($this->engineSettings[
'AUTH'])
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']);
89 if($settings ===
null)
94 if($settings instanceof \CGoogleOAuthInterface)
97 'access_token' => $settings->getToken(),
98 'refresh_token' => $settings->getRefreshToken(),
99 'expires_in' => $settings->getAccessTokenExpires()
103 $this->engineSettings[
'AUTH'] = $settings;
110 if(!$ob->checkAccessToken())
120 if($ob->getNewAccessToken())
126 throw new \Exception($ob->getError());
134 if($ob->getAccessToken($this->engine[
'REDIRECT_URI']))
136 unset($this->engineSettings[
'AUTH_USER']);
142 throw new \Exception($ob->getError());
149 if(!isset($this->engineSettings[
'AUTH_USER']) || !is_array($this->engineSettings[
'AUTH_USER']))
151 $queryResult = $this->
queryJson(self::QUERY_USER);
158 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
160 $res = Json::decode($queryResult->getResult());
163 $this->engineSettings[
'AUTH_USER'] = $APPLICATION->convertCharsetArray($res,
'utf-8', LANG_CHARSET);
166 return $this->engineSettings[
'AUTH_USER'];
170 throw new \Exception(
'Query error! '.$queryResult->getStatus().
': '.$queryResult->getResult());
174 return $this->engineSettings[
'AUTH_USER'];
180 $queryResult = $this->
queryJson(self::QUERY_BASE.self::SCOPE_FEED_SITES);
181 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
183 $result = Json::decode($queryResult->getResult());
185 if(is_array($result) && is_array($result[
'siteEntry']))
187 foreach($result[
'siteEntry'] as $key => $siteInfo)
189 $siteUrlInfo = parse_url($siteInfo[
'siteUrl']);
193 $hostKey = \CBXPunycode::toASCII($siteUrlInfo[
"host"], $errors);
194 if(count($errors) > 0)
196 $hostKey = $siteUrlInfo[
"host"];
199 $response[$hostKey] = array(
200 'binded' => $siteInfo[
"permissionLevel"] !==
"siteRestrictedUser",
201 'verified' => $siteInfo[
"permissionLevel"] !==
"siteRestrictedUser"
202 && $siteInfo[
"permissionLevel"] !==
"siteUnverifiedUser",
212 throw new \Exception(
'Query error! '.$queryResult->getStatus().
': '.$queryResult->getResult());
218 $queryResult = $this->
queryJson(self::QUERY_BASE.self::SCOPE_FEED_SITES.
"/".$domain,
"PUT");
225 if($queryResult->getStatus() == self::HTTP_STATUS_NO_CONTENT)
231 throw new \Exception(
'Query error! '.$queryResult->getStatus().
': '.$queryResult->getResult());
238 "verificationMethod" =>
"FILE",
240 "identifier" => self::SCOPE_DOMAIN_PROTOCOL.$domain.$dir,
246 static::QUERY_VERIFY_TOKEN,
256 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
258 $result = Json::decode($queryResult->getResult());
259 return $result[
"token"];
263 throw new \Exception(
'Query error! '.$queryResult->getStatus().
': '.$queryResult->getResult());
271 "identifier" => self::SCOPE_DOMAIN_PROTOCOL.$domain.$dir,
287 if($queryResult->getStatus() == self::HTTP_STATUS_OK && $queryResult->getResult() <>
'')
293 throw new \Exception(
'Query error! '.$queryResult->getStatus().
': '.$queryResult->getResult());
298 protected function queryJson(
$scope, $method =
"GET", $data =
null, $bSkipRefreshAuth =
false)
300 return $this->
query(
$scope, $method, $data, $bSkipRefreshAuth,
'application/json');
303 protected function query(
$scope, $method =
"GET", $data =
null, $bSkipRefreshAuth =
false, $contentType =
'application/json')
305 if($this->engineSettings[
'AUTH'])
308 $http->setHeader(
"Authorization",
'Bearer '.$this->engineSettings[
'AUTH'][
'access_token']);
322 $result = $http->get(
$scope);
326 $http->setHeader(
"Content-Type", $contentType);
330 $http->setHeader(
"Content-Length", 0);
333 $result = $http->query($method,
$scope, $data);
341 if($http->getStatus() == 401 && !$bSkipRefreshAuth)
345 return $this->
query(
$scope, $method, $data,
true, $contentType);
checkAuthExpired($bGetNew)
query($scope, $method="GET", $data=null, $bSkipRefreshAuth=false, $contentType='application/json')
setAuthSettings($settings=null)
verifyGetToken($domain, $dir)
verifySite($domain, $dir)
queryJson($scope, $method="GET", $data=null, $bSkipRefreshAuth=false)
const SCOPE_FEED_MESSAGES
addSite($domain, $dir='/')
const SCOPE_DOMAIN_PROTOCOL
const SCOPE_FEED_CRAWLISSUES