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