/libraries/openid/Services/Yadis/ParanoidHTTPFetcher.php

https://bitbucket.org/stager94/skmz-joomla · PHP · 179 lines · 113 code · 38 blank · 28 comment · 12 complexity · 8814038e847d673ce5ae83ca352ddc14 MD5 · raw file

  1. <?php
  2. // Check to ensure this file is within the rest of the framework
  3. defined('JPATH_BASE') or die();
  4. /**
  5. * This module contains the CURL-based HTTP fetcher implementation.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: See the COPYING file included in this distribution.
  10. *
  11. * @package Yadis
  12. * @author JanRain, Inc. <openid@janrain.com>
  13. * @copyright 2005 Janrain, Inc.
  14. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  15. */
  16. /**
  17. * Interface import
  18. */
  19. require_once "Services/Yadis/HTTPFetcher.php";
  20. /**
  21. * A paranoid {@link Services_Yadis_HTTPFetcher} class which uses CURL
  22. * for fetching.
  23. *
  24. * @package Yadis
  25. */
  26. class Services_Yadis_ParanoidHTTPFetcher extends Services_Yadis_HTTPFetcher {
  27. function Services_Yadis_ParanoidHTTPFetcher()
  28. {
  29. $this->reset();
  30. }
  31. function reset()
  32. {
  33. $this->headers = array();
  34. $this->data = "";
  35. }
  36. /**
  37. * @access private
  38. */
  39. function _writeHeader($ch, $header)
  40. {
  41. array_push($this->headers, rtrim($header));
  42. return strlen($header);
  43. }
  44. /**
  45. * @access private
  46. */
  47. function _writeData($ch, $data)
  48. {
  49. $this->data .= $data;
  50. return strlen($data);
  51. }
  52. function get($url, $extra_headers = null)
  53. {
  54. $stop = time() + $this->timeout;
  55. $off = $this->timeout;
  56. $redir = true;
  57. while ($redir && ($off > 0)) {
  58. $this->reset();
  59. $c = curl_init();
  60. if (defined('CURLOPT_NOSIGNAL')) {
  61. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  62. }
  63. if (!$this->allowedURL($url)) {
  64. trigger_error(sprintf("Fetching URL not allowed: %s", $url),
  65. E_USER_WARNING);
  66. return null;
  67. }
  68. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  69. array(&$this, "_writeData"));
  70. curl_setopt($c, CURLOPT_HEADERFUNCTION,
  71. array(&$this, "_writeHeader"));
  72. if ($extra_headers) {
  73. curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
  74. }
  75. curl_setopt($c, CURLOPT_TIMEOUT, $off);
  76. curl_setopt($c, CURLOPT_URL, $url);
  77. curl_exec($c);
  78. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  79. $body = $this->data;
  80. $headers = $this->headers;
  81. if (!$code) {
  82. return null;
  83. }
  84. if (in_array($code, array(301, 302, 303, 307))) {
  85. $url = $this->_findRedirect($headers);
  86. $redir = true;
  87. } else {
  88. $redir = false;
  89. curl_close($c);
  90. $new_headers = array();
  91. foreach ($headers as $header) {
  92. if (preg_match("/:/", $header)) {
  93. list($name, $value) = explode(": ", $header, 2);
  94. $new_headers[$name] = $value;
  95. }
  96. }
  97. return new Services_Yadis_HTTPResponse($url, $code,
  98. $new_headers, $body);
  99. }
  100. $off = $stop - time();
  101. }
  102. trigger_error(sprintf("Timed out fetching: %s", $url),
  103. E_USER_WARNING);
  104. return null;
  105. }
  106. function post($url, $body)
  107. {
  108. $this->reset();
  109. if (!$this->allowedURL($url)) {
  110. trigger_error(sprintf("Fetching URL not allowed: %s", $url),
  111. E_USER_WARNING);
  112. return null;
  113. }
  114. $c = curl_init();
  115. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  116. curl_setopt($c, CURLOPT_POST, true);
  117. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  118. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  119. curl_setopt($c, CURLOPT_URL, $url);
  120. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  121. array(&$this, "_writeData"));
  122. curl_exec($c);
  123. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  124. if (!$code) {
  125. trigger_error("No HTTP code returned", E_USER_WARNING);
  126. return null;
  127. }
  128. $body = $this->data;
  129. curl_close($c);
  130. $new_headers = array();
  131. foreach ($this->headers as $header) {
  132. if (preg_match("/:/", $header)) {
  133. list($name, $value) = explode(": ", $header, 2);
  134. $new_headers[$name] = $value;
  135. }
  136. }
  137. return new Services_Yadis_HTTPResponse($url, $code,
  138. $new_headers, $body);
  139. }
  140. }
  141. ?>