| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2010 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
if (!class_exists('Google_Client')) { |
| 19 |
require_once dirname(__FILE__) . '/../autoload.php'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* The Google API Client |
| 24 |
* http://code.google.com/p/google-api-php-client/ |
| 25 |
*/ |
| 26 |
class Google_Client |
| 27 |
{ |
| 28 |
const LIBVER = "1.1.4"; |
| 29 |
const USER_AGENT_SUFFIX = "google-api-php-client/"; |
| 30 |
/** |
| 31 |
* @var Google_Auth_Abstract $auth |
| 32 |
*/ |
| 33 |
private $auth; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Google_IO_Abstract $io |
| 37 |
*/ |
| 38 |
private $io; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Google_Cache_Abstract $cache |
| 42 |
*/ |
| 43 |
private $cache; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var Google_Config $config |
| 47 |
*/ |
| 48 |
private $config; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var Google_Logger_Abstract $logger |
| 52 |
*/ |
| 53 |
private $logger; |
| 54 |
|
| 55 |
/** |
| 56 |
* @var boolean $deferExecution |
| 57 |
*/ |
| 58 |
private $deferExecution = false; |
| 59 |
|
| 60 |
/** @var array $scopes */ |
| 61 |
// Scopes requested by the client |
| 62 |
protected $requestedScopes = array(); |
| 63 |
|
| 64 |
// definitions of services that are discovered. |
| 65 |
protected $services = array(); |
| 66 |
|
| 67 |
// Used to track authenticated state, can't discover services after doing authenticate() |
| 68 |
private $authenticated = false; |
| 69 |
|
| 70 |
/** |
| 71 |
* Construct the Google Client. |
| 72 |
* |
| 73 |
* @param $config Google_Config or string for the ini file to load |
| 74 |
*/ |
| 75 |
public function __construct($config = null) |
| 76 |
{ |
| 77 |
if (is_string($config) && strlen($config)) { |
| 78 |
$config = new Google_Config($config); |
| 79 |
} else if ( !($config instanceof Google_Config)) { |
| 80 |
$config = new Google_Config(); |
| 81 |
|
| 82 |
if ($this->isAppEngine()) { |
| 83 |
// Automatically use Memcache if we're in AppEngine. |
| 84 |
$config->setCacheClass('Google_Cache_Memcache'); |
| 85 |
} |
| 86 |
|
| 87 |
if (version_compare(phpversion(), "5.3.4", "<=") || $this->isAppEngine()) { |
| 88 |
// Automatically disable compress.zlib, as currently unsupported. |
| 89 |
$config->setClassConfig('Google_Http_Request', 'disable_gzip', true); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ($config->getIoClass() == Google_Config::USE_AUTO_IO_SELECTION) { |
| 94 |
if (function_exists('curl_version') && function_exists('curl_exec') |
| 95 |
&& !$this->isAppEngine()) { |
| 96 |
$config->setIoClass("Google_IO_Curl"); |
| 97 |
} else { |
| 98 |
$config->setIoClass("Google_IO_Stream"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$this->config = $config; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get a string containing the version of the library. |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function getLibraryVersion() |
| 111 |
{ |
| 112 |
return self::LIBVER; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Attempt to exchange a code for an valid authentication token. |
| 117 |
* Helper wrapped around the OAuth 2.0 implementation. |
| 118 |
* |
| 119 |
* @param $code string code from accounts.google.com |
| 120 |
* @return string token |
| 121 |
*/ |
| 122 |
public function authenticate($code) |
| 123 |
{ |
| 124 |
$this->authenticated = true; |
| 125 |
return $this->getAuth()->authenticate($code); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Loads a service account key and parameters from a JSON |
| 130 |
* file from the Google Developer Console. Uses that and the |
| 131 |
* given array of scopes to return an assertion credential for |
| 132 |
* use with refreshTokenWithAssertionCredential. |
| 133 |
* |
| 134 |
* @param string $jsonLocation File location of the project-key.json. |
| 135 |
* @param array $scopes The scopes to assert. |
| 136 |
* @return Google_Auth_AssertionCredentials. |
| 137 |
* @ |
| 138 |
*/ |
| 139 |
public function loadServiceAccountJson($jsonLocation, $scopes) |
| 140 |
{ |
| 141 |
$data = json_decode(file_get_contents($jsonLocation)); |
| 142 |
if (isset($data->type) && $data->type == 'service_account') { |
| 143 |
// Service Account format. |
| 144 |
$cred = new Google_Auth_AssertionCredentials( |
| 145 |
$data->client_email, |
| 146 |
$scopes, |
| 147 |
$data->private_key |
| 148 |
); |
| 149 |
return $cred; |
| 150 |
} else { |
| 151 |
throw new Google_Exception("Invalid service account JSON file."); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Set the auth config from the JSON string provided. |
| 157 |
* This structure should match the file downloaded from |
| 158 |
* the "Download JSON" button on in the Google Developer |
| 159 |
* Console. |
| 160 |
* @param string $json the configuration json |
| 161 |
* @throws Google_Exception |
| 162 |
*/ |
| 163 |
public function setAuthConfig($json) |
| 164 |
{ |
| 165 |
$data = json_decode($json); |
| 166 |
$key = isset($data->installed) ? 'installed' : 'web'; |
| 167 |
if (!isset($data->$key)) { |
| 168 |
throw new Google_Exception("Invalid client secret JSON file."); |
| 169 |
} |
| 170 |
$this->setClientId($data->$key->client_id); |
| 171 |
$this->setClientSecret($data->$key->client_secret); |
| 172 |
if (isset($data->$key->redirect_uris)) { |
| 173 |
$this->setRedirectUri($data->$key->redirect_uris[0]); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Set the auth config from the JSON file in the path |
| 179 |
* provided. This should match the file downloaded from |
| 180 |
* the "Download JSON" button on in the Google Developer |
| 181 |
* Console. |
| 182 |
* @param string $file the file location of the client json |
| 183 |
*/ |
| 184 |
public function setAuthConfigFile($file) |
| 185 |
{ |
| 186 |
$this->setAuthConfig(file_get_contents($file)); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @throws Google_Auth_Exception |
| 191 |
* @return array |
| 192 |
* @visible For Testing |
| 193 |
*/ |
| 194 |
public function prepareScopes() |
| 195 |
{ |
| 196 |
if (empty($this->requestedScopes)) { |
| 197 |
throw new Google_Auth_Exception("No scopes specified"); |
| 198 |
} |
| 199 |
$scopes = implode(' ', $this->requestedScopes); |
| 200 |
return $scopes; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Set the OAuth 2.0 access token using the string that resulted from calling createAuthUrl() |
| 205 |
* or Google_Client#getAccessToken(). |
| 206 |
* @param string $accessToken JSON encoded string containing in the following format: |
| 207 |
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer", |
| 208 |
* "expires_in":3600, "id_token":"TOKEN", "created":1320790426} |
| 209 |
*/ |
| 210 |
public function setAccessToken($accessToken) |
| 211 |
{ |
| 212 |
if ($accessToken == 'null') { |
| 213 |
$accessToken = null; |
| 214 |
} |
| 215 |
$this->getAuth()->setAccessToken($accessToken); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Set the authenticator object |
| 222 |
* @param Google_Auth_Abstract $auth |
| 223 |
*/ |
| 224 |
public function setAuth(Google_Auth_Abstract $auth) |
| 225 |
{ |
| 226 |
$this->config->setAuthClass(get_class($auth)); |
| 227 |
$this->auth = $auth; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Set the IO object |
| 232 |
* @param Google_IO_Abstract $io |
| 233 |
*/ |
| 234 |
public function setIo(Google_IO_Abstract $io) |
| 235 |
{ |
| 236 |
$this->config->setIoClass(get_class($io)); |
| 237 |
$this->io = $io; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Set the Cache object |
| 242 |
* @param Google_Cache_Abstract $cache |
| 243 |
*/ |
| 244 |
public function setCache(Google_Cache_Abstract $cache) |
| 245 |
{ |
| 246 |
$this->config->setCacheClass(get_class($cache)); |
| 247 |
$this->cache = $cache; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Set the Logger object |
| 252 |
* @param Google_Logger_Abstract $logger |
| 253 |
*/ |
| 254 |
public function setLogger(Google_Logger_Abstract $logger) |
| 255 |
{ |
| 256 |
$this->config->setLoggerClass(get_class($logger)); |
| 257 |
$this->logger = $logger; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Construct the OAuth 2.0 authorization request URI. |
| 262 |
* @return string |
| 263 |
*/ |
| 264 |
public function createAuthUrl() |
| 265 |
{ |
| 266 |
$scopes = $this->prepareScopes(); |
| 267 |
return $this->getAuth()->createAuthUrl($scopes); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Get the OAuth 2.0 access token. |
| 272 |
* @return string $accessToken JSON encoded string in the following format: |
| 273 |
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer", |
| 274 |
* "expires_in":3600,"id_token":"TOKEN", "created":1320790426} |
| 275 |
*/ |
| 276 |
public function getAccessToken() |
| 277 |
{ |
| 278 |
$token = $this->getAuth()->getAccessToken(); |
| 279 |
// The response is json encoded, so could be the string null. |
| 280 |
// It is arguable whether this check should be here or lower |
| 281 |
// in the library. |
| 282 |
return (null == $token || 'null' == $token || '[]' == $token) ? null : $token; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Get the OAuth 2.0 refresh token. |
| 287 |
* @return string $refreshToken refresh token or null if not available |
| 288 |
*/ |
| 289 |
public function getRefreshToken() |
| 290 |
{ |
| 291 |
return $this->getAuth()->getRefreshToken(); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns if the access_token is expired. |
| 296 |
* @return bool Returns True if the access_token is expired. |
| 297 |
*/ |
| 298 |
public function isAccessTokenExpired() |
| 299 |
{ |
| 300 |
return $this->getAuth()->isAccessTokenExpired(); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Set OAuth 2.0 "state" parameter to achieve per-request customization. |
| 305 |
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 |
| 306 |
* @param string $state |
| 307 |
*/ |
| 308 |
public function setState($state) |
| 309 |
{ |
| 310 |
$this->getAuth()->setState($state); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* @param string $accessType Possible values for access_type include: |
| 315 |
* {@code "offline"} to request offline access from the user. |
| 316 |
* {@code "online"} to request online access from the user. |
| 317 |
*/ |
| 318 |
public function setAccessType($accessType) |
| 319 |
{ |
| 320 |
$this->config->setAccessType($accessType); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* @param string $approvalPrompt Possible values for approval_prompt include: |
| 325 |
* {@code "force"} to force the approval UI to appear. (This is the default value) |
| 326 |
* {@code "auto"} to request auto-approval when possible. |
| 327 |
*/ |
| 328 |
public function setApprovalPrompt($approvalPrompt) |
| 329 |
{ |
| 330 |
$this->config->setApprovalPrompt($approvalPrompt); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Set the login hint, email address or sub id. |
| 335 |
* @param string $loginHint |
| 336 |
*/ |
| 337 |
public function setLoginHint($loginHint) |
| 338 |
{ |
| 339 |
$this->config->setLoginHint($loginHint); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Set the application name, this is included in the User-Agent HTTP header. |
| 344 |
* @param string $applicationName |
| 345 |
*/ |
| 346 |
public function setApplicationName($applicationName) |
| 347 |
{ |
| 348 |
$this->config->setApplicationName($applicationName); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Set the OAuth 2.0 Client ID. |
| 353 |
* @param string $clientId |
| 354 |
*/ |
| 355 |
public function setClientId($clientId) |
| 356 |
{ |
| 357 |
$this->config->setClientId($clientId); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Set the OAuth 2.0 Client Secret. |
| 362 |
* @param string $clientSecret |
| 363 |
*/ |
| 364 |
public function setClientSecret($clientSecret) |
| 365 |
{ |
| 366 |
$this->config->setClientSecret($clientSecret); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Set the OAuth 2.0 Redirect URI. |
| 371 |
* @param string $redirectUri |
| 372 |
*/ |
| 373 |
public function setRedirectUri($redirectUri) |
| 374 |
{ |
| 375 |
$this->config->setRedirectUri($redirectUri); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* If 'plus.login' is included in the list of requested scopes, you can use |
| 380 |
* this method to define types of app activities that your app will write. |
| 381 |
* You can find a list of available types here: |
| 382 |
* @link https://developers.google.com/+/api/moment-types |
| 383 |
* |
| 384 |
* @param array $requestVisibleActions Array of app activity types |
| 385 |
*/ |
| 386 |
public function setRequestVisibleActions($requestVisibleActions) |
| 387 |
{ |
| 388 |
if (is_array($requestVisibleActions)) { |
| 389 |
$requestVisibleActions = join(" ", $requestVisibleActions); |
| 390 |
} |
| 391 |
$this->config->setRequestVisibleActions($requestVisibleActions); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Set the developer key to use, these are obtained through the API Console. |
| 396 |
* @see http://code.google.com/apis/console-help/#generatingdevkeys |
| 397 |
* @param string $developerKey |
| 398 |
*/ |
| 399 |
public function setDeveloperKey($developerKey) |
| 400 |
{ |
| 401 |
$this->config->setDeveloperKey($developerKey); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Set the hd (hosted domain) parameter streamlines the login process for |
| 406 |
* Google Apps hosted accounts. By including the domain of the user, you |
| 407 |
* restrict sign-in to accounts at that domain. |
| 408 |
* @param $hd string - the domain to use. |
| 409 |
*/ |
| 410 |
public function setHostedDomain($hd) |
| 411 |
{ |
| 412 |
$this->config->setHostedDomain($hd); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Set the prompt hint. Valid values are none, consent and select_account. |
| 417 |
* If no value is specified and the user has not previously authorized |
| 418 |
* access, then the user is shown a consent screen. |
| 419 |
* @param $prompt string |
| 420 |
*/ |
| 421 |
public function setPrompt($prompt) |
| 422 |
{ |
| 423 |
$this->config->setPrompt($prompt); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth |
| 428 |
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which |
| 429 |
* an authentication request is valid. |
| 430 |
* @param $realm string - the URL-space to use. |
| 431 |
*/ |
| 432 |
public function setOpenidRealm($realm) |
| 433 |
{ |
| 434 |
$this->config->setOpenidRealm($realm); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* If this is provided with the value true, and the authorization request is |
| 439 |
* granted, the authorization will include any previous authorizations |
| 440 |
* granted to this user/application combination for other scopes. |
| 441 |
* @param $include boolean - the URL-space to use. |
| 442 |
*/ |
| 443 |
public function setIncludeGrantedScopes($include) |
| 444 |
{ |
| 445 |
$this->config->setIncludeGrantedScopes($include); |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Fetches a fresh OAuth 2.0 access token with the given refresh token. |
| 450 |
* @param string $refreshToken |
| 451 |
*/ |
| 452 |
public function refreshToken($refreshToken) |
| 453 |
{ |
| 454 |
$this->getAuth()->refreshToken($refreshToken); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access |
| 459 |
* token, if a token isn't provided. |
| 460 |
* @throws Google_Auth_Exception |
| 461 |
* @param string|null $token The token (access token or a refresh token) that should be revoked. |
| 462 |
* @return boolean Returns True if the revocation was successful, otherwise False. |
| 463 |
*/ |
| 464 |
public function revokeToken($token = null) |
| 465 |
{ |
| 466 |
return $this->getAuth()->revokeToken($token); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Verify an id_token. This method will verify the current id_token, if one |
| 471 |
* isn't provided. |
| 472 |
* @throws Google_Auth_Exception |
| 473 |
* @param string|null $token The token (id_token) that should be verified. |
| 474 |
* @return Google_Auth_LoginTicket Returns an apiLoginTicket if the verification was |
| 475 |
* successful. |
| 476 |
*/ |
| 477 |
public function verifyIdToken($token = null) |
| 478 |
{ |
| 479 |
return $this->getAuth()->verifyIdToken($token); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Verify a JWT that was signed with your own certificates. |
| 484 |
* |
| 485 |
* @param $id_token string The JWT token |
| 486 |
* @param $cert_location array of certificates |
| 487 |
* @param $audience string the expected consumer of the token |
| 488 |
* @param $issuer string the expected issuer, defaults to Google |
| 489 |
* @param [$max_expiry] the max lifetime of a token, defaults to MAX_TOKEN_LIFETIME_SECS |
| 490 |
* @return mixed token information if valid, false if not |
| 491 |
*/ |
| 492 |
public function verifySignedJwt($id_token, $cert_location, $audience, $issuer, $max_expiry = null) |
| 493 |
{ |
| 494 |
$auth = new Google_Auth_OAuth2($this); |
| 495 |
$certs = $auth->retrieveCertsFromLocation($cert_location); |
| 496 |
return $auth->verifySignedJwtWithCerts($id_token, $certs, $audience, $issuer, $max_expiry); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* @param $creds Google_Auth_AssertionCredentials |
| 501 |
*/ |
| 502 |
public function setAssertionCredentials(Google_Auth_AssertionCredentials $creds) |
| 503 |
{ |
| 504 |
$this->getAuth()->setAssertionCredentials($creds); |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* Set the scopes to be requested. Must be called before createAuthUrl(). |
| 509 |
* Will remove any previously configured scopes. |
| 510 |
* @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login', |
| 511 |
* 'https://www.googleapis.com/auth/moderator') |
| 512 |
*/ |
| 513 |
public function setScopes($scopes) |
| 514 |
{ |
| 515 |
$this->requestedScopes = array(); |
| 516 |
$this->addScope($scopes); |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* This functions adds a scope to be requested as part of the OAuth2.0 flow. |
| 521 |
* Will append any scopes not previously requested to the scope parameter. |
| 522 |
* A single string will be treated as a scope to request. An array of strings |
| 523 |
* will each be appended. |
| 524 |
* @param $scope_or_scopes string|array e.g. "profile" |
| 525 |
*/ |
| 526 |
public function addScope($scope_or_scopes) |
| 527 |
{ |
| 528 |
if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { |
| 529 |
$this->requestedScopes[] = $scope_or_scopes; |
| 530 |
} else if (is_array($scope_or_scopes)) { |
| 531 |
foreach ($scope_or_scopes as $scope) { |
| 532 |
$this->addScope($scope); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Returns the list of scopes requested by the client |
| 539 |
* @return array the list of scopes |
| 540 |
* |
| 541 |
*/ |
| 542 |
public function getScopes() |
| 543 |
{ |
| 544 |
return $this->requestedScopes; |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Declare whether batch calls should be used. This may increase throughput |
| 549 |
* by making multiple requests in one connection. |
| 550 |
* |
| 551 |
* @param boolean $useBatch True if the batch support should |
| 552 |
* be enabled. Defaults to False. |
| 553 |
*/ |
| 554 |
public function setUseBatch($useBatch) |
| 555 |
{ |
| 556 |
// This is actually an alias for setDefer. |
| 557 |
$this->setDefer($useBatch); |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Declare whether making API calls should make the call immediately, or |
| 562 |
* return a request which can be called with ->execute(); |
| 563 |
* |
| 564 |
* @param boolean $defer True if calls should not be executed right away. |
| 565 |
*/ |
| 566 |
public function setDefer($defer) |
| 567 |
{ |
| 568 |
$this->deferExecution = $defer; |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* Helper method to execute deferred HTTP requests. |
| 573 |
* |
| 574 |
* @param $request Google_Http_Request|Google_Http_Batch |
| 575 |
* @throws Google_Exception |
| 576 |
* @return object of the type of the expected class or array. |
| 577 |
*/ |
| 578 |
public function execute($request) |
| 579 |
{ |
| 580 |
if ($request instanceof Google_Http_Request) { |
| 581 |
$request->setUserAgent( |
| 582 |
$this->getApplicationName() |
| 583 |
. " " . self::USER_AGENT_SUFFIX |
| 584 |
. $this->getLibraryVersion() |
| 585 |
); |
| 586 |
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) { |
| 587 |
$request->enableGzip(); |
| 588 |
} |
| 589 |
$request->maybeMoveParametersToBody(); |
| 590 |
return Google_Http_REST::execute($this, $request); |
| 591 |
} else if ($request instanceof Google_Http_Batch) { |
| 592 |
return $request->execute(); |
| 593 |
} else { |
| 594 |
throw new Google_Exception("Do not know how to execute this type of object."); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Whether or not to return raw requests |
| 600 |
* @return boolean |
| 601 |
*/ |
| 602 |
public function shouldDefer() |
| 603 |
{ |
| 604 |
return $this->deferExecution; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* @return Google_Auth_Abstract Authentication implementation |
| 609 |
*/ |
| 610 |
public function getAuth() |
| 611 |
{ |
| 612 |
if (!isset($this->auth)) { |
| 613 |
$class = $this->config->getAuthClass(); |
| 614 |
$this->auth = new $class($this); |
| 615 |
} |
| 616 |
return $this->auth; |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* @return Google_IO_Abstract IO implementation |
| 621 |
*/ |
| 622 |
public function getIo() |
| 623 |
{ |
| 624 |
if (!isset($this->io)) { |
| 625 |
$class = $this->config->getIoClass(); |
| 626 |
$this->io = new $class($this); |
| 627 |
} |
| 628 |
return $this->io; |
| 629 |
} |
| 630 |
|
| 631 |
/** |
| 632 |
* @return Google_Cache_Abstract Cache implementation |
| 633 |
*/ |
| 634 |
public function getCache() |
| 635 |
{ |
| 636 |
if (!isset($this->cache)) { |
| 637 |
$class = $this->config->getCacheClass(); |
| 638 |
$this->cache = new $class($this); |
| 639 |
} |
| 640 |
return $this->cache; |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
* @return Google_Logger_Abstract Logger implementation |
| 645 |
*/ |
| 646 |
public function getLogger() |
| 647 |
{ |
| 648 |
if (!isset($this->logger)) { |
| 649 |
$class = $this->config->getLoggerClass(); |
| 650 |
$this->logger = new $class($this); |
| 651 |
} |
| 652 |
return $this->logger; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Retrieve custom configuration for a specific class. |
| 657 |
* @param $class string|object - class or instance of class to retrieve |
| 658 |
* @param $key string optional - key to retrieve |
| 659 |
* @return array |
| 660 |
*/ |
| 661 |
public function getClassConfig($class, $key = null) |
| 662 |
{ |
| 663 |
if (!is_string($class)) { |
| 664 |
$class = get_class($class); |
| 665 |
} |
| 666 |
return $this->config->getClassConfig($class, $key); |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Set configuration specific to a given class. |
| 671 |
* $config->setClassConfig('Google_Cache_File', |
| 672 |
* array('directory' => '/tmp/cache')); |
| 673 |
* @param $class string|object - The class name for the configuration |
| 674 |
* @param $config string key or an array of configuration values |
| 675 |
* @param $value string optional - if $config is a key, the value |
| 676 |
* |
| 677 |
*/ |
| 678 |
public function setClassConfig($class, $config, $value = null) |
| 679 |
{ |
| 680 |
if (!is_string($class)) { |
| 681 |
$class = get_class($class); |
| 682 |
} |
| 683 |
$this->config->setClassConfig($class, $config, $value); |
| 684 |
|
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* @return string the base URL to use for calls to the APIs |
| 689 |
*/ |
| 690 |
public function getBasePath() |
| 691 |
{ |
| 692 |
return $this->config->getBasePath(); |
| 693 |
} |
| 694 |
|
| 695 |
/** |
| 696 |
* @return string the name of the application |
| 697 |
*/ |
| 698 |
public function getApplicationName() |
| 699 |
{ |
| 700 |
return $this->config->getApplicationName(); |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* Are we running in Google AppEngine? |
| 705 |
* return bool |
| 706 |
*/ |
| 707 |
public function isAppEngine() |
| 708 |
{ |
| 709 |
return (isset($_SERVER['SERVER_SOFTWARE']) && |
| 710 |
strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false); |
| 711 |
} |
| 712 |
} |
| 713 |
|