PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.13
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.13
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / Destination / Google / google-api-php-client / src / io / Google_HttpStreamIO.php

Google_HttpStreamIO.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.13, at includes/admin/Destination/Google/google-api-php-client/src/io/Google_HttpStreamIO.php

172 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- third party library
3 /*
4 * Copyright 2013 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /**
20 * Http Streams based implementation of Google_IO.
21 *
22 * @author Stuart Langley <slangley@google.com>
23 */
24
25 require_once 'Google_CacheParser.php';
26
27 class Google_HttpStreamIO extends Google_IO {
28
29 private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null);
30
31 private static $DEFAULT_HTTP_CONTEXT = array(
32 "follow_location" => 0,
33 "ignore_errors" => 1,
34 );
35
36 private static $DEFAULT_SSL_CONTEXT = array(
37 "verify_peer" => true,
38 );
39
40 /**
41 * Perform an authenticated / signed apiHttpRequest.
42 * This function takes the apiHttpRequest, calls apiAuth->sign on it
43 * (which can modify the request in what ever way fits the auth mechanism)
44 * and then calls Google_HttpStreamIO::makeRequest on the signed request
45 *
46 * @param Google_HttpRequest $request
47 * @return Google_HttpRequest The resulting HTTP response including the
48 * responseHttpCode, responseHeaders and responseBody.
49 */
50 public function authenticatedRequest(Google_HttpRequest $request) {
51 $request = Google_Client::$auth->sign($request);
52 return $this->makeRequest($request);
53 }
54
55 /**
56 * Execute a apiHttpRequest
57 *
58 * @param Google_HttpRequest $request the http request to be executed
59 * @return Google_HttpRequest http request with the response http code,
60 * response headers and response body filled in
61 * @throws Google_IOException on curl or IO error
62 */
63 public function makeRequest(Google_HttpRequest $request) {
64 // First, check to see if we have a valid cached version.
65 $cached = $this->getCachedRequest($request);
66 if ($cached !== false) {
67 if (!$this->checkMustRevaliadateCachedRequest($cached, $request)) {
68 return $cached;
69 }
70 }
71
72 $default_options = stream_context_get_options(stream_context_get_default());
73
74 $requestHttpContext = array_key_exists('http', $default_options) ?
75 $default_options['http'] : array();
76 if (array_key_exists($request->getRequestMethod(),
77 self::$ENTITY_HTTP_METHODS)) {
78 $request = $this->processEntityRequest($request);
79 }
80
81 if ($request->getPostBody()) {
82 $requestHttpContext["content"] = $request->getPostBody();
83 }
84
85 $requestHeaders = $request->getRequestHeaders();
86 if ($requestHeaders && is_array($requestHeaders)) {
87 $headers = "";
88 foreach($requestHeaders as $k => $v) {
89 $headers .= "$k: $v\n";
90 }
91 $requestHttpContext["header"] = $headers;
92 }
93
94 $requestHttpContext["method"] = $request->getRequestMethod();
95 $requestHttpContext["user_agent"] = $request->getUserAgent();
96
97 $requestSslContext = array_key_exists('ssl', $default_options) ?
98 $default_options['ssl'] : array();
99
100 if (!array_key_exists("cafile", $requestSslContext)) {
101 $requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
102 }
103
104 $options = array("http" => array_merge(self::$DEFAULT_HTTP_CONTEXT,
105 $requestHttpContext),
106 "ssl" => array_merge(self::$DEFAULT_SSL_CONTEXT,
107 $requestSslContext));
108
109 $context = stream_context_create($options);
110
111 $response_data = file_get_contents($request->getUrl(),
112 false,
113 $context);
114
115 if (false === $response_data) {
116 throw new Google_IOException("HTTP Error: Unable to connect");
117 }
118
119 $respHttpCode = $this->getHttpResponseCode($http_response_header);
120 $responseHeaders = $this->getHttpResponseHeaders($http_response_header);
121
122 if ($respHttpCode == 304 && $cached) {
123 // If the server responded NOT_MODIFIED, return the cached request.
124 $this->updateCachedRequest($cached, $responseHeaders);
125 return $cached;
126 }
127
128 $request->setResponseHttpCode($respHttpCode);
129 $request->setResponseHeaders($responseHeaders);
130 $request->setResponseBody($response_data);
131 // Store the request in cache (the function checks to see if the request
132 // can actually be cached)
133 $this->setCachedRequest($request);
134 return $request;
135 }
136
137 /**
138 * Set options that update the transport implementation's behavior.
139 * @param $options
140 */
141 public function setOptions($options) {
142 }
143
144 private function getHttpResponseCode($response_headers) {
145 $header_count = count($response_headers);
146
147 for ($i = 0; $i < $header_count; $i++) {
148 $header = $response_headers[$i];
149 if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) {
150 $response = explode(' ', $header);
151 return $response[1];
152 }
153 }
154 return 'UNKNOWN';
155 }
156
157 private function getHttpResponseHeaders($response_headers) {
158 $header_count = count($response_headers);
159 $headers = array();
160
161 for ($i = 0; $i < $header_count; $i++) {
162 $header = $response_headers[$i];
163 $header_parts = explode(':', $header);
164 if (count($header_parts) == 2) {
165 $headers[$header_parts[0]] = $header_parts[1];
166 }
167 }
168
169 return $headers;
170 }
171 }
172