PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Auth/Yadis/HTTPFetcher.php

http://github.com/openid/php-openid
PHP | 189 lines | 93 code | 18 blank | 78 comment | 18 complexity | 6a32ff8ea5bf9312d046940214cf6a25 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * This module contains the HTTP fetcher interface
  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. * Require logging functionality
  16. */
  17. require_once "Auth/OpenID.php";
  18. define('Auth_OpenID_FETCHER_MAX_RESPONSE_KB', 1024);
  19. define('Auth_OpenID_USER_AGENT',
  20. 'php-openid/'.Auth_OpenID_VERSION.' (php/'.phpversion().')');
  21. class Auth_Yadis_HTTPResponse {
  22. public $final_url = '';
  23. public $status = '';
  24. public $body = '';
  25. public $headers = [];
  26. function __construct($final_url = null, $status = null,
  27. $headers = null, $body = null)
  28. {
  29. $this->final_url = $final_url;
  30. $this->status = $status;
  31. $this->headers = $headers;
  32. $this->body = $body;
  33. }
  34. }
  35. /**
  36. * This class is the interface for HTTP fetchers the Yadis library
  37. * uses. This interface is only important if you need to write a new
  38. * fetcher for some reason.
  39. *
  40. * @access private
  41. * @package OpenID
  42. */
  43. class Auth_Yadis_HTTPFetcher {
  44. public $timeout = 20; // timeout in seconds.
  45. /**
  46. * Return whether a URL can be fetched. Returns false if the URL
  47. * scheme is not allowed or is not supported by this fetcher
  48. * implementation; returns true otherwise.
  49. *
  50. * @param string $url
  51. * @return bool
  52. */
  53. function canFetchURL($url)
  54. {
  55. if ($this->isHTTPS($url) && !$this->supportsSSL()) {
  56. Auth_OpenID::log("HTTPS URL unsupported fetching %s",
  57. $url);
  58. return false;
  59. }
  60. if (!$this->allowedURL($url)) {
  61. Auth_OpenID::log("URL fetching not allowed for '%s'",
  62. $url);
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * Return whether a URL should be allowed. Override this method to
  69. * conform to your local policy.
  70. *
  71. * By default, will attempt to fetch any http or https URL.
  72. *
  73. * @param string $url
  74. * @return bool
  75. */
  76. function allowedURL($url)
  77. {
  78. return $this->URLHasAllowedScheme($url);
  79. }
  80. /**
  81. * Does this fetcher implementation (and runtime) support fetching
  82. * HTTPS URLs? May inspect the runtime environment.
  83. *
  84. * @return bool $support True if this fetcher supports HTTPS
  85. * fetching; false if not.
  86. */
  87. function supportsSSL()
  88. {
  89. trigger_error("not implemented", E_USER_ERROR);
  90. return false;
  91. }
  92. /**
  93. * Is this an https URL?
  94. *
  95. * @access private
  96. * @param string $url
  97. * @return bool
  98. */
  99. function isHTTPS($url)
  100. {
  101. return (bool)preg_match('/^https:\/\//i', $url);
  102. }
  103. /**
  104. * Is this an http or https URL?
  105. *
  106. * @access private
  107. * @param string $url
  108. * @return bool
  109. */
  110. function URLHasAllowedScheme($url)
  111. {
  112. return (bool)preg_match('/^https?:\/\//i', $url);
  113. }
  114. /**
  115. * @access private
  116. * @param array $headers
  117. * @param string $url
  118. * @return null|string
  119. */
  120. function _findRedirect($headers, $url)
  121. {
  122. foreach ($headers as $line) {
  123. if (strpos(strtolower($line), "location: ") === 0) {
  124. $parts = explode(" ", $line, 2);
  125. $loc = $parts[1];
  126. $ppos = strpos($loc, "://");
  127. if ($ppos === false || $ppos > strpos($loc, "/")) {
  128. /* no host; add it */
  129. $hpos = strpos($url, "://");
  130. $prt = substr($url, 0, $hpos+3);
  131. $url = substr($url, $hpos+3);
  132. if (substr($loc, 0, 1) == "/") {
  133. /* absolute path */
  134. $fspos = strpos($url, "/");
  135. if ($fspos) $loc = $prt.substr($url, 0, $fspos).$loc;
  136. else $loc = $prt.$url.$loc;
  137. } else {
  138. /* relative path */
  139. $pp = $prt;
  140. while (1) {
  141. $xpos = strpos($url, "/");
  142. if ($xpos === false) break;
  143. $apos = strpos($url, "?");
  144. if ($apos !== false && $apos < $xpos) break;
  145. $apos = strpos($url, "&");
  146. if ($apos !== false && $apos < $xpos) break;
  147. $pp .= substr($url, 0, $xpos+1);
  148. $url = substr($url, $xpos+1);
  149. }
  150. $loc = $pp.$loc;
  151. }
  152. }
  153. return $loc;
  154. }
  155. }
  156. return null;
  157. }
  158. /**
  159. * Fetches the specified URL using optional extra headers and
  160. * returns the server's response.
  161. *
  162. * @param string $url The URL to be fetched.
  163. * @param array $headers
  164. * @return Auth_Yadis_HTTPResponse|null
  165. */
  166. function get($url, $headers = null)
  167. {
  168. trigger_error("not implemented", E_USER_ERROR);
  169. return null;
  170. }
  171. }