/lib/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. private $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.  
  46. // Don't change these unless you're working against a special development 
  47. // or testing environment. 
  48. 'base_path' => 'https://www.googleapis.com',  
  49.  
  50. // Definition of class specific values, like file paths and so on. 
  51. 'classes' => array( 
  52. 'Google_IO_Abstract' => array( 
  53. 'request_timeout_seconds' => 100,  
  54. ),  
  55. 'Google_Http_Request' => array( 
  56. // Disable the use of gzip on calls if set to true. Defaults to false. 
  57. 'disable_gzip' => self::GZIP_ENABLED,  
  58.  
  59. // We default gzip to disabled on uploads even if gzip is otherwise 
  60. // enabled, due to some issues seen with small packet sizes for uploads. 
  61. // Please test with this option before enabling gzip for uploads in 
  62. // a production environment. 
  63. 'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,  
  64. ),  
  65. // If you want to pass in OAuth 2.0 settings, they will need to be 
  66. // structured like this. 
  67. 'Google_Auth_OAuth2' => array( 
  68. // Keys for OAuth 2.0 access, see the API console at 
  69. // https://developers.google.com/console 
  70. 'client_id' => '',  
  71. 'client_secret' => '',  
  72. 'redirect_uri' => '',  
  73.  
  74. // Simple API access key, also from the API console. Ensure you get 
  75. // a Server key, and not a Browser key. 
  76. 'developer_key' => '',  
  77.  
  78. // Other parameters. 
  79. 'access_type' => 'online',  
  80. 'approval_prompt' => 'auto',  
  81. 'request_visible_actions' => '',  
  82. 'federated_signon_certs_url' => 
  83. 'https://www.googleapis.com/oauth2/v1/certs',  
  84. ),  
  85. // Set a default directory for the file cache. 
  86. 'Google_Cache_File' => array( 
  87. 'directory' => sys_get_temp_dir() . '/Google_Client' 
  88. ),  
  89.  
  90. // Definition of service specific values like scopes, oauth token URLs,  
  91. // etc. Example: 
  92. 'services' => array( 
  93. ),  
  94. ); 
  95. if ($ini_file_location) { 
  96. $ini = parse_ini_file($ini_file_location, true); 
  97. if (is_array($ini) && count($ini)) { 
  98. $this->configuration = array_merge($this->configuration, $ini); 
  99.  
  100. /** 
  101. * Set configuration specific to a given class. 
  102. * $config->setClassConfig('Google_Cache_File',  
  103. * array('directory' => '/tmp/cache')); 
  104. * @param $class The class name for the configuration 
  105. * @param $config string key or an array of configuration values 
  106. * @param $value optional - if $config is a key, the value 
  107. */ 
  108. public function setClassConfig($class, $config, $value = null) 
  109. if (!is_array($config)) { 
  110. if (!isset($this->configuration['classes'][$class])) { 
  111. $this->configuration['classes'][$class] = array(); 
  112. $this->configuration['classes'][$class][$config] = $value; 
  113. } else { 
  114. $this->configuration['classes'][$class] = $config; 
  115.  
  116. public function getClassConfig($class, $key = null) 
  117. if (!isset($this->configuration['classes'][$class])) { 
  118. return null; 
  119. if ($key === null) { 
  120. return $this->configuration['classes'][$class]; 
  121. } else { 
  122. return $this->configuration['classes'][$class][$key]; 
  123.  
  124. /** 
  125. * Return the configured cache class. 
  126. * @return string 
  127. */ 
  128. public function getCacheClass() 
  129. return $this->configuration['cache_class']; 
  130.  
  131. /** 
  132. * Return the configured Auth class. 
  133. * @return string 
  134. */ 
  135. public function getAuthClass() 
  136. return $this->configuration['auth_class']; 
  137.  
  138. /** 
  139. * Set the auth class. 
  140. * 
  141. * @param $class the class name to set 
  142. */ 
  143. public function setAuthClass($class) 
  144. $prev = $this->configuration['auth_class']; 
  145. if (!isset($this->configuration['classes'][$class]) && 
  146. isset($this->configuration['classes'][$prev])) { 
  147. $this->configuration['classes'][$class] = 
  148. $this->configuration['classes'][$prev]; 
  149. $this->configuration['auth_class'] = $class; 
  150.  
  151. /** 
  152. * Set the IO class. 
  153. * 
  154. * @param $class the class name to set 
  155. */ 
  156. public function setIoClass($class) 
  157. $prev = $this->configuration['io_class']; 
  158. if (!isset($this->configuration['classes'][$class]) && 
  159. isset($this->configuration['classes'][$prev])) { 
  160. $this->configuration['classes'][$class] = 
  161. $this->configuration['classes'][$prev]; 
  162. $this->configuration['io_class'] = $class; 
  163.  
  164. /** 
  165. * Set the cache class. 
  166. * 
  167. * @param $class the class name to set 
  168. */ 
  169. public function setCacheClass($class) 
  170. $prev = $this->configuration['cache_class']; 
  171. if (!isset($this->configuration['classes'][$class]) && 
  172. isset($this->configuration['classes'][$prev])) { 
  173. $this->configuration['classes'][$class] = 
  174. $this->configuration['classes'][$prev]; 
  175. $this->configuration['cache_class'] = $class; 
  176.  
  177. /** 
  178. * Return the configured IO class. 
  179. * @return string 
  180. */ 
  181. public function getIoClass() 
  182. return $this->configuration['io_class']; 
  183.  
  184. /** 
  185. * Set the application name, this is included in the User-Agent HTTP header. 
  186. * @param string $name 
  187. */ 
  188. public function setApplicationName($name) 
  189. $this->configuration['application_name'] = $name; 
  190.  
  191. /** 
  192. * @return string the name of the application 
  193. */ 
  194. public function getApplicationName() 
  195. return $this->configuration['application_name']; 
  196.  
  197. /** 
  198. * Set the client ID for the auth class. 
  199. * @param $key string - the API console client ID 
  200. */ 
  201. public function setClientId($clientId) 
  202. $this->setAuthConfig('client_id', $clientId); 
  203.  
  204. /** 
  205. * Set the client secret for the auth class. 
  206. * @param $key string - the API console client secret 
  207. */ 
  208. public function setClientSecret($secret) 
  209. $this->setAuthConfig('client_secret', $secret); 
  210.  
  211. /** 
  212. * Set the redirect uri for the auth class. Note that if using the 
  213. * Javascript based sign in flow, this should be the string 'postmessage'. 
  214. * @param $key string - the URI that users should be redirected to 
  215. */ 
  216. public function setRedirectUri($uri) 
  217. $this->setAuthConfig('redirect_uri', $uri); 
  218.  
  219. /** 
  220. * Set the app activities for the auth class. 
  221. * @param $rva string a space separated list of app activity types 
  222. */ 
  223. public function setRequestVisibleActions($rva) 
  224. $this->setAuthConfig('request_visible_actions', $rva); 
  225.  
  226. /** 
  227. * Set the the access type requested (offline or online.) 
  228. * @param $access string - the access type 
  229. */ 
  230. public function setAccessType($access) 
  231. $this->setAuthConfig('access_type', $access); 
  232.  
  233. /** 
  234. * Set when to show the approval prompt (auto or force) 
  235. * @param $approval string - the approval request 
  236. */ 
  237. public function setApprovalPrompt($approval) 
  238. $this->setAuthConfig('approval_prompt', $approval); 
  239.  
  240. /** 
  241. * Set the developer key for the auth class. Note that this is separate value 
  242. * from the client ID - if it looks like a URL, its a client ID! 
  243. * @param $key string - the API console developer key 
  244. */ 
  245. public function setDeveloperKey($key) 
  246. $this->setAuthConfig('developer_key', $key); 
  247.  
  248. /** 
  249. * @return string the base URL to use for API calls 
  250. */ 
  251. public function getBasePath() 
  252. return $this->configuration['base_path']; 
  253.  
  254. /** 
  255. * Set the auth configuration for the current auth class. 
  256. * @param $key - the key to set 
  257. * @param $value - the parameter value 
  258. */ 
  259. private function setAuthConfig($key, $value) 
  260. if (!isset($this->configuration['classes'][$this->getAuthClass()])) { 
  261. $this->configuration['classes'][$this->getAuthClass()] = array(); 
  262. $this->configuration['classes'][$this->getAuthClass()][$key] = $value; 
.