PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hybridauth/Hybrid/thirdparty/Auth/Yadis/ParanoidHTTPFetcher.php

https://github.com/DartmouthHackerClub/Monologue_Community
PHP | 245 lines | 169 code | 46 blank | 30 comment | 27 complexity | 002f85996f7e83f4a8b7661521f9366c 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. if (defined('Auth_OpenID_VERIFY_HOST')) {
  112. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  113. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  114. }
  115. curl_exec($c);
  116. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  117. $body = $this->data;
  118. $headers = $this->headers;
  119. if (!$code) {
  120. Auth_OpenID::log("Got no response code when fetching %s", $url);
  121. Auth_OpenID::log("CURL error (%s): %s",
  122. curl_errno($c), curl_error($c));
  123. return null;
  124. }
  125. if (in_array($code, array(301, 302, 303, 307))) {
  126. $url = $this->_findRedirect($headers, $url);
  127. $redir = true;
  128. } else {
  129. $redir = false;
  130. curl_close($c);
  131. if (defined('Auth_OpenID_VERIFY_HOST') &&
  132. $this->isHTTPS($url)) {
  133. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  134. 'curl/get', $url);
  135. }
  136. $new_headers = array();
  137. foreach ($headers as $header) {
  138. if (strpos($header, ': ')) {
  139. list($name, $value) = explode(': ', $header, 2);
  140. $new_headers[$name] = $value;
  141. }
  142. }
  143. Auth_OpenID::log(
  144. "Successfully fetched '%s': GET response code %s",
  145. $url, $code);
  146. return new Auth_Yadis_HTTPResponse($url, $code,
  147. $new_headers, $body);
  148. }
  149. $off = $stop - time();
  150. }
  151. return null;
  152. }
  153. function post($url, $body, $extra_headers = null)
  154. {
  155. if (!$this->canFetchURL($url)) {
  156. return null;
  157. }
  158. $this->reset();
  159. $c = curl_init();
  160. if (defined('CURLOPT_NOSIGNAL')) {
  161. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  162. }
  163. curl_setopt($c, CURLOPT_POST, true);
  164. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  165. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  166. curl_setopt($c, CURLOPT_URL, $url);
  167. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  168. array($this, "_writeData"));
  169. if (defined('Auth_OpenID_VERIFY_HOST')) {
  170. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  171. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  172. }
  173. curl_exec($c);
  174. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  175. if (!$code) {
  176. Auth_OpenID::log("Got no response code when fetching %s", $url);
  177. Auth_OpenID::log("CURL error (%s): %s",
  178. curl_errno($c), curl_error($c));
  179. return null;
  180. }
  181. if (defined('Auth_OpenID_VERIFY_HOST') && $this->isHTTPS($url)) {
  182. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  183. 'curl/post', $url);
  184. }
  185. $body = $this->data;
  186. curl_close($c);
  187. $new_headers = $extra_headers;
  188. foreach ($this->headers as $header) {
  189. if (strpos($header, ': ')) {
  190. list($name, $value) = explode(': ', $header, 2);
  191. $new_headers[$name] = $value;
  192. }
  193. }
  194. Auth_OpenID::log("Successfully fetched '%s': POST response code %s",
  195. $url, $code);
  196. return new Auth_Yadis_HTTPResponse($url, $code,
  197. $new_headers, $body);
  198. }
  199. }