/vTigerCRM/vtigercrm/libraries/google-api-php-client/src/Google/IO/Curl.php

https://gitlab.com/hop23typhu/list-theme · PHP · 139 lines · 66 code · 20 blank · 53 comment · 6 complexity · d464d9806692c01fa1715c7511c2ef32 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 the http request to be executed
  32. * @return Google_HttpRequest http request with the response http code,
  33. * response headers and response body filled in
  34. * @throws Google_IO_Exception on curl or IO error
  35. */
  36. public function executeRequest(Google_Http_Request $request)
  37. {
  38. $curl = curl_init();
  39. if ($request->getPostBody()) {
  40. curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
  41. }
  42. $requestHeaders = $request->getRequestHeaders();
  43. if ($requestHeaders && is_array($requestHeaders)) {
  44. $curlHeaders = array();
  45. foreach ($requestHeaders as $k => $v) {
  46. $curlHeaders[] = "$k: $v";
  47. }
  48. curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
  49. }
  50. curl_setopt($curl, CURLOPT_URL, $request->getUrl());
  51. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
  52. curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
  53. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  54. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  55. // 1 is CURL_SSLVERSION_TLSv1_0, which is not always defined in PHP.
  56. curl_setopt($curl, CURLOPT_SSLVERSION, 1);
  57. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  58. curl_setopt($curl, CURLOPT_HEADER, true);
  59. if ($request->canGzip()) {
  60. curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  61. }
  62. foreach ($this->options as $key => $var) {
  63. curl_setopt($curl, $key, $var);
  64. }
  65. if (!isset($this->options[CURLOPT_CAINFO])) {
  66. curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
  67. }
  68. $response = curl_exec($curl);
  69. if ($response === false) {
  70. throw new Google_IO_Exception(curl_error($curl));
  71. }
  72. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  73. list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
  74. $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  75. return array($responseBody, $responseHeaders, $responseCode);
  76. }
  77. /**
  78. * Set options that update the transport implementation's behavior.
  79. * @param $options
  80. */
  81. public function setOptions($options)
  82. {
  83. $this->options = $options + $this->options;
  84. }
  85. /**
  86. * Set the maximum request time in seconds.
  87. * @param $timeout in seconds
  88. */
  89. public function setTimeout($timeout)
  90. {
  91. // Since this timeout is really for putting a bound on the time
  92. // we'll set them both to the same. If you need to specify a longer
  93. // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
  94. // do is use the setOptions method for the values individually.
  95. $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
  96. $this->options[CURLOPT_TIMEOUT] = $timeout;
  97. }
  98. /**
  99. * Get the maximum request time in seconds.
  100. * @return timeout in seconds
  101. */
  102. public function getTimeout()
  103. {
  104. return $this->options[CURLOPT_TIMEOUT];
  105. }
  106. /**
  107. * Test for the presence of a cURL header processing bug
  108. *
  109. * {@inheritDoc}
  110. *
  111. * @return boolean
  112. */
  113. protected function needsQuirk()
  114. {
  115. $ver = curl_version();
  116. $versionNum = $ver['version_number'];
  117. return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION;
  118. }
  119. }