Yadis.php

Go to the documentation of this file.
00001 <?php
00002 
00020 require_once "Auth/Yadis/PlainHTTPFetcher.php";
00021 require_once "Auth/Yadis/ParanoidHTTPFetcher.php";
00022 
00026 require_once "Auth/Yadis/ParseHTML.php";
00027 
00031 require_once "Auth/Yadis/XRDS.php";
00032 
00036 define('Auth_Yadis_CONTENT_TYPE', 'application/xrds+xml');
00037 
00041 define('Auth_Yadis_HEADER_NAME', 'X-XRDS-Location');
00042 
00048 class Auth_Yadis_DiscoveryResult {
00049 
00050     // The URI that was passed to the fetcher
00051     var $request_uri = null;
00052 
00053     // The result of following redirects from the request_uri
00054     var $normalized_uri = null;
00055 
00056     // The URI from which the response text was returned (set to
00057     // None if there was no XRDS document found)
00058     var $xrds_uri = null;
00059 
00060     var $xrds = null;
00061 
00062     // The content-type returned with the response_text
00063     var $content_type = null;
00064 
00065     // The document returned from the xrds_uri
00066     var $response_text = null;
00067 
00068     // Did the discovery fail miserably?
00069     var $failed = false;
00070 
00071     function Auth_Yadis_DiscoveryResult($request_uri)
00072     {
00073         // Initialize the state of the object
00074         // sets all attributes to None except the request_uri
00075         $this->request_uri = $request_uri;
00076     }
00077 
00078     function fail()
00079     {
00080         $this->failed = true;
00081     }
00082 
00083     function isFailure()
00084     {
00085         return $this->failed;
00086     }
00087 
00096     function services()
00097     {
00098         if ($this->xrds) {
00099             return $this->xrds->services();
00100         }
00101 
00102         return null;
00103     }
00104 
00105     function usedYadisLocation()
00106     {
00107         // Was the Yadis protocol's indirection used?
00108         return $this->normalized_uri != $this->xrds_uri;
00109     }
00110 
00111     function isXRDS()
00112     {
00113         // Is the response text supposed to be an XRDS document?
00114         return ($this->usedYadisLocation() ||
00115                 $this->content_type == Auth_Yadis_CONTENT_TYPE);
00116     }
00117 }
00118 
00136 function Auth_Yadis_getServiceEndpoints($input_url, $xrds_parse_func,
00137                                         $discover_func=null, $fetcher=null)
00138 {
00139     if ($discover_func === null) {
00140         $discover_function = array('Auth_Yadis_Yadis', 'discover');
00141     }
00142 
00143     $yadis_result = call_user_func_array($discover_func,
00144                                          array($input_url, $fetcher));
00145 
00146     if ($yadis_result === null) {
00147         return array($input_url, array());
00148     }
00149 
00150     $endpoints = call_user_func_array($xrds_parse_func,
00151                       array($yadis_result->normalized_uri,
00152                             $yadis_result->response_text));
00153 
00154     if ($endpoints === null) {
00155         $endpoints = array();
00156     }
00157 
00158     return array($yadis_result->normalized_uri, $endpoints);
00159 }
00160 
00242 class Auth_Yadis_Yadis {
00243 
00253     function getHTTPFetcher($timeout = 20)
00254     {
00255         if (Auth_Yadis_Yadis::curlPresent() &&
00256             (!defined('Auth_Yadis_CURL_OVERRIDE'))) {
00257             $fetcher = new Auth_Yadis_ParanoidHTTPFetcher($timeout);
00258         } else {
00259             $fetcher = new Auth_Yadis_PlainHTTPFetcher($timeout);
00260         }
00261         return $fetcher;
00262     }
00263 
00264     function curlPresent()
00265     {
00266         return function_exists('curl_init');
00267     }
00268 
00272     function _getHeader($header_list, $names)
00273     {
00274         foreach ($header_list as $name => $value) {
00275             foreach ($names as $n) {
00276                 if (strtolower($name) == strtolower($n)) {
00277                     return $value;
00278                 }
00279             }
00280         }
00281 
00282         return null;
00283     }
00284 
00288     function _getContentType($content_type_header)
00289     {
00290         if ($content_type_header) {
00291             $parts = explode(";", $content_type_header);
00292             return strtolower($parts[0]);
00293         }
00294     }
00295 
00320     function discover($uri, &$fetcher,
00321                       $extra_ns_map = null, $timeout = 20)
00322     {
00323         $result = new Auth_Yadis_DiscoveryResult($uri);
00324 
00325         $request_uri = $uri;
00326         $headers = array("Accept: " . Auth_Yadis_CONTENT_TYPE .
00327                          ', text/html; q=0.3, application/xhtml+xml; q=0.5');
00328 
00329         if ($fetcher === null) {
00330             $fetcher = Auth_Yadis_Yadis::getHTTPFetcher($timeout);
00331         }
00332 
00333         $response = $fetcher->get($uri, $headers);
00334 
00335         if (!$response || ($response->status != 200 and
00336                            $response->status != 206)) {
00337             $result->fail();
00338             return $result;
00339         }
00340 
00341         $result->normalized_uri = $response->final_url;
00342         $result->content_type = Auth_Yadis_Yadis::_getHeader(
00343                                        $response->headers,
00344                                        array('content-type'));
00345 
00346         if ($result->content_type &&
00347             (Auth_Yadis_Yadis::_getContentType($result->content_type) ==
00348              Auth_Yadis_CONTENT_TYPE)) {
00349             $result->xrds_uri = $result->normalized_uri;
00350         } else {
00351             $yadis_location = Auth_Yadis_Yadis::_getHeader(
00352                                                  $response->headers,
00353                                                  array(Auth_Yadis_HEADER_NAME));
00354 
00355             if (!$yadis_location) {
00356                 $parser = new Auth_Yadis_ParseHTML();
00357                 $yadis_location = $parser->getHTTPEquiv($response->body);
00358             }
00359 
00360             if ($yadis_location) {
00361                 $result->xrds_uri = $yadis_location;
00362 
00363                 $response = $fetcher->get($yadis_location);
00364 
00365                 if ((!$response) || ($response->status != 200 and
00366                                      $response->status != 206)) {
00367                     $result->fail();
00368                     return $result;
00369                 }
00370 
00371                 $result->content_type = Auth_Yadis_Yadis::_getHeader(
00372                                                          $response->headers,
00373                                                          array('content-type'));
00374             }
00375         }
00376 
00377         $result->response_text = $response->body;
00378         return $result;
00379     }
00380 }
00381 
00382 ?>

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5