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