@@ -28,13 +28,13 @@ discard block |
||
| 28 | 28 | class Google_Auth_ComputeEngine extends Google_Auth_Abstract |
| 29 | 29 | { |
| 30 | 30 | const METADATA_AUTH_URL = |
| 31 | - 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'; |
|
| 31 | + 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'; |
|
| 32 | 32 | private $client; |
| 33 | 33 | private $token; |
| 34 | 34 | |
| 35 | 35 | public function __construct(Google_Client $client, $config = null) |
| 36 | 36 | { |
| 37 | - $this->client = $client; |
|
| 37 | + $this->client = $client; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -49,8 +49,8 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function authenticatedRequest(Google_Http_Request $request) |
| 51 | 51 | { |
| 52 | - $request = $this->sign($request); |
|
| 53 | - return $this->client->getIo()->makeRequest($request); |
|
| 52 | + $request = $this->sign($request); |
|
| 53 | + return $this->client->getIo()->makeRequest($request); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
@@ -59,20 +59,20 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function setAccessToken($token) |
| 61 | 61 | { |
| 62 | - $token = json_decode($token, true); |
|
| 63 | - if ($token == null) { |
|
| 64 | - throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 65 | - } |
|
| 66 | - if (! isset($token['access_token'])) { |
|
| 67 | - throw new Google_Auth_Exception("Invalid token format"); |
|
| 68 | - } |
|
| 69 | - $token['created'] = time(); |
|
| 70 | - $this->token = $token; |
|
| 62 | + $token = json_decode($token, true); |
|
| 63 | + if ($token == null) { |
|
| 64 | + throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 65 | + } |
|
| 66 | + if (! isset($token['access_token'])) { |
|
| 67 | + throw new Google_Auth_Exception("Invalid token format"); |
|
| 68 | + } |
|
| 69 | + $token['created'] = time(); |
|
| 70 | + $this->token = $token; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | public function getAccessToken() |
| 74 | 74 | { |
| 75 | - return json_encode($this->token); |
|
| 75 | + return json_encode($this->token); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -81,29 +81,29 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function acquireAccessToken() |
| 83 | 83 | { |
| 84 | - $request = new Google_Http_Request( |
|
| 85 | - self::METADATA_AUTH_URL, |
|
| 86 | - 'GET', |
|
| 87 | - array( |
|
| 88 | - 'Metadata-Flavor' => 'Google' |
|
| 89 | - ) |
|
| 90 | - ); |
|
| 91 | - $request->disableGzip(); |
|
| 92 | - $response = $this->client->getIo()->makeRequest($request); |
|
| 84 | + $request = new Google_Http_Request( |
|
| 85 | + self::METADATA_AUTH_URL, |
|
| 86 | + 'GET', |
|
| 87 | + array( |
|
| 88 | + 'Metadata-Flavor' => 'Google' |
|
| 89 | + ) |
|
| 90 | + ); |
|
| 91 | + $request->disableGzip(); |
|
| 92 | + $response = $this->client->getIo()->makeRequest($request); |
|
| 93 | 93 | |
| 94 | - if ($response->getResponseHttpCode() == 200) { |
|
| 95 | - $this->setAccessToken($response->getResponseBody()); |
|
| 96 | - $this->token['created'] = time(); |
|
| 97 | - return $this->getAccessToken(); |
|
| 98 | - } else { |
|
| 99 | - throw new Google_Auth_Exception( |
|
| 100 | - sprintf( |
|
| 101 | - "Error fetching service account access token, message: '%s'", |
|
| 102 | - $response->getResponseBody() |
|
| 103 | - ), |
|
| 104 | - $response->getResponseHttpCode() |
|
| 105 | - ); |
|
| 106 | - } |
|
| 94 | + if ($response->getResponseHttpCode() == 200) { |
|
| 95 | + $this->setAccessToken($response->getResponseBody()); |
|
| 96 | + $this->token['created'] = time(); |
|
| 97 | + return $this->getAccessToken(); |
|
| 98 | + } else { |
|
| 99 | + throw new Google_Auth_Exception( |
|
| 100 | + sprintf( |
|
| 101 | + "Error fetching service account access token, message: '%s'", |
|
| 102 | + $response->getResponseBody() |
|
| 103 | + ), |
|
| 104 | + $response->getResponseHttpCode() |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -114,17 +114,17 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | public function sign(Google_Http_Request $request) |
| 116 | 116 | { |
| 117 | - if ($this->isAccessTokenExpired()) { |
|
| 118 | - $this->acquireAccessToken(); |
|
| 119 | - } |
|
| 117 | + if ($this->isAccessTokenExpired()) { |
|
| 118 | + $this->acquireAccessToken(); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - $this->client->getLogger()->debug('Compute engine service account authentication'); |
|
| 121 | + $this->client->getLogger()->debug('Compute engine service account authentication'); |
|
| 122 | 122 | |
| 123 | - $request->setRequestHeaders( |
|
| 124 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 125 | - ); |
|
| 123 | + $request->setRequestHeaders( |
|
| 124 | + array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 125 | + ); |
|
| 126 | 126 | |
| 127 | - return $request; |
|
| 127 | + return $request; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -133,14 +133,14 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function isAccessTokenExpired() |
| 135 | 135 | { |
| 136 | - if (!$this->token || !isset($this->token['created'])) { |
|
| 137 | - return true; |
|
| 138 | - } |
|
| 136 | + if (!$this->token || !isset($this->token['created'])) { |
|
| 137 | + return true; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - // If the token is set to expire in the next 30 seconds. |
|
| 141 | - $expired = ($this->token['created'] |
|
| 142 | - + ($this->token['expires_in'] - 30)) < time(); |
|
| 140 | + // If the token is set to expire in the next 30 seconds. |
|
| 141 | + $expired = ($this->token['created'] |
|
| 142 | + + ($this->token['expires_in'] - 30)) < time(); |
|
| 143 | 143 | |
| 144 | - return $expired; |
|
| 144 | + return $expired; |
|
| 145 | 145 | } |
| 146 | 146 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | if ($token == null) { |
| 64 | 64 | throw new Google_Auth_Exception('Could not json decode the token'); |
| 65 | 65 | } |
| 66 | - if (! isset($token['access_token'])) { |
|
| 66 | + if (!isset($token['access_token'])) { |
|
| 67 | 67 | throw new Google_Auth_Exception("Invalid token format"); |
| 68 | 68 | } |
| 69 | 69 | $token['created'] = time(); |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | $this->client->getLogger()->debug('Compute engine service account authentication'); |
| 122 | 122 | |
| 123 | 123 | $request->setRequestHeaders( |
| 124 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 124 | + array('Authorization' => 'Bearer '.$this->token['access_token']) |
|
| 125 | 125 | ); |
| 126 | 126 | |
| 127 | 127 | return $request; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function __construct(Google_Client $client) |
| 59 | 59 | { |
| 60 | - $this->client = $client; |
|
| 60 | + $this->client = $client; |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -72,8 +72,8 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public function authenticatedRequest(Google_Http_Request $request) |
| 74 | 74 | { |
| 75 | - $request = $this->sign($request); |
|
| 76 | - return $this->client->getIo()->makeRequest($request); |
|
| 75 | + $request = $this->sign($request); |
|
| 76 | + return $this->client->getIo()->makeRequest($request); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
@@ -84,52 +84,52 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function authenticate($code, $crossClient = false) |
| 86 | 86 | { |
| 87 | - if (strlen($code) == 0) { |
|
| 88 | - throw new Google_Auth_Exception("Invalid code"); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $arguments = array( |
|
| 92 | - 'code' => $code, |
|
| 93 | - 'grant_type' => 'authorization_code', |
|
| 94 | - 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 95 | - 'client_secret' => $this->client->getClassConfig($this, 'client_secret') |
|
| 96 | - ); |
|
| 97 | - |
|
| 98 | - if ($crossClient !== true) { |
|
| 99 | - $arguments['redirect_uri'] = $this->client->getClassConfig($this, 'redirect_uri'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // We got here from the redirect from a successful authorization grant, |
|
| 103 | - // fetch the access token |
|
| 104 | - $request = new Google_Http_Request( |
|
| 105 | - self::OAUTH2_TOKEN_URI, |
|
| 106 | - 'POST', |
|
| 107 | - array(), |
|
| 108 | - $arguments |
|
| 109 | - ); |
|
| 110 | - $request->disableGzip(); |
|
| 111 | - $response = $this->client->getIo()->makeRequest($request); |
|
| 112 | - |
|
| 113 | - if ($response->getResponseHttpCode() == 200) { |
|
| 114 | - $this->setAccessToken($response->getResponseBody()); |
|
| 115 | - $this->token['created'] = time(); |
|
| 116 | - return $this->getAccessToken(); |
|
| 117 | - } else { |
|
| 118 | - $decodedResponse = json_decode($response->getResponseBody(), true); |
|
| 119 | - if ($decodedResponse != null && $decodedResponse['error']) { |
|
| 120 | - $errorText = $decodedResponse['error']; |
|
| 121 | - if (isset($decodedResponse['error_description'])) { |
|
| 122 | - $errorText .= ": " . $decodedResponse['error_description']; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - throw new Google_Auth_Exception( |
|
| 126 | - sprintf( |
|
| 127 | - "Error fetching OAuth2 access token, message: '%s'", |
|
| 128 | - $errorText |
|
| 129 | - ), |
|
| 130 | - $response->getResponseHttpCode() |
|
| 131 | - ); |
|
| 132 | - } |
|
| 87 | + if (strlen($code) == 0) { |
|
| 88 | + throw new Google_Auth_Exception("Invalid code"); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $arguments = array( |
|
| 92 | + 'code' => $code, |
|
| 93 | + 'grant_type' => 'authorization_code', |
|
| 94 | + 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 95 | + 'client_secret' => $this->client->getClassConfig($this, 'client_secret') |
|
| 96 | + ); |
|
| 97 | + |
|
| 98 | + if ($crossClient !== true) { |
|
| 99 | + $arguments['redirect_uri'] = $this->client->getClassConfig($this, 'redirect_uri'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // We got here from the redirect from a successful authorization grant, |
|
| 103 | + // fetch the access token |
|
| 104 | + $request = new Google_Http_Request( |
|
| 105 | + self::OAUTH2_TOKEN_URI, |
|
| 106 | + 'POST', |
|
| 107 | + array(), |
|
| 108 | + $arguments |
|
| 109 | + ); |
|
| 110 | + $request->disableGzip(); |
|
| 111 | + $response = $this->client->getIo()->makeRequest($request); |
|
| 112 | + |
|
| 113 | + if ($response->getResponseHttpCode() == 200) { |
|
| 114 | + $this->setAccessToken($response->getResponseBody()); |
|
| 115 | + $this->token['created'] = time(); |
|
| 116 | + return $this->getAccessToken(); |
|
| 117 | + } else { |
|
| 118 | + $decodedResponse = json_decode($response->getResponseBody(), true); |
|
| 119 | + if ($decodedResponse != null && $decodedResponse['error']) { |
|
| 120 | + $errorText = $decodedResponse['error']; |
|
| 121 | + if (isset($decodedResponse['error_description'])) { |
|
| 122 | + $errorText .= ": " . $decodedResponse['error_description']; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + throw new Google_Auth_Exception( |
|
| 126 | + sprintf( |
|
| 127 | + "Error fetching OAuth2 access token, message: '%s'", |
|
| 128 | + $errorText |
|
| 129 | + ), |
|
| 130 | + $response->getResponseHttpCode() |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -141,37 +141,37 @@ discard block |
||
| 141 | 141 | */ |
| 142 | 142 | public function createAuthUrl($scope) |
| 143 | 143 | { |
| 144 | - $params = array( |
|
| 145 | - 'response_type' => 'code', |
|
| 146 | - 'redirect_uri' => $this->client->getClassConfig($this, 'redirect_uri'), |
|
| 147 | - 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 148 | - 'scope' => $scope, |
|
| 149 | - 'access_type' => $this->client->getClassConfig($this, 'access_type'), |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - // Prefer prompt to approval prompt. |
|
| 153 | - if ($this->client->getClassConfig($this, 'prompt')) { |
|
| 154 | - $params = $this->maybeAddParam($params, 'prompt'); |
|
| 155 | - } else { |
|
| 156 | - $params = $this->maybeAddParam($params, 'approval_prompt'); |
|
| 157 | - } |
|
| 158 | - $params = $this->maybeAddParam($params, 'login_hint'); |
|
| 159 | - $params = $this->maybeAddParam($params, 'hd'); |
|
| 160 | - $params = $this->maybeAddParam($params, 'openid.realm'); |
|
| 161 | - $params = $this->maybeAddParam($params, 'include_granted_scopes'); |
|
| 162 | - |
|
| 163 | - // If the list of scopes contains plus.login, add request_visible_actions |
|
| 164 | - // to auth URL. |
|
| 165 | - $rva = $this->client->getClassConfig($this, 'request_visible_actions'); |
|
| 166 | - if (strpos($scope, 'plus.login') && strlen($rva) > 0) { |
|
| 167 | - $params['request_visible_actions'] = $rva; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (isset($this->state)) { |
|
| 171 | - $params['state'] = $this->state; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return self::OAUTH2_AUTH_URL . "?" . http_build_query($params, '', '&'); |
|
| 144 | + $params = array( |
|
| 145 | + 'response_type' => 'code', |
|
| 146 | + 'redirect_uri' => $this->client->getClassConfig($this, 'redirect_uri'), |
|
| 147 | + 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 148 | + 'scope' => $scope, |
|
| 149 | + 'access_type' => $this->client->getClassConfig($this, 'access_type'), |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + // Prefer prompt to approval prompt. |
|
| 153 | + if ($this->client->getClassConfig($this, 'prompt')) { |
|
| 154 | + $params = $this->maybeAddParam($params, 'prompt'); |
|
| 155 | + } else { |
|
| 156 | + $params = $this->maybeAddParam($params, 'approval_prompt'); |
|
| 157 | + } |
|
| 158 | + $params = $this->maybeAddParam($params, 'login_hint'); |
|
| 159 | + $params = $this->maybeAddParam($params, 'hd'); |
|
| 160 | + $params = $this->maybeAddParam($params, 'openid.realm'); |
|
| 161 | + $params = $this->maybeAddParam($params, 'include_granted_scopes'); |
|
| 162 | + |
|
| 163 | + // If the list of scopes contains plus.login, add request_visible_actions |
|
| 164 | + // to auth URL. |
|
| 165 | + $rva = $this->client->getClassConfig($this, 'request_visible_actions'); |
|
| 166 | + if (strpos($scope, 'plus.login') && strlen($rva) > 0) { |
|
| 167 | + $params['request_visible_actions'] = $rva; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (isset($this->state)) { |
|
| 171 | + $params['state'] = $this->state; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return self::OAUTH2_AUTH_URL . "?" . http_build_query($params, '', '&'); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
@@ -180,38 +180,38 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | public function setAccessToken($token) |
| 182 | 182 | { |
| 183 | - $token = json_decode($token, true); |
|
| 184 | - if ($token == null) { |
|
| 185 | - throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 186 | - } |
|
| 187 | - if (! isset($token['access_token'])) { |
|
| 188 | - throw new Google_Auth_Exception("Invalid token format"); |
|
| 189 | - } |
|
| 190 | - $this->token = $token; |
|
| 183 | + $token = json_decode($token, true); |
|
| 184 | + if ($token == null) { |
|
| 185 | + throw new Google_Auth_Exception('Could not json decode the token'); |
|
| 186 | + } |
|
| 187 | + if (! isset($token['access_token'])) { |
|
| 188 | + throw new Google_Auth_Exception("Invalid token format"); |
|
| 189 | + } |
|
| 190 | + $this->token = $token; |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | public function getAccessToken() |
| 194 | 194 | { |
| 195 | - return json_encode($this->token); |
|
| 195 | + return json_encode($this->token); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | public function getRefreshToken() |
| 199 | 199 | { |
| 200 | - if (array_key_exists('refresh_token', $this->token)) { |
|
| 201 | - return $this->token['refresh_token']; |
|
| 202 | - } else { |
|
| 203 | - return null; |
|
| 204 | - } |
|
| 200 | + if (array_key_exists('refresh_token', $this->token)) { |
|
| 201 | + return $this->token['refresh_token']; |
|
| 202 | + } else { |
|
| 203 | + return null; |
|
| 204 | + } |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | public function setState($state) |
| 208 | 208 | { |
| 209 | - $this->state = $state; |
|
| 209 | + $this->state = $state; |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | public function setAssertionCredentials(Google_Auth_AssertionCredentials $creds) |
| 213 | 213 | { |
| 214 | - $this->assertionCredentials = $creds; |
|
| 214 | + $this->assertionCredentials = $creds; |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -222,43 +222,43 @@ discard block |
||
| 222 | 222 | */ |
| 223 | 223 | public function sign(Google_Http_Request $request) |
| 224 | 224 | { |
| 225 | - // add the developer key to the request before signing it |
|
| 226 | - if ($this->client->getClassConfig($this, 'developer_key')) { |
|
| 227 | - $request->setQueryParam('key', $this->client->getClassConfig($this, 'developer_key')); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - // Cannot sign the request without an OAuth access token. |
|
| 231 | - if (null == $this->token && null == $this->assertionCredentials) { |
|
| 232 | - return $request; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - // Check if the token is set to expire in the next 30 seconds |
|
| 236 | - // (or has already expired). |
|
| 237 | - if ($this->isAccessTokenExpired()) { |
|
| 238 | - if ($this->assertionCredentials) { |
|
| 239 | - $this->refreshTokenWithAssertion(); |
|
| 240 | - } else { |
|
| 241 | - $this->client->getLogger()->debug('OAuth2 access token expired'); |
|
| 242 | - if (! array_key_exists('refresh_token', $this->token)) { |
|
| 243 | - $error = "The OAuth 2.0 access token has expired," |
|
| 244 | - ." and a refresh token is not available. Refresh tokens" |
|
| 245 | - ." are not returned for responses that were auto-approved."; |
|
| 246 | - |
|
| 247 | - $this->client->getLogger()->error($error); |
|
| 248 | - throw new Google_Auth_Exception($error); |
|
| 249 | - } |
|
| 250 | - $this->refreshToken($this->token['refresh_token']); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $this->client->getLogger()->debug('OAuth2 authentication'); |
|
| 255 | - |
|
| 256 | - // Add the OAuth2 header to the request |
|
| 257 | - $request->setRequestHeaders( |
|
| 258 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 259 | - ); |
|
| 260 | - |
|
| 261 | - return $request; |
|
| 225 | + // add the developer key to the request before signing it |
|
| 226 | + if ($this->client->getClassConfig($this, 'developer_key')) { |
|
| 227 | + $request->setQueryParam('key', $this->client->getClassConfig($this, 'developer_key')); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + // Cannot sign the request without an OAuth access token. |
|
| 231 | + if (null == $this->token && null == $this->assertionCredentials) { |
|
| 232 | + return $request; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + // Check if the token is set to expire in the next 30 seconds |
|
| 236 | + // (or has already expired). |
|
| 237 | + if ($this->isAccessTokenExpired()) { |
|
| 238 | + if ($this->assertionCredentials) { |
|
| 239 | + $this->refreshTokenWithAssertion(); |
|
| 240 | + } else { |
|
| 241 | + $this->client->getLogger()->debug('OAuth2 access token expired'); |
|
| 242 | + if (! array_key_exists('refresh_token', $this->token)) { |
|
| 243 | + $error = "The OAuth 2.0 access token has expired," |
|
| 244 | + ." and a refresh token is not available. Refresh tokens" |
|
| 245 | + ." are not returned for responses that were auto-approved."; |
|
| 246 | + |
|
| 247 | + $this->client->getLogger()->error($error); |
|
| 248 | + throw new Google_Auth_Exception($error); |
|
| 249 | + } |
|
| 250 | + $this->refreshToken($this->token['refresh_token']); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $this->client->getLogger()->debug('OAuth2 authentication'); |
|
| 255 | + |
|
| 256 | + // Add the OAuth2 header to the request |
|
| 257 | + $request->setRequestHeaders( |
|
| 258 | + array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 259 | + ); |
|
| 260 | + |
|
| 261 | + return $request; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -268,14 +268,14 @@ discard block |
||
| 268 | 268 | */ |
| 269 | 269 | public function refreshToken($refreshToken) |
| 270 | 270 | { |
| 271 | - $this->refreshTokenRequest( |
|
| 272 | - array( |
|
| 273 | - 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 274 | - 'client_secret' => $this->client->getClassConfig($this, 'client_secret'), |
|
| 275 | - 'refresh_token' => $refreshToken, |
|
| 276 | - 'grant_type' => 'refresh_token' |
|
| 277 | - ) |
|
| 278 | - ); |
|
| 271 | + $this->refreshTokenRequest( |
|
| 272 | + array( |
|
| 273 | + 'client_id' => $this->client->getClassConfig($this, 'client_id'), |
|
| 274 | + 'client_secret' => $this->client->getClassConfig($this, 'client_secret'), |
|
| 275 | + 'refresh_token' => $refreshToken, |
|
| 276 | + 'grant_type' => 'refresh_token' |
|
| 277 | + ) |
|
| 278 | + ); |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | /** |
@@ -285,83 +285,83 @@ discard block |
||
| 285 | 285 | */ |
| 286 | 286 | public function refreshTokenWithAssertion($assertionCredentials = null) |
| 287 | 287 | { |
| 288 | - if (!$assertionCredentials) { |
|
| 289 | - $assertionCredentials = $this->assertionCredentials; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $cacheKey = $assertionCredentials->getCacheKey(); |
|
| 293 | - |
|
| 294 | - if ($cacheKey) { |
|
| 295 | - // We can check whether we have a token available in the |
|
| 296 | - // cache. If it is expired, we can retrieve a new one from |
|
| 297 | - // the assertion. |
|
| 298 | - $token = $this->client->getCache()->get($cacheKey); |
|
| 299 | - if ($token) { |
|
| 300 | - $this->setAccessToken($token); |
|
| 301 | - } |
|
| 302 | - if (!$this->isAccessTokenExpired()) { |
|
| 303 | - return; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $this->client->getLogger()->debug('OAuth2 access token expired'); |
|
| 308 | - $this->refreshTokenRequest( |
|
| 309 | - array( |
|
| 310 | - 'grant_type' => 'assertion', |
|
| 311 | - 'assertion_type' => $assertionCredentials->assertionType, |
|
| 312 | - 'assertion' => $assertionCredentials->generateAssertion(), |
|
| 313 | - ) |
|
| 314 | - ); |
|
| 315 | - |
|
| 316 | - if ($cacheKey) { |
|
| 317 | - // Attempt to cache the token. |
|
| 318 | - $this->client->getCache()->set( |
|
| 319 | - $cacheKey, |
|
| 320 | - $this->getAccessToken() |
|
| 321 | - ); |
|
| 322 | - } |
|
| 288 | + if (!$assertionCredentials) { |
|
| 289 | + $assertionCredentials = $this->assertionCredentials; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $cacheKey = $assertionCredentials->getCacheKey(); |
|
| 293 | + |
|
| 294 | + if ($cacheKey) { |
|
| 295 | + // We can check whether we have a token available in the |
|
| 296 | + // cache. If it is expired, we can retrieve a new one from |
|
| 297 | + // the assertion. |
|
| 298 | + $token = $this->client->getCache()->get($cacheKey); |
|
| 299 | + if ($token) { |
|
| 300 | + $this->setAccessToken($token); |
|
| 301 | + } |
|
| 302 | + if (!$this->isAccessTokenExpired()) { |
|
| 303 | + return; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $this->client->getLogger()->debug('OAuth2 access token expired'); |
|
| 308 | + $this->refreshTokenRequest( |
|
| 309 | + array( |
|
| 310 | + 'grant_type' => 'assertion', |
|
| 311 | + 'assertion_type' => $assertionCredentials->assertionType, |
|
| 312 | + 'assertion' => $assertionCredentials->generateAssertion(), |
|
| 313 | + ) |
|
| 314 | + ); |
|
| 315 | + |
|
| 316 | + if ($cacheKey) { |
|
| 317 | + // Attempt to cache the token. |
|
| 318 | + $this->client->getCache()->set( |
|
| 319 | + $cacheKey, |
|
| 320 | + $this->getAccessToken() |
|
| 321 | + ); |
|
| 322 | + } |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | private function refreshTokenRequest($params) |
| 326 | 326 | { |
| 327 | - if (isset($params['assertion'])) { |
|
| 328 | - $this->client->getLogger()->info( |
|
| 329 | - 'OAuth2 access token refresh with Signed JWT assertion grants.' |
|
| 330 | - ); |
|
| 331 | - } else { |
|
| 332 | - $this->client->getLogger()->info('OAuth2 access token refresh'); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - $http = new Google_Http_Request( |
|
| 336 | - self::OAUTH2_TOKEN_URI, |
|
| 337 | - 'POST', |
|
| 338 | - array(), |
|
| 339 | - $params |
|
| 340 | - ); |
|
| 341 | - $http->disableGzip(); |
|
| 342 | - $request = $this->client->getIo()->makeRequest($http); |
|
| 343 | - |
|
| 344 | - $code = $request->getResponseHttpCode(); |
|
| 345 | - $body = $request->getResponseBody(); |
|
| 346 | - if (200 == $code) { |
|
| 347 | - $token = json_decode($body, true); |
|
| 348 | - if ($token == null) { |
|
| 349 | - throw new Google_Auth_Exception("Could not json decode the access token"); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - if (! isset($token['access_token']) || ! isset($token['expires_in'])) { |
|
| 353 | - throw new Google_Auth_Exception("Invalid token format"); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - if (isset($token['id_token'])) { |
|
| 357 | - $this->token['id_token'] = $token['id_token']; |
|
| 358 | - } |
|
| 359 | - $this->token['access_token'] = $token['access_token']; |
|
| 360 | - $this->token['expires_in'] = $token['expires_in']; |
|
| 361 | - $this->token['created'] = time(); |
|
| 362 | - } else { |
|
| 363 | - throw new Google_Auth_Exception("Error refreshing the OAuth2 token, message: '$body'", $code); |
|
| 364 | - } |
|
| 327 | + if (isset($params['assertion'])) { |
|
| 328 | + $this->client->getLogger()->info( |
|
| 329 | + 'OAuth2 access token refresh with Signed JWT assertion grants.' |
|
| 330 | + ); |
|
| 331 | + } else { |
|
| 332 | + $this->client->getLogger()->info('OAuth2 access token refresh'); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + $http = new Google_Http_Request( |
|
| 336 | + self::OAUTH2_TOKEN_URI, |
|
| 337 | + 'POST', |
|
| 338 | + array(), |
|
| 339 | + $params |
|
| 340 | + ); |
|
| 341 | + $http->disableGzip(); |
|
| 342 | + $request = $this->client->getIo()->makeRequest($http); |
|
| 343 | + |
|
| 344 | + $code = $request->getResponseHttpCode(); |
|
| 345 | + $body = $request->getResponseBody(); |
|
| 346 | + if (200 == $code) { |
|
| 347 | + $token = json_decode($body, true); |
|
| 348 | + if ($token == null) { |
|
| 349 | + throw new Google_Auth_Exception("Could not json decode the access token"); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + if (! isset($token['access_token']) || ! isset($token['expires_in'])) { |
|
| 353 | + throw new Google_Auth_Exception("Invalid token format"); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + if (isset($token['id_token'])) { |
|
| 357 | + $this->token['id_token'] = $token['id_token']; |
|
| 358 | + } |
|
| 359 | + $this->token['access_token'] = $token['access_token']; |
|
| 360 | + $this->token['expires_in'] = $token['expires_in']; |
|
| 361 | + $this->token['created'] = time(); |
|
| 362 | + } else { |
|
| 363 | + throw new Google_Auth_Exception("Error refreshing the OAuth2 token, message: '$body'", $code); |
|
| 364 | + } |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -373,31 +373,31 @@ discard block |
||
| 373 | 373 | */ |
| 374 | 374 | public function revokeToken($token = null) |
| 375 | 375 | { |
| 376 | - if (!$token) { |
|
| 377 | - if (!$this->token) { |
|
| 378 | - // Not initialized, no token to actually revoke |
|
| 379 | - return false; |
|
| 380 | - } elseif (array_key_exists('refresh_token', $this->token)) { |
|
| 381 | - $token = $this->token['refresh_token']; |
|
| 382 | - } else { |
|
| 383 | - $token = $this->token['access_token']; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - $request = new Google_Http_Request( |
|
| 387 | - self::OAUTH2_REVOKE_URI, |
|
| 388 | - 'POST', |
|
| 389 | - array(), |
|
| 390 | - "token=$token" |
|
| 391 | - ); |
|
| 392 | - $request->disableGzip(); |
|
| 393 | - $response = $this->client->getIo()->makeRequest($request); |
|
| 394 | - $code = $response->getResponseHttpCode(); |
|
| 395 | - if ($code == 200) { |
|
| 396 | - $this->token = null; |
|
| 397 | - return true; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - return false; |
|
| 376 | + if (!$token) { |
|
| 377 | + if (!$this->token) { |
|
| 378 | + // Not initialized, no token to actually revoke |
|
| 379 | + return false; |
|
| 380 | + } elseif (array_key_exists('refresh_token', $this->token)) { |
|
| 381 | + $token = $this->token['refresh_token']; |
|
| 382 | + } else { |
|
| 383 | + $token = $this->token['access_token']; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + $request = new Google_Http_Request( |
|
| 387 | + self::OAUTH2_REVOKE_URI, |
|
| 388 | + 'POST', |
|
| 389 | + array(), |
|
| 390 | + "token=$token" |
|
| 391 | + ); |
|
| 392 | + $request->disableGzip(); |
|
| 393 | + $response = $this->client->getIo()->makeRequest($request); |
|
| 394 | + $code = $response->getResponseHttpCode(); |
|
| 395 | + if ($code == 200) { |
|
| 396 | + $this->token = null; |
|
| 397 | + return true; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + return false; |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | /** |
@@ -406,15 +406,15 @@ discard block |
||
| 406 | 406 | */ |
| 407 | 407 | public function isAccessTokenExpired() |
| 408 | 408 | { |
| 409 | - if (!$this->token || !isset($this->token['created'])) { |
|
| 410 | - return true; |
|
| 411 | - } |
|
| 409 | + if (!$this->token || !isset($this->token['created'])) { |
|
| 410 | + return true; |
|
| 411 | + } |
|
| 412 | 412 | |
| 413 | - // If the token is set to expire in the next 30 seconds. |
|
| 414 | - $expired = ($this->token['created'] |
|
| 415 | - + ($this->token['expires_in'] - 30)) < time(); |
|
| 413 | + // If the token is set to expire in the next 30 seconds. |
|
| 414 | + $expired = ($this->token['created'] |
|
| 415 | + + ($this->token['expires_in'] - 30)) < time(); |
|
| 416 | 416 | |
| 417 | - return $expired; |
|
| 417 | + return $expired; |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | // Gets federated sign-on certificates to use for verifying identity tokens. |
@@ -422,9 +422,9 @@ discard block |
||
| 422 | 422 | // are PEM encoded certificates. |
| 423 | 423 | private function getFederatedSignOnCerts() |
| 424 | 424 | { |
| 425 | - return $this->retrieveCertsFromLocation( |
|
| 426 | - $this->client->getClassConfig($this, 'federated_signon_certs_url') |
|
| 427 | - ); |
|
| 425 | + return $this->retrieveCertsFromLocation( |
|
| 426 | + $this->client->getClassConfig($this, 'federated_signon_certs_url') |
|
| 427 | + ); |
|
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | /** |
@@ -436,36 +436,36 @@ discard block |
||
| 436 | 436 | */ |
| 437 | 437 | public function retrieveCertsFromLocation($url) |
| 438 | 438 | { |
| 439 | - // If we're retrieving a local file, just grab it. |
|
| 440 | - if ("http" != substr($url, 0, 4)) { |
|
| 441 | - $file = file_get_contents($url); |
|
| 442 | - if ($file) { |
|
| 443 | - return json_decode($file, true); |
|
| 444 | - } else { |
|
| 445 | - throw new Google_Auth_Exception( |
|
| 446 | - "Failed to retrieve verification certificates: '" . |
|
| 447 | - $url . "'." |
|
| 448 | - ); |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - // This relies on makeRequest caching certificate responses. |
|
| 453 | - $request = $this->client->getIo()->makeRequest( |
|
| 454 | - new Google_Http_Request( |
|
| 455 | - $url |
|
| 456 | - ) |
|
| 457 | - ); |
|
| 458 | - if ($request->getResponseHttpCode() == 200) { |
|
| 459 | - $certs = json_decode($request->getResponseBody(), true); |
|
| 460 | - if ($certs) { |
|
| 461 | - return $certs; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - throw new Google_Auth_Exception( |
|
| 465 | - "Failed to retrieve verification certificates: '" . |
|
| 466 | - $request->getResponseBody() . "'.", |
|
| 467 | - $request->getResponseHttpCode() |
|
| 468 | - ); |
|
| 439 | + // If we're retrieving a local file, just grab it. |
|
| 440 | + if ("http" != substr($url, 0, 4)) { |
|
| 441 | + $file = file_get_contents($url); |
|
| 442 | + if ($file) { |
|
| 443 | + return json_decode($file, true); |
|
| 444 | + } else { |
|
| 445 | + throw new Google_Auth_Exception( |
|
| 446 | + "Failed to retrieve verification certificates: '" . |
|
| 447 | + $url . "'." |
|
| 448 | + ); |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + // This relies on makeRequest caching certificate responses. |
|
| 453 | + $request = $this->client->getIo()->makeRequest( |
|
| 454 | + new Google_Http_Request( |
|
| 455 | + $url |
|
| 456 | + ) |
|
| 457 | + ); |
|
| 458 | + if ($request->getResponseHttpCode() == 200) { |
|
| 459 | + $certs = json_decode($request->getResponseBody(), true); |
|
| 460 | + if ($certs) { |
|
| 461 | + return $certs; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + throw new Google_Auth_Exception( |
|
| 465 | + "Failed to retrieve verification certificates: '" . |
|
| 466 | + $request->getResponseBody() . "'.", |
|
| 467 | + $request->getResponseHttpCode() |
|
| 468 | + ); |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | /** |
@@ -480,15 +480,15 @@ discard block |
||
| 480 | 480 | */ |
| 481 | 481 | public function verifyIdToken($id_token = null, $audience = null) |
| 482 | 482 | { |
| 483 | - if (!$id_token) { |
|
| 484 | - $id_token = $this->token['id_token']; |
|
| 485 | - } |
|
| 486 | - $certs = $this->getFederatedSignonCerts(); |
|
| 487 | - if (!$audience) { |
|
| 488 | - $audience = $this->client->getClassConfig($this, 'client_id'); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - return $this->verifySignedJwtWithCerts($id_token, $certs, $audience, self::OAUTH2_ISSUER); |
|
| 483 | + if (!$id_token) { |
|
| 484 | + $id_token = $this->token['id_token']; |
|
| 485 | + } |
|
| 486 | + $certs = $this->getFederatedSignonCerts(); |
|
| 487 | + if (!$audience) { |
|
| 488 | + $audience = $this->client->getClassConfig($this, 'client_id'); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + return $this->verifySignedJwtWithCerts($id_token, $certs, $audience, self::OAUTH2_ISSUER); |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | 494 | /** |
@@ -503,125 +503,125 @@ discard block |
||
| 503 | 503 | * @return mixed token information if valid, false if not |
| 504 | 504 | */ |
| 505 | 505 | public function verifySignedJwtWithCerts( |
| 506 | - $jwt, |
|
| 507 | - $certs, |
|
| 508 | - $required_audience, |
|
| 509 | - $issuer = null, |
|
| 510 | - $max_expiry = null |
|
| 506 | + $jwt, |
|
| 507 | + $certs, |
|
| 508 | + $required_audience, |
|
| 509 | + $issuer = null, |
|
| 510 | + $max_expiry = null |
|
| 511 | 511 | ) { |
| 512 | - if (!$max_expiry) { |
|
| 513 | - // Set the maximum time we will accept a token for. |
|
| 514 | - $max_expiry = self::MAX_TOKEN_LIFETIME_SECS; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - $segments = explode(".", $jwt); |
|
| 518 | - if (count($segments) != 3) { |
|
| 519 | - throw new Google_Auth_Exception("Wrong number of segments in token: $jwt"); |
|
| 520 | - } |
|
| 521 | - $signed = $segments[0] . "." . $segments[1]; |
|
| 522 | - $signature = Google_Utils::urlSafeB64Decode($segments[2]); |
|
| 523 | - |
|
| 524 | - // Parse envelope. |
|
| 525 | - $envelope = json_decode(Google_Utils::urlSafeB64Decode($segments[0]), true); |
|
| 526 | - if (!$envelope) { |
|
| 527 | - throw new Google_Auth_Exception("Can't parse token envelope: " . $segments[0]); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - // Parse token |
|
| 531 | - $json_body = Google_Utils::urlSafeB64Decode($segments[1]); |
|
| 532 | - $payload = json_decode($json_body, true); |
|
| 533 | - if (!$payload) { |
|
| 534 | - throw new Google_Auth_Exception("Can't parse token payload: " . $segments[1]); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - // Check signature |
|
| 538 | - $verified = false; |
|
| 539 | - foreach ($certs as $keyName => $pem) { |
|
| 540 | - $public_key = new Google_Verifier_Pem($pem); |
|
| 541 | - if ($public_key->verify($signed, $signature)) { |
|
| 542 | - $verified = true; |
|
| 543 | - break; |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - if (!$verified) { |
|
| 548 | - throw new Google_Auth_Exception("Invalid token signature: $jwt"); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - // Check issued-at timestamp |
|
| 552 | - $iat = 0; |
|
| 553 | - if (array_key_exists("iat", $payload)) { |
|
| 554 | - $iat = $payload["iat"]; |
|
| 555 | - } |
|
| 556 | - if (!$iat) { |
|
| 557 | - throw new Google_Auth_Exception("No issue time in token: $json_body"); |
|
| 558 | - } |
|
| 559 | - $earliest = $iat - self::CLOCK_SKEW_SECS; |
|
| 560 | - |
|
| 561 | - // Check expiration timestamp |
|
| 562 | - $now = time(); |
|
| 563 | - $exp = 0; |
|
| 564 | - if (array_key_exists("exp", $payload)) { |
|
| 565 | - $exp = $payload["exp"]; |
|
| 566 | - } |
|
| 567 | - if (!$exp) { |
|
| 568 | - throw new Google_Auth_Exception("No expiration time in token: $json_body"); |
|
| 569 | - } |
|
| 570 | - if ($exp >= $now + $max_expiry) { |
|
| 571 | - throw new Google_Auth_Exception( |
|
| 572 | - sprintf("Expiration time too far in future: %s", $json_body) |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - $latest = $exp + self::CLOCK_SKEW_SECS; |
|
| 577 | - if ($now < $earliest) { |
|
| 578 | - throw new Google_Auth_Exception( |
|
| 579 | - sprintf( |
|
| 580 | - "Token used too early, %s < %s: %s", |
|
| 581 | - $now, |
|
| 582 | - $earliest, |
|
| 583 | - $json_body |
|
| 584 | - ) |
|
| 585 | - ); |
|
| 586 | - } |
|
| 587 | - if ($now > $latest) { |
|
| 588 | - throw new Google_Auth_Exception( |
|
| 589 | - sprintf( |
|
| 590 | - "Token used too late, %s > %s: %s", |
|
| 591 | - $now, |
|
| 592 | - $latest, |
|
| 593 | - $json_body |
|
| 594 | - ) |
|
| 595 | - ); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - $iss = $payload['iss']; |
|
| 599 | - if ($issuer && $iss != $issuer) { |
|
| 600 | - throw new Google_Auth_Exception( |
|
| 601 | - sprintf( |
|
| 602 | - "Invalid issuer, %s != %s: %s", |
|
| 603 | - $iss, |
|
| 604 | - $issuer, |
|
| 605 | - $json_body |
|
| 606 | - ) |
|
| 607 | - ); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - // Check audience |
|
| 611 | - $aud = $payload["aud"]; |
|
| 612 | - if ($aud != $required_audience) { |
|
| 613 | - throw new Google_Auth_Exception( |
|
| 614 | - sprintf( |
|
| 615 | - "Wrong recipient, %s != %s:", |
|
| 616 | - $aud, |
|
| 617 | - $required_audience, |
|
| 618 | - $json_body |
|
| 619 | - ) |
|
| 620 | - ); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // All good. |
|
| 624 | - return new Google_Auth_LoginTicket($envelope, $payload); |
|
| 512 | + if (!$max_expiry) { |
|
| 513 | + // Set the maximum time we will accept a token for. |
|
| 514 | + $max_expiry = self::MAX_TOKEN_LIFETIME_SECS; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + $segments = explode(".", $jwt); |
|
| 518 | + if (count($segments) != 3) { |
|
| 519 | + throw new Google_Auth_Exception("Wrong number of segments in token: $jwt"); |
|
| 520 | + } |
|
| 521 | + $signed = $segments[0] . "." . $segments[1]; |
|
| 522 | + $signature = Google_Utils::urlSafeB64Decode($segments[2]); |
|
| 523 | + |
|
| 524 | + // Parse envelope. |
|
| 525 | + $envelope = json_decode(Google_Utils::urlSafeB64Decode($segments[0]), true); |
|
| 526 | + if (!$envelope) { |
|
| 527 | + throw new Google_Auth_Exception("Can't parse token envelope: " . $segments[0]); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + // Parse token |
|
| 531 | + $json_body = Google_Utils::urlSafeB64Decode($segments[1]); |
|
| 532 | + $payload = json_decode($json_body, true); |
|
| 533 | + if (!$payload) { |
|
| 534 | + throw new Google_Auth_Exception("Can't parse token payload: " . $segments[1]); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + // Check signature |
|
| 538 | + $verified = false; |
|
| 539 | + foreach ($certs as $keyName => $pem) { |
|
| 540 | + $public_key = new Google_Verifier_Pem($pem); |
|
| 541 | + if ($public_key->verify($signed, $signature)) { |
|
| 542 | + $verified = true; |
|
| 543 | + break; |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + if (!$verified) { |
|
| 548 | + throw new Google_Auth_Exception("Invalid token signature: $jwt"); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + // Check issued-at timestamp |
|
| 552 | + $iat = 0; |
|
| 553 | + if (array_key_exists("iat", $payload)) { |
|
| 554 | + $iat = $payload["iat"]; |
|
| 555 | + } |
|
| 556 | + if (!$iat) { |
|
| 557 | + throw new Google_Auth_Exception("No issue time in token: $json_body"); |
|
| 558 | + } |
|
| 559 | + $earliest = $iat - self::CLOCK_SKEW_SECS; |
|
| 560 | + |
|
| 561 | + // Check expiration timestamp |
|
| 562 | + $now = time(); |
|
| 563 | + $exp = 0; |
|
| 564 | + if (array_key_exists("exp", $payload)) { |
|
| 565 | + $exp = $payload["exp"]; |
|
| 566 | + } |
|
| 567 | + if (!$exp) { |
|
| 568 | + throw new Google_Auth_Exception("No expiration time in token: $json_body"); |
|
| 569 | + } |
|
| 570 | + if ($exp >= $now + $max_expiry) { |
|
| 571 | + throw new Google_Auth_Exception( |
|
| 572 | + sprintf("Expiration time too far in future: %s", $json_body) |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + $latest = $exp + self::CLOCK_SKEW_SECS; |
|
| 577 | + if ($now < $earliest) { |
|
| 578 | + throw new Google_Auth_Exception( |
|
| 579 | + sprintf( |
|
| 580 | + "Token used too early, %s < %s: %s", |
|
| 581 | + $now, |
|
| 582 | + $earliest, |
|
| 583 | + $json_body |
|
| 584 | + ) |
|
| 585 | + ); |
|
| 586 | + } |
|
| 587 | + if ($now > $latest) { |
|
| 588 | + throw new Google_Auth_Exception( |
|
| 589 | + sprintf( |
|
| 590 | + "Token used too late, %s > %s: %s", |
|
| 591 | + $now, |
|
| 592 | + $latest, |
|
| 593 | + $json_body |
|
| 594 | + ) |
|
| 595 | + ); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + $iss = $payload['iss']; |
|
| 599 | + if ($issuer && $iss != $issuer) { |
|
| 600 | + throw new Google_Auth_Exception( |
|
| 601 | + sprintf( |
|
| 602 | + "Invalid issuer, %s != %s: %s", |
|
| 603 | + $iss, |
|
| 604 | + $issuer, |
|
| 605 | + $json_body |
|
| 606 | + ) |
|
| 607 | + ); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + // Check audience |
|
| 611 | + $aud = $payload["aud"]; |
|
| 612 | + if ($aud != $required_audience) { |
|
| 613 | + throw new Google_Auth_Exception( |
|
| 614 | + sprintf( |
|
| 615 | + "Wrong recipient, %s != %s:", |
|
| 616 | + $aud, |
|
| 617 | + $required_audience, |
|
| 618 | + $json_body |
|
| 619 | + ) |
|
| 620 | + ); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // All good. |
|
| 624 | + return new Google_Auth_LoginTicket($envelope, $payload); |
|
| 625 | 625 | } |
| 626 | 626 | |
| 627 | 627 | /** |
@@ -629,10 +629,10 @@ discard block |
||
| 629 | 629 | */ |
| 630 | 630 | private function maybeAddParam($params, $name) |
| 631 | 631 | { |
| 632 | - $param = $this->client->getClassConfig($this, $name); |
|
| 633 | - if ($param != '') { |
|
| 634 | - $params[$name] = $param; |
|
| 635 | - } |
|
| 636 | - return $params; |
|
| 632 | + $param = $this->client->getClassConfig($this, $name); |
|
| 633 | + if ($param != '') { |
|
| 634 | + $params[$name] = $param; |
|
| 635 | + } |
|
| 636 | + return $params; |
|
| 637 | 637 | } |
| 638 | 638 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | if ($decodedResponse != null && $decodedResponse['error']) { |
| 120 | 120 | $errorText = $decodedResponse['error']; |
| 121 | 121 | if (isset($decodedResponse['error_description'])) { |
| 122 | - $errorText .= ": " . $decodedResponse['error_description']; |
|
| 122 | + $errorText .= ": ".$decodedResponse['error_description']; |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | throw new Google_Auth_Exception( |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | $params['state'] = $this->state; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - return self::OAUTH2_AUTH_URL . "?" . http_build_query($params, '', '&'); |
|
| 174 | + return self::OAUTH2_AUTH_URL."?".http_build_query($params, '', '&'); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | if ($token == null) { |
| 185 | 185 | throw new Google_Auth_Exception('Could not json decode the token'); |
| 186 | 186 | } |
| 187 | - if (! isset($token['access_token'])) { |
|
| 187 | + if (!isset($token['access_token'])) { |
|
| 188 | 188 | throw new Google_Auth_Exception("Invalid token format"); |
| 189 | 189 | } |
| 190 | 190 | $this->token = $token; |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | $this->refreshTokenWithAssertion(); |
| 240 | 240 | } else { |
| 241 | 241 | $this->client->getLogger()->debug('OAuth2 access token expired'); |
| 242 | - if (! array_key_exists('refresh_token', $this->token)) { |
|
| 242 | + if (!array_key_exists('refresh_token', $this->token)) { |
|
| 243 | 243 | $error = "The OAuth 2.0 access token has expired," |
| 244 | 244 | ." and a refresh token is not available. Refresh tokens" |
| 245 | 245 | ." are not returned for responses that were auto-approved."; |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | |
| 256 | 256 | // Add the OAuth2 header to the request |
| 257 | 257 | $request->setRequestHeaders( |
| 258 | - array('Authorization' => 'Bearer ' . $this->token['access_token']) |
|
| 258 | + array('Authorization' => 'Bearer '.$this->token['access_token']) |
|
| 259 | 259 | ); |
| 260 | 260 | |
| 261 | 261 | return $request; |
@@ -349,7 +349,7 @@ discard block |
||
| 349 | 349 | throw new Google_Auth_Exception("Could not json decode the access token"); |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - if (! isset($token['access_token']) || ! isset($token['expires_in'])) { |
|
| 352 | + if (!isset($token['access_token']) || !isset($token['expires_in'])) { |
|
| 353 | 353 | throw new Google_Auth_Exception("Invalid token format"); |
| 354 | 354 | } |
| 355 | 355 | |
@@ -443,8 +443,8 @@ discard block |
||
| 443 | 443 | return json_decode($file, true); |
| 444 | 444 | } else { |
| 445 | 445 | throw new Google_Auth_Exception( |
| 446 | - "Failed to retrieve verification certificates: '" . |
|
| 447 | - $url . "'." |
|
| 446 | + "Failed to retrieve verification certificates: '". |
|
| 447 | + $url."'." |
|
| 448 | 448 | ); |
| 449 | 449 | } |
| 450 | 450 | } |
@@ -462,8 +462,8 @@ discard block |
||
| 462 | 462 | } |
| 463 | 463 | } |
| 464 | 464 | throw new Google_Auth_Exception( |
| 465 | - "Failed to retrieve verification certificates: '" . |
|
| 466 | - $request->getResponseBody() . "'.", |
|
| 465 | + "Failed to retrieve verification certificates: '". |
|
| 466 | + $request->getResponseBody()."'.", |
|
| 467 | 467 | $request->getResponseHttpCode() |
| 468 | 468 | ); |
| 469 | 469 | } |
@@ -518,20 +518,20 @@ discard block |
||
| 518 | 518 | if (count($segments) != 3) { |
| 519 | 519 | throw new Google_Auth_Exception("Wrong number of segments in token: $jwt"); |
| 520 | 520 | } |
| 521 | - $signed = $segments[0] . "." . $segments[1]; |
|
| 521 | + $signed = $segments[0].".".$segments[1]; |
|
| 522 | 522 | $signature = Google_Utils::urlSafeB64Decode($segments[2]); |
| 523 | 523 | |
| 524 | 524 | // Parse envelope. |
| 525 | 525 | $envelope = json_decode(Google_Utils::urlSafeB64Decode($segments[0]), true); |
| 526 | 526 | if (!$envelope) { |
| 527 | - throw new Google_Auth_Exception("Can't parse token envelope: " . $segments[0]); |
|
| 527 | + throw new Google_Auth_Exception("Can't parse token envelope: ".$segments[0]); |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | // Parse token |
| 531 | 531 | $json_body = Google_Utils::urlSafeB64Decode($segments[1]); |
| 532 | 532 | $payload = json_decode($json_body, true); |
| 533 | 533 | if (!$payload) { |
| 534 | - throw new Google_Auth_Exception("Can't parse token payload: " . $segments[1]); |
|
| 534 | + throw new Google_Auth_Exception("Can't parse token payload: ".$segments[1]); |
|
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | // Check signature |
@@ -38,20 +38,20 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public static function isRequestCacheable(Google_Http_Request $resp) |
| 40 | 40 | { |
| 41 | - $method = $resp->getRequestMethod(); |
|
| 42 | - if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | - return false; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - // Don't cache authorized requests/responses. |
|
| 47 | - // [rfc2616-14.8] When a shared cache receives a request containing an |
|
| 48 | - // Authorization field, it MUST NOT return the corresponding response |
|
| 49 | - // as a reply to any other request... |
|
| 50 | - if ($resp->getRequestHeader("authorization")) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return true; |
|
| 41 | + $method = $resp->getRequestMethod(); |
|
| 42 | + if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | + return false; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + // Don't cache authorized requests/responses. |
|
| 47 | + // [rfc2616-14.8] When a shared cache receives a request containing an |
|
| 48 | + // Authorization field, it MUST NOT return the corresponding response |
|
| 49 | + // as a reply to any other request... |
|
| 50 | + if ($resp->getRequestHeader("authorization")) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return true; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -64,48 +64,48 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public static function isResponseCacheable(Google_Http_Request $resp) |
| 66 | 66 | { |
| 67 | - // First, check if the HTTP request was cacheable before inspecting the |
|
| 68 | - // HTTP response. |
|
| 69 | - if (false == self::isRequestCacheable($resp)) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $code = $resp->getResponseHttpCode(); |
|
| 74 | - if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // The resource is uncacheable if the resource is already expired and |
|
| 79 | - // the resource doesn't have an ETag for revalidation. |
|
| 80 | - $etag = $resp->getResponseHeader("etag"); |
|
| 81 | - if (self::isExpired($resp) && $etag == false) { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT |
|
| 86 | - // store any part of either this response or the request that elicited it. |
|
| 87 | - $cacheControl = $resp->getParsedCacheControl(); |
|
| 88 | - if (isset($cacheControl['no-store'])) { |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Pragma: no-cache is an http request directive, but is occasionally |
|
| 93 | - // used as a response header incorrectly. |
|
| 94 | - $pragma = $resp->getResponseHeader('pragma'); |
|
| 95 | - if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) { |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that |
|
| 100 | - // a cache cannot determine from the request headers of a subsequent request |
|
| 101 | - // whether this response is the appropriate representation." |
|
| 102 | - // Given this, we deem responses with the Vary header as uncacheable. |
|
| 103 | - $vary = $resp->getResponseHeader('vary'); |
|
| 104 | - if ($vary) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return true; |
|
| 67 | + // First, check if the HTTP request was cacheable before inspecting the |
|
| 68 | + // HTTP response. |
|
| 69 | + if (false == self::isRequestCacheable($resp)) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $code = $resp->getResponseHttpCode(); |
|
| 74 | + if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // The resource is uncacheable if the resource is already expired and |
|
| 79 | + // the resource doesn't have an ETag for revalidation. |
|
| 80 | + $etag = $resp->getResponseHeader("etag"); |
|
| 81 | + if (self::isExpired($resp) && $etag == false) { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT |
|
| 86 | + // store any part of either this response or the request that elicited it. |
|
| 87 | + $cacheControl = $resp->getParsedCacheControl(); |
|
| 88 | + if (isset($cacheControl['no-store'])) { |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Pragma: no-cache is an http request directive, but is occasionally |
|
| 93 | + // used as a response header incorrectly. |
|
| 94 | + $pragma = $resp->getResponseHeader('pragma'); |
|
| 95 | + if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) { |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that |
|
| 100 | + // a cache cannot determine from the request headers of a subsequent request |
|
| 101 | + // whether this response is the appropriate representation." |
|
| 102 | + // Given this, we deem responses with the Vary header as uncacheable. |
|
| 103 | + $vary = $resp->getResponseHeader('vary'); |
|
| 104 | + if ($vary) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return true; |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -116,57 +116,57 @@ discard block |
||
| 116 | 116 | */ |
| 117 | 117 | public static function isExpired(Google_Http_Request $resp) |
| 118 | 118 | { |
| 119 | - // HTTP/1.1 clients and caches MUST treat other invalid date formats, |
|
| 120 | - // especially including the value “0”, as in the past. |
|
| 121 | - $parsedExpires = false; |
|
| 122 | - $responseHeaders = $resp->getResponseHeaders(); |
|
| 123 | - |
|
| 124 | - if (isset($responseHeaders['expires'])) { |
|
| 125 | - $rawExpires = $responseHeaders['expires']; |
|
| 126 | - // Check for a malformed expires header first. |
|
| 127 | - if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) { |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // See if we can parse the expires header. |
|
| 132 | - $parsedExpires = strtotime($rawExpires); |
|
| 133 | - if (false == $parsedExpires || $parsedExpires <= 0) { |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Calculate the freshness of an http response. |
|
| 139 | - $freshnessLifetime = false; |
|
| 140 | - $cacheControl = $resp->getParsedCacheControl(); |
|
| 141 | - if (isset($cacheControl['max-age'])) { |
|
| 142 | - $freshnessLifetime = $cacheControl['max-age']; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $rawDate = $resp->getResponseHeader('date'); |
|
| 146 | - $parsedDate = strtotime($rawDate); |
|
| 147 | - |
|
| 148 | - if (empty($rawDate) || false == $parsedDate) { |
|
| 149 | - // We can't default this to now, as that means future cache reads |
|
| 150 | - // will always pass with the logic below, so we will require a |
|
| 151 | - // date be injected if not supplied. |
|
| 152 | - throw new Google_Exception("All cacheable requests must have creation dates."); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { |
|
| 156 | - $freshnessLifetime = $parsedExpires - $parsedDate; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (false == $freshnessLifetime) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Calculate the age of an http response. |
|
| 164 | - $age = max(0, time() - $parsedDate); |
|
| 165 | - if (isset($responseHeaders['age'])) { |
|
| 166 | - $age = max($age, strtotime($responseHeaders['age'])); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $freshnessLifetime <= $age; |
|
| 119 | + // HTTP/1.1 clients and caches MUST treat other invalid date formats, |
|
| 120 | + // especially including the value “0”, as in the past. |
|
| 121 | + $parsedExpires = false; |
|
| 122 | + $responseHeaders = $resp->getResponseHeaders(); |
|
| 123 | + |
|
| 124 | + if (isset($responseHeaders['expires'])) { |
|
| 125 | + $rawExpires = $responseHeaders['expires']; |
|
| 126 | + // Check for a malformed expires header first. |
|
| 127 | + if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) { |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // See if we can parse the expires header. |
|
| 132 | + $parsedExpires = strtotime($rawExpires); |
|
| 133 | + if (false == $parsedExpires || $parsedExpires <= 0) { |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Calculate the freshness of an http response. |
|
| 139 | + $freshnessLifetime = false; |
|
| 140 | + $cacheControl = $resp->getParsedCacheControl(); |
|
| 141 | + if (isset($cacheControl['max-age'])) { |
|
| 142 | + $freshnessLifetime = $cacheControl['max-age']; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $rawDate = $resp->getResponseHeader('date'); |
|
| 146 | + $parsedDate = strtotime($rawDate); |
|
| 147 | + |
|
| 148 | + if (empty($rawDate) || false == $parsedDate) { |
|
| 149 | + // We can't default this to now, as that means future cache reads |
|
| 150 | + // will always pass with the logic below, so we will require a |
|
| 151 | + // date be injected if not supplied. |
|
| 152 | + throw new Google_Exception("All cacheable requests must have creation dates."); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { |
|
| 156 | + $freshnessLifetime = $parsedExpires - $parsedDate; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (false == $freshnessLifetime) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Calculate the age of an http response. |
|
| 164 | + $age = max(0, time() - $parsedDate); |
|
| 165 | + if (isset($responseHeaders['age'])) { |
|
| 166 | + $age = max($age, strtotime($responseHeaders['age'])); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $freshnessLifetime <= $age; |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | /** |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | public static function mustRevalidate(Google_Http_Request $response) |
| 179 | 179 | { |
| 180 | - // [13.3] When a cache has a stale entry that it would like to use as a |
|
| 181 | - // response to a client's request, it first has to check with the origin |
|
| 182 | - // server to see if its cached entry is still usable. |
|
| 183 | - return self::isExpired($response); |
|
| 180 | + // [13.3] When a cache has a stale entry that it would like to use as a |
|
| 181 | + // response to a client's request, it first has to check with the origin |
|
| 182 | + // server to see if its cached entry is still usable. |
|
| 183 | + return self::isExpired($response); |
|
| 184 | 184 | } |
| 185 | 185 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | public static function isRequestCacheable(Google_Http_Request $resp) |
| 40 | 40 | { |
| 41 | 41 | $method = $resp->getRequestMethod(); |
| 42 | - if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 42 | + if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) { |
|
| 43 | 43 | return false; |
| 44 | 44 | } |
| 45 | 45 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $code = $resp->getResponseHttpCode(); |
| 74 | - if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 74 | + if (!in_array($code, self::$CACHEABLE_STATUS_CODES)) { |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -73,29 +73,29 @@ discard block |
||
| 73 | 73 | * only used if resumable=True |
| 74 | 74 | */ |
| 75 | 75 | public function __construct( |
| 76 | - Google_Client $client, |
|
| 77 | - Google_Http_Request $request, |
|
| 78 | - $mimeType, |
|
| 79 | - $data, |
|
| 80 | - $resumable = false, |
|
| 81 | - $chunkSize = false, |
|
| 82 | - $boundary = false |
|
| 76 | + Google_Client $client, |
|
| 77 | + Google_Http_Request $request, |
|
| 78 | + $mimeType, |
|
| 79 | + $data, |
|
| 80 | + $resumable = false, |
|
| 81 | + $chunkSize = false, |
|
| 82 | + $boundary = false |
|
| 83 | 83 | ) { |
| 84 | - $this->client = $client; |
|
| 85 | - $this->request = $request; |
|
| 86 | - $this->mimeType = $mimeType; |
|
| 87 | - $this->data = $data; |
|
| 88 | - $this->size = strlen($this->data); |
|
| 89 | - $this->resumable = $resumable; |
|
| 90 | - if (!$chunkSize) { |
|
| 91 | - $chunkSize = 256 * 1024; |
|
| 92 | - } |
|
| 93 | - $this->chunkSize = $chunkSize; |
|
| 94 | - $this->progress = 0; |
|
| 95 | - $this->boundary = $boundary; |
|
| 96 | - |
|
| 97 | - // Process Media Request |
|
| 98 | - $this->process(); |
|
| 84 | + $this->client = $client; |
|
| 85 | + $this->request = $request; |
|
| 86 | + $this->mimeType = $mimeType; |
|
| 87 | + $this->data = $data; |
|
| 88 | + $this->size = strlen($this->data); |
|
| 89 | + $this->resumable = $resumable; |
|
| 90 | + if (!$chunkSize) { |
|
| 91 | + $chunkSize = 256 * 1024; |
|
| 92 | + } |
|
| 93 | + $this->chunkSize = $chunkSize; |
|
| 94 | + $this->progress = 0; |
|
| 95 | + $this->boundary = $boundary; |
|
| 96 | + |
|
| 97 | + // Process Media Request |
|
| 98 | + $this->process(); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function setFileSize($size) |
| 106 | 106 | { |
| 107 | - $this->size = $size; |
|
| 107 | + $this->size = $size; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getProgress() |
| 115 | 115 | { |
| 116 | - return $this->progress; |
|
| 116 | + return $this->progress; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function getHttpResultCode() |
| 124 | 124 | { |
| 125 | - return $this->httpResultCode; |
|
| 125 | + return $this->httpResultCode; |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -132,56 +132,56 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | public function nextChunk($chunk = false) |
| 134 | 134 | { |
| 135 | - if (false == $this->resumeUri) { |
|
| 136 | - $this->resumeUri = $this->getResumeUri(); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (false == $chunk) { |
|
| 140 | - $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $lastBytePos = $this->progress + strlen($chunk) - 1; |
|
| 144 | - $headers = array( |
|
| 145 | - 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
| 146 | - 'content-type' => $this->request->getRequestHeader('content-type'), |
|
| 147 | - 'content-length' => $this->chunkSize, |
|
| 148 | - 'expect' => '', |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - $httpRequest = new Google_Http_Request( |
|
| 152 | - $this->resumeUri, |
|
| 153 | - 'PUT', |
|
| 154 | - $headers, |
|
| 155 | - $chunk |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) { |
|
| 159 | - $httpRequest->enableGzip(); |
|
| 160 | - } else { |
|
| 161 | - $httpRequest->disableGzip(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 165 | - $response->setExpectedClass($this->request->getExpectedClass()); |
|
| 166 | - $code = $response->getResponseHttpCode(); |
|
| 167 | - $this->httpResultCode = $code; |
|
| 168 | - |
|
| 169 | - if (308 == $code) { |
|
| 170 | - // Track the amount uploaded. |
|
| 171 | - $range = explode('-', $response->getResponseHeader('range')); |
|
| 172 | - $this->progress = $range[1] + 1; |
|
| 173 | - |
|
| 174 | - // Allow for changing upload URLs. |
|
| 175 | - $location = $response->getResponseHeader('location'); |
|
| 176 | - if ($location) { |
|
| 177 | - $this->resumeUri = $location; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // No problems, but upload not complete. |
|
| 181 | - return false; |
|
| 182 | - } else { |
|
| 183 | - return Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 184 | - } |
|
| 135 | + if (false == $this->resumeUri) { |
|
| 136 | + $this->resumeUri = $this->getResumeUri(); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (false == $chunk) { |
|
| 140 | + $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $lastBytePos = $this->progress + strlen($chunk) - 1; |
|
| 144 | + $headers = array( |
|
| 145 | + 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
| 146 | + 'content-type' => $this->request->getRequestHeader('content-type'), |
|
| 147 | + 'content-length' => $this->chunkSize, |
|
| 148 | + 'expect' => '', |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + $httpRequest = new Google_Http_Request( |
|
| 152 | + $this->resumeUri, |
|
| 153 | + 'PUT', |
|
| 154 | + $headers, |
|
| 155 | + $chunk |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + if ($this->client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) { |
|
| 159 | + $httpRequest->enableGzip(); |
|
| 160 | + } else { |
|
| 161 | + $httpRequest->disableGzip(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 165 | + $response->setExpectedClass($this->request->getExpectedClass()); |
|
| 166 | + $code = $response->getResponseHttpCode(); |
|
| 167 | + $this->httpResultCode = $code; |
|
| 168 | + |
|
| 169 | + if (308 == $code) { |
|
| 170 | + // Track the amount uploaded. |
|
| 171 | + $range = explode('-', $response->getResponseHeader('range')); |
|
| 172 | + $this->progress = $range[1] + 1; |
|
| 173 | + |
|
| 174 | + // Allow for changing upload URLs. |
|
| 175 | + $location = $response->getResponseHeader('location'); |
|
| 176 | + if ($location) { |
|
| 177 | + $this->resumeUri = $location; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // No problems, but upload not complete. |
|
| 181 | + return false; |
|
| 182 | + } else { |
|
| 183 | + return Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 184 | + } |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
@@ -192,53 +192,53 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | private function process() |
| 194 | 194 | { |
| 195 | - $postBody = false; |
|
| 196 | - $contentType = false; |
|
| 197 | - |
|
| 198 | - $meta = $this->request->getPostBody(); |
|
| 199 | - $meta = is_string($meta) ? json_decode($meta, true) : $meta; |
|
| 200 | - |
|
| 201 | - $uploadType = $this->getUploadType($meta); |
|
| 202 | - $this->request->setQueryParam('uploadType', $uploadType); |
|
| 203 | - $this->transformToUploadUrl(); |
|
| 204 | - $mimeType = $this->mimeType ? |
|
| 205 | - $this->mimeType : |
|
| 206 | - $this->request->getRequestHeader('content-type'); |
|
| 207 | - |
|
| 208 | - if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
|
| 209 | - $contentType = $mimeType; |
|
| 210 | - $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
| 211 | - } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { |
|
| 212 | - $contentType = $mimeType; |
|
| 213 | - $postBody = $this->data; |
|
| 214 | - } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { |
|
| 215 | - // This is a multipart/related upload. |
|
| 216 | - $boundary = $this->boundary ? $this->boundary : mt_rand(); |
|
| 217 | - $boundary = str_replace('"', '', $boundary); |
|
| 218 | - $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 219 | - $related = "--$boundary\r\n"; |
|
| 220 | - $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
| 221 | - $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 222 | - $related .= "--$boundary\r\n"; |
|
| 223 | - $related .= "Content-Type: $mimeType\r\n"; |
|
| 224 | - $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
| 225 | - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 226 | - $related .= "--$boundary--"; |
|
| 227 | - $postBody = $related; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $this->request->setPostBody($postBody); |
|
| 231 | - |
|
| 232 | - if (isset($contentType) && $contentType) { |
|
| 233 | - $contentTypeHeader['content-type'] = $contentType; |
|
| 234 | - $this->request->setRequestHeaders($contentTypeHeader); |
|
| 235 | - } |
|
| 195 | + $postBody = false; |
|
| 196 | + $contentType = false; |
|
| 197 | + |
|
| 198 | + $meta = $this->request->getPostBody(); |
|
| 199 | + $meta = is_string($meta) ? json_decode($meta, true) : $meta; |
|
| 200 | + |
|
| 201 | + $uploadType = $this->getUploadType($meta); |
|
| 202 | + $this->request->setQueryParam('uploadType', $uploadType); |
|
| 203 | + $this->transformToUploadUrl(); |
|
| 204 | + $mimeType = $this->mimeType ? |
|
| 205 | + $this->mimeType : |
|
| 206 | + $this->request->getRequestHeader('content-type'); |
|
| 207 | + |
|
| 208 | + if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
|
| 209 | + $contentType = $mimeType; |
|
| 210 | + $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
| 211 | + } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { |
|
| 212 | + $contentType = $mimeType; |
|
| 213 | + $postBody = $this->data; |
|
| 214 | + } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { |
|
| 215 | + // This is a multipart/related upload. |
|
| 216 | + $boundary = $this->boundary ? $this->boundary : mt_rand(); |
|
| 217 | + $boundary = str_replace('"', '', $boundary); |
|
| 218 | + $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 219 | + $related = "--$boundary\r\n"; |
|
| 220 | + $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
| 221 | + $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 222 | + $related .= "--$boundary\r\n"; |
|
| 223 | + $related .= "Content-Type: $mimeType\r\n"; |
|
| 224 | + $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
| 225 | + $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 226 | + $related .= "--$boundary--"; |
|
| 227 | + $postBody = $related; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $this->request->setPostBody($postBody); |
|
| 231 | + |
|
| 232 | + if (isset($contentType) && $contentType) { |
|
| 233 | + $contentTypeHeader['content-type'] = $contentType; |
|
| 234 | + $this->request->setRequestHeaders($contentTypeHeader); |
|
| 235 | + } |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | private function transformToUploadUrl() |
| 239 | 239 | { |
| 240 | - $base = $this->request->getBaseComponent(); |
|
| 241 | - $this->request->setBaseComponent($base . '/upload'); |
|
| 240 | + $base = $this->request->getBaseComponent(); |
|
| 241 | + $this->request->setBaseComponent($base . '/upload'); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -252,56 +252,56 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | public function getUploadType($meta) |
| 254 | 254 | { |
| 255 | - if ($this->resumable) { |
|
| 256 | - return self::UPLOAD_RESUMABLE_TYPE; |
|
| 257 | - } |
|
| 255 | + if ($this->resumable) { |
|
| 256 | + return self::UPLOAD_RESUMABLE_TYPE; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - if (false == $meta && $this->data) { |
|
| 260 | - return self::UPLOAD_MEDIA_TYPE; |
|
| 261 | - } |
|
| 259 | + if (false == $meta && $this->data) { |
|
| 260 | + return self::UPLOAD_MEDIA_TYPE; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - return self::UPLOAD_MULTIPART_TYPE; |
|
| 263 | + return self::UPLOAD_MULTIPART_TYPE; |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | private function getResumeUri() |
| 267 | 267 | { |
| 268 | - $result = null; |
|
| 269 | - $body = $this->request->getPostBody(); |
|
| 270 | - if ($body) { |
|
| 271 | - $headers = array( |
|
| 272 | - 'content-type' => 'application/json; charset=UTF-8', |
|
| 273 | - 'content-length' => Google_Utils::getStrLen($body), |
|
| 274 | - 'x-upload-content-type' => $this->mimeType, |
|
| 275 | - 'x-upload-content-length' => $this->size, |
|
| 276 | - 'expect' => '', |
|
| 277 | - ); |
|
| 278 | - $this->request->setRequestHeaders($headers); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - $response = $this->client->getIo()->makeRequest($this->request); |
|
| 282 | - $location = $response->getResponseHeader('location'); |
|
| 283 | - $code = $response->getResponseHttpCode(); |
|
| 284 | - |
|
| 285 | - if (200 == $code && true == $location) { |
|
| 286 | - return $location; |
|
| 287 | - } |
|
| 288 | - $message = $code; |
|
| 289 | - $body = @json_decode($response->getResponseBody()); |
|
| 290 | - if (!empty($body->error->errors) ) { |
|
| 291 | - $message .= ': '; |
|
| 292 | - foreach ($body->error->errors as $error) { |
|
| 293 | - $message .= "{$error->domain}, {$error->message};"; |
|
| 294 | - } |
|
| 295 | - $message = rtrim($message, ';'); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $error = "Failed to start the resumable upload (HTTP {$message})"; |
|
| 299 | - $this->client->getLogger()->error($error); |
|
| 300 | - throw new Google_Exception($error); |
|
| 268 | + $result = null; |
|
| 269 | + $body = $this->request->getPostBody(); |
|
| 270 | + if ($body) { |
|
| 271 | + $headers = array( |
|
| 272 | + 'content-type' => 'application/json; charset=UTF-8', |
|
| 273 | + 'content-length' => Google_Utils::getStrLen($body), |
|
| 274 | + 'x-upload-content-type' => $this->mimeType, |
|
| 275 | + 'x-upload-content-length' => $this->size, |
|
| 276 | + 'expect' => '', |
|
| 277 | + ); |
|
| 278 | + $this->request->setRequestHeaders($headers); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + $response = $this->client->getIo()->makeRequest($this->request); |
|
| 282 | + $location = $response->getResponseHeader('location'); |
|
| 283 | + $code = $response->getResponseHttpCode(); |
|
| 284 | + |
|
| 285 | + if (200 == $code && true == $location) { |
|
| 286 | + return $location; |
|
| 287 | + } |
|
| 288 | + $message = $code; |
|
| 289 | + $body = @json_decode($response->getResponseBody()); |
|
| 290 | + if (!empty($body->error->errors) ) { |
|
| 291 | + $message .= ': '; |
|
| 292 | + foreach ($body->error->errors as $error) { |
|
| 293 | + $message .= "{$error->domain}, {$error->message};"; |
|
| 294 | + } |
|
| 295 | + $message = rtrim($message, ';'); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $error = "Failed to start the resumable upload (HTTP {$message})"; |
|
| 299 | + $this->client->getLogger()->error($error); |
|
| 300 | + throw new Google_Exception($error); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | public function setChunkSize($chunkSize) |
| 304 | 304 | { |
| 305 | - $this->chunkSize = $chunkSize; |
|
| 305 | + $this->chunkSize = $chunkSize; |
|
| 306 | 306 | } |
| 307 | 307 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -202,8 +202,7 @@ discard block |
||
| 202 | 202 | $this->request->setQueryParam('uploadType', $uploadType); |
| 203 | 203 | $this->transformToUploadUrl(); |
| 204 | 204 | $mimeType = $this->mimeType ? |
| 205 | - $this->mimeType : |
|
| 206 | - $this->request->getRequestHeader('content-type'); |
|
| 205 | + $this->mimeType : $this->request->getRequestHeader('content-type'); |
|
| 207 | 206 | |
| 208 | 207 | if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
| 209 | 208 | $contentType = $mimeType; |
@@ -215,14 +214,14 @@ discard block |
||
| 215 | 214 | // This is a multipart/related upload. |
| 216 | 215 | $boundary = $this->boundary ? $this->boundary : mt_rand(); |
| 217 | 216 | $boundary = str_replace('"', '', $boundary); |
| 218 | - $contentType = 'multipart/related; boundary=' . $boundary; |
|
| 217 | + $contentType = 'multipart/related; boundary='.$boundary; |
|
| 219 | 218 | $related = "--$boundary\r\n"; |
| 220 | 219 | $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
| 221 | - $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
| 220 | + $related .= "\r\n".json_encode($meta)."\r\n"; |
|
| 222 | 221 | $related .= "--$boundary\r\n"; |
| 223 | 222 | $related .= "Content-Type: $mimeType\r\n"; |
| 224 | 223 | $related .= "Content-Transfer-Encoding: base64\r\n"; |
| 225 | - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
| 224 | + $related .= "\r\n".base64_encode($this->data)."\r\n"; |
|
| 226 | 225 | $related .= "--$boundary--"; |
| 227 | 226 | $postBody = $related; |
| 228 | 227 | } |
@@ -238,7 +237,7 @@ discard block |
||
| 238 | 237 | private function transformToUploadUrl() |
| 239 | 238 | { |
| 240 | 239 | $base = $this->request->getBaseComponent(); |
| 241 | - $this->request->setBaseComponent($base . '/upload'); |
|
| 240 | + $this->request->setBaseComponent($base.'/upload'); |
|
| 242 | 241 | } |
| 243 | 242 | |
| 244 | 243 | /** |
@@ -287,7 +286,7 @@ discard block |
||
| 287 | 286 | } |
| 288 | 287 | $message = $code; |
| 289 | 288 | $body = @json_decode($response->getResponseBody()); |
| 290 | - if (!empty($body->error->errors) ) { |
|
| 289 | + if (!empty($body->error->errors)) { |
|
| 291 | 290 | $message .= ': '; |
| 292 | 291 | foreach ($body->error->errors as $error) { |
| 293 | 292 | $message .= "{$error->domain}, {$error->message};"; |
@@ -41,105 +41,105 @@ |
||
| 41 | 41 | |
| 42 | 42 | public function __construct(Google_Client $client, $boundary = false, $rootUrl = '', $batchPath = '') |
| 43 | 43 | { |
| 44 | - $this->client = $client; |
|
| 45 | - $this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/'); |
|
| 46 | - $this->batch_path = $batchPath ? $batchPath : 'batch'; |
|
| 47 | - $this->expected_classes = array(); |
|
| 48 | - $boundary = (false == $boundary) ? mt_rand() : $boundary; |
|
| 49 | - $this->boundary = str_replace('"', '', $boundary); |
|
| 44 | + $this->client = $client; |
|
| 45 | + $this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/'); |
|
| 46 | + $this->batch_path = $batchPath ? $batchPath : 'batch'; |
|
| 47 | + $this->expected_classes = array(); |
|
| 48 | + $boundary = (false == $boundary) ? mt_rand() : $boundary; |
|
| 49 | + $this->boundary = str_replace('"', '', $boundary); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | public function add(Google_Http_Request $request, $key = false) |
| 53 | 53 | { |
| 54 | - if (false == $key) { |
|
| 55 | - $key = mt_rand(); |
|
| 56 | - } |
|
| 54 | + if (false == $key) { |
|
| 55 | + $key = mt_rand(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->requests[$key] = $request; |
|
| 58 | + $this->requests[$key] = $request; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | public function execute() |
| 62 | 62 | { |
| 63 | - $body = ''; |
|
| 63 | + $body = ''; |
|
| 64 | 64 | |
| 65 | - /** @var Google_Http_Request $req */ |
|
| 66 | - foreach ($this->requests as $key => $req) { |
|
| 67 | - $body .= "--{$this->boundary}\n"; |
|
| 68 | - $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | - $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 70 | - } |
|
| 65 | + /** @var Google_Http_Request $req */ |
|
| 66 | + foreach ($this->requests as $key => $req) { |
|
| 67 | + $body .= "--{$this->boundary}\n"; |
|
| 68 | + $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | + $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $body .= "--{$this->boundary}--"; |
|
| 72 | + $body .= "--{$this->boundary}--"; |
|
| 73 | 73 | |
| 74 | - $url = $this->root_url . '/' . $this->batch_path; |
|
| 75 | - $httpRequest = new Google_Http_Request($url, 'POST'); |
|
| 76 | - $httpRequest->setRequestHeaders( |
|
| 77 | - array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 78 | - ); |
|
| 74 | + $url = $this->root_url . '/' . $this->batch_path; |
|
| 75 | + $httpRequest = new Google_Http_Request($url, 'POST'); |
|
| 76 | + $httpRequest->setRequestHeaders( |
|
| 77 | + array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 78 | + ); |
|
| 79 | 79 | |
| 80 | - $httpRequest->setPostBody($body); |
|
| 81 | - $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 80 | + $httpRequest->setPostBody($body); |
|
| 81 | + $response = $this->client->getIo()->makeRequest($httpRequest); |
|
| 82 | 82 | |
| 83 | - return $this->parseResponse($response); |
|
| 83 | + return $this->parseResponse($response); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | public function parseResponse(Google_Http_Request $response) |
| 87 | 87 | { |
| 88 | - $contentType = $response->getResponseHeader('content-type'); |
|
| 89 | - $contentType = explode(';', $contentType); |
|
| 90 | - $boundary = false; |
|
| 91 | - foreach ($contentType as $part) { |
|
| 92 | - $part = (explode('=', $part, 2)); |
|
| 93 | - if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
| 94 | - $boundary = $part[1]; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $body = $response->getResponseBody(); |
|
| 99 | - if ($body) { |
|
| 100 | - $body = str_replace("--$boundary--", "--$boundary", $body); |
|
| 101 | - $parts = explode("--$boundary", $body); |
|
| 102 | - $responses = array(); |
|
| 103 | - |
|
| 104 | - foreach ($parts as $part) { |
|
| 105 | - $part = trim($part); |
|
| 106 | - if (!empty($part)) { |
|
| 107 | - list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
| 108 | - $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); |
|
| 109 | - |
|
| 110 | - $status = substr($part, 0, strpos($part, "\n")); |
|
| 111 | - $status = explode(" ", $status); |
|
| 112 | - $status = $status[1]; |
|
| 113 | - |
|
| 114 | - list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); |
|
| 115 | - $response = new Google_Http_Request(""); |
|
| 116 | - $response->setResponseHttpCode($status); |
|
| 117 | - $response->setResponseHeaders($partHeaders); |
|
| 118 | - $response->setResponseBody($partBody); |
|
| 119 | - |
|
| 120 | - // Need content id. |
|
| 121 | - $key = $metaHeaders['content-id']; |
|
| 122 | - |
|
| 123 | - if (isset($this->expected_classes[$key]) && |
|
| 124 | - strlen($this->expected_classes[$key]) > 0) { |
|
| 125 | - $class = $this->expected_classes[$key]; |
|
| 126 | - $response->setExpectedClass($class); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - try { |
|
| 130 | - $response = Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 131 | - $responses[$key] = $response; |
|
| 132 | - } catch (Google_Service_Exception $e) { |
|
| 133 | - // Store the exception as the response, so successful responses |
|
| 134 | - // can be processed. |
|
| 135 | - $responses[$key] = $e; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $responses; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return null; |
|
| 88 | + $contentType = $response->getResponseHeader('content-type'); |
|
| 89 | + $contentType = explode(';', $contentType); |
|
| 90 | + $boundary = false; |
|
| 91 | + foreach ($contentType as $part) { |
|
| 92 | + $part = (explode('=', $part, 2)); |
|
| 93 | + if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
| 94 | + $boundary = $part[1]; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $body = $response->getResponseBody(); |
|
| 99 | + if ($body) { |
|
| 100 | + $body = str_replace("--$boundary--", "--$boundary", $body); |
|
| 101 | + $parts = explode("--$boundary", $body); |
|
| 102 | + $responses = array(); |
|
| 103 | + |
|
| 104 | + foreach ($parts as $part) { |
|
| 105 | + $part = trim($part); |
|
| 106 | + if (!empty($part)) { |
|
| 107 | + list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
| 108 | + $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); |
|
| 109 | + |
|
| 110 | + $status = substr($part, 0, strpos($part, "\n")); |
|
| 111 | + $status = explode(" ", $status); |
|
| 112 | + $status = $status[1]; |
|
| 113 | + |
|
| 114 | + list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); |
|
| 115 | + $response = new Google_Http_Request(""); |
|
| 116 | + $response->setResponseHttpCode($status); |
|
| 117 | + $response->setResponseHeaders($partHeaders); |
|
| 118 | + $response->setResponseBody($partBody); |
|
| 119 | + |
|
| 120 | + // Need content id. |
|
| 121 | + $key = $metaHeaders['content-id']; |
|
| 122 | + |
|
| 123 | + if (isset($this->expected_classes[$key]) && |
|
| 124 | + strlen($this->expected_classes[$key]) > 0) { |
|
| 125 | + $class = $this->expected_classes[$key]; |
|
| 126 | + $response->setExpectedClass($class); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + try { |
|
| 130 | + $response = Google_Http_REST::decodeHttpResponse($response, $this->client); |
|
| 131 | + $responses[$key] = $response; |
|
| 132 | + } catch (Google_Service_Exception $e) { |
|
| 133 | + // Store the exception as the response, so successful responses |
|
| 134 | + // can be processed. |
|
| 135 | + $responses[$key] = $e; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $responses; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return null; |
|
| 144 | 144 | } |
| 145 | 145 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -65,16 +65,16 @@ discard block |
||
| 65 | 65 | /** @var Google_Http_Request $req */ |
| 66 | 66 | foreach ($this->requests as $key => $req) { |
| 67 | 67 | $body .= "--{$this->boundary}\n"; |
| 68 | - $body .= $req->toBatchString($key) . "\n\n"; |
|
| 69 | - $this->expected_classes["response-" . $key] = $req->getExpectedClass(); |
|
| 68 | + $body .= $req->toBatchString($key)."\n\n"; |
|
| 69 | + $this->expected_classes["response-".$key] = $req->getExpectedClass(); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $body .= "--{$this->boundary}--"; |
| 73 | 73 | |
| 74 | - $url = $this->root_url . '/' . $this->batch_path; |
|
| 74 | + $url = $this->root_url.'/'.$this->batch_path; |
|
| 75 | 75 | $httpRequest = new Google_Http_Request($url, 'POST'); |
| 76 | 76 | $httpRequest->setRequestHeaders( |
| 77 | - array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) |
|
| 77 | + array('Content-Type' => 'multipart/mixed; boundary='.$this->boundary) |
|
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | $httpRequest->setPostBody($body); |
@@ -32,9 +32,9 @@ discard block |
||
| 32 | 32 | const GZIP_UA = " (gzip)"; |
| 33 | 33 | |
| 34 | 34 | private $batchHeaders = array( |
| 35 | - 'Content-Type' => 'application/http', |
|
| 36 | - 'Content-Transfer-Encoding' => 'binary', |
|
| 37 | - 'MIME-Version' => '1.0', |
|
| 35 | + 'Content-Type' => 'application/http', |
|
| 36 | + 'Content-Transfer-Encoding' => 'binary', |
|
| 37 | + 'MIME-Version' => '1.0', |
|
| 38 | 38 | ); |
| 39 | 39 | |
| 40 | 40 | protected $queryParams; |
@@ -56,15 +56,15 @@ discard block |
||
| 56 | 56 | public $accessKey; |
| 57 | 57 | |
| 58 | 58 | public function __construct( |
| 59 | - $url, |
|
| 60 | - $method = 'GET', |
|
| 61 | - $headers = array(), |
|
| 62 | - $postBody = null |
|
| 59 | + $url, |
|
| 60 | + $method = 'GET', |
|
| 61 | + $headers = array(), |
|
| 62 | + $postBody = null |
|
| 63 | 63 | ) { |
| 64 | - $this->setUrl($url); |
|
| 65 | - $this->setRequestMethod($method); |
|
| 66 | - $this->setRequestHeaders($headers); |
|
| 67 | - $this->setPostBody($postBody); |
|
| 64 | + $this->setUrl($url); |
|
| 65 | + $this->setRequestMethod($method); |
|
| 66 | + $this->setRequestHeaders($headers); |
|
| 67 | + $this->setPostBody($postBody); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | public function getBaseComponent() |
| 76 | 76 | { |
| 77 | - return $this->baseComponent; |
|
| 77 | + return $this->baseComponent; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | public function setBaseComponent($baseComponent) |
| 85 | 85 | { |
| 86 | - $this->baseComponent = rtrim($baseComponent, '/'); |
|
| 86 | + $this->baseComponent = rtrim($baseComponent, '/'); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function enableGzip() |
| 93 | 93 | { |
| 94 | - $this->setRequestHeaders(array("Accept-Encoding" => "gzip")); |
|
| 95 | - $this->canGzip = true; |
|
| 96 | - $this->setUserAgent($this->userAgent); |
|
| 94 | + $this->setRequestHeaders(array("Accept-Encoding" => "gzip")); |
|
| 95 | + $this->canGzip = true; |
|
| 96 | + $this->setUserAgent($this->userAgent); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -101,14 +101,14 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function disableGzip() |
| 103 | 103 | { |
| 104 | - if ( |
|
| 105 | - isset($this->requestHeaders['accept-encoding']) && |
|
| 106 | - $this->requestHeaders['accept-encoding'] == "gzip" |
|
| 107 | - ) { |
|
| 108 | - unset($this->requestHeaders['accept-encoding']); |
|
| 109 | - } |
|
| 110 | - $this->canGzip = false; |
|
| 111 | - $this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent); |
|
| 104 | + if ( |
|
| 105 | + isset($this->requestHeaders['accept-encoding']) && |
|
| 106 | + $this->requestHeaders['accept-encoding'] == "gzip" |
|
| 107 | + ) { |
|
| 108 | + unset($this->requestHeaders['accept-encoding']); |
|
| 109 | + } |
|
| 110 | + $this->canGzip = false; |
|
| 111 | + $this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function canGzip() |
| 119 | 119 | { |
| 120 | - return $this->canGzip; |
|
| 120 | + return $this->canGzip; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | public function getQueryParams() |
| 129 | 129 | { |
| 130 | - return $this->queryParams; |
|
| 130 | + return $this->queryParams; |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function setQueryParam($key, $value) |
| 139 | 139 | { |
| 140 | - $this->queryParams[$key] = $value; |
|
| 140 | + $this->queryParams[$key] = $value; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | */ |
| 146 | 146 | public function getResponseHttpCode() |
| 147 | 147 | { |
| 148 | - return (int) $this->responseHttpCode; |
|
| 148 | + return (int) $this->responseHttpCode; |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /** |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function setResponseHttpCode($responseHttpCode) |
| 155 | 155 | { |
| 156 | - $this->responseHttpCode = $responseHttpCode; |
|
| 156 | + $this->responseHttpCode = $responseHttpCode; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public function getResponseHeaders() |
| 163 | 163 | { |
| 164 | - return $this->responseHeaders; |
|
| 164 | + return $this->responseHeaders; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | /** |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | public function getResponseBody() |
| 171 | 171 | { |
| 172 | - return $this->responseBody; |
|
| 172 | + return $this->responseBody; |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /** |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function setExpectedClass($class) |
| 181 | 181 | { |
| 182 | - $this->expectedClass = $class; |
|
| 182 | + $this->expectedClass = $class; |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | public function getExpectedClass() |
| 190 | 190 | { |
| 191 | - return $this->expectedClass; |
|
| 191 | + return $this->expectedClass; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function enableExpectedRaw() |
| 198 | 198 | { |
| 199 | - $this->expectedRaw = true; |
|
| 199 | + $this->expectedRaw = true; |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | /** |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | public function disableExpectedRaw() |
| 206 | 206 | { |
| 207 | - $this->expectedRaw = false; |
|
| 207 | + $this->expectedRaw = false; |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | */ |
| 214 | 214 | public function getExpectedRaw() |
| 215 | 215 | { |
| 216 | - return $this->expectedRaw; |
|
| 216 | + return $this->expectedRaw; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -222,12 +222,12 @@ discard block |
||
| 222 | 222 | */ |
| 223 | 223 | public function setResponseHeaders($headers) |
| 224 | 224 | { |
| 225 | - $headers = Google_Utils::normalize($headers); |
|
| 226 | - if ($this->responseHeaders) { |
|
| 227 | - $headers = array_merge($this->responseHeaders, $headers); |
|
| 228 | - } |
|
| 225 | + $headers = Google_Utils::normalize($headers); |
|
| 226 | + if ($this->responseHeaders) { |
|
| 227 | + $headers = array_merge($this->responseHeaders, $headers); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - $this->responseHeaders = $headers; |
|
| 230 | + $this->responseHeaders = $headers; |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -237,9 +237,9 @@ discard block |
||
| 237 | 237 | */ |
| 238 | 238 | public function getResponseHeader($key) |
| 239 | 239 | { |
| 240 | - return isset($this->responseHeaders[$key]) |
|
| 241 | - ? $this->responseHeaders[$key] |
|
| 242 | - : false; |
|
| 240 | + return isset($this->responseHeaders[$key]) |
|
| 241 | + ? $this->responseHeaders[$key] |
|
| 242 | + : false; |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | */ |
| 248 | 248 | public function setResponseBody($responseBody) |
| 249 | 249 | { |
| 250 | - $this->responseBody = $responseBody; |
|
| 250 | + $this->responseBody = $responseBody; |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | /** |
@@ -255,10 +255,10 @@ discard block |
||
| 255 | 255 | */ |
| 256 | 256 | public function getUrl() |
| 257 | 257 | { |
| 258 | - return $this->baseComponent . $this->path . |
|
| 259 | - (count($this->queryParams) ? |
|
| 260 | - "?" . $this->buildQuery($this->queryParams) : |
|
| 261 | - ''); |
|
| 258 | + return $this->baseComponent . $this->path . |
|
| 259 | + (count($this->queryParams) ? |
|
| 260 | + "?" . $this->buildQuery($this->queryParams) : |
|
| 261 | + ''); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | public function getRequestMethod() |
| 268 | 268 | { |
| 269 | - return $this->requestMethod; |
|
| 269 | + return $this->requestMethod; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | /** |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | */ |
| 275 | 275 | public function getRequestHeaders() |
| 276 | 276 | { |
| 277 | - return $this->requestHeaders; |
|
| 277 | + return $this->requestHeaders; |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
@@ -284,9 +284,9 @@ discard block |
||
| 284 | 284 | */ |
| 285 | 285 | public function getRequestHeader($key) |
| 286 | 286 | { |
| 287 | - return isset($this->requestHeaders[$key]) |
|
| 288 | - ? $this->requestHeaders[$key] |
|
| 289 | - : false; |
|
| 287 | + return isset($this->requestHeaders[$key]) |
|
| 288 | + ? $this->requestHeaders[$key] |
|
| 289 | + : false; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | */ |
| 295 | 295 | public function getPostBody() |
| 296 | 296 | { |
| 297 | - return $this->postBody; |
|
| 297 | + return $this->postBody; |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -302,26 +302,26 @@ discard block |
||
| 302 | 302 | */ |
| 303 | 303 | public function setUrl($url) |
| 304 | 304 | { |
| 305 | - if (substr($url, 0, 4) != 'http') { |
|
| 306 | - // Force the path become relative. |
|
| 307 | - if (substr($url, 0, 1) !== '/') { |
|
| 308 | - $url = '/' . $url; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - $parts = parse_url($url); |
|
| 312 | - if (isset($parts['host'])) { |
|
| 313 | - $this->baseComponent = sprintf( |
|
| 314 | - "%s%s%s", |
|
| 315 | - isset($parts['scheme']) ? $parts['scheme'] . "://" : '', |
|
| 316 | - isset($parts['host']) ? $parts['host'] : '', |
|
| 317 | - isset($parts['port']) ? ":" . $parts['port'] : '' |
|
| 318 | - ); |
|
| 319 | - } |
|
| 320 | - $this->path = isset($parts['path']) ? $parts['path'] : ''; |
|
| 321 | - $this->queryParams = array(); |
|
| 322 | - if (isset($parts['query'])) { |
|
| 323 | - $this->queryParams = $this->parseQuery($parts['query']); |
|
| 324 | - } |
|
| 305 | + if (substr($url, 0, 4) != 'http') { |
|
| 306 | + // Force the path become relative. |
|
| 307 | + if (substr($url, 0, 1) !== '/') { |
|
| 308 | + $url = '/' . $url; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + $parts = parse_url($url); |
|
| 312 | + if (isset($parts['host'])) { |
|
| 313 | + $this->baseComponent = sprintf( |
|
| 314 | + "%s%s%s", |
|
| 315 | + isset($parts['scheme']) ? $parts['scheme'] . "://" : '', |
|
| 316 | + isset($parts['host']) ? $parts['host'] : '', |
|
| 317 | + isset($parts['port']) ? ":" . $parts['port'] : '' |
|
| 318 | + ); |
|
| 319 | + } |
|
| 320 | + $this->path = isset($parts['path']) ? $parts['path'] : ''; |
|
| 321 | + $this->queryParams = array(); |
|
| 322 | + if (isset($parts['query'])) { |
|
| 323 | + $this->queryParams = $this->parseQuery($parts['query']); |
|
| 324 | + } |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | /** |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | */ |
| 332 | 332 | public function setRequestMethod($method) |
| 333 | 333 | { |
| 334 | - $this->requestMethod = strtoupper($method); |
|
| 334 | + $this->requestMethod = strtoupper($method); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | /** |
@@ -340,11 +340,11 @@ discard block |
||
| 340 | 340 | */ |
| 341 | 341 | public function setRequestHeaders($headers) |
| 342 | 342 | { |
| 343 | - $headers = Google_Utils::normalize($headers); |
|
| 344 | - if ($this->requestHeaders) { |
|
| 345 | - $headers = array_merge($this->requestHeaders, $headers); |
|
| 346 | - } |
|
| 347 | - $this->requestHeaders = $headers; |
|
| 343 | + $headers = Google_Utils::normalize($headers); |
|
| 344 | + if ($this->requestHeaders) { |
|
| 345 | + $headers = array_merge($this->requestHeaders, $headers); |
|
| 346 | + } |
|
| 347 | + $this->requestHeaders = $headers; |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | */ |
| 353 | 353 | public function setPostBody($postBody) |
| 354 | 354 | { |
| 355 | - $this->postBody = $postBody; |
|
| 355 | + $this->postBody = $postBody; |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | /** |
@@ -361,10 +361,10 @@ discard block |
||
| 361 | 361 | */ |
| 362 | 362 | public function setUserAgent($userAgent) |
| 363 | 363 | { |
| 364 | - $this->userAgent = $userAgent; |
|
| 365 | - if ($this->canGzip) { |
|
| 366 | - $this->userAgent = $userAgent . self::GZIP_UA; |
|
| 367 | - } |
|
| 364 | + $this->userAgent = $userAgent; |
|
| 365 | + if ($this->canGzip) { |
|
| 366 | + $this->userAgent = $userAgent . self::GZIP_UA; |
|
| 367 | + } |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | */ |
| 373 | 373 | public function getUserAgent() |
| 374 | 374 | { |
| 375 | - return $this->userAgent; |
|
| 375 | + return $this->userAgent; |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | /** |
@@ -383,29 +383,29 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | public function getCacheKey() |
| 385 | 385 | { |
| 386 | - $key = $this->getUrl(); |
|
| 386 | + $key = $this->getUrl(); |
|
| 387 | 387 | |
| 388 | - if (isset($this->accessKey)) { |
|
| 389 | - $key .= $this->accessKey; |
|
| 390 | - } |
|
| 388 | + if (isset($this->accessKey)) { |
|
| 389 | + $key .= $this->accessKey; |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | - if (isset($this->requestHeaders['authorization'])) { |
|
| 393 | - $key .= $this->requestHeaders['authorization']; |
|
| 394 | - } |
|
| 392 | + if (isset($this->requestHeaders['authorization'])) { |
|
| 393 | + $key .= $this->requestHeaders['authorization']; |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - return md5($key); |
|
| 396 | + return md5($key); |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | public function getParsedCacheControl() |
| 400 | 400 | { |
| 401 | - $parsed = array(); |
|
| 402 | - $rawCacheControl = $this->getResponseHeader('cache-control'); |
|
| 403 | - if ($rawCacheControl) { |
|
| 404 | - $rawCacheControl = str_replace(', ', '&', $rawCacheControl); |
|
| 405 | - parse_str($rawCacheControl, $parsed); |
|
| 406 | - } |
|
| 401 | + $parsed = array(); |
|
| 402 | + $rawCacheControl = $this->getResponseHeader('cache-control'); |
|
| 403 | + if ($rawCacheControl) { |
|
| 404 | + $rawCacheControl = str_replace(', ', '&', $rawCacheControl); |
|
| 405 | + parse_str($rawCacheControl, $parsed); |
|
| 406 | + } |
|
| 407 | 407 | |
| 408 | - return $parsed; |
|
| 408 | + return $parsed; |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | /** |
@@ -414,29 +414,29 @@ discard block |
||
| 414 | 414 | */ |
| 415 | 415 | public function toBatchString($id) |
| 416 | 416 | { |
| 417 | - $str = ''; |
|
| 418 | - $path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . |
|
| 419 | - http_build_query($this->queryParams); |
|
| 420 | - $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; |
|
| 417 | + $str = ''; |
|
| 418 | + $path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . |
|
| 419 | + http_build_query($this->queryParams); |
|
| 420 | + $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; |
|
| 421 | 421 | |
| 422 | - foreach ($this->getRequestHeaders() as $key => $val) { |
|
| 423 | - $str .= $key . ': ' . $val . "\n"; |
|
| 424 | - } |
|
| 422 | + foreach ($this->getRequestHeaders() as $key => $val) { |
|
| 423 | + $str .= $key . ': ' . $val . "\n"; |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - if ($this->getPostBody()) { |
|
| 427 | - $str .= "\n"; |
|
| 428 | - $str .= $this->getPostBody(); |
|
| 429 | - } |
|
| 426 | + if ($this->getPostBody()) { |
|
| 427 | + $str .= "\n"; |
|
| 428 | + $str .= $this->getPostBody(); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - $headers = ''; |
|
| 432 | - foreach ($this->batchHeaders as $key => $val) { |
|
| 433 | - $headers .= $key . ': ' . $val . "\n"; |
|
| 434 | - } |
|
| 431 | + $headers = ''; |
|
| 432 | + foreach ($this->batchHeaders as $key => $val) { |
|
| 433 | + $headers .= $key . ': ' . $val . "\n"; |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - $headers .= "Content-ID: $id\n"; |
|
| 437 | - $str = $headers . "\n" . $str; |
|
| 436 | + $headers .= "Content-ID: $id\n"; |
|
| 437 | + $str = $headers . "\n" . $str; |
|
| 438 | 438 | |
| 439 | - return $str; |
|
| 439 | + return $str; |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /** |
@@ -446,21 +446,21 @@ discard block |
||
| 446 | 446 | */ |
| 447 | 447 | private function parseQuery($string) |
| 448 | 448 | { |
| 449 | - $return = array(); |
|
| 450 | - $parts = explode("&", $string); |
|
| 451 | - foreach ($parts as $part) { |
|
| 452 | - list($key, $value) = explode('=', $part, 2); |
|
| 453 | - $value = urldecode($value); |
|
| 454 | - if (isset($return[$key])) { |
|
| 455 | - if (!is_array($return[$key])) { |
|
| 456 | - $return[$key] = array($return[$key]); |
|
| 457 | - } |
|
| 458 | - $return[$key][] = $value; |
|
| 459 | - } else { |
|
| 460 | - $return[$key] = $value; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - return $return; |
|
| 449 | + $return = array(); |
|
| 450 | + $parts = explode("&", $string); |
|
| 451 | + foreach ($parts as $part) { |
|
| 452 | + list($key, $value) = explode('=', $part, 2); |
|
| 453 | + $value = urldecode($value); |
|
| 454 | + if (isset($return[$key])) { |
|
| 455 | + if (!is_array($return[$key])) { |
|
| 456 | + $return[$key] = array($return[$key]); |
|
| 457 | + } |
|
| 458 | + $return[$key][] = $value; |
|
| 459 | + } else { |
|
| 460 | + $return[$key] = $value; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + return $return; |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | /** |
@@ -470,17 +470,17 @@ discard block |
||
| 470 | 470 | */ |
| 471 | 471 | private function buildQuery($parts) |
| 472 | 472 | { |
| 473 | - $return = array(); |
|
| 474 | - foreach ($parts as $key => $value) { |
|
| 475 | - if (is_array($value)) { |
|
| 476 | - foreach ($value as $v) { |
|
| 477 | - $return[] = urlencode($key) . "=" . urlencode($v); |
|
| 478 | - } |
|
| 479 | - } else { |
|
| 480 | - $return[] = urlencode($key) . "=" . urlencode($value); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - return implode('&', $return); |
|
| 473 | + $return = array(); |
|
| 474 | + foreach ($parts as $key => $value) { |
|
| 475 | + if (is_array($value)) { |
|
| 476 | + foreach ($value as $v) { |
|
| 477 | + $return[] = urlencode($key) . "=" . urlencode($v); |
|
| 478 | + } |
|
| 479 | + } else { |
|
| 480 | + $return[] = urlencode($key) . "=" . urlencode($value); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + return implode('&', $return); |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | /** |
@@ -490,15 +490,15 @@ discard block |
||
| 490 | 490 | */ |
| 491 | 491 | public function maybeMoveParametersToBody() |
| 492 | 492 | { |
| 493 | - if ($this->getRequestMethod() == "POST" && empty($this->postBody)) { |
|
| 494 | - $this->setRequestHeaders( |
|
| 495 | - array( |
|
| 496 | - "content-type" => |
|
| 497 | - "application/x-www-form-urlencoded; charset=UTF-8" |
|
| 498 | - ) |
|
| 499 | - ); |
|
| 500 | - $this->setPostBody($this->buildQuery($this->queryParams)); |
|
| 501 | - $this->queryParams = array(); |
|
| 502 | - } |
|
| 493 | + if ($this->getRequestMethod() == "POST" && empty($this->postBody)) { |
|
| 494 | + $this->setRequestHeaders( |
|
| 495 | + array( |
|
| 496 | + "content-type" => |
|
| 497 | + "application/x-www-form-urlencoded; charset=UTF-8" |
|
| 498 | + ) |
|
| 499 | + ); |
|
| 500 | + $this->setPostBody($this->buildQuery($this->queryParams)); |
|
| 501 | + $this->queryParams = array(); |
|
| 502 | + } |
|
| 503 | 503 | } |
| 504 | 504 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -255,10 +255,9 @@ discard block |
||
| 255 | 255 | */ |
| 256 | 256 | public function getUrl() |
| 257 | 257 | { |
| 258 | - return $this->baseComponent . $this->path . |
|
| 258 | + return $this->baseComponent.$this->path. |
|
| 259 | 259 | (count($this->queryParams) ? |
| 260 | - "?" . $this->buildQuery($this->queryParams) : |
|
| 261 | - ''); |
|
| 260 | + "?".$this->buildQuery($this->queryParams) : ''); |
|
| 262 | 261 | } |
| 263 | 262 | |
| 264 | 263 | /** |
@@ -305,16 +304,16 @@ discard block |
||
| 305 | 304 | if (substr($url, 0, 4) != 'http') { |
| 306 | 305 | // Force the path become relative. |
| 307 | 306 | if (substr($url, 0, 1) !== '/') { |
| 308 | - $url = '/' . $url; |
|
| 307 | + $url = '/'.$url; |
|
| 309 | 308 | } |
| 310 | 309 | } |
| 311 | 310 | $parts = parse_url($url); |
| 312 | 311 | if (isset($parts['host'])) { |
| 313 | 312 | $this->baseComponent = sprintf( |
| 314 | 313 | "%s%s%s", |
| 315 | - isset($parts['scheme']) ? $parts['scheme'] . "://" : '', |
|
| 314 | + isset($parts['scheme']) ? $parts['scheme']."://" : '', |
|
| 316 | 315 | isset($parts['host']) ? $parts['host'] : '', |
| 317 | - isset($parts['port']) ? ":" . $parts['port'] : '' |
|
| 316 | + isset($parts['port']) ? ":".$parts['port'] : '' |
|
| 318 | 317 | ); |
| 319 | 318 | } |
| 320 | 319 | $this->path = isset($parts['path']) ? $parts['path'] : ''; |
@@ -363,7 +362,7 @@ discard block |
||
| 363 | 362 | { |
| 364 | 363 | $this->userAgent = $userAgent; |
| 365 | 364 | if ($this->canGzip) { |
| 366 | - $this->userAgent = $userAgent . self::GZIP_UA; |
|
| 365 | + $this->userAgent = $userAgent.self::GZIP_UA; |
|
| 367 | 366 | } |
| 368 | 367 | } |
| 369 | 368 | |
@@ -415,12 +414,12 @@ discard block |
||
| 415 | 414 | public function toBatchString($id) |
| 416 | 415 | { |
| 417 | 416 | $str = ''; |
| 418 | - $path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . |
|
| 417 | + $path = parse_url($this->getUrl(), PHP_URL_PATH)."?". |
|
| 419 | 418 | http_build_query($this->queryParams); |
| 420 | - $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; |
|
| 419 | + $str .= $this->getRequestMethod().' '.$path." HTTP/1.1\n"; |
|
| 421 | 420 | |
| 422 | 421 | foreach ($this->getRequestHeaders() as $key => $val) { |
| 423 | - $str .= $key . ': ' . $val . "\n"; |
|
| 422 | + $str .= $key.': '.$val."\n"; |
|
| 424 | 423 | } |
| 425 | 424 | |
| 426 | 425 | if ($this->getPostBody()) { |
@@ -430,11 +429,11 @@ discard block |
||
| 430 | 429 | |
| 431 | 430 | $headers = ''; |
| 432 | 431 | foreach ($this->batchHeaders as $key => $val) { |
| 433 | - $headers .= $key . ': ' . $val . "\n"; |
|
| 432 | + $headers .= $key.': '.$val."\n"; |
|
| 434 | 433 | } |
| 435 | 434 | |
| 436 | 435 | $headers .= "Content-ID: $id\n"; |
| 437 | - $str = $headers . "\n" . $str; |
|
| 436 | + $str = $headers."\n".$str; |
|
| 438 | 437 | |
| 439 | 438 | return $str; |
| 440 | 439 | } |
@@ -474,10 +473,10 @@ discard block |
||
| 474 | 473 | foreach ($parts as $key => $value) { |
| 475 | 474 | if (is_array($value)) { |
| 476 | 475 | foreach ($value as $v) { |
| 477 | - $return[] = urlencode($key) . "=" . urlencode($v); |
|
| 476 | + $return[] = urlencode($key)."=".urlencode($v); |
|
| 478 | 477 | } |
| 479 | 478 | } else { |
| 480 | - $return[] = urlencode($key) . "=" . urlencode($value); |
|
| 479 | + $return[] = urlencode($key)."=".urlencode($value); |
|
| 481 | 480 | } |
| 482 | 481 | } |
| 483 | 482 | return implode('&', $return); |
@@ -36,14 +36,14 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public static function execute(Google_Client $client, Google_Http_Request $req) |
| 38 | 38 | { |
| 39 | - $runner = new Google_Task_Runner( |
|
| 40 | - $client, |
|
| 41 | - sprintf('%s %s', $req->getRequestMethod(), $req->getUrl()), |
|
| 42 | - array(get_class(), 'doExecute'), |
|
| 43 | - array($client, $req) |
|
| 44 | - ); |
|
| 39 | + $runner = new Google_Task_Runner( |
|
| 40 | + $client, |
|
| 41 | + sprintf('%s %s', $req->getRequestMethod(), $req->getUrl()), |
|
| 42 | + array(get_class(), 'doExecute'), |
|
| 43 | + array($client, $req) |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - return $runner->run(); |
|
| 46 | + return $runner->run(); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public static function doExecute(Google_Client $client, Google_Http_Request $req) |
| 59 | 59 | { |
| 60 | - $httpRequest = $client->getIo()->makeRequest($req); |
|
| 61 | - $httpRequest->setExpectedClass($req->getExpectedClass()); |
|
| 62 | - return self::decodeHttpResponse($httpRequest, $client); |
|
| 60 | + $httpRequest = $client->getIo()->makeRequest($req); |
|
| 61 | + $httpRequest->setExpectedClass($req->getExpectedClass()); |
|
| 62 | + return self::decodeHttpResponse($httpRequest, $client); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -72,65 +72,65 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public static function decodeHttpResponse($response, Google_Client $client = null) |
| 74 | 74 | { |
| 75 | - $code = $response->getResponseHttpCode(); |
|
| 76 | - $body = $response->getResponseBody(); |
|
| 77 | - $decoded = null; |
|
| 75 | + $code = $response->getResponseHttpCode(); |
|
| 76 | + $body = $response->getResponseBody(); |
|
| 77 | + $decoded = null; |
|
| 78 | 78 | |
| 79 | - if ((intVal($code)) >= 300) { |
|
| 80 | - $decoded = json_decode($body, true); |
|
| 81 | - $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 82 | - if (isset($decoded['error']) && |
|
| 83 | - isset($decoded['error']['message']) && |
|
| 84 | - isset($decoded['error']['code'])) { |
|
| 85 | - // if we're getting a json encoded error definition, use that instead of the raw response |
|
| 86 | - // body for improved readability |
|
| 87 | - $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; |
|
| 88 | - } else { |
|
| 89 | - $err .= ": ($code) $body"; |
|
| 90 | - } |
|
| 79 | + if ((intVal($code)) >= 300) { |
|
| 80 | + $decoded = json_decode($body, true); |
|
| 81 | + $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 82 | + if (isset($decoded['error']) && |
|
| 83 | + isset($decoded['error']['message']) && |
|
| 84 | + isset($decoded['error']['code'])) { |
|
| 85 | + // if we're getting a json encoded error definition, use that instead of the raw response |
|
| 86 | + // body for improved readability |
|
| 87 | + $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; |
|
| 88 | + } else { |
|
| 89 | + $err .= ": ($code) $body"; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $errors = null; |
|
| 93 | - // Specific check for APIs which don't return error details, such as Blogger. |
|
| 94 | - if (isset($decoded['error']) && isset($decoded['error']['errors'])) { |
|
| 95 | - $errors = $decoded['error']['errors']; |
|
| 96 | - } |
|
| 92 | + $errors = null; |
|
| 93 | + // Specific check for APIs which don't return error details, such as Blogger. |
|
| 94 | + if (isset($decoded['error']) && isset($decoded['error']['errors'])) { |
|
| 95 | + $errors = $decoded['error']['errors']; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - $map = null; |
|
| 99 | - if ($client) { |
|
| 100 | - $client->getLogger()->error( |
|
| 101 | - $err, |
|
| 102 | - array('code' => $code, 'errors' => $errors) |
|
| 103 | - ); |
|
| 98 | + $map = null; |
|
| 99 | + if ($client) { |
|
| 100 | + $client->getLogger()->error( |
|
| 101 | + $err, |
|
| 102 | + array('code' => $code, 'errors' => $errors) |
|
| 103 | + ); |
|
| 104 | 104 | |
| 105 | - $map = $client->getClassConfig( |
|
| 106 | - 'Google_Service_Exception', |
|
| 107 | - 'retry_map' |
|
| 108 | - ); |
|
| 109 | - } |
|
| 110 | - throw new Google_Service_Exception($err, $code, null, $errors, $map); |
|
| 111 | - } |
|
| 105 | + $map = $client->getClassConfig( |
|
| 106 | + 'Google_Service_Exception', |
|
| 107 | + 'retry_map' |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | + throw new Google_Service_Exception($err, $code, null, $errors, $map); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - // Only attempt to decode the response, if the response code wasn't (204) 'no content' |
|
| 114 | - if ($code != '204') { |
|
| 115 | - if ($response->getExpectedRaw()) { |
|
| 116 | - return $body; |
|
| 117 | - } |
|
| 113 | + // Only attempt to decode the response, if the response code wasn't (204) 'no content' |
|
| 114 | + if ($code != '204') { |
|
| 115 | + if ($response->getExpectedRaw()) { |
|
| 116 | + return $body; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $decoded = json_decode($body, true); |
|
| 120 | - if ($decoded === null || $decoded === "") { |
|
| 121 | - $error = "Invalid json in service response: $body"; |
|
| 122 | - if ($client) { |
|
| 123 | - $client->getLogger()->error($error); |
|
| 124 | - } |
|
| 125 | - throw new Google_Service_Exception($error); |
|
| 126 | - } |
|
| 119 | + $decoded = json_decode($body, true); |
|
| 120 | + if ($decoded === null || $decoded === "") { |
|
| 121 | + $error = "Invalid json in service response: $body"; |
|
| 122 | + if ($client) { |
|
| 123 | + $client->getLogger()->error($error); |
|
| 124 | + } |
|
| 125 | + throw new Google_Service_Exception($error); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - if ($response->getExpectedClass()) { |
|
| 129 | - $class = $response->getExpectedClass(); |
|
| 130 | - $decoded = new $class($decoded); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - return $decoded; |
|
| 128 | + if ($response->getExpectedClass()) { |
|
| 129 | + $class = $response->getExpectedClass(); |
|
| 130 | + $decoded = new $class($decoded); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + return $decoded; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -144,35 +144,35 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public static function createRequestUri($servicePath, $restPath, $params) |
| 146 | 146 | { |
| 147 | - $requestUrl = $servicePath . $restPath; |
|
| 148 | - $uriTemplateVars = array(); |
|
| 149 | - $queryVars = array(); |
|
| 150 | - foreach ($params as $paramName => $paramSpec) { |
|
| 151 | - if ($paramSpec['type'] == 'boolean') { |
|
| 152 | - $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; |
|
| 153 | - } |
|
| 154 | - if ($paramSpec['location'] == 'path') { |
|
| 155 | - $uriTemplateVars[$paramName] = $paramSpec['value']; |
|
| 156 | - } else if ($paramSpec['location'] == 'query') { |
|
| 157 | - if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
|
| 158 | - foreach ($paramSpec['value'] as $value) { |
|
| 159 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 160 | - } |
|
| 161 | - } else { |
|
| 162 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 147 | + $requestUrl = $servicePath . $restPath; |
|
| 148 | + $uriTemplateVars = array(); |
|
| 149 | + $queryVars = array(); |
|
| 150 | + foreach ($params as $paramName => $paramSpec) { |
|
| 151 | + if ($paramSpec['type'] == 'boolean') { |
|
| 152 | + $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; |
|
| 153 | + } |
|
| 154 | + if ($paramSpec['location'] == 'path') { |
|
| 155 | + $uriTemplateVars[$paramName] = $paramSpec['value']; |
|
| 156 | + } else if ($paramSpec['location'] == 'query') { |
|
| 157 | + if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
|
| 158 | + foreach ($paramSpec['value'] as $value) { |
|
| 159 | + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 160 | + } |
|
| 161 | + } else { |
|
| 162 | + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if (count($uriTemplateVars)) { |
|
| 168 | - $uriTemplateParser = new Google_Utils_URITemplate(); |
|
| 169 | - $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); |
|
| 170 | - } |
|
| 167 | + if (count($uriTemplateVars)) { |
|
| 168 | + $uriTemplateParser = new Google_Utils_URITemplate(); |
|
| 169 | + $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - if (count($queryVars)) { |
|
| 173 | - $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 174 | - } |
|
| 172 | + if (count($queryVars)) { |
|
| 173 | + $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - return $requestUrl; |
|
| 176 | + return $requestUrl; |
|
| 177 | 177 | } |
| 178 | 178 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!class_exists('Google_Client')) { |
| 19 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 19 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | if ((intVal($code)) >= 300) { |
| 80 | 80 | $decoded = json_decode($body, true); |
| 81 | - $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); |
|
| 81 | + $err = 'Error calling '.$response->getRequestMethod().' '.$response->getUrl(); |
|
| 82 | 82 | if (isset($decoded['error']) && |
| 83 | - isset($decoded['error']['message']) && |
|
| 83 | + isset($decoded['error']['message']) && |
|
| 84 | 84 | isset($decoded['error']['code'])) { |
| 85 | 85 | // if we're getting a json encoded error definition, use that instead of the raw response |
| 86 | 86 | // body for improved readability |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public static function createRequestUri($servicePath, $restPath, $params) |
| 146 | 146 | { |
| 147 | - $requestUrl = $servicePath . $restPath; |
|
| 147 | + $requestUrl = $servicePath.$restPath; |
|
| 148 | 148 | $uriTemplateVars = array(); |
| 149 | 149 | $queryVars = array(); |
| 150 | 150 | foreach ($params as $paramName => $paramSpec) { |
@@ -156,10 +156,10 @@ discard block |
||
| 156 | 156 | } else if ($paramSpec['location'] == 'query') { |
| 157 | 157 | if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
| 158 | 158 | foreach ($paramSpec['value'] as $value) { |
| 159 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
|
| 159 | + $queryVars[] = $paramName.'='.rawurlencode(rawurldecode($value)); |
|
| 160 | 160 | } |
| 161 | 161 | } else { |
| 162 | - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 162 | + $queryVars[] = $paramName.'='.rawurlencode(rawurldecode($paramSpec['value'])); |
|
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | if (count($queryVars)) { |
| 173 | - $requestUrl .= '?' . implode($queryVars, '&'); |
|
| 173 | + $requestUrl .= '?'.implode($queryVars, '&'); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | return $requestUrl; |
@@ -34,13 +34,13 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | public function __construct(Google_Client $client) |
| 36 | 36 | { |
| 37 | - if (!extension_loaded('curl')) { |
|
| 38 | - $error = 'The cURL IO handler requires the cURL extension to be enabled'; |
|
| 39 | - $client->getLogger()->critical($error); |
|
| 40 | - throw new Google_IO_Exception($error); |
|
| 41 | - } |
|
| 37 | + if (!extension_loaded('curl')) { |
|
| 38 | + $error = 'The cURL IO handler requires the cURL extension to be enabled'; |
|
| 39 | + $client->getLogger()->critical($error); |
|
| 40 | + throw new Google_IO_Exception($error); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - parent::__construct($client); |
|
| 43 | + parent::__construct($client); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -52,83 +52,83 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function executeRequest(Google_Http_Request $request) |
| 54 | 54 | { |
| 55 | - $curl = curl_init(); |
|
| 56 | - |
|
| 57 | - if ($request->getPostBody()) { |
|
| 58 | - curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $requestHeaders = $request->getRequestHeaders(); |
|
| 62 | - if ($requestHeaders && is_array($requestHeaders)) { |
|
| 63 | - $curlHeaders = array(); |
|
| 64 | - foreach ($requestHeaders as $k => $v) { |
|
| 65 | - $curlHeaders[] = "$k: $v"; |
|
| 66 | - } |
|
| 67 | - curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); |
|
| 68 | - } |
|
| 69 | - curl_setopt($curl, CURLOPT_URL, $request->getUrl()); |
|
| 70 | - |
|
| 71 | - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); |
|
| 72 | - curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); |
|
| 73 | - |
|
| 74 | - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
|
| 75 | - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
|
| 76 | - // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. |
|
| 77 | - curl_setopt($curl, CURLOPT_SSLVERSION, 1); |
|
| 78 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 79 | - curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 80 | - |
|
| 81 | - if ($request->canGzip()) { |
|
| 82 | - curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); |
|
| 83 | - } |
|
| 55 | + $curl = curl_init(); |
|
| 56 | + |
|
| 57 | + if ($request->getPostBody()) { |
|
| 58 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $requestHeaders = $request->getRequestHeaders(); |
|
| 62 | + if ($requestHeaders && is_array($requestHeaders)) { |
|
| 63 | + $curlHeaders = array(); |
|
| 64 | + foreach ($requestHeaders as $k => $v) { |
|
| 65 | + $curlHeaders[] = "$k: $v"; |
|
| 66 | + } |
|
| 67 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); |
|
| 68 | + } |
|
| 69 | + curl_setopt($curl, CURLOPT_URL, $request->getUrl()); |
|
| 70 | + |
|
| 71 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); |
|
| 72 | + curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); |
|
| 73 | + |
|
| 74 | + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
|
| 75 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
|
| 76 | + // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. |
|
| 77 | + curl_setopt($curl, CURLOPT_SSLVERSION, 1); |
|
| 78 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 79 | + curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 80 | + |
|
| 81 | + if ($request->canGzip()) { |
|
| 82 | + curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); |
|
| 86 | - if (is_array($options)) { |
|
| 87 | - $this->setOptions($options); |
|
| 88 | - } |
|
| 85 | + $options = $this->client->getClassConfig('Google_IO_Curl', 'options'); |
|
| 86 | + if (is_array($options)) { |
|
| 87 | + $this->setOptions($options); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - foreach ($this->options as $key => $var) { |
|
| 91 | - curl_setopt($curl, $key, $var); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (!isset($this->options[CURLOPT_CAINFO])) { |
|
| 95 | - curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $this->client->getLogger()->debug( |
|
| 99 | - 'cURL request', |
|
| 100 | - array( |
|
| 101 | - 'url' => $request->getUrl(), |
|
| 102 | - 'method' => $request->getRequestMethod(), |
|
| 103 | - 'headers' => $requestHeaders, |
|
| 104 | - 'body' => $request->getPostBody() |
|
| 105 | - ) |
|
| 106 | - ); |
|
| 107 | - |
|
| 108 | - $response = curl_exec($curl); |
|
| 109 | - if ($response === false) { |
|
| 110 | - $error = curl_error($curl); |
|
| 111 | - $code = curl_errno($curl); |
|
| 112 | - $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
|
| 113 | - |
|
| 114 | - $this->client->getLogger()->error('cURL ' . $error); |
|
| 115 | - throw new Google_IO_Exception($error, $code, null, $map); |
|
| 116 | - } |
|
| 117 | - $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 118 | - |
|
| 119 | - list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); |
|
| 120 | - $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 121 | - |
|
| 122 | - $this->client->getLogger()->debug( |
|
| 123 | - 'cURL response', |
|
| 124 | - array( |
|
| 125 | - 'code' => $responseCode, |
|
| 126 | - 'headers' => $responseHeaders, |
|
| 127 | - 'body' => $responseBody, |
|
| 128 | - ) |
|
| 129 | - ); |
|
| 130 | - |
|
| 131 | - return array($responseBody, $responseHeaders, $responseCode); |
|
| 90 | + foreach ($this->options as $key => $var) { |
|
| 91 | + curl_setopt($curl, $key, $var); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (!isset($this->options[CURLOPT_CAINFO])) { |
|
| 95 | + curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $this->client->getLogger()->debug( |
|
| 99 | + 'cURL request', |
|
| 100 | + array( |
|
| 101 | + 'url' => $request->getUrl(), |
|
| 102 | + 'method' => $request->getRequestMethod(), |
|
| 103 | + 'headers' => $requestHeaders, |
|
| 104 | + 'body' => $request->getPostBody() |
|
| 105 | + ) |
|
| 106 | + ); |
|
| 107 | + |
|
| 108 | + $response = curl_exec($curl); |
|
| 109 | + if ($response === false) { |
|
| 110 | + $error = curl_error($curl); |
|
| 111 | + $code = curl_errno($curl); |
|
| 112 | + $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
|
| 113 | + |
|
| 114 | + $this->client->getLogger()->error('cURL ' . $error); |
|
| 115 | + throw new Google_IO_Exception($error, $code, null, $map); |
|
| 116 | + } |
|
| 117 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 118 | + |
|
| 119 | + list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); |
|
| 120 | + $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 121 | + |
|
| 122 | + $this->client->getLogger()->debug( |
|
| 123 | + 'cURL response', |
|
| 124 | + array( |
|
| 125 | + 'code' => $responseCode, |
|
| 126 | + 'headers' => $responseHeaders, |
|
| 127 | + 'body' => $responseBody, |
|
| 128 | + ) |
|
| 129 | + ); |
|
| 130 | + |
|
| 131 | + return array($responseBody, $responseHeaders, $responseCode); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function setOptions($options) |
| 139 | 139 | { |
| 140 | - $this->options = $options + $this->options; |
|
| 140 | + $this->options = $options + $this->options; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | public function setTimeout($timeout) |
| 148 | 148 | { |
| 149 | - // Since this timeout is really for putting a bound on the time |
|
| 150 | - // we'll set them both to the same. If you need to specify a longer |
|
| 151 | - // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to |
|
| 152 | - // do is use the setOptions method for the values individually. |
|
| 153 | - $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; |
|
| 154 | - $this->options[CURLOPT_TIMEOUT] = $timeout; |
|
| 149 | + // Since this timeout is really for putting a bound on the time |
|
| 150 | + // we'll set them both to the same. If you need to specify a longer |
|
| 151 | + // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to |
|
| 152 | + // do is use the setOptions method for the values individually. |
|
| 153 | + $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; |
|
| 154 | + $this->options[CURLOPT_TIMEOUT] = $timeout; |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | public function getTimeout() |
| 162 | 162 | { |
| 163 | - return $this->options[CURLOPT_TIMEOUT]; |
|
| 163 | + return $this->options[CURLOPT_TIMEOUT]; |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | protected function needsQuirk() |
| 174 | 174 | { |
| 175 | - $ver = curl_version(); |
|
| 176 | - $versionNum = $ver['version_number']; |
|
| 177 | - return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION; |
|
| 175 | + $ver = curl_version(); |
|
| 176 | + $versionNum = $ver['version_number']; |
|
| 177 | + return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION; |
|
| 178 | 178 | } |
| 179 | 179 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!class_exists('Google_Client')) { |
| 25 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 25 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | class Google_IO_Curl extends Google_IO_Abstract |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | if (!isset($this->options[CURLOPT_CAINFO])) { |
| 95 | - curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
|
| 95 | + curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__).'/cacerts.pem'); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $this->client->getLogger()->debug( |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $code = curl_errno($curl); |
| 112 | 112 | $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map'); |
| 113 | 113 | |
| 114 | - $this->client->getLogger()->error('cURL ' . $error); |
|
| 114 | + $this->client->getLogger()->error('cURL '.$error); |
|
| 115 | 115 | throw new Google_IO_Exception($error, $code, null, $map); |
| 116 | 116 | } |
| 117 | 117 | $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
@@ -34,24 +34,24 @@ discard block |
||
| 34 | 34 | private $trappedErrorString; |
| 35 | 35 | |
| 36 | 36 | private static $DEFAULT_HTTP_CONTEXT = array( |
| 37 | - "follow_location" => 0, |
|
| 38 | - "ignore_errors" => 1, |
|
| 37 | + "follow_location" => 0, |
|
| 38 | + "ignore_errors" => 1, |
|
| 39 | 39 | ); |
| 40 | 40 | |
| 41 | 41 | private static $DEFAULT_SSL_CONTEXT = array( |
| 42 | - "verify_peer" => true, |
|
| 42 | + "verify_peer" => true, |
|
| 43 | 43 | ); |
| 44 | 44 | |
| 45 | 45 | public function __construct(Google_Client $client) |
| 46 | 46 | { |
| 47 | - if (!ini_get('allow_url_fopen')) { |
|
| 48 | - $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 49 | - 'configuration to be enabled'; |
|
| 50 | - $client->getLogger()->critical($error); |
|
| 51 | - throw new Google_IO_Exception($error); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - parent::__construct($client); |
|
| 47 | + if (!ini_get('allow_url_fopen')) { |
|
| 48 | + $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 49 | + 'configuration to be enabled'; |
|
| 50 | + $client->getLogger()->critical($error); |
|
| 51 | + throw new Google_IO_Exception($error); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + parent::__construct($client); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -63,119 +63,119 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public function executeRequest(Google_Http_Request $request) |
| 65 | 65 | { |
| 66 | - $default_options = stream_context_get_options(stream_context_get_default()); |
|
| 67 | - |
|
| 68 | - $requestHttpContext = array_key_exists('http', $default_options) ? |
|
| 69 | - $default_options['http'] : array(); |
|
| 70 | - |
|
| 71 | - if ($request->getPostBody()) { |
|
| 72 | - $requestHttpContext["content"] = $request->getPostBody(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $requestHeaders = $request->getRequestHeaders(); |
|
| 76 | - if ($requestHeaders && is_array($requestHeaders)) { |
|
| 77 | - $headers = ""; |
|
| 78 | - foreach ($requestHeaders as $k => $v) { |
|
| 79 | - $headers .= "$k: $v\r\n"; |
|
| 80 | - } |
|
| 81 | - $requestHttpContext["header"] = $headers; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $requestHttpContext["method"] = $request->getRequestMethod(); |
|
| 85 | - $requestHttpContext["user_agent"] = $request->getUserAgent(); |
|
| 86 | - |
|
| 87 | - $requestSslContext = array_key_exists('ssl', $default_options) ? |
|
| 88 | - $default_options['ssl'] : array(); |
|
| 89 | - |
|
| 90 | - if (!array_key_exists("cafile", $requestSslContext)) { |
|
| 91 | - $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $options = array( |
|
| 95 | - "http" => array_merge( |
|
| 96 | - self::$DEFAULT_HTTP_CONTEXT, |
|
| 97 | - $requestHttpContext |
|
| 98 | - ), |
|
| 99 | - "ssl" => array_merge( |
|
| 100 | - self::$DEFAULT_SSL_CONTEXT, |
|
| 101 | - $requestSslContext |
|
| 102 | - ) |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - $context = stream_context_create($options); |
|
| 106 | - |
|
| 107 | - $url = $request->getUrl(); |
|
| 108 | - |
|
| 109 | - if ($request->canGzip()) { |
|
| 110 | - $url = self::ZLIB . $url; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->client->getLogger()->debug( |
|
| 114 | - 'Stream request', |
|
| 115 | - array( |
|
| 116 | - 'url' => $url, |
|
| 117 | - 'method' => $request->getRequestMethod(), |
|
| 118 | - 'headers' => $requestHeaders, |
|
| 119 | - 'body' => $request->getPostBody() |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // We are trapping any thrown errors in this method only and |
|
| 124 | - // throwing an exception. |
|
| 125 | - $this->trappedErrorNumber = null; |
|
| 126 | - $this->trappedErrorString = null; |
|
| 127 | - |
|
| 128 | - // START - error trap. |
|
| 129 | - set_error_handler(array($this, 'trapError')); |
|
| 130 | - $fh = fopen($url, 'r', false, $context); |
|
| 131 | - restore_error_handler(); |
|
| 132 | - // END - error trap. |
|
| 133 | - |
|
| 134 | - if ($this->trappedErrorNumber) { |
|
| 135 | - $error = sprintf( |
|
| 136 | - "HTTP Error: Unable to connect: '%s'", |
|
| 137 | - $this->trappedErrorString |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 141 | - throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $response_data = false; |
|
| 145 | - $respHttpCode = self::UNKNOWN_CODE; |
|
| 146 | - if ($fh) { |
|
| 147 | - if (isset($this->options[self::TIMEOUT])) { |
|
| 148 | - stream_set_timeout($fh, $this->options[self::TIMEOUT]); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $response_data = stream_get_contents($fh); |
|
| 152 | - fclose($fh); |
|
| 153 | - |
|
| 154 | - $respHttpCode = $this->getHttpResponseCode($http_response_header); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (false === $response_data) { |
|
| 158 | - $error = sprintf( |
|
| 159 | - "HTTP Error: Unable to connect: '%s'", |
|
| 160 | - $respHttpCode |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 164 | - throw new Google_IO_Exception($error, $respHttpCode); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $responseHeaders = $this->getHttpResponseHeaders($http_response_header); |
|
| 168 | - |
|
| 169 | - $this->client->getLogger()->debug( |
|
| 170 | - 'Stream response', |
|
| 171 | - array( |
|
| 172 | - 'code' => $respHttpCode, |
|
| 173 | - 'headers' => $responseHeaders, |
|
| 174 | - 'body' => $response_data, |
|
| 175 | - ) |
|
| 176 | - ); |
|
| 177 | - |
|
| 178 | - return array($response_data, $responseHeaders, $respHttpCode); |
|
| 66 | + $default_options = stream_context_get_options(stream_context_get_default()); |
|
| 67 | + |
|
| 68 | + $requestHttpContext = array_key_exists('http', $default_options) ? |
|
| 69 | + $default_options['http'] : array(); |
|
| 70 | + |
|
| 71 | + if ($request->getPostBody()) { |
|
| 72 | + $requestHttpContext["content"] = $request->getPostBody(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $requestHeaders = $request->getRequestHeaders(); |
|
| 76 | + if ($requestHeaders && is_array($requestHeaders)) { |
|
| 77 | + $headers = ""; |
|
| 78 | + foreach ($requestHeaders as $k => $v) { |
|
| 79 | + $headers .= "$k: $v\r\n"; |
|
| 80 | + } |
|
| 81 | + $requestHttpContext["header"] = $headers; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $requestHttpContext["method"] = $request->getRequestMethod(); |
|
| 85 | + $requestHttpContext["user_agent"] = $request->getUserAgent(); |
|
| 86 | + |
|
| 87 | + $requestSslContext = array_key_exists('ssl', $default_options) ? |
|
| 88 | + $default_options['ssl'] : array(); |
|
| 89 | + |
|
| 90 | + if (!array_key_exists("cafile", $requestSslContext)) { |
|
| 91 | + $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $options = array( |
|
| 95 | + "http" => array_merge( |
|
| 96 | + self::$DEFAULT_HTTP_CONTEXT, |
|
| 97 | + $requestHttpContext |
|
| 98 | + ), |
|
| 99 | + "ssl" => array_merge( |
|
| 100 | + self::$DEFAULT_SSL_CONTEXT, |
|
| 101 | + $requestSslContext |
|
| 102 | + ) |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + $context = stream_context_create($options); |
|
| 106 | + |
|
| 107 | + $url = $request->getUrl(); |
|
| 108 | + |
|
| 109 | + if ($request->canGzip()) { |
|
| 110 | + $url = self::ZLIB . $url; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->client->getLogger()->debug( |
|
| 114 | + 'Stream request', |
|
| 115 | + array( |
|
| 116 | + 'url' => $url, |
|
| 117 | + 'method' => $request->getRequestMethod(), |
|
| 118 | + 'headers' => $requestHeaders, |
|
| 119 | + 'body' => $request->getPostBody() |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // We are trapping any thrown errors in this method only and |
|
| 124 | + // throwing an exception. |
|
| 125 | + $this->trappedErrorNumber = null; |
|
| 126 | + $this->trappedErrorString = null; |
|
| 127 | + |
|
| 128 | + // START - error trap. |
|
| 129 | + set_error_handler(array($this, 'trapError')); |
|
| 130 | + $fh = fopen($url, 'r', false, $context); |
|
| 131 | + restore_error_handler(); |
|
| 132 | + // END - error trap. |
|
| 133 | + |
|
| 134 | + if ($this->trappedErrorNumber) { |
|
| 135 | + $error = sprintf( |
|
| 136 | + "HTTP Error: Unable to connect: '%s'", |
|
| 137 | + $this->trappedErrorString |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + $this->client->getLogger()->error('Stream ' . $error); |
|
| 141 | + throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $response_data = false; |
|
| 145 | + $respHttpCode = self::UNKNOWN_CODE; |
|
| 146 | + if ($fh) { |
|
| 147 | + if (isset($this->options[self::TIMEOUT])) { |
|
| 148 | + stream_set_timeout($fh, $this->options[self::TIMEOUT]); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $response_data = stream_get_contents($fh); |
|
| 152 | + fclose($fh); |
|
| 153 | + |
|
| 154 | + $respHttpCode = $this->getHttpResponseCode($http_response_header); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (false === $response_data) { |
|
| 158 | + $error = sprintf( |
|
| 159 | + "HTTP Error: Unable to connect: '%s'", |
|
| 160 | + $respHttpCode |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + $this->client->getLogger()->error('Stream ' . $error); |
|
| 164 | + throw new Google_IO_Exception($error, $respHttpCode); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $responseHeaders = $this->getHttpResponseHeaders($http_response_header); |
|
| 168 | + |
|
| 169 | + $this->client->getLogger()->debug( |
|
| 170 | + 'Stream response', |
|
| 171 | + array( |
|
| 172 | + 'code' => $respHttpCode, |
|
| 173 | + 'headers' => $responseHeaders, |
|
| 174 | + 'body' => $response_data, |
|
| 175 | + ) |
|
| 176 | + ); |
|
| 177 | + |
|
| 178 | + return array($response_data, $responseHeaders, $respHttpCode); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | public function setOptions($options) |
| 186 | 186 | { |
| 187 | - $this->options = $options + $this->options; |
|
| 187 | + $this->options = $options + $this->options; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -193,8 +193,8 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | public function trapError($errno, $errstr) |
| 195 | 195 | { |
| 196 | - $this->trappedErrorNumber = $errno; |
|
| 197 | - $this->trappedErrorString = $errstr; |
|
| 196 | + $this->trappedErrorNumber = $errno; |
|
| 197 | + $this->trappedErrorString = $errstr; |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | */ |
| 204 | 204 | public function setTimeout($timeout) |
| 205 | 205 | { |
| 206 | - $this->options[self::TIMEOUT] = $timeout; |
|
| 206 | + $this->options[self::TIMEOUT] = $timeout; |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | public function getTimeout() |
| 214 | 214 | { |
| 215 | - return $this->options[self::TIMEOUT]; |
|
| 215 | + return $this->options[self::TIMEOUT]; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -224,20 +224,20 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | protected function needsQuirk() |
| 226 | 226 | { |
| 227 | - return false; |
|
| 227 | + return false; |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | protected function getHttpResponseCode($response_headers) |
| 231 | 231 | { |
| 232 | - $header_count = count($response_headers); |
|
| 233 | - |
|
| 234 | - for ($i = 0; $i < $header_count; $i++) { |
|
| 235 | - $header = $response_headers[$i]; |
|
| 236 | - if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { |
|
| 237 | - $response = explode(' ', $header); |
|
| 238 | - return $response[1]; |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - return self::UNKNOWN_CODE; |
|
| 232 | + $header_count = count($response_headers); |
|
| 233 | + |
|
| 234 | + for ($i = 0; $i < $header_count; $i++) { |
|
| 235 | + $header = $response_headers[$i]; |
|
| 236 | + if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) { |
|
| 237 | + $response = explode(' ', $header); |
|
| 238 | + return $response[1]; |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + return self::UNKNOWN_CODE; |
|
| 242 | 242 | } |
| 243 | 243 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!class_exists('Google_Client')) { |
| 25 | - require_once dirname(__FILE__) . '/../autoload.php'; |
|
| 25 | + require_once dirname(__FILE__).'/../autoload.php'; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | class Google_IO_Stream extends Google_IO_Abstract |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | public function __construct(Google_Client $client) |
| 46 | 46 | { |
| 47 | 47 | if (!ini_get('allow_url_fopen')) { |
| 48 | - $error = 'The stream IO handler requires the allow_url_fopen runtime ' . |
|
| 48 | + $error = 'The stream IO handler requires the allow_url_fopen runtime '. |
|
| 49 | 49 | 'configuration to be enabled'; |
| 50 | 50 | $client->getLogger()->critical($error); |
| 51 | 51 | throw new Google_IO_Exception($error); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $default_options['ssl'] : array(); |
| 89 | 89 | |
| 90 | 90 | if (!array_key_exists("cafile", $requestSslContext)) { |
| 91 | - $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem'; |
|
| 91 | + $requestSslContext["cafile"] = dirname(__FILE__).'/cacerts.pem'; |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $options = array( |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | $url = $request->getUrl(); |
| 108 | 108 | |
| 109 | 109 | if ($request->canGzip()) { |
| 110 | - $url = self::ZLIB . $url; |
|
| 110 | + $url = self::ZLIB.$url; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | $this->client->getLogger()->debug( |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | $this->trappedErrorString |
| 138 | 138 | ); |
| 139 | 139 | |
| 140 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 140 | + $this->client->getLogger()->error('Stream '.$error); |
|
| 141 | 141 | throw new Google_IO_Exception($error, $this->trappedErrorNumber); |
| 142 | 142 | } |
| 143 | 143 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | $respHttpCode |
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | - $this->client->getLogger()->error('Stream ' . $error); |
|
| 163 | + $this->client->getLogger()->error('Stream '.$error); |
|
| 164 | 164 | throw new Google_IO_Exception($error, $respHttpCode); |
| 165 | 165 | } |
| 166 | 166 | |