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 / IO / Curl.php

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

182 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2014 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /**
19 * Curl based implementation of Google_IO.
20 *
21 * @author Stuart Langley <slangley@google.com>
22 */
23
24 if (!class_exists('Google_Client')) {
25 require_once dirname(__FILE__) . '/../autoload.php';
26 }
27
28 class Google_IO_Curl extends Google_IO_Abstract
29 {
30 // cURL hex representation of version 7.30.0
31 const NO_QUIRK_VERSION = 0x071E00;
32
33 private $options = array();
34
35 public function __construct(Google_Client $client)
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 }
42
43 parent::__construct($client);
44 }
45
46 /**
47 * Execute an HTTP Request
48 *
49 * @param Google_Http_Request $request the http request to be executed
50 * @return array containing response headers, body, and http code
51 * @throws Google_IO_Exception on curl or IO error
52 */
53 public function executeRequest(Google_Http_Request $request)
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 // UpdraftPlus patch
78 // The SDK leaves this on the default setting in later releases
79 // curl_setopt($curl, CURLOPT_SSLVERSION, 1);
80 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
81 curl_setopt($curl, CURLOPT_HEADER, true);
82
83 if ($request->canGzip()) {
84 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
85 }
86
87 $options = $this->client->getClassConfig('Google_IO_Curl', 'options');
88 if (is_array($options)) {
89 $this->setOptions($options);
90 }
91
92 foreach ($this->options as $key => $var) {
93 curl_setopt($curl, $key, $var);
94 }
95
96 if (!isset($this->options[CURLOPT_CAINFO])) {
97 curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
98 }
99
100 $this->client->getLogger()->debug(
101 'cURL request',
102 array(
103 'url' => $request->getUrl(),
104 'method' => $request->getRequestMethod(),
105 'headers' => $requestHeaders,
106 'body' => $request->getPostBody()
107 )
108 );
109
110 $response = curl_exec($curl);
111 if ($response === false) {
112 $error = curl_error($curl);
113 $code = curl_errno($curl);
114 $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map');
115
116 $this->client->getLogger()->error('cURL ' . $error);
117 throw new Google_IO_Exception($error, $code, null, $map);
118 }
119 $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
120
121 list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
122 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
123
124 $this->client->getLogger()->debug(
125 'cURL response',
126 array(
127 'code' => $responseCode,
128 'headers' => $responseHeaders,
129 'body' => $responseBody,
130 )
131 );
132
133 return array($responseBody, $responseHeaders, $responseCode);
134 }
135
136 /**
137 * Set options that update the transport implementation's behavior.
138 * @param $options
139 */
140 public function setOptions($options)
141 {
142 $this->options = $options + $this->options;
143 }
144
145 /**
146 * Set the maximum request time in seconds.
147 * @param $timeout in seconds
148 */
149 public function setTimeout($timeout)
150 {
151 // Since this timeout is really for putting a bound on the time
152 // we'll set them both to the same. If you need to specify a longer
153 // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to
154 // do is use the setOptions method for the values individually.
155 $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
156 $this->options[CURLOPT_TIMEOUT] = $timeout;
157 }
158
159 /**
160 * Get the maximum request time in seconds.
161 * @return timeout in seconds
162 */
163 public function getTimeout()
164 {
165 return $this->options[CURLOPT_TIMEOUT];
166 }
167
168 /**
169 * Test for the presence of a cURL header processing bug
170 *
171 * {@inheritDoc}
172 *
173 * @return boolean
174 */
175 protected function needsQuirk()
176 {
177 $ver = curl_version();
178 $versionNum = $ver['version_number'];
179 return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION;
180 }
181 }
182