/google_api/Config.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. /** 
  19. * A class to contain the library configuration for the Google API client. 
  20. */ 
  21. class Google_Config 
  22. const GZIP_DISABLED = true; 
  23. const GZIP_ENABLED = false; 
  24. const GZIP_UPLOADS_ENABLED = true; 
  25. const GZIP_UPLOADS_DISABLED = false; 
  26. const USE_AUTO_IO_SELECTION = "auto"; 
  27. const TASK_RETRY_NEVER = 0; 
  28. const TASK_RETRY_ONCE = 1; 
  29. const TASK_RETRY_ALWAYS = -1; 
  30. protected $configuration; 
  31.  
  32. /** 
  33. * Create a new Google_Config. Can accept an ini file location with the 
  34. * local configuration. For example: 
  35. * application_name="My App" 
  36. * 
  37. * @param [$ini_file_location] - optional - The location of the ini file to load 
  38. */ 
  39. public function __construct($ini_file_location = null) 
  40. $this->configuration = array( 
  41. // The application_name is included in the User-Agent HTTP header. 
  42. 'application_name' => '',  
  43.  
  44. // Which Authentication, Storage and HTTP IO classes to use. 
  45. 'auth_class' => 'Google_Auth_OAuth2',  
  46. 'io_class' => self::USE_AUTO_IO_SELECTION,  
  47. 'cache_class' => 'Google_Cache_File',  
  48. 'logger_class' => 'Google_Logger_Null',  
  49.  
  50. // Don't change these unless you're working against a special development 
  51. // or testing environment. 
  52. 'base_path' => 'https://www.googleapis.com',  
  53.  
  54. // Definition of class specific values, like file paths and so on. 
  55. 'classes' => array( 
  56. 'Google_IO_Abstract' => array( 
  57. 'request_timeout_seconds' => 100,  
  58. ),  
  59. 'Google_Logger_Abstract' => array( 
  60. 'level' => 'debug',  
  61. 'log_format' => "[%datetime%] %level%: %message% %context%\n",  
  62. 'date_format' => 'd/M/Y:H:i:s O',  
  63. 'allow_newlines' => true 
  64. ),  
  65. 'Google_Logger_File' => array( 
  66. 'file' => 'php://stdout',  
  67. 'mode' => 0640,  
  68. 'lock' => false,  
  69. ),  
  70. 'Google_Http_Request' => array( 
  71. // Disable the use of gzip on calls if set to true. Defaults to false. 
  72. 'disable_gzip' => self::GZIP_ENABLED,  
  73.  
  74. // We default gzip to disabled on uploads even if gzip is otherwise 
  75. // enabled, due to some issues seen with small packet sizes for uploads. 
  76. // Please test with this option before enabling gzip for uploads in 
  77. // a production environment. 
  78. 'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,  
  79. ),  
  80. // If you want to pass in OAuth 2.0 settings, they will need to be 
  81. // structured like this. 
  82. 'Google_Auth_OAuth2' => array( 
  83. // Keys for OAuth 2.0 access, see the API console at 
  84. // https://developers.google.com/console 
  85. 'client_id' => '',  
  86. 'client_secret' => '',  
  87. 'redirect_uri' => '',  
  88.  
  89. // Simple API access key, also from the API console. Ensure you get 
  90. // a Server key, and not a Browser key. 
  91. 'developer_key' => '',  
  92.  
  93. // Other parameters. 
  94. 'hd' => '',  
  95. 'prompt' => '',  
  96. 'openid.realm' => '',  
  97. 'include_granted_scopes' => '',  
  98. 'login_hint' => '',  
  99. 'request_visible_actions' => '',  
  100. 'access_type' => 'online',  
  101. 'approval_prompt' => 'auto',  
  102. 'federated_signon_certs_url' => 
  103. 'https://www.googleapis.com/oauth2/v1/certs',  
  104. ),  
  105. 'Google_Task_Runner' => array( 
  106. // Delays are specified in seconds 
  107. 'initial_delay' => 1,  
  108. 'max_delay' => 60,  
  109. // Base number for exponential backoff 
  110. 'factor' => 2,  
  111. // A random number between -jitter and jitter will be added to the 
  112. // factor on each iteration to allow for better distribution of 
  113. // retries. 
  114. 'jitter' => .5,  
  115. // Maximum number of retries allowed 
  116. 'retries' => 0 
  117. ),  
  118. 'Google_Service_Exception' => array( 
  119. 'retry_map' => array( 
  120. '500' => self::TASK_RETRY_ALWAYS,  
  121. '503' => self::TASK_RETRY_ALWAYS,  
  122. 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS,  
  123. 'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS 
  124. ),  
  125. 'Google_IO_Exception' => array( 
  126. 'retry_map' => !extension_loaded('curl') ? array() : array( 
  127. CURLE_COULDNT_RESOLVE_HOST => self::TASK_RETRY_ALWAYS,  
  128. CURLE_COULDNT_CONNECT => self::TASK_RETRY_ALWAYS,  
  129. CURLE_OPERATION_TIMEOUTED => self::TASK_RETRY_ALWAYS,  
  130. CURLE_SSL_CONNECT_ERROR => self::TASK_RETRY_ALWAYS,  
  131. CURLE_GOT_NOTHING => self::TASK_RETRY_ALWAYS 
  132. ),  
  133. // Set a default directory for the file cache. 
  134. 'Google_Cache_File' => array( 
  135. 'directory' => sys_get_temp_dir() . '/Google_Client' 
  136. ),  
  137. ); 
  138. if ($ini_file_location) { 
  139. $ini = parse_ini_file($ini_file_location, true); 
  140. if (is_array($ini) && count($ini)) { 
  141. $merged_configuration = $ini + $this->configuration; 
  142. if (isset($ini['classes']) && isset($this->configuration['classes'])) { 
  143. $merged_configuration['classes'] = $ini['classes'] + $this->configuration['classes']; 
  144. $this->configuration = $merged_configuration; 
  145.  
  146. /** 
  147. * Set configuration specific to a given class. 
  148. * $config->setClassConfig('Google_Cache_File',  
  149. * array('directory' => '/tmp/cache')); 
  150. * @param $class string The class name for the configuration 
  151. * @param $config string key or an array of configuration values 
  152. * @param $value string optional - if $config is a key, the value 
  153. */ 
  154. public function setClassConfig($class, $config, $value = null) 
  155. if (!is_array($config)) { 
  156. if (!isset($this->configuration['classes'][$class])) { 
  157. $this->configuration['classes'][$class] = array(); 
  158. $this->configuration['classes'][$class][$config] = $value; 
  159. } else { 
  160. $this->configuration['classes'][$class] = $config; 
  161.  
  162. public function getClassConfig($class, $key = null) 
  163. if (!isset($this->configuration['classes'][$class])) { 
  164. return null; 
  165. if ($key === null) { 
  166. return $this->configuration['classes'][$class]; 
  167. } else { 
  168. return $this->configuration['classes'][$class][$key]; 
  169.  
  170. /** 
  171. * Return the configured cache class. 
  172. * @return string 
  173. */ 
  174. public function getCacheClass() 
  175. return $this->configuration['cache_class']; 
  176.  
  177. /** 
  178. * Return the configured logger class. 
  179. * @return string 
  180. */ 
  181. public function getLoggerClass() 
  182. return $this->configuration['logger_class']; 
  183.  
  184. /** 
  185. * Return the configured Auth class. 
  186. * @return string 
  187. */ 
  188. public function getAuthClass() 
  189. return $this->configuration['auth_class']; 
  190.  
  191. /** 
  192. * Set the auth class. 
  193. * 
  194. * @param $class string the class name to set 
  195. */ 
  196. public function setAuthClass($class) 
  197. $prev = $this->configuration['auth_class']; 
  198. if (!isset($this->configuration['classes'][$class]) && 
  199. isset($this->configuration['classes'][$prev])) { 
  200. $this->configuration['classes'][$class] = 
  201. $this->configuration['classes'][$prev]; 
  202. $this->configuration['auth_class'] = $class; 
  203.  
  204. /** 
  205. * Set the IO class. 
  206. * 
  207. * @param $class string the class name to set 
  208. */ 
  209. public function setIoClass($class) 
  210. $prev = $this->configuration['io_class']; 
  211. if (!isset($this->configuration['classes'][$class]) && 
  212. isset($this->configuration['classes'][$prev])) { 
  213. $this->configuration['classes'][$class] = 
  214. $this->configuration['classes'][$prev]; 
  215. $this->configuration['io_class'] = $class; 
  216.  
  217. /** 
  218. * Set the cache class. 
  219. * 
  220. * @param $class string the class name to set 
  221. */ 
  222. public function setCacheClass($class) 
  223. $prev = $this->configuration['cache_class']; 
  224. if (!isset($this->configuration['classes'][$class]) && 
  225. isset($this->configuration['classes'][$prev])) { 
  226. $this->configuration['classes'][$class] = 
  227. $this->configuration['classes'][$prev]; 
  228. $this->configuration['cache_class'] = $class; 
  229.  
  230. /** 
  231. * Set the logger class. 
  232. * 
  233. * @param $class string the class name to set 
  234. */ 
  235. public function setLoggerClass($class) 
  236. $prev = $this->configuration['logger_class']; 
  237. if (!isset($this->configuration['classes'][$class]) && 
  238. isset($this->configuration['classes'][$prev])) { 
  239. $this->configuration['classes'][$class] = 
  240. $this->configuration['classes'][$prev]; 
  241. $this->configuration['logger_class'] = $class; 
  242.  
  243. /** 
  244. * Return the configured IO class. 
  245. * 
  246. * @return string 
  247. */ 
  248. public function getIoClass() 
  249. return $this->configuration['io_class']; 
  250.  
  251. /** 
  252. * Set the application name, this is included in the User-Agent HTTP header. 
  253. * @param string $name 
  254. */ 
  255. public function setApplicationName($name) 
  256. $this->configuration['application_name'] = $name; 
  257.  
  258. /** 
  259. * @return string the name of the application 
  260. */ 
  261. public function getApplicationName() 
  262. return $this->configuration['application_name']; 
  263.  
  264. /** 
  265. * Set the client ID for the auth class. 
  266. * @param $clientId string - the API console client ID 
  267. */ 
  268. public function setClientId($clientId) 
  269. $this->setAuthConfig('client_id', $clientId); 
  270.  
  271. /** 
  272. * Set the client secret for the auth class. 
  273. * @param $secret string - the API console client secret 
  274. */ 
  275. public function setClientSecret($secret) 
  276. $this->setAuthConfig('client_secret', $secret); 
  277.  
  278. /** 
  279. * Set the redirect uri for the auth class. Note that if using the 
  280. * Javascript based sign in flow, this should be the string 'postmessage'. 
  281. * 
  282. * @param $uri string - the URI that users should be redirected to 
  283. */ 
  284. public function setRedirectUri($uri) 
  285. $this->setAuthConfig('redirect_uri', $uri); 
  286.  
  287. /** 
  288. * Set the app activities for the auth class. 
  289. * @param $rva string a space separated list of app activity types 
  290. */ 
  291. public function setRequestVisibleActions($rva) 
  292. $this->setAuthConfig('request_visible_actions', $rva); 
  293.  
  294. /** 
  295. * Set the the access type requested (offline or online.) 
  296. * @param $access string - the access type 
  297. */ 
  298. public function setAccessType($access) 
  299. $this->setAuthConfig('access_type', $access); 
  300.  
  301. /** 
  302. * Set when to show the approval prompt (auto or force) 
  303. * @param $approval string - the approval request 
  304. */ 
  305. public function setApprovalPrompt($approval) 
  306. $this->setAuthConfig('approval_prompt', $approval); 
  307.  
  308. /** 
  309. * Set the login hint (email address or sub identifier) 
  310. * @param $hint string 
  311. */ 
  312. public function setLoginHint($hint) 
  313. $this->setAuthConfig('login_hint', $hint); 
  314.  
  315. /** 
  316. * Set the developer key for the auth class. Note that this is separate value 
  317. * from the client ID - if it looks like a URL, its a client ID! 
  318. * @param $key string - the API console developer key 
  319. */ 
  320. public function setDeveloperKey($key) 
  321. $this->setAuthConfig('developer_key', $key); 
  322.  
  323. /** 
  324. * Set the hd (hosted domain) parameter streamlines the login process for 
  325. * Google Apps hosted accounts. By including the domain of the user, you 
  326. * restrict sign-in to accounts at that domain.  
  327. *  
  328. * This should not be used to ensure security on your application - check 
  329. * the hd values within an id token (@see Google_Auth_LoginTicket) after sign 
  330. * in to ensure that the user is from the domain you were expecting. 
  331. * 
  332. * @param $hd string - the domain to use. 
  333. */ 
  334. public function setHostedDomain($hd) 
  335. $this->setAuthConfig('hd', $hd); 
  336.  
  337. /** 
  338. * Set the prompt hint. Valid values are none, consent and select_account. 
  339. * If no value is specified and the user has not previously authorized 
  340. * access, then the user is shown a consent screen. 
  341. * @param $prompt string 
  342. */ 
  343. public function setPrompt($prompt) 
  344. $this->setAuthConfig('prompt', $prompt); 
  345.  
  346. /** 
  347. * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth 
  348. * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which 
  349. * an authentication request is valid. 
  350. * @param $realm string - the URL-space to use. 
  351. */ 
  352. public function setOpenidRealm($realm) 
  353. $this->setAuthConfig('openid.realm', $realm); 
  354.  
  355. /** 
  356. * If this is provided with the value true, and the authorization request is 
  357. * granted, the authorization will include any previous authorizations 
  358. * granted to this user/application combination for other scopes. 
  359. * @param $include boolean - the URL-space to use. 
  360. */ 
  361. public function setIncludeGrantedScopes($include) 
  362. $this->setAuthConfig( 
  363. 'include_granted_scopes',  
  364. $include ? "true" : "false" 
  365. ); 
  366.  
  367. /** 
  368. * @return string the base URL to use for API calls 
  369. */ 
  370. public function getBasePath() 
  371. return $this->configuration['base_path']; 
  372.  
  373. /** 
  374. * Set the auth configuration for the current auth class. 
  375. * @param $key - the key to set 
  376. * @param $value - the parameter value 
  377. */ 
  378. private function setAuthConfig($key, $value) 
  379. if (!isset($this->configuration['classes'][$this->getAuthClass()])) { 
  380. $this->configuration['classes'][$this->getAuthClass()] = array(); 
  381. $this->configuration['classes'][$this->getAuthClass()][$key] = $value; 
.