PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.16.5
UpdraftPlus: WP Backup & Migration Plugin v1.16.5
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / includes / Google / Http / REST.php

REST.php in UpdraftPlus: WP Backup & Migration Plugin 1.16.5, at includes/Google/Http/REST.php

180 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 if (!class_exists('Google_Client')) {
19 require_once dirname(__FILE__) . '/../autoload.php';
20 }
21
22 /**
23 * This class implements the RESTful transport of apiServiceRequest()'s
24 */
25 class Google_Http_REST
26 {
27 /**
28 * Executes a Google_Http_Request and (if applicable) automatically retries
29 * when errors occur.
30 *
31 * @param Google_Client $client
32 * @param Google_Http_Request $req
33 * @return array decoded result
34 * @throws Google_Service_Exception on server side error (ie: not authenticated,
35 * invalid or malformed post body, invalid url)
36 */
37 public static function execute(Google_Client $client, Google_Http_Request $req)
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 );
45
46 return $runner->run();
47 }
48
49 /**
50 * Executes a Google_Http_Request
51 *
52 * @param Google_Client $client
53 * @param Google_Http_Request $req
54 * @return array decoded result
55 * @throws Google_Service_Exception on server side error (ie: not authenticated,
56 * invalid or malformed post body, invalid url)
57 */
58 public static function doExecute(Google_Client $client, Google_Http_Request $req)
59 {
60 $httpRequest = $client->getIo()->makeRequest($req);
61 $httpRequest->setExpectedClass($req->getExpectedClass());
62 return self::decodeHttpResponse($httpRequest, $client);
63 }
64
65 /**
66 * Decode an HTTP Response.
67 * @static
68 * @throws Google_Service_Exception
69 * @param Google_Http_Request $response The http response to be decoded.
70 * @param Google_Client $client
71 * @return mixed|null
72 */
73 public static function decodeHttpResponse($response, Google_Client $client = null)
74 {
75 $code = $response->getResponseHttpCode();
76 $body = $response->getResponseBody();
77 $decoded = null;
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 }
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 }
97
98 $map = null;
99 if ($client) {
100 $client->getLogger()->error(
101 $err,
102 array('code' => $code, 'errors' => $errors)
103 );
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 }
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 }
118
119 $decoded = json_decode($body, true);
120 if ($decoded === null || $decoded === "") {
121 # UpdraftPlus patch
122 $error = "Invalid json in service response ($code): $body";
123 if ($client) {
124 $client->getLogger()->error($error);
125 }
126 throw new Google_Service_Exception($error);
127 }
128
129 if ($response->getExpectedClass()) {
130 $class = $response->getExpectedClass();
131 $decoded = new $class($decoded);
132 }
133 }
134 return $decoded;
135 }
136
137 /**
138 * Parse/expand request parameters and create a fully qualified
139 * request uri.
140 * @static
141 * @param string $servicePath
142 * @param string $restPath
143 * @param array $params
144 * @return string $requestUrl
145 */
146 public static function createRequestUri($servicePath, $restPath, $params)
147 {
148 $requestUrl = $servicePath . $restPath;
149 $uriTemplateVars = array();
150 $queryVars = array();
151 foreach ($params as $paramName => $paramSpec) {
152 if ($paramSpec['type'] == 'boolean') {
153 $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
154 }
155 if ($paramSpec['location'] == 'path') {
156 $uriTemplateVars[$paramName] = $paramSpec['value'];
157 } else if ($paramSpec['location'] == 'query') {
158 if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) {
159 foreach ($paramSpec['value'] as $value) {
160 $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value));
161 }
162 } else {
163 $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value']));
164 }
165 }
166 }
167
168 if (count($uriTemplateVars)) {
169 $uriTemplateParser = new Google_Utils_URITemplate();
170 $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars);
171 }
172
173 if (count($queryVars)) {
174 $requestUrl .= '?' . implode($queryVars, '&');
175 }
176
177 return $requestUrl;
178 }
179 }
180