PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/google-api-php-client/src/Google/Config.php

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