PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/Microsoft/Http/Client/Adapter/Curl.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 501 lines | 262 code | 60 blank | 179 comment | 55 complexity | c947550fbdbd971459e954fe0ffa2f9c MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Microsoft
  16. * @package Microsoft_Http
  17. * @subpackage Client_Adapter
  18. * @version $Id: Curl.php 19238 2009-11-25 17:13:38Z bate $
  19. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. if (!defined('W3TC')) {
  23. die();
  24. }
  25. /**
  26. * @see Microsoft_Uri_Http
  27. */
  28. require_once 'Microsoft/Uri/Http.php';
  29. /**
  30. * @see Microsoft_Http_Client_Adapter_Interface
  31. */
  32. require_once 'Microsoft/Http/Client/Adapter/Interface.php';
  33. /**
  34. * @see Microsoft_Http_Client_Adapter_Stream
  35. */
  36. require_once 'Microsoft/Http/Client/Adapter/Stream.php';
  37. /**
  38. * An adapter class for Microsoft_Http_Client based on the curl extension.
  39. * Curl requires libcurl. See for full requirements the PHP manual: http://php.net/curl
  40. *
  41. * @category Microsoft
  42. * @package Microsoft_Http
  43. * @subpackage Client_Adapter
  44. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Microsoft_Http_Client_Adapter_Curl implements Microsoft_Http_Client_Adapter_Interface, Microsoft_Http_Client_Adapter_Stream
  48. {
  49. /**
  50. * Parameters array
  51. *
  52. * @var array
  53. */
  54. protected $_config = array();
  55. /**
  56. * What host/port are we connected to?
  57. *
  58. * @var array
  59. */
  60. protected $_connected_to = array(null, null);
  61. /**
  62. * The curl session handle
  63. *
  64. * @var resource|null
  65. */
  66. protected $_curl = null;
  67. /**
  68. * List of cURL options that should never be overwritten
  69. *
  70. * @var array
  71. */
  72. protected $_invalidOverwritableCurlOptions = array(
  73. CURLOPT_HTTPGET,
  74. CURLOPT_POST,
  75. CURLOPT_PUT,
  76. CURLOPT_CUSTOMREQUEST,
  77. CURLOPT_HEADER,
  78. CURLOPT_RETURNTRANSFER,
  79. CURLOPT_HTTPHEADER,
  80. CURLOPT_POSTFIELDS,
  81. CURLOPT_INFILE,
  82. CURLOPT_INFILESIZE,
  83. CURLOPT_PORT,
  84. CURLOPT_MAXREDIRS,
  85. CURLOPT_CONNECTTIMEOUT,
  86. CURL_HTTP_VERSION_1_1,
  87. CURL_HTTP_VERSION_1_0,
  88. );
  89. /**
  90. * Response gotten from server
  91. *
  92. * @var string
  93. */
  94. protected $_response = null;
  95. /**
  96. * Stream for storing output
  97. *
  98. * @var resource
  99. */
  100. protected $out_stream;
  101. /**
  102. * Adapter constructor
  103. *
  104. * Config is set using setConfig()
  105. *
  106. * @return void
  107. * @throws Microsoft_Http_Client_Adapter_Exception
  108. */
  109. public function __construct()
  110. {
  111. if (!extension_loaded('curl')) {
  112. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  113. throw new Microsoft_Http_Client_Adapter_Exception('cURL extension has to be loaded to use this Microsoft_Http_Client adapter.');
  114. }
  115. }
  116. /**
  117. * Set the configuration array for the adapter
  118. *
  119. * @throws Microsoft_Http_Client_Adapter_Exception
  120. * @param array $config
  121. * @return Microsoft_Http_Client_Adapter_Curl
  122. */
  123. public function setConfig($config = array())
  124. {
  125. if (! is_array($config)) {
  126. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  127. throw new Microsoft_Http_Client_Adapter_Exception(
  128. 'Array expected, got ' . gettype($config)
  129. );
  130. }
  131. if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {
  132. $this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user'].":".$config['proxy_pass']);
  133. unset($config['proxy_user'], $config['proxy_pass']);
  134. }
  135. foreach ($config as $k => $v) {
  136. $option = strtolower($k);
  137. switch($option) {
  138. case 'proxy_host':
  139. $this->setCurlOption(CURLOPT_PROXY, $v);
  140. break;
  141. case 'proxy_port':
  142. $this->setCurlOption(CURLOPT_PROXYPORT, $v);
  143. break;
  144. default:
  145. $this->_config[$option] = $v;
  146. break;
  147. }
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Retrieve the array of all configuration options
  153. *
  154. * @return array
  155. */
  156. public function getConfig()
  157. {
  158. return $this->_config;
  159. }
  160. /**
  161. * Direct setter for cURL adapter related options.
  162. *
  163. * @param string|int $option
  164. * @param mixed $value
  165. * @return Microsoft_Http_Adapter_Curl
  166. */
  167. public function setCurlOption($option, $value)
  168. {
  169. if (!isset($this->_config['curloptions'])) {
  170. $this->_config['curloptions'] = array();
  171. }
  172. $this->_config['curloptions'][$option] = $value;
  173. return $this;
  174. }
  175. /**
  176. * Initialize curl
  177. *
  178. * @param string $host
  179. * @param int $port
  180. * @param boolean $secure
  181. * @return void
  182. * @throws Microsoft_Http_Client_Adapter_Exception if unable to connect
  183. */
  184. public function connect($host, $port = 80, $secure = false)
  185. {
  186. // If we're already connected, disconnect first
  187. if ($this->_curl) {
  188. $this->close();
  189. }
  190. // If we are connected to a different server or port, disconnect first
  191. if ($this->_curl
  192. && is_array($this->_connected_to)
  193. && ($this->_connected_to[0] != $host
  194. || $this->_connected_to[1] != $port)
  195. ) {
  196. $this->close();
  197. }
  198. // Do the actual connection
  199. $this->_curl = curl_init();
  200. if ($port != 80) {
  201. curl_setopt($this->_curl, CURLOPT_PORT, intval($port));
  202. }
  203. // Set timeout
  204. curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT, $this->_config['timeout']);
  205. // Set Max redirects
  206. curl_setopt($this->_curl, CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
  207. if (!$this->_curl) {
  208. $this->close();
  209. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  210. throw new Microsoft_Http_Client_Adapter_Exception('Unable to Connect to ' . $host . ':' . $port);
  211. }
  212. if ($secure !== false) {
  213. // Behave the same like Microsoft_Http_Adapter_Socket on SSL options.
  214. if (isset($this->_config['sslcert'])) {
  215. curl_setopt($this->_curl, CURLOPT_SSLCERT, $this->_config['sslcert']);
  216. }
  217. if (isset($this->_config['sslpassphrase'])) {
  218. curl_setopt($this->_curl, CURLOPT_SSLCERTPASSWD, $this->_config['sslpassphrase']);
  219. }
  220. }
  221. // Update connected_to
  222. $this->_connected_to = array($host, $port);
  223. }
  224. /**
  225. * Send request to the remote server
  226. *
  227. * @param string $method
  228. * @param Microsoft_Uri_Http $uri
  229. * @param float $http_ver
  230. * @param array $headers
  231. * @param string $body
  232. * @return string $request
  233. * @throws Microsoft_Http_Client_Adapter_Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
  234. */
  235. public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '')
  236. {
  237. // Make sure we're properly connected
  238. if (!$this->_curl) {
  239. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  240. throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  241. }
  242. if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) {
  243. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  244. throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong host");
  245. }
  246. // set URL
  247. curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString());
  248. // ensure correct curl call
  249. $curlValue = true;
  250. switch ($method) {
  251. case Microsoft_Http_Client::GET:
  252. $curlMethod = CURLOPT_HTTPGET;
  253. break;
  254. case Microsoft_Http_Client::POST:
  255. $curlMethod = CURLOPT_POST;
  256. break;
  257. case Microsoft_Http_Client::PUT:
  258. // There are two different types of PUT request, either a Raw Data string has been set
  259. // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
  260. if(is_resource($body)) {
  261. $this->_config['curloptions'][CURLOPT_INFILE] = $body;
  262. }
  263. if (isset($this->_config['curloptions'][CURLOPT_INFILE])) {
  264. // Now we will probably already have Content-Length set, so that we have to delete it
  265. // from $headers at this point:
  266. foreach ($headers AS $k => $header) {
  267. if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) {
  268. if(is_resource($body)) {
  269. $this->_config['curloptions'][CURLOPT_INFILESIZE] = (int)$m[1];
  270. }
  271. unset($headers[$k]);
  272. }
  273. }
  274. if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
  275. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  276. throw new Microsoft_Http_Client_Adapter_Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
  277. }
  278. if(is_resource($body)) {
  279. $body = '';
  280. }
  281. $curlMethod = CURLOPT_PUT;
  282. } else {
  283. $curlMethod = CURLOPT_CUSTOMREQUEST;
  284. $curlValue = "PUT";
  285. }
  286. break;
  287. case Microsoft_Http_Client::DELETE:
  288. $curlMethod = CURLOPT_CUSTOMREQUEST;
  289. $curlValue = "DELETE";
  290. break;
  291. case Microsoft_Http_Client::OPTIONS:
  292. $curlMethod = CURLOPT_CUSTOMREQUEST;
  293. $curlValue = "OPTIONS";
  294. break;
  295. case Microsoft_Http_Client::TRACE:
  296. $curlMethod = CURLOPT_CUSTOMREQUEST;
  297. $curlValue = "TRACE";
  298. break;
  299. default:
  300. // For now, through an exception for unsupported request methods
  301. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  302. throw new Microsoft_Http_Client_Adapter_Exception("Method currently not supported");
  303. }
  304. if(is_resource($body) && $curlMethod != CURLOPT_PUT) {
  305. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  306. throw new Microsoft_Http_Client_Adapter_Exception("Streaming requests are allowed only with PUT");
  307. }
  308. // get http version to use
  309. $curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
  310. // mark as HTTP request and set HTTP method
  311. curl_setopt($this->_curl, $curlHttp, true);
  312. curl_setopt($this->_curl, $curlMethod, $curlValue);
  313. if($this->out_stream) {
  314. // headers will be read into the response
  315. curl_setopt($this->_curl, CURLOPT_HEADER, false);
  316. curl_setopt($this->_curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader"));
  317. // and data will be written into the file
  318. curl_setopt($this->_curl, CURLOPT_FILE, $this->out_stream);
  319. } else {
  320. // ensure headers are also returned
  321. curl_setopt($this->_curl, CURLOPT_HEADER, true);
  322. // ensure actual response is returned
  323. curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
  324. }
  325. // set additional headers
  326. $headers['Accept'] = '';
  327. curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
  328. /**
  329. * Make sure POSTFIELDS is set after $curlMethod is set:
  330. * @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
  331. */
  332. if ($method == Microsoft_Http_Client::POST) {
  333. curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
  334. } elseif ($curlMethod == CURLOPT_PUT) {
  335. // this covers a PUT by file-handle:
  336. // Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
  337. // to group common functionality together.
  338. curl_setopt($this->_curl, CURLOPT_INFILE, $this->_config['curloptions'][CURLOPT_INFILE]);
  339. curl_setopt($this->_curl, CURLOPT_INFILESIZE, $this->_config['curloptions'][CURLOPT_INFILESIZE]);
  340. unset($this->_config['curloptions'][CURLOPT_INFILE]);
  341. unset($this->_config['curloptions'][CURLOPT_INFILESIZE]);
  342. } elseif ($method == Microsoft_Http_Client::PUT) {
  343. // This is a PUT by a setRawData string, not by file-handle
  344. curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
  345. }
  346. // set additional curl options
  347. if (isset($this->_config['curloptions'])) {
  348. foreach ((array)$this->_config['curloptions'] as $k => $v) {
  349. if (!in_array($k, $this->_invalidOverwritableCurlOptions)) {
  350. if (@curl_setopt($this->_curl, $k, $v) == false) {
  351. require_once 'Microsoft/Http/Client/Exception.php';
  352. throw new Microsoft_Http_Client_Exception(sprintf("Unknown or erroreous cURL option '%s' set", $k));
  353. }
  354. }
  355. }
  356. }
  357. // send the request
  358. $response = curl_exec($this->_curl);
  359. // if we used streaming, headers are already there
  360. if(!is_resource($this->out_stream)) {
  361. $this->_response = $response;
  362. }
  363. $request = curl_getinfo($this->_curl, CURLINFO_HEADER_OUT);
  364. $request .= $body;
  365. if (empty($this->_response)) {
  366. require_once 'Microsoft/Http/Client/Exception.php';
  367. throw new Microsoft_Http_Client_Exception("Error in cURL request: " . curl_error($this->_curl));
  368. }
  369. // cURL automatically decodes chunked-messages, this means we have to disallow the Microsoft_Http_Response to do it again
  370. if (stripos($this->_response, "Transfer-Encoding: chunked\r\n")) {
  371. $this->_response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->_response);
  372. }
  373. // Eliminate multiple HTTP responses.
  374. do {
  375. $parts = preg_split('|(?:\r?\n){2}|m', $this->_response, 2);
  376. $again = false;
  377. if (isset($parts[1]) && preg_match("|^HTTP/1\.[01](.*?)\r\n|mi", $parts[1])) {
  378. $this->_response = $parts[1];
  379. $again = true;
  380. }
  381. } while ($again);
  382. // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
  383. if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
  384. $this->_response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->_response);
  385. }
  386. return $request;
  387. }
  388. /**
  389. * Return read response from server
  390. *
  391. * @return string
  392. */
  393. public function read()
  394. {
  395. return $this->_response;
  396. }
  397. /**
  398. * Close the connection to the server
  399. *
  400. */
  401. public function close()
  402. {
  403. if(is_resource($this->_curl)) {
  404. curl_close($this->_curl);
  405. }
  406. $this->_curl = null;
  407. $this->_connected_to = array(null, null);
  408. }
  409. /**
  410. * Get cUrl Handle
  411. *
  412. * @return resource
  413. */
  414. public function getHandle()
  415. {
  416. return $this->_curl;
  417. }
  418. /**
  419. * Set output stream for the response
  420. *
  421. * @param resource $stream
  422. * @return Microsoft_Http_Client_Adapter_Socket
  423. */
  424. public function setOutputStream($stream)
  425. {
  426. $this->out_stream = $stream;
  427. return $this;
  428. }
  429. /**
  430. * Header reader function for CURL
  431. *
  432. * @param resource $curl
  433. * @param string $header
  434. * @return int
  435. */
  436. public function readHeader($curl, $header)
  437. {
  438. $this->_response .= $header;
  439. return strlen($header);
  440. }
  441. }