wp-database-backup
/
includes
/
admin
/
Destination
/
Google
/
google-api-php-client
/
src
/
io
/
Google_CurlIO.php
Google_CurlIO.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.1, at includes/admin/Destination/Google/google-api-php-client/src/io/Google_CurlIO.php
| 1 | <?php |
| 2 | // phpcs:ignoreFile -- third party library |
| 3 | /* |
| 4 | * Copyright 2010 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 | * Curl based implementation of apiIO. |
| 21 | * |
| 22 | * @author Chris Chabot <chabotc@google.com> |
| 23 | * @author Chirag Shah <chirags@google.com> |
| 24 | */ |
| 25 | |
| 26 | require_once 'Google_CacheParser.php'; |
| 27 | |
| 28 | class Google_CurlIO extends Google_IO { |
| 29 | private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null); |
| 30 | private static $HOP_BY_HOP = array( |
| 31 | 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', |
| 32 | 'te', 'trailers', 'transfer-encoding', 'upgrade'); |
| 33 | |
| 34 | private $curlParams = array ( |
| 35 | CURLOPT_RETURNTRANSFER => true, |
| 36 | CURLOPT_FOLLOWLOCATION => 0, |
| 37 | CURLOPT_FAILONERROR => false, |
| 38 | CURLOPT_SSL_VERIFYPEER => true, |
| 39 | CURLOPT_HEADER => true, |
| 40 | CURLOPT_VERBOSE => false, |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Check for cURL availability. |
| 45 | */ |
| 46 | public function __construct() { |
| 47 | if (! function_exists('curl_init')) { |
| 48 | throw new Exception( |
| 49 | 'Google CurlIO client requires the CURL PHP extension'); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Perform an authenticated / signed apiHttpRequest. |
| 55 | * This function takes the apiHttpRequest, calls apiAuth->sign on it |
| 56 | * (which can modify the request in what ever way fits the auth mechanism) |
| 57 | * and then calls apiCurlIO::makeRequest on the signed request |
| 58 | * |
| 59 | * @param Google_HttpRequest $request |
| 60 | * @return Google_HttpRequest The resulting HTTP response including the |
| 61 | * responseHttpCode, responseHeaders and responseBody. |
| 62 | */ |
| 63 | public function authenticatedRequest(Google_HttpRequest $request) { |
| 64 | $request = Google_Client::$auth->sign($request); |
| 65 | return $this->makeRequest($request); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Execute a apiHttpRequest |
| 70 | * |
| 71 | * @param Google_HttpRequest $request the http request to be executed |
| 72 | * @return Google_HttpRequest http request with the response http code, response |
| 73 | * headers and response body filled in |
| 74 | * @throws Google_IOException on curl or IO error |
| 75 | */ |
| 76 | public function makeRequest(Google_HttpRequest $request) { |
| 77 | // First, check to see if we have a valid cached version. |
| 78 | $cached = $this->getCachedRequest($request); |
| 79 | if ($cached !== false) { |
| 80 | if (!$this->checkMustRevaliadateCachedRequest($cached, $request)) { |
| 81 | return $cached; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (array_key_exists($request->getRequestMethod(), |
| 86 | self::$ENTITY_HTTP_METHODS)) { |
| 87 | $request = $this->processEntityRequest($request); |
| 88 | } |
| 89 | |
| 90 | $ch = curl_init(); |
| 91 | curl_setopt_array($ch, $this->curlParams); |
| 92 | curl_setopt($ch, CURLOPT_URL, $request->getUrl()); |
| 93 | if ($request->getPostBody()) { |
| 94 | curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getPostBody()); |
| 95 | } |
| 96 | |
| 97 | $requestHeaders = $request->getRequestHeaders(); |
| 98 | if ($requestHeaders && is_array($requestHeaders)) { |
| 99 | $parsed = array(); |
| 100 | foreach ($requestHeaders as $k => $v) { |
| 101 | $parsed[] = "$k: $v"; |
| 102 | } |
| 103 | curl_setopt($ch, CURLOPT_HTTPHEADER, $parsed); |
| 104 | } |
| 105 | |
| 106 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); |
| 107 | curl_setopt($ch, CURLOPT_USERAGENT, $request->getUserAgent()); |
| 108 | $respData = curl_exec($ch); |
| 109 | |
| 110 | // Retry if certificates are missing. |
| 111 | if (curl_errno($ch) == CURLE_SSL_CACERT) { |
| 112 | error_log('SSL certificate problem, verify that the CA cert is OK.' |
| 113 | . ' Retrying with the CA cert bundle from google-api-php-client.'); |
| 114 | curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); |
| 115 | $respData = curl_exec($ch); |
| 116 | } |
| 117 | |
| 118 | $respHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
| 119 | $respHttpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 120 | $curlErrorNum = curl_errno($ch); |
| 121 | $curlError = curl_error($ch); |
| 122 | curl_close($ch); |
| 123 | if ($curlErrorNum != CURLE_OK) { |
| 124 | throw new Google_IOException("HTTP Error: (".esc_html($respHttpCode).") ".esc_html($curlError)); |
| 125 | } |
| 126 | |
| 127 | // Parse out the raw response into usable bits |
| 128 | list($responseHeaders, $responseBody) = |
| 129 | self::parseHttpResponse($respData, $respHeaderSize); |
| 130 | |
| 131 | if ($respHttpCode == 304 && $cached) { |
| 132 | // If the server responded NOT_MODIFIED, return the cached request. |
| 133 | $this->updateCachedRequest($cached, $responseHeaders); |
| 134 | return $cached; |
| 135 | } |
| 136 | |
| 137 | // Fill in the apiHttpRequest with the response values |
| 138 | $request->setResponseHttpCode($respHttpCode); |
| 139 | $request->setResponseHeaders($responseHeaders); |
| 140 | $request->setResponseBody($responseBody); |
| 141 | // Store the request in cache (the function checks to see if the request |
| 142 | // can actually be cached) |
| 143 | $this->setCachedRequest($request); |
| 144 | // And finally return it |
| 145 | return $request; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set options that update cURL's default behavior. |
| 150 | * The list of accepted options are: |
| 151 | * {@link http://php.net/manual/en/function.curl-setopt.php] |
| 152 | * |
| 153 | * @param array $optCurlParams Multiple options used by a cURL session. |
| 154 | */ |
| 155 | public function setOptions($optCurlParams) { |
| 156 | foreach ($optCurlParams as $key => $val) { |
| 157 | $this->curlParams[$key] = $val; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param $respData |
| 163 | * @param $headerSize |
| 164 | * @return array |
| 165 | */ |
| 166 | private static function parseHttpResponse($respData, $headerSize) { |
| 167 | if (stripos($respData, parent::CONNECTION_ESTABLISHED) !== false) { |
| 168 | $respData = str_ireplace(parent::CONNECTION_ESTABLISHED, '', $respData); |
| 169 | } |
| 170 | |
| 171 | if ($headerSize) { |
| 172 | $responseBody = substr($respData, $headerSize); |
| 173 | $responseHeaders = substr($respData, 0, $headerSize); |
| 174 | } else { |
| 175 | list($responseHeaders, $responseBody) = explode("\r\n\r\n", $respData, 2); |
| 176 | } |
| 177 | |
| 178 | $responseHeaders = self::parseResponseHeaders($responseHeaders); |
| 179 | return array($responseHeaders, $responseBody); |
| 180 | } |
| 181 | |
| 182 | private static function parseResponseHeaders($rawHeaders) { |
| 183 | $responseHeaders = array(); |
| 184 | |
| 185 | $responseHeaderLines = explode("\r\n", $rawHeaders); |
| 186 | foreach ($responseHeaderLines as $headerLine) { |
| 187 | if ($headerLine && strpos($headerLine, ':') !== false) { |
| 188 | list($header, $value) = explode(': ', $headerLine, 2); |
| 189 | $header = strtolower($header); |
| 190 | if (isset($responseHeaders[$header])) { |
| 191 | $responseHeaders[$header] .= "\n" . $value; |
| 192 | } else { |
| 193 | $responseHeaders[$header] = $value; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return $responseHeaders; |
| 198 | } |
| 199 | } |
| 200 |