/FSN/mediatheque/zp-core/zp-extensions/federated_logon/Auth/Yadis/ParanoidHTTPFetcher.php

https://gitlab.com/r.collas/site_central · PHP · 242 lines · 164 code · 46 blank · 32 comment · 27 complexity · 8476f26ce345a2ee918be08e8dcf0d44 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 '.'curl/get', $url);
  134. }
  135. $new_headers = array();
  136. foreach ($headers as $header) {
  137. if (strpos($header, ': ')) {
  138. list($name, $value) = explode(': ', $header, 2);
  139. $new_headers[$name] = $value;
  140. }
  141. }
  142. // Auth_OpenID::log("Successfully fetched '%s': GET response code %s",$url, $code);
  143. return new Auth_Yadis_HTTPResponse($url, $code,
  144. $new_headers, $body);
  145. }
  146. $off = $stop - time();
  147. }
  148. return null;
  149. }
  150. function post($url, $body, $extra_headers = null)
  151. {
  152. if (!$this->canFetchURL($url)) {
  153. return null;
  154. }
  155. $this->reset();
  156. $c = curl_init();
  157. if (defined('CURLOPT_NOSIGNAL')) {
  158. curl_setopt($c, CURLOPT_NOSIGNAL, true);
  159. }
  160. curl_setopt($c, CURLOPT_POST, true);
  161. curl_setopt($c, CURLOPT_POSTFIELDS, $body);
  162. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  163. curl_setopt($c, CURLOPT_URL, $url);
  164. curl_setopt($c, CURLOPT_WRITEFUNCTION,
  165. array($this, "_writeData"));
  166. if (defined('Auth_OpenID_VERIFY_HOST')) {
  167. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  168. curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
  169. }
  170. curl_exec($c);
  171. $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  172. if (!$code) {
  173. Auth_OpenID::log("Got no response code when fetching %s", $url);
  174. Auth_OpenID::log("CURL error (%s): %s",
  175. curl_errno($c), curl_error($c));
  176. return null;
  177. }
  178. if (defined('Auth_OpenID_VERIFY_HOST') && $this->isHTTPS($url)) {
  179. Auth_OpenID::log('OpenID: Verified SSL host %s using '.
  180. 'curl/post', $url);
  181. }
  182. $body = $this->data;
  183. curl_close($c);
  184. $new_headers = $extra_headers;
  185. foreach ($this->headers as $header) {
  186. if (strpos($header, ': ')) {
  187. list($name, $value) = explode(': ', $header, 2);
  188. $new_headers[$name] = $value;
  189. }
  190. }
  191. Auth_OpenID::log("Successfully fetched '%s': POST response code %s",
  192. $url, $code);
  193. return new Auth_Yadis_HTTPResponse($url, $code,
  194. $new_headers, $body);
  195. }
  196. }