/library/vendors/Auth/Yadis/ParanoidHTTPFetcher.php

https://github.com/Emaratilicious/Garden · PHP · 226 lines · 151 code · 45 blank · 30 comment · 22 complexity · 85b42fe497f9b8f243ee7a1189803515 MD5 · raw file

  1. <?php
  2. /**
  3. * This module contains the CURL-based HTTP fetcher implementation.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. /**
  15. * Interface import
  16. */
  17. require_once "Auth/Yadis/HTTPFetcher.php";
  18. require_once "Auth/OpenID.php";
  19. /**
  20. * A paranoid {@link Auth_Yadis_HTTPFetcher} class which uses CURL
  21. * for fetching.
  22. *
  23. * @package OpenID
  24. */
  25. class Auth_Yadis_ParanoidHTTPFetcher extends Auth_Yadis_HTTPFetcher {
  26. function Auth_Yadis_ParanoidHTTPFetcher()
  27. {
  28. $this->reset();
  29. }
  30. function reset()
  31. {
  32. $this->headers = array();
  33. $this->data = "";
  34. }
  35. /**
  36. * @access private
  37. */
  38. function _writeHeader($ch, $header)
  39. {
  40. array_push($this->headers, rtrim($header));
  41. return strlen($header);
  42. }
  43. /**
  44. * @access private
  45. */
  46. function _writeData($ch, $data)
  47. {
  48. if (strlen($this->data) > 1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB) {
  49. return 0;
  50. } else {
  51. $this->data .= $data;
  52. return strlen($data);
  53. }
  54. }
  55. /**
  56. * Does this fetcher support SSL URLs?
  57. */
  58. function supportsSSL()
  59. {
  60. $v = curl_version();
  61. if(is_array($v)) {
  62. return in_array('https', $v['protocols']);
  63. } elseif (is_string($v)) {
  64. return preg_match('/OpenSSL/i', $v);
  65. } else {
  66. return 0;
  67. }
  68. }
  69. function get($url, $extra_headers = null)
  70. {
  71. if (!$this->canFetchURL($url)) {
  72. return null;
  73. }
  74. $stop = time() + $this->timeout;
  75. $off = $this->timeout;
  76. $redir = true;
  77. while ($redir && ($off > 0)) {
  78. $this->reset();
  79. $c = curl_init();
  80. if ($c === false) {
  81. Auth_OpenID::log(
  82. "curl_init returned false; could not " .
  83. "initialize for URL '%s'", $url);
  84. return null;
  85. }
  86. if (defined('CURLOPT_NOSIGNAL')) {
  87. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  88. }
  89. if (!$this->allowedURL($url)) {
  90. Auth_OpenID::log("Fetching URL not allowed: %s",
  91. $url);
  92. return null;
  93. }
  94. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  95. array(&$this, "_writeData"));
  96. curl_setopt($c, CURLOPT_HEADERFUNCTION,
  97. array(&$this, "_writeHeader"));
  98. if ($extra_headers) {
  99. curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
  100. }
  101. $cv = curl_version();
  102. if(is_array($cv)) {
  103. $curl_user_agent = 'curl/'.$cv['version'];
  104. } else {
  105. $curl_user_agent = $cv;
  106. }
  107. curl_setopt($c, CURLOPT_USERAGENT,
  108. Auth_OpenID_USER_AGENT.' '.$curl_user_agent);
  109. curl_setopt($c, CURLOPT_TIMEOUT, $off);
  110. curl_setopt($c, CURLOPT_URL, $url);
  111. curl_exec($c);
  112. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  113. $body = $this->data;
  114. $headers = $this->headers;
  115. if (!$code) {
  116. Auth_OpenID::log("Got no response code when fetching %s", $url);
  117. Auth_OpenID::log("CURL error (%s): %s",
  118. curl_errno($c), curl_error($c));
  119. return null;
  120. }
  121. if (in_array($code, array(301, 302, 303, 307))) {
  122. $url = $this->_findRedirect($headers);
  123. $redir = true;
  124. } else {
  125. $redir = false;
  126. curl_close($c);
  127. $new_headers = array();
  128. foreach ($headers as $header) {
  129. if (strpos($header, ': ')) {
  130. list($name, $value) = explode(': ', $header, 2);
  131. $new_headers[$name] = $value;
  132. }
  133. }
  134. Auth_OpenID::log(
  135. "Successfully fetched '%s': GET response code %s",
  136. $url, $code);
  137. return new Auth_Yadis_HTTPResponse($url, $code,
  138. $new_headers, $body);
  139. }
  140. $off = $stop - time();
  141. }
  142. return null;
  143. }
  144. function post($url, $body, $extra_headers = null)
  145. {
  146. if (!$this->canFetchURL($url)) {
  147. return null;
  148. }
  149. $this->reset();
  150. $c = curl_init();
  151. if (defined('CURLOPT_NOSIGNAL')) {
  152. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  153. }
  154. curl_setopt($c, CURLOPT_POST, true);
  155. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  156. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  157. curl_setopt($c, CURLOPT_URL, $url);
  158. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  159. array(&$this, "_writeData"));
  160. curl_exec($c);
  161. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  162. if (!$code) {
  163. Auth_OpenID::log("Got no response code when fetching %s", $url);
  164. return null;
  165. }
  166. $body = $this->data;
  167. curl_close($c);
  168. $new_headers = $extra_headers;
  169. foreach ($this->headers as $header) {
  170. if (strpos($header, ': ')) {
  171. list($name, $value) = explode(': ', $header, 2);
  172. $new_headers[$name] = $value;
  173. }
  174. }
  175. Auth_OpenID::log("Successfully fetched '%s': POST response code %s",
  176. $url, $code);
  177. return new Auth_Yadis_HTTPResponse($url, $code,
  178. $new_headers, $body);
  179. }
  180. }
  181. ?>