Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Unknown

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Auth/OpenID/Discover.php

Documentation is available at Discover.php

  1. <?php
  2.  
  3. /**
  4.  * The OpenID and Yadis discovery implementation for OpenID 1.2.
  5.  */
  6.  
  7. require_once "Auth/OpenID.php";
  8. require_once "Auth/OpenID/Parse.php";
  9. require_once "Services/Yadis/XRIRes.php";
  10. require_once "Services/Yadis/Yadis.php";
  11.  
  12. define('_OPENID_1_0_NS''http://openid.net/xmlns/1.0');
  13. define('_OPENID_1_2_TYPE''http://openid.net/signon/1.2');
  14. define('_OPENID_1_1_TYPE''http://openid.net/signon/1.1');
  15. define('_OPENID_1_0_TYPE''http://openid.net/signon/1.0');
  16.  
  17. /**
  18.  * Object representing an OpenID service endpoint.
  19.  */
  20.     function Auth_OpenID_ServiceEndpoint()
  21.     {
  22.         $this->identity_url null;
  23.         $this->server_url null;
  24.         $this->type_uris array();
  25.         $this->delegate null;
  26.         $this->canonicalID null;
  27.         $this->used_yadis false// whether this came from an XRDS
  28.     }
  29.  
  30.     function usesExtension($extension_uri)
  31.     {
  32.         return in_array($extension_uri$this->type_uris);
  33.     }
  34.  
  35.     function parseService($yadis_url$uri$type_uris$service_element)
  36.     {
  37.         // Set the state of this object based on the contents of the
  38.         // service element.
  39.         $this->type_uris $type_uris;
  40.         $this->identity_url $yadis_url;
  41.         $this->server_url $uri;
  42.         $this->delegate Auth_OpenID_ServiceEndpoint::findDelegate(
  43.                                                          $service_element);
  44.         $this->used_yadis true;
  45.     }
  46.  
  47.     function findDelegate($service)
  48.     {
  49.         // Extract a openid:Delegate value from a Yadis Service
  50.         // element.  If no delegate is found, returns null.
  51.  
  52.         // Try to register new namespace.
  53.         $service->parser->registerNamespace('openid',
  54.                                             'http://openid.net/xmlns/1.0');
  55.  
  56.         // XXX: should this die if there is more than one delegate
  57.         // element?
  58.         $delegates $service->getElements("openid:Delegate");
  59.  
  60.         if ($delegates{
  61.             return $service->parser->content($delegates[0]);
  62.         else {
  63.             return null;
  64.         }
  65.     }
  66.  
  67.     function getServerID()
  68.     {
  69.         // Return the identifier that should be sent as the
  70.         // openid.identity_url parameter to the server.
  71.         if ($this->delegate === null{
  72.             if ($this->canonicalID{
  73.                 return $this->canonicalID;
  74.             else {
  75.                 return $this->identity_url;
  76.             }
  77.         else {
  78.             return $this->delegate;
  79.         }
  80.     }
  81.  
  82.     function fromHTML($uri$html)
  83.     {
  84.         // Parse the given document as HTML looking for an OpenID <link
  85.         // rel=...>
  86.         $urls Auth_OpenID_legacy_discover($html);
  87.         if ($urls === false{
  88.             return null;
  89.         }
  90.  
  91.         list($delegate_url$server_url$urls;
  92.  
  93.         $service new Auth_OpenID_ServiceEndpoint();
  94.         $service->identity_url $uri;
  95.         $service->delegate $delegate_url;
  96.         $service->server_url $server_url;
  97.         $service->type_uris array(_OPENID_1_0_TYPE);
  98.         return $service;
  99.     }
  100. }
  101.  
  102. function filter_MatchesAnyOpenIDType(&$service)
  103. {
  104.     $uris $service->getTypes();
  105.  
  106.     foreach ($uris as $uri{
  107.         if (in_array($uri,
  108.                      array(_OPENID_1_0_TYPE,
  109.                            _OPENID_1_1_TYPE,
  110.                            _OPENID_1_2_TYPE))) {
  111.             return true;
  112.         }
  113.     }
  114.  
  115.     return false;
  116. }
  117.  
  118. function Auth_OpenID_makeOpenIDEndpoints($uri$endpoints)
  119. {
  120.     $s array();
  121.  
  122.     if (!$endpoints{
  123.         return $s;
  124.     }
  125.  
  126.     foreach ($endpoints as $service{
  127.         $type_uris $service->getTypes();
  128.         $uris $service->getURIs();
  129.  
  130.         // If any Type URIs match and there is an endpoint URI
  131.         // specified, then this is an OpenID endpoint
  132.         if ($type_uris &&
  133.             $uris{
  134.  
  135.             foreach ($uris as $service_uri{
  136.                 $openid_endpoint new Auth_OpenID_ServiceEndpoint();
  137.                 $openid_endpoint->parseService($uri,
  138.                                                $service_uri,
  139.                                                $type_uris,
  140.                                                $service);
  141.  
  142.                 $s[$openid_endpoint;
  143.             }
  144.         }
  145.     }
  146.  
  147.     return $s;
  148. }
  149.  
  150. function Auth_OpenID_discoverWithYadis($uri&$fetcher)
  151. {
  152.     // Discover OpenID services for a URI. Tries Yadis and falls back
  153.     // on old-style <link rel='...'> discovery if Yadis fails.
  154.  
  155.     // Might raise a yadis.discover.DiscoveryFailure if no document
  156.     // came back for that URI at all.  I don't think falling back to
  157.     // OpenID 1.0 discovery on the same URL will help, so don't bother
  158.     // to catch it.
  159.     $openid_services array();
  160.  
  161.     $http_response null;
  162.     $response Services_Yadis_Yadis::discover($uri$http_response,
  163.                                                 $fetcher);
  164.  
  165.     if ($response{
  166.         $identity_url $response->uri;
  167.         $openid_services =
  168.             $response->xrds->services(array('filter_MatchesAnyOpenIDType'));
  169.     }
  170.  
  171.     if (!$openid_services{
  172.         return @Auth_OpenID_discoverWithoutYadis($uri,
  173.                                                  $fetcher);
  174.     }
  175.  
  176.     if (!$openid_services{
  177.         $body $response->body;
  178.  
  179.         // Try to parse the response as HTML to get OpenID 1.0/1.1
  180.         // <link rel="...">
  181.         $service Auth_OpenID_ServiceEndpoint::fromHTML($identity_url,
  182.                                                          $body);
  183.  
  184.         if ($service !== null{
  185.             $openid_services array($service);
  186.         }
  187.     else {
  188.         $openid_services Auth_OpenID_makeOpenIDEndpoints($response->uri,
  189.                                                            $openid_services);
  190.     }
  191.  
  192.     return array($identity_url$openid_services$http_response);
  193. }
  194.  
  195. function _Auth_OpenID_discoverServiceList($uri&$fetcher)
  196. {
  197.     list($url$services$respAuth_OpenID_discoverWithYadis($uri,
  198.                                                                  $fetcher);
  199.  
  200.     return $services;
  201. }
  202.  
  203. function _Auth_OpenID_discoverXRIServiceList($uri&$fetcher)
  204. {
  205.     list($url$services$resp_Auth_OpenID_discoverXRI($uri,
  206.                                                             $fetcher);
  207.     return $services;
  208. }
  209.  
  210. function Auth_OpenID_discoverWithoutYadis($uri&$fetcher)
  211. {
  212.     $http_resp @$fetcher->get($uri);
  213.  
  214.     if ($http_resp->status != 200{
  215.         return array(nullarray()$http_resp);
  216.     }
  217.  
  218.     $identity_url $http_resp->final_url;
  219.  
  220.     // Try to parse the response as HTML to get OpenID 1.0/1.1 <link
  221.     // rel="...">
  222.     $endpoint =new Auth_OpenID_ServiceEndpoint();
  223.     $service $endpoint->fromHTML($identity_url$http_resp->body);
  224.     if ($service === null{
  225.         $openid_services array();
  226.     else {
  227.         $openid_services array($service);
  228.     }
  229.  
  230.     return array($identity_url$openid_services$http_resp);
  231. }
  232.  
  233. function _Auth_OpenID_discoverXRI($iname&$fetcher)
  234. {
  235.     $services new Services_Yadis_ProxyResolver($fetcher);
  236.     list($canonicalID$service_list$services->query($iname,
  237.                                                   array(_OPENID_1_0_TYPE,
  238.                                                         _OPENID_1_1_TYPE,
  239.                                                         _OPENID_1_2_TYPE),
  240.                                      array('filter_MatchesAnyOpenIDType'));
  241.  
  242.     $endpoints Auth_OpenID_makeOpenIDEndpoints($iname$service_list);
  243.  
  244.     for ($i 0$i count($endpoints)$i++{
  245.         $endpoints[$i]->canonicalID $canonicalID;
  246.     }
  247.  
  248.     // FIXME: returned xri should probably be in some normal form
  249.     return array($iname$endpointsnull);
  250. }
  251.  
  252. function Auth_OpenID_discover($uri&$fetcher)
  253. {
  254.     return @Auth_OpenID_discoverWithYadis($uri$fetcher);
  255. }
  256.  
  257. ?>

Documentation generated on Mon, 05 Mar 2007 20:56:31 +0000 by phpDocumentor 1.3.1