/google-api/src/Google_Client.php

  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. // Check for the required json and curl extensions, the Google APIs PHP Client 
  19. // won't function without them. 
  20. if (! function_exists('curl_init')) { 
  21. throw new Exception('Google PHP API Client requires the CURL PHP extension'); 
  22.  
  23. if (! function_exists('json_decode')) { 
  24. throw new Exception('Google PHP API Client requires the JSON PHP extension'); 
  25.  
  26. if (! function_exists('http_build_query')) { 
  27. throw new Exception('Google PHP API Client requires http_build_query()'); 
  28.  
  29. if (! ini_get('date.timezone') && function_exists('date_default_timezone_set')) { 
  30. date_default_timezone_set('UTC'); 
  31.  
  32. // hack around with the include paths a bit so the library 'just works' 
  33. set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path()); 
  34.  
  35. require_once "config.php"; 
  36. // If a local configuration file is found, merge it's values with the default configuration 
  37. if (file_exists(dirname(__FILE__) . '/local_config.php')) { 
  38. $defaultConfig = $apiConfig; 
  39. require_once (dirname(__FILE__) . '/local_config.php'); 
  40. $apiConfig = array_merge($defaultConfig, $apiConfig); 
  41.  
  42. // Include the top level classes, they each include their own dependencies 
  43. require_once 'service/Google_Model.php'; 
  44. require_once 'service/Google_Service.php'; 
  45. require_once 'service/Google_ServiceResource.php'; 
  46. require_once 'auth/Google_AssertionCredentials.php'; 
  47. require_once 'auth/Google_Signer.php'; 
  48. require_once 'auth/Google_P12Signer.php'; 
  49. require_once 'service/Google_BatchRequest.php'; 
  50. require_once 'external/URITemplateParser.php'; 
  51. require_once 'auth/Google_Auth.php'; 
  52. require_once 'cache/Google_Cache.php'; 
  53. require_once 'io/Google_IO.php'; 
  54. require_once('service/Google_MediaFileUpload.php'); 
  55.  
  56. /** 
  57. * The Google API Client 
  58. * http://code.google.com/p/google-api-php-client/ 
  59. * 
  60. * @author Chris Chabot <chabotc@google.com> 
  61. * @author Chirag Shah <chirags@google.com> 
  62. */ 
  63. class Google_Client { 
  64. /** 
  65. * @static 
  66. * @var Google_Auth $auth 
  67. */ 
  68. static $auth; 
  69.  
  70. /** 
  71. * @static 
  72. * @var Google_IO $io 
  73. */ 
  74. static $io; 
  75.  
  76. /** 
  77. * @static 
  78. * @var Google_Cache $cache 
  79. */ 
  80. static $cache; 
  81.  
  82. /** 
  83. * @static 
  84. * @var boolean $useBatch 
  85. */ 
  86. static $useBatch = false; 
  87.  
  88. /** @var array $scopes */ 
  89. protected $scopes = array(); 
  90.  
  91. /** @var bool $useObjects */ 
  92. protected $useObjects = false; 
  93.  
  94. // definitions of services that are discovered. 
  95. protected $services = array(); 
  96.  
  97. // Used to track authenticated state, can't discover services after doing authenticate() 
  98. private $authenticated = false; 
  99.  
  100. public function __construct($config = array()) { 
  101. global $apiConfig; 
  102. $apiConfig = array_merge($apiConfig, $config); 
  103. self::$cache = new $apiConfig['cacheClass'](); 
  104. self::$auth = new $apiConfig['authClass'](); 
  105. self::$io = new $apiConfig['ioClass'](); 
  106.  
  107. /** 
  108. * Add a service 
  109. */ 
  110. public function addService($service, $version = false) { 
  111. global $apiConfig; 
  112. if ($this->authenticated) { 
  113. throw new Google_Exception('Cant add services after having authenticated'); 
  114. $this->services[$service] = array(); 
  115. if (isset($apiConfig['services'][$service])) { 
  116. // Merge the service descriptor with the default values 
  117. $this->services[$service] = array_merge($this->services[$service], $apiConfig['services'][$service]); 
  118.  
  119. public function authenticate($code = null) { 
  120. $service = $this->prepareService(); 
  121. $this->authenticated = true; 
  122. return self::$auth->authenticate($service, $code); 
  123.  
  124. /** 
  125. * @return array 
  126. * @visible For Testing 
  127. */ 
  128. public function prepareService() { 
  129. $service = array(); 
  130. $scopes = array(); 
  131. if ($this->scopes) { 
  132. $scopes = $this->scopes; 
  133. } else { 
  134. foreach ($this->services as $key => $val) { 
  135. if (isset($val['scope'])) { 
  136. if (is_array($val['scope'])) { 
  137. $scopes = array_merge($val['scope'], $scopes); 
  138. } else { 
  139. $scopes[] = $val['scope']; 
  140. } else { 
  141. $scopes[] = 'https://www.googleapis.com/auth/' . $key; 
  142. unset($val['discoveryURI']); 
  143. unset($val['scope']); 
  144. $service = array_merge($service, $val); 
  145. $service['scope'] = implode(' ', $scopes); 
  146. return $service; 
  147.  
  148. /** 
  149. * Set the OAuth 2.0 access token using the string that resulted from calling authenticate() 
  150. * or Google_Client#getAccessToken(). 
  151. * @param string $accessToken JSON encoded string containing in the following format: 
  152. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  153. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  154. */ 
  155. public function setAccessToken($accessToken) { 
  156. if ($accessToken == null || 'null' == $accessToken) { 
  157. $accessToken = null; 
  158. self::$auth->setAccessToken($accessToken); 
  159.  
  160. /** 
  161. * Set the type of Auth class the client should use. 
  162. * @param string $authClassName 
  163. */ 
  164. public function setAuthClass($authClassName) { 
  165. self::$auth = new $authClassName(); 
  166.  
  167. /** 
  168. * Construct the OAuth 2.0 authorization request URI. 
  169. * @return string 
  170. */ 
  171. public function createAuthUrl() { 
  172. $service = $this->prepareService(); 
  173. return self::$auth->createAuthUrl($service['scope']); 
  174.  
  175. /** 
  176. * Get the OAuth 2.0 access token. 
  177. * @return string $accessToken JSON encoded string in the following format: 
  178. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  179. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  180. */ 
  181. public function getAccessToken() { 
  182. $token = self::$auth->getAccessToken(); 
  183. return (null == $token || 'null' == $token) ? null : $token; 
  184.  
  185. /** 
  186. * Returns if the access_token is expired. 
  187. * @return bool Returns True if the access_token is expired. 
  188. */ 
  189. public function isAccessTokenExpired() { 
  190. return self::$auth->isAccessTokenExpired(); 
  191.  
  192. /** 
  193. * Set the developer key to use, these are obtained through the API Console. 
  194. * @see http://code.google.com/apis/console-help/#generatingdevkeys 
  195. * @param string $developerKey 
  196. */ 
  197. public function setDeveloperKey($developerKey) { 
  198. self::$auth->setDeveloperKey($developerKey); 
  199.  
  200. /** 
  201. * Set OAuth 2.0 "state" parameter to achieve per-request customization. 
  202. * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 
  203. * @param string $state 
  204. */ 
  205. public function setState($state) { 
  206. self::$auth->setState($state); 
  207.  
  208. /** 
  209. * @param string $accessType Possible values for access_type include: 
  210. * {@code "offline"} to request offline access from the user. (This is the default value) 
  211. * {@code "online"} to request online access from the user. 
  212. */ 
  213. public function setAccessType($accessType) { 
  214. self::$auth->setAccessType($accessType); 
  215.  
  216. /** 
  217. * @param string $approvalPrompt Possible values for approval_prompt include: 
  218. * {@code "force"} to force the approval UI to appear. (This is the default value) 
  219. * {@code "auto"} to request auto-approval when possible. 
  220. */ 
  221. public function setApprovalPrompt($approvalPrompt) { 
  222. self::$auth->setApprovalPrompt($approvalPrompt); 
  223.  
  224. /** 
  225. * Set the application name, this is included in the User-Agent HTTP header. 
  226. * @param string $applicationName 
  227. */ 
  228. public function setApplicationName($applicationName) { 
  229. global $apiConfig; 
  230. $apiConfig['application_name'] = $applicationName; 
  231.  
  232. /** 
  233. * Set the OAuth 2.0 Client ID. 
  234. * @param string $clientId 
  235. */ 
  236. public function setClientId($clientId) { 
  237. global $apiConfig; 
  238. $apiConfig['oauth2_client_id'] = $clientId; 
  239. self::$auth->clientId = $clientId; 
  240.  
  241. /** 
  242. * Get the OAuth 2.0 Client ID. 
  243. */ 
  244. public function getClientId() { 
  245. return self::$auth->clientId; 
  246.  
  247. /** 
  248. * Set the OAuth 2.0 Client Secret. 
  249. * @param string $clientSecret 
  250. */ 
  251. public function setClientSecret($clientSecret) { 
  252. global $apiConfig; 
  253. $apiConfig['oauth2_client_secret'] = $clientSecret; 
  254. self::$auth->clientSecret = $clientSecret; 
  255.  
  256. /** 
  257. * Get the OAuth 2.0 Client Secret. 
  258. */ 
  259. public function getClientSecret() { 
  260. return self::$auth->clientSecret; 
  261.  
  262. /** 
  263. * Set the OAuth 2.0 Redirect URI. 
  264. * @param string $redirectUri 
  265. */ 
  266. public function setRedirectUri($redirectUri) { 
  267. global $apiConfig; 
  268. $apiConfig['oauth2_redirect_uri'] = $redirectUri; 
  269. self::$auth->redirectUri = $redirectUri; 
  270.  
  271. /** 
  272. * Get the OAuth 2.0 Redirect URI. 
  273. */ 
  274. public function getRedirectUri() { 
  275. return self::$auth->redirectUri; 
  276.  
  277. /** 
  278. * Fetches a fresh OAuth 2.0 access token with the given refresh token. 
  279. * @param string $refreshToken 
  280. * @return void 
  281. */ 
  282. public function refreshToken($refreshToken) { 
  283. self::$auth->refreshToken($refreshToken); 
  284.  
  285. /** 
  286. * Revoke an OAuth2 access token or refresh token. This method will revoke the current access 
  287. * token, if a token isn't provided. 
  288. * @throws Google_AuthException 
  289. * @param string|null $token The token (access token or a refresh token) that should be revoked. 
  290. * @return boolean Returns True if the revocation was successful, otherwise False. 
  291. */ 
  292. public function revokeToken($token = null) { 
  293. self::$auth->revokeToken($token); 
  294.  
  295. /** 
  296. * Verify an id_token. This method will verify the current id_token, if one 
  297. * isn't provided. 
  298. * @throws Google_AuthException 
  299. * @param string|null $token The token (id_token) that should be verified. 
  300. * @return Google_LoginTicket Returns an apiLoginTicket if the verification was 
  301. * successful. 
  302. */ 
  303. public function verifyIdToken($token = null) { 
  304. return self::$auth->verifyIdToken($token); 
  305.  
  306. /** 
  307. * @param Google_AssertionCredentials $creds 
  308. * @return void 
  309. */ 
  310. public function setAssertionCredentials(Google_AssertionCredentials $creds) { 
  311. self::$auth->setAssertionCredentials($creds); 
  312.  
  313. /** 
  314. * This function allows you to overrule the automatically generated scopes,  
  315. * so that you can ask for more or less permission in the auth flow 
  316. * Set this before you call authenticate() though! 
  317. * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/moderator') 
  318. */ 
  319. public function setScopes($scopes) { 
  320. $this->scopes = is_string($scopes) ? explode(" ", $scopes) : $scopes; 
  321.  
  322. /** 
  323. * Returns the list of scopes set on the client 
  324. * @return array the list of scopes 
  325. * 
  326. */ 
  327. public function getScopes() { 
  328. return $this->scopes; 
  329.  
  330. /** 
  331. * If 'plus.login' is included in the list of requested scopes, you can use 
  332. * this method to define types of app activities that your app will write. 
  333. * You can find a list of available types here: 
  334. * @link https://developers.google.com/+/api/moment-types 
  335. * 
  336. * @param array $requestVisibleActions Array of app activity types 
  337. */ 
  338. public function setRequestVisibleActions($requestVisibleActions) { 
  339. self::$auth->requestVisibleActions = 
  340. join(" ", $requestVisibleActions); 
  341.  
  342. /** 
  343. * Declare if objects should be returned by the api service classes. 
  344. * 
  345. * @param boolean $useObjects True if objects should be returned by the service classes. 
  346. * False if associative arrays should be returned (default behavior). 
  347. * @experimental 
  348. */ 
  349. public function setUseObjects($useObjects) { 
  350. global $apiConfig; 
  351. $apiConfig['use_objects'] = $useObjects; 
  352.  
  353. /** 
  354. * Declare if objects should be returned by the api service classes. 
  355. * 
  356. * @param boolean $useBatch True if the experimental batch support should 
  357. * be enabled. Defaults to False. 
  358. * @experimental 
  359. */ 
  360. public function setUseBatch($useBatch) { 
  361. self::$useBatch = $useBatch; 
  362.  
  363. /** 
  364. * @static 
  365. * @return Google_Auth the implementation of apiAuth. 
  366. */ 
  367. public static function getAuth() { 
  368. return Google_Client::$auth; 
  369.  
  370. /** 
  371. * @static 
  372. * @return Google_IO the implementation of apiIo. 
  373. */ 
  374. public static function getIo() { 
  375. return Google_Client::$io; 
  376.  
  377. /** 
  378. * @return Google_Cache the implementation of apiCache. 
  379. */ 
  380. public function getCache() { 
  381. return Google_Client::$cache; 
  382.  
  383. // Exceptions that the Google PHP API Library can throw 
  384. class Google_Exception extends Exception {} 
  385. class Google_AuthException extends Google_Exception {} 
  386. class Google_CacheException extends Google_Exception {} 
  387. class Google_IOException extends Google_Exception {} 
  388. class Google_ServiceException extends Google_Exception { 
  389. /** 
  390. * Optional list of errors returned in a JSON body of an HTTP error response. 
  391. */ 
  392. protected $errors = array(); 
  393.  
  394. /** 
  395. * Override default constructor to add ability to set $errors. 
  396. * 
  397. * @param string $message 
  398. * @param int $code 
  399. * @param Exception|null $previous 
  400. * @param [{string, string}] errors List of errors returned in an HTTP 
  401. * response. Defaults to []. 
  402. */ 
  403. public function __construct($message, $code = 0, Exception $previous = null,  
  404. $errors = array()) { 
  405. if(version_compare(PHP_VERSION, '5.3.0') >= 0) { 
  406. parent::__construct($message, $code, $previous); 
  407. } else { 
  408. parent::__construct($message, $code); 
  409.  
  410. $this->errors = $errors; 
  411.  
  412. /** 
  413. * An example of the possible errors returned. 
  414. * 
  415. * { 
  416. * "domain": "global",  
  417. * "reason": "authError",  
  418. * "message": "Invalid Credentials",  
  419. * "locationType": "header",  
  420. * "location": "Authorization",  
  421. * } 
  422. * 
  423. * @return [{string, string}] List of errors return in an HTTP response or []. 
  424. */ 
  425. public function getErrors() { 
  426. return $this->errors; 
.