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