/wp-content/plugins/google-analytics-dashboard-for-wp/tools/src/Google/IO/Curl.php

https://gitlab.com/bhargavi_dcw/dflocal · PHP · 153 lines · 87 code · 7 blank · 59 comment · 7 complexity · 384109068c04eacfc351c4ebd32399e2 MD5 · raw file

  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. * Curl based implementation of Google_IO.
  19. *
  20. * @author Stuart Langley <slangley@google.com>
  21. */
  22. require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
  23. class Google_IO_Curl extends Google_IO_Abstract
  24. {
  25. // cURL hex representation of version 7.30.0
  26. const NO_QUIRK_VERSION = 0x071E00;
  27. private $options = array();
  28. /**
  29. * Execute an HTTP Request
  30. *
  31. * @param Google_HttpRequest $request
  32. * the http request to be executed
  33. * @return Google_HttpRequest http request with the response http code,
  34. * response headers and response body filled in
  35. * @throws Google_IO_Exception on curl or IO error
  36. */
  37. public function executeRequest(Google_Http_Request $request)
  38. {
  39. $curl = curl_init();
  40. if ($request->getPostBody()) {
  41. curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
  42. }
  43. $requestHeaders = $request->getRequestHeaders();
  44. if ($requestHeaders && is_array($requestHeaders)) {
  45. $curlHeaders = array();
  46. foreach ($requestHeaders as $k => $v) {
  47. $curlHeaders[] = "$k: $v";
  48. }
  49. curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
  50. }
  51. curl_setopt($curl, CURLOPT_URL, $request->getUrl());
  52. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
  53. curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
  54. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  56. // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
  57. curl_setopt($curl, CURLOPT_SSLVERSION, 1);
  58. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  59. curl_setopt($curl, CURLOPT_HEADER, true);
  60. if ($request->canGzip()) {
  61. curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  62. }
  63. $options = $this->client->getClassConfig('Google_IO_Curl', 'options');
  64. if (is_array($options)) {
  65. $this->setOptions($options);
  66. }
  67. foreach ($this->options as $key => $var) {
  68. curl_setopt($curl, $key, $var);
  69. }
  70. if (! isset($this->options[CURLOPT_CAINFO])) {
  71. curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
  72. }
  73. $this->client->getLogger()->debug('cURL request', array(
  74. 'url' => $request->getUrl(),
  75. 'method' => $request->getRequestMethod(),
  76. 'headers' => $requestHeaders,
  77. 'body' => $request->getPostBody()
  78. ));
  79. $response = curl_exec($curl);
  80. if ($response === false) {
  81. $error = curl_error($curl);
  82. $this->client->getLogger()->error('cURL ' . $error);
  83. throw new Google_IO_Exception($error);
  84. }
  85. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  86. list ($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
  87. $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  88. $this->client->getLogger()->debug('cURL response', array(
  89. 'code' => $responseCode,
  90. 'headers' => $responseHeaders,
  91. 'body' => $responseBody
  92. ));
  93. return array(
  94. $responseBody,
  95. $responseHeaders,
  96. $responseCode
  97. );
  98. }
  99. /**
  100. * Set options that update the transport implementation's behavior.
  101. *
  102. * @param
  103. * $options
  104. */
  105. public function setOptions($options)
  106. {
  107. $this->options = $options + $this->options;
  108. }
  109. /**
  110. * Set the maximum request time in seconds.
  111. *
  112. * @param $timeout in
  113. * seconds
  114. */
  115. public function setTimeout($timeout)
  116. {
  117. // Since this timeout is really for putting a bound on the time
  118. // we'll set them both to the same. If you need to specify a longer
  119. // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
  120. // do is use the setOptions method for the values individually.
  121. $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
  122. $this->options[CURLOPT_TIMEOUT] = $timeout;
  123. }
  124. /**
  125. * Get the maximum request time in seconds.
  126. *
  127. * @return timeout in seconds
  128. */
  129. public function getTimeout()
  130. {
  131. return $this->options[CURLOPT_TIMEOUT];
  132. }
  133. /**
  134. * Test for the presence of a cURL header processing bug
  135. *
  136. * {@inheritDoc}
  137. *
  138. * @return boolean
  139. */
  140. protected function needsQuirk()
  141. {
  142. $ver = curl_version();
  143. $versionNum = $ver['version_number'];
  144. return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION;
  145. }
  146. }