123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- require_once "Auth/Yadis/PlainHTTPFetcher.php";
- require_once "Auth/Yadis/ParanoidHTTPFetcher.php";
- require_once "Auth/Yadis/ParseHTML.php";
- require_once "Auth/Yadis/XRDS.php";
- define('Auth_Yadis_CONTENT_TYPE', 'application/xrds+xml');
- define('Auth_Yadis_HEADER_NAME', 'X-XRDS-Location');
- class Auth_Yadis_DiscoveryResult {
-
- var $request_uri = null;
-
- var $normalized_uri = null;
-
-
- var $xrds_uri = null;
- var $xrds = null;
-
- var $content_type = null;
-
- var $response_text = null;
-
- var $failed = false;
- function Auth_Yadis_DiscoveryResult($request_uri)
- {
-
-
- $this->request_uri = $request_uri;
- }
- function fail()
- {
- $this->failed = true;
- }
- function isFailure()
- {
- return $this->failed;
- }
-
- function services()
- {
- if ($this->xrds) {
- return $this->xrds->services();
- }
- return null;
- }
- function usedYadisLocation()
- {
-
- return ($this->xrds_uri && $this->normalized_uri != $this->xrds_uri);
- }
- function isXRDS()
- {
-
- return ($this->usedYadisLocation() ||
- $this->content_type == Auth_Yadis_CONTENT_TYPE);
- }
- }
- function Auth_Yadis_getServiceEndpoints($input_url, $xrds_parse_func,
- $discover_func=null, $fetcher=null)
- {
- if ($discover_func === null) {
- $discover_function = array('Auth_Yadis_Yadis', 'discover');
- }
- $yadis_result = call_user_func_array($discover_func,
- array($input_url, &$fetcher));
- if ($yadis_result === null) {
- return array($input_url, array());
- }
- $endpoints = call_user_func_array($xrds_parse_func,
- array($yadis_result->normalized_uri,
- $yadis_result->response_text));
- if ($endpoints === null) {
- $endpoints = array();
- }
- return array($yadis_result->normalized_uri, $endpoints);
- }
- class Auth_Yadis_Yadis {
-
- static function getHTTPFetcher($timeout = 20)
- {
- if (Auth_Yadis_Yadis::curlPresent() &&
- (!defined('Auth_Yadis_CURL_OVERRIDE'))) {
- $fetcher = new Auth_Yadis_ParanoidHTTPFetcher($timeout);
- } else {
- $fetcher = new Auth_Yadis_PlainHTTPFetcher($timeout);
- }
- return $fetcher;
- }
- static function curlPresent()
- {
- return function_exists('curl_init');
- }
-
- static function _getHeader($header_list, $names)
- {
- foreach ($header_list as $name => $value) {
- foreach ($names as $n) {
- if (strtolower($name) == strtolower($n)) {
- return $value;
- }
- }
- }
- return null;
- }
-
- static function _getContentType($content_type_header)
- {
- if ($content_type_header) {
- $parts = explode(";", $content_type_header);
- return strtolower($parts[0]);
- }
- }
-
- static function discover($uri, $fetcher,
- $extra_ns_map = null, $timeout = 20)
- {
- $result = new Auth_Yadis_DiscoveryResult($uri);
- $request_uri = $uri;
- $headers = array("Accept: " . Auth_Yadis_CONTENT_TYPE .
- ', text/html; q=0.3, application/xhtml+xml; q=0.5');
- if ($fetcher === null) {
- $fetcher = Auth_Yadis_Yadis::getHTTPFetcher($timeout);
- }
- $response = $fetcher->get($uri, $headers);
- if (!$response || ($response->status != 200 and
- $response->status != 206)) {
- $result->fail();
- return $result;
- }
- $result->normalized_uri = $response->final_url;
- $result->content_type = Auth_Yadis_Yadis::_getHeader(
- $response->headers,
- array('content-type'));
- if ($result->content_type &&
- (Auth_Yadis_Yadis::_getContentType($result->content_type) ==
- Auth_Yadis_CONTENT_TYPE)) {
- $result->xrds_uri = $result->normalized_uri;
- } else {
- $yadis_location = Auth_Yadis_Yadis::_getHeader(
- $response->headers,
- array(Auth_Yadis_HEADER_NAME));
- if (!$yadis_location) {
- $parser = new Auth_Yadis_ParseHTML();
- $yadis_location = $parser->getHTTPEquiv($response->body);
- }
- if ($yadis_location) {
- $result->xrds_uri = $yadis_location;
- $response = $fetcher->get($yadis_location);
- if ((!$response) || ($response->status != 200 and
- $response->status != 206)) {
- $result->fail();
- return $result;
- }
- $result->content_type = Auth_Yadis_Yadis::_getHeader(
- $response->headers,
- array('content-type'));
- }
- }
- $result->response_text = $response->body;
- return $result;
- }
- }
|