ParanoidHTTPFetcher.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. if (defined('Auth_OpenID_VERIFY_HOST')) {
  112. // set SSL verification options only if Auth_OpenID_VERIFY_HOST
  113. // is explicitly set, otherwise use system default.
  114. if (Auth_OpenID_VERIFY_HOST) {
  115. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  116. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  117. if (defined('Auth_OpenID_CAINFO')) {
  118. curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
  119. }
  120. } else {
  121. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  122. }
  123. }
  124. if (defined('Auth_OpenID_HTTP_PROXY')) {
  125. curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
  126. }
  127. curl_exec($c);
  128. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  129. $body = $this->data;
  130. $headers = $this->headers;
  131. if (!$code) {
  132. Auth_OpenID::log("Got no response code when fetching %s", $url);
  133. Auth_OpenID::log("CURL error (%s): %s",
  134. curl_errno($c), curl_error($c));
  135. return null;
  136. }
  137. if (in_array($code, array(301, 302, 303, 307))) {
  138. $url = $this->_findRedirect($headers, $url);
  139. $redir = true;
  140. } else {
  141. $redir = false;
  142. curl_close($c);
  143. if (defined('Auth_OpenID_VERIFY_HOST') &&
  144. Auth_OpenID_VERIFY_HOST == true &&
  145. $this->isHTTPS($url)) {
  146. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  147. 'curl/get', $url);
  148. }
  149. $new_headers = array();
  150. foreach ($headers as $header) {
  151. if (strpos($header, ': ')) {
  152. list($name, $value) = explode(': ', $header, 2);
  153. $new_headers[$name] = $value;
  154. }
  155. }
  156. return new Auth_Yadis_HTTPResponse($url, $code,
  157. $new_headers, $body);
  158. }
  159. $off = $stop - time();
  160. }
  161. return null;
  162. }
  163. function post($url, $body, $extra_headers = null)
  164. {
  165. if (!$this->canFetchURL($url)) {
  166. return null;
  167. }
  168. $this->reset();
  169. $c = curl_init();
  170. if (defined('CURLOPT_NOSIGNAL')) {
  171. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  172. }
  173. if (defined('Auth_OpenID_HTTP_PROXY')) {
  174. curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
  175. }
  176. curl_setopt($c, CURLOPT_POST, true);
  177. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  178. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  179. curl_setopt($c, CURLOPT_URL, $url);
  180. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  181. array($this, "_writeData"));
  182. if (defined('Auth_OpenID_VERIFY_HOST')) {
  183. // set SSL verification options only if Auth_OpenID_VERIFY_HOST
  184. // is explicitly set, otherwise use system default.
  185. if (Auth_OpenID_VERIFY_HOST) {
  186. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  187. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  188. if (defined('Auth_OpenID_CAINFO')) {
  189. curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
  190. }
  191. } else {
  192. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  193. }
  194. }
  195. curl_exec($c);
  196. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  197. if (!$code) {
  198. Auth_OpenID::log("Got no response code when fetching %s", $url);
  199. Auth_OpenID::log("CURL error (%s): %s",
  200. curl_errno($c), curl_error($c));
  201. return null;
  202. }
  203. if (defined('Auth_OpenID_VERIFY_HOST') &&
  204. Auth_OpenID_VERIFY_HOST == true &&
  205. $this->isHTTPS($url)) {
  206. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  207. 'curl/post', $url);
  208. }
  209. $body = $this->data;
  210. curl_close($c);
  211. $new_headers = $extra_headers;
  212. foreach ($this->headers as $header) {
  213. if (strpos($header, ': ')) {
  214. list($name, $value) = explode(': ', $header, 2);
  215. $new_headers[$name] = $value;
  216. }
  217. }
  218. return new Auth_Yadis_HTTPResponse($url, $code,
  219. $new_headers, $body);
  220. }
  221. }