PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/http/transport/curl.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 183 lines | 84 code | 27 blank | 72 comment | 9 complexity | 99f662bdc9c68472d161e21d684c6640 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTTP
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * HTTP transport class for using cURL.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTTP
  15. * @since 11.3
  16. */
  17. class JHttpTransportCurl implements JHttpTransport
  18. {
  19. /**
  20. * @var JRegistry The client options.
  21. * @since 11.3
  22. */
  23. protected $options;
  24. /**
  25. * Constructor.
  26. *
  27. * @param JRegistry &$options Client options object.
  28. *
  29. * @since 11.3
  30. * @throws RuntimeException
  31. */
  32. public function __construct(JRegistry &$options)
  33. {
  34. if (!function_exists('curl_init') || !is_callable('curl_init'))
  35. {
  36. throw new RuntimeException('Cannot use a cURL transport when curl_init() is not available.');
  37. }
  38. $this->options = $options;
  39. }
  40. /**
  41. * Send a request to the server and return a JHttpResponse object with the response.
  42. *
  43. * @param string $method The HTTP method for sending the request.
  44. * @param JUri $uri The URI to the resource to request.
  45. * @param mixed $data Either an associative array or a string to be sent with the request.
  46. * @param array $headers An array of request headers to send with the request.
  47. * @param integer $timeout Read timeout in seconds.
  48. * @param string $userAgent The optional user agent string to send with the request.
  49. *
  50. * @return JHttpResponse
  51. *
  52. * @since 11.3
  53. */
  54. public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
  55. {
  56. // Setup the cURL handle.
  57. $ch = curl_init();
  58. // Set the request method.
  59. $options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  60. // Initialize the certificate store
  61. $options[CURLOPT_CAINFO] = dirname(__FILE__) . '/cacert.pem';
  62. // If data exists let's encode it and make sure our Content-type header is set.
  63. if (isset($data))
  64. {
  65. // If the data is a scalar value simply add it to the cURL post fields.
  66. if (is_scalar($data))
  67. {
  68. $options[CURLOPT_POSTFIELDS] = $data;
  69. }
  70. // Otherwise we need to encode the value first.
  71. else
  72. {
  73. $options[CURLOPT_POSTFIELDS] = http_build_query($data);
  74. }
  75. if (!isset($headers['Content-type']))
  76. {
  77. $headers['Content-type'] = 'application/x-www-form-urlencoded';
  78. }
  79. $headers['Content-length'] = strlen($options[CURLOPT_POSTFIELDS]);
  80. }
  81. // Build the headers string for the request.
  82. $headerArray = array();
  83. if (isset($headers))
  84. {
  85. foreach ($headers as $key => $value)
  86. {
  87. $headerArray[] = $key . ': ' . $value;
  88. }
  89. // Add the headers string into the stream context options array.
  90. $options[CURLOPT_HTTPHEADER] = $headerArray;
  91. }
  92. // If an explicit timeout is given user it.
  93. if (isset($timeout))
  94. {
  95. $options[CURLOPT_TIMEOUT] = (int) $timeout;
  96. $options[CURLOPT_CONNECTTIMEOUT] = (int) $timeout;
  97. }
  98. // If an explicit user agent is given use it.
  99. if (isset($userAgent))
  100. {
  101. $headers[CURLOPT_USERAGENT] = $userAgent;
  102. }
  103. // Set the request URL.
  104. $options[CURLOPT_URL] = (string) $uri;
  105. // We want our headers. :-)
  106. $options[CURLOPT_HEADER] = true;
  107. // Return it... echoing it would be tacky.
  108. $options[CURLOPT_RETURNTRANSFER] = true;
  109. // Set the cURL options.
  110. curl_setopt_array($ch, $options);
  111. // Execute the request and close the connection.
  112. $content = curl_exec($ch);
  113. curl_close($ch);
  114. return $this->getResponse($content);
  115. }
  116. /**
  117. * Method to get a response object from a server response.
  118. *
  119. * @param string $content The complete server response, including headers.
  120. *
  121. * @return JHttpResponse
  122. *
  123. * @since 11.3
  124. * @throws UnexpectedValueException
  125. */
  126. protected function getResponse($content)
  127. {
  128. // Create the response object.
  129. $return = new JHttpResponse;
  130. // Split the response into headers and body.
  131. $response = explode("\r\n\r\n", $content, 2);
  132. // Get the response headers as an array.
  133. $headers = explode("\r\n", $response[0]);
  134. // Set the body for the response.
  135. $return->body = $response[1];
  136. // Get the response code from the first offset of the response headers.
  137. preg_match('/[0-9]{3}/', array_shift($headers), $matches);
  138. $code = $matches[0];
  139. if (is_numeric($code))
  140. {
  141. $return->code = (int) $code;
  142. }
  143. // No valid response code was detected.
  144. else
  145. {
  146. throw new UnexpectedValueException('No HTTP response code found.');
  147. }
  148. // Add the response headers to the response object.
  149. foreach ($headers as $header)
  150. {
  151. $pos = strpos($header, ':');
  152. $return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
  153. }
  154. return $return;
  155. }
  156. }