Google_Client

The Google API Client http://code.google.com/p/google-api-php-client/.

Defined (1)

The class is defined in the following location(s).

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