ParanoidHTTPFetcher.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 (defined('Auth_OpenID_DISABLE_SSL_VERIFYPEER')
  81. && Auth_OpenID_DISABLE_SSL_VERIFYPEER === true) {
  82. trigger_error(
  83. 'You have disabled SSL verifcation, this is a TERRIBLE ' .
  84. 'idea in almost all cases. Set Auth_OpenID_DISABLE_SSL_' .
  85. 'VERIFYPEER to false if you want to be safe again',
  86. E_USER_WARNING);
  87. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  88. }
  89. if ($c === false) {
  90. Auth_OpenID::log(
  91. "curl_init returned false; could not " .
  92. "initialize for URL '%s'", $url);
  93. return null;
  94. }
  95. if (defined('CURLOPT_NOSIGNAL')) {
  96. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  97. }
  98. if (!$this->allowedURL($url)) {
  99. Auth_OpenID::log("Fetching URL not allowed: %s",
  100. $url);
  101. return null;
  102. }
  103. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  104. array($this, "_writeData"));
  105. curl_setopt($c, CURLOPT_HEADERFUNCTION,
  106. array($this, "_writeHeader"));
  107. if ($extra_headers) {
  108. curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
  109. }
  110. $cv = curl_version();
  111. if(is_array($cv)) {
  112. $curl_user_agent = 'curl/'.$cv['version'];
  113. } else {
  114. $curl_user_agent = $cv;
  115. }
  116. curl_setopt($c, CURLOPT_USERAGENT,
  117. Auth_OpenID_USER_AGENT.' '.$curl_user_agent);
  118. curl_setopt($c, CURLOPT_TIMEOUT, $off);
  119. curl_setopt($c, CURLOPT_URL, $url);
  120. if (defined('Auth_OpenID_VERIFY_HOST')) {
  121. // set SSL verification options only if Auth_OpenID_VERIFY_HOST
  122. // is explicitly set, otherwise use system default.
  123. if (Auth_OpenID_VERIFY_HOST) {
  124. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  125. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  126. if (defined('Auth_OpenID_CAINFO')) {
  127. curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
  128. }
  129. } else {
  130. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  131. }
  132. }
  133. if (defined('Auth_OpenID_HTTP_PROXY')) {
  134. curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
  135. }
  136. curl_exec($c);
  137. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  138. $body = $this->data;
  139. $headers = $this->headers;
  140. if (!$code) {
  141. Auth_OpenID::log("Got no response code when fetching %s", $url);
  142. Auth_OpenID::log("CURL error (%s): %s",
  143. curl_errno($c), curl_error($c));
  144. return null;
  145. }
  146. if (in_array($code, array(301, 302, 303, 307))) {
  147. $url = $this->_findRedirect($headers, $url);
  148. $redir = true;
  149. } else {
  150. $redir = false;
  151. curl_close($c);
  152. if (defined('Auth_OpenID_VERIFY_HOST') &&
  153. Auth_OpenID_VERIFY_HOST == true &&
  154. $this->isHTTPS($url)) {
  155. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  156. 'curl/get', $url);
  157. }
  158. $new_headers = array();
  159. foreach ($headers as $header) {
  160. if (strpos($header, ': ')) {
  161. list($name, $value) = explode(': ', $header, 2);
  162. $new_headers[$name] = $value;
  163. }
  164. }
  165. return new Auth_Yadis_HTTPResponse($url, $code,
  166. $new_headers, $body);
  167. }
  168. $off = $stop - time();
  169. }
  170. return null;
  171. }
  172. function post($url, $body, $extra_headers = null)
  173. {
  174. if (!$this->canFetchURL($url)) {
  175. return null;
  176. }
  177. $this->reset();
  178. $c = curl_init();
  179. if (defined('CURLOPT_NOSIGNAL')) {
  180. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  181. }
  182. if (defined('Auth_OpenID_HTTP_PROXY')) {
  183. curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
  184. }
  185. curl_setopt($c, CURLOPT_POST, true);
  186. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  187. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  188. curl_setopt($c, CURLOPT_URL, $url);
  189. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  190. array($this, "_writeData"));
  191. if (defined('Auth_OpenID_VERIFY_HOST')) {
  192. // set SSL verification options only if Auth_OpenID_VERIFY_HOST
  193. // is explicitly set, otherwise use system default.
  194. if (Auth_OpenID_VERIFY_HOST) {
  195. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  196. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  197. if (defined('Auth_OpenID_CAINFO')) {
  198. curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
  199. }
  200. } else {
  201. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  202. }
  203. }
  204. curl_exec($c);
  205. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  206. if (!$code) {
  207. Auth_OpenID::log("Got no response code when fetching %s", $url);
  208. Auth_OpenID::log("CURL error (%s): %s",
  209. curl_errno($c), curl_error($c));
  210. return null;
  211. }
  212. if (defined('Auth_OpenID_VERIFY_HOST') &&
  213. Auth_OpenID_VERIFY_HOST == true &&
  214. $this->isHTTPS($url)) {
  215. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  216. 'curl/post', $url);
  217. }
  218. $body = $this->data;
  219. curl_close($c);
  220. $new_headers = $extra_headers;
  221. foreach ($this->headers as $header) {
  222. if (strpos($header, ': ')) {
  223. list($name, $value) = explode(': ', $header, 2);
  224. $new_headers[$name] = $value;
  225. }
  226. }
  227. return new Auth_Yadis_HTTPResponse($url, $code,
  228. $new_headers, $body);
  229. }
  230. }