Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Yadis

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/Services/Yadis/Yadis.php

Documentation is available at Yadis.php

  1. <?php
  2.  
  3. /**
  4.  * The core PHP Yadis implementation.
  5.  *
  6.  * PHP versions 4 and 5
  7.  *
  8.  * LICENSE: See the COPYING file included in this distribution.
  9.  *
  10.  * @package Yadis
  11.  * @author JanRain, Inc. <[email protected]>
  12.  * @copyright 2005 Janrain, Inc.
  13.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  14.  */
  15.  
  16. /**
  17.  * Need both fetcher types so we can use the right one based on the
  18.  * presence or absence of CURL.
  19.  */
  20. require_once "Services/Yadis/PlainHTTPFetcher.php";
  21. require_once "Services/Yadis/ParanoidHTTPFetcher.php";
  22.  
  23. /**
  24.  * Need this for parsing HTML (looking for META tags).
  25.  */
  26. require_once "Services/Yadis/ParseHTML.php";
  27.  
  28. /**
  29.  * Need this to parse the XRDS document during Yadis discovery.
  30.  */
  31. require_once "Services/Yadis/XRDS.php";
  32.  
  33. /**
  34.  * This is the core of the PHP Yadis library.  This is the only class
  35.  * a user needs to use to perform Yadis discovery.  This class
  36.  * performs the discovery AND stores the result of the discovery.
  37.  *
  38.  * First, require this library into your program source:
  39.  *
  40.  * <pre>  require_once "Services/Yadis/Yadis.php";</pre>
  41.  *
  42.  * To perform Yadis discovery, first call the "discover" method
  43.  * statically with a URI parameter:
  44.  *
  45.  * <pre>  $http_response = array();
  46.  *  $fetcher = Services_Yadis_Yadis::getHTTPFetcher();
  47.  *  $yadis_object = Services_Yadis_Yadis::discover($uri,
  48.  *                                    $http_response, $fetcher);</pre>
  49.  *
  50.  * If the discovery succeeds, $yadis_object will be an instance of
  51.  * {@link Services_Yadis_Yadis}.  If not, it will be null.  The XRDS
  52.  * document found during discovery should have service descriptions,
  53.  * which can be accessed by calling
  54.  *
  55.  * <pre>  $service_list = $yadis_object->services();</pre>
  56.  *
  57.  * which returns an array of objects which describe each service.
  58.  * These objects are instances of Services_Yadis_Service.  Each object
  59.  * describes exactly one whole Service element, complete with all of
  60.  * its Types and URIs (no expansion is performed).  The common use
  61.  * case for using the service objects returned by services() is to
  62.  * write one or more filter functions and pass those to services():
  63.  *
  64.  * <pre>  $service_list = $yadis_object->services(
  65.  *                               array("filterByURI",
  66.  *                                     "filterByExtension"));</pre>
  67.  *
  68.  * The filter functions (whose names appear in the array passed to
  69.  * services()) take the following form:
  70.  *
  71.  * <pre>  function myFilter(&$service) {
  72.  *       // Query $service object here.  Return true if the service
  73.  *       // matches your query; false if not.
  74.  *  }</pre>
  75.  *
  76.  * This is an example of a filter which uses a regular expression to
  77.  * match the content of URI tags (note that the Services_Yadis_Service
  78.  * class provides a getURIs() method which you should use instead of
  79.  * this contrived example):
  80.  *
  81.  * <pre>
  82.  *  function URIMatcher(&$service) {
  83.  *      foreach ($service->getElements('xrd:URI') as $uri) {
  84.  *          if (preg_match("/some_pattern/",
  85.  *                         $service->parser->content($uri))) {
  86.  *              return true;
  87.  *          }
  88.  *      }
  89.  *      return false;
  90.  *  }</pre>
  91.  *
  92.  * The filter functions you pass will be called for each service
  93.  * object to determine which ones match the criteria your filters
  94.  * specify.  The default behavior is that if a given service object
  95.  * matches ANY of the filters specified in the services() call, it
  96.  * will be returned.  You can specify that a given service object will
  97.  * be returned ONLY if it matches ALL specified filters by changing
  98.  * the match mode of services():
  99.  *
  100.  * <pre>  $yadis_object->services(array("filter1", "filter2"),
  101.  *                          SERVICES_YADIS_MATCH_ALL);</pre>
  102.  *
  103.  * See {@link SERVICES_YADIS_MATCH_ALL} and {@link }
  104.  * SERVICES_YADIS_MATCH_ANY}.
  105.  *
  106.  * Services described in an XRDS should have a library which you'll
  107.  * probably be using.  Those libraries are responsible for defining
  108.  * filters that can be used with the "services()" call.  If you need
  109.  * to write your own filter, see the documentation for {@link }
  110.  * Services_Yadis_Service}.
  111.  *
  112.  * @package Yadis
  113.  */
  114.  
  115.     /**
  116.      * Returns an HTTP fetcher object.  If the CURL extension is
  117.      * present, an instance of {@link Services_Yadis_ParanoidHTTPFetcher}
  118.      * is returned.  If not, an instance of
  119.      * {@link Services_Yadis_PlainHTTPFetcher} is returned.
  120.      */
  121.     function getHTTPFetcher($timeout 20)
  122.     {
  123.         if (Services_Yadis_Yadis::curlPresent()) {
  124.             $fetcher new Services_Yadis_ParanoidHTTPFetcher($timeout);
  125.         else {
  126.             $fetcher new Services_Yadis_PlainHTTPFetcher($timeout);
  127.         }
  128.         return $fetcher;
  129.     }
  130.  
  131.     function curlPresent()
  132.     {
  133.         return function_exists('curl_init');
  134.     }
  135.  
  136.     /**
  137.      * @access private
  138.      */
  139.     function _getHeader($header_list$names)
  140.     {
  141.         foreach ($header_list as $name => $value{
  142.             foreach ($names as $n{
  143.                 if (strtolower($name== strtolower($n)) {
  144.                     return $value;
  145.                 }
  146.             }
  147.         }
  148.  
  149.         return null;
  150.     }
  151.  
  152.     /**
  153.      * @access private
  154.      */
  155.     function _getContentType($content_type_header)
  156.     {
  157.         if ($content_type_header{
  158.             $parts explode(";"$content_type_header);
  159.             return strtolower($parts[0]);
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * This should be called statically and will build a Yadis
  165.      * instance if the discovery process succeeds.  This implements
  166.      * Yadis discovery as specified in the Yadis specification.
  167.      *
  168.      * @param string $uri The URI on which to perform Yadis discovery.
  169.      *
  170.      * @param array $http_response An array reference where the HTTP
  171.      *  response object will be stored (see {@link }
  172.      *  Services_Yadis_HTTPResponse}.
  173.      *
  174.      * @param Services_Yadis_HTTPFetcher $fetcher An instance of a
  175.      *  Services_Yadis_HTTPFetcher subclass.
  176.      *
  177.      * @param array $extra_ns_map An array which maps namespace names
  178.      *  to namespace URIs to be used when parsing the Yadis XRDS
  179.      *  document.
  180.      *
  181.      * @param integer $timeout An optional fetcher timeout, in seconds.
  182.      *
  183.      * @return mixed $obj Either null or an instance of
  184.      *  Services_Yadis_Yadis, depending on whether the discovery
  185.      *  succeeded.
  186.      */
  187.     function discover($uri&$http_response&$fetcher,
  188.                       $extra_ns_map null$timeout 20)
  189.     {
  190.         if (!$uri{
  191.             return null;
  192.         }
  193.  
  194.         $request_uri $uri;
  195.         $headers array("Accept: application/xrds+xml");
  196.  
  197.         if (!$fetcher{
  198.             $fetcher Services_Yadis_Yadis::getHTTPFetcher($timeout);
  199.         }
  200.  
  201.         $response $fetcher->get($uri$headers);
  202.         $http_response $response;
  203.  
  204.         if (!$response{
  205.             return null;
  206.         }
  207.  
  208.         if ($response->status != 200{
  209.           return null;
  210.         }
  211.  
  212.         $xrds_uri $response->final_url;
  213.         $uri $response->final_url;
  214.         $body $response->body;
  215.  
  216.         $xrds_header_uri Services_Yadis_Yadis::_getHeader(
  217.                                                     $response->headers,
  218.                                                     array('x-xrds-location',
  219.                                                           'x-yadis-location'));
  220.  
  221.         $content_type Services_Yadis_Yadis::_getHeader($response->headers,
  222.                                                          array('content-type'));
  223.  
  224.         if ($xrds_header_uri{
  225.             $xrds_uri $xrds_header_uri;
  226.             $response $fetcher->get($xrds_uri);
  227.             $http_response $response;
  228.             if (!$response{
  229.                 return null;
  230.             else {
  231.                 $body $response->body;
  232.                 $headers $response->headers;
  233.                 $content_type Services_Yadis_Yadis::_getHeader($headers,
  234.                                                        array('content-type'));
  235.             }
  236.         }
  237.  
  238.         if (Services_Yadis_Yadis::_getContentType($content_type!=
  239.             'application/xrds+xml'{
  240.             // Treat the body as HTML and look for a META tag.
  241.             $parser new Services_Yadis_ParseHTML();
  242.             $new_uri $parser->getHTTPEquiv($body);
  243.             $xrds_uri null;
  244.             if ($new_uri{
  245.                 $response $fetcher->get($new_uri);
  246.                 if ($response->status != 200{
  247.                   return null;
  248.                 }
  249.                 $http_response $response;
  250.                 $body $response->body;
  251.                 $xrds_uri $new_uri;
  252.                 $content_type Services_Yadis_Yadis::_getHeader(
  253.                                                          $response->headers,
  254.                                                          array('content-type'));
  255.             }
  256.         }
  257.  
  258.         $xrds Services_Yadis_XRDS::parseXRDS($body$extra_ns_map);
  259.  
  260.         if ($xrds !== null{
  261.             $y new Services_Yadis_Yadis();
  262.  
  263.             $y->request_uri $request_uri;
  264.             $y->xrds $xrds;
  265.             $y->uri $uri;
  266.             $y->xrds_uri $xrds_uri;
  267.             $y->body $body;
  268.             $y->content_type $content_type;
  269.  
  270.             return $y;
  271.         else {
  272.             return null;
  273.         }
  274.     }
  275.  
  276.     /**
  277.      * Instantiates an empty Services_Yadis_Yadis object.  This
  278.      * constructor should not be used by any user of the library.
  279.      * This constructor results in a completely useless object which
  280.      * must be populated with valid discovery information.  Instead of
  281.      * using this constructor, call
  282.      * Services_Yadis_Yadis::discover($uri).
  283.      */
  284.     function Services_Yadis_Yadis()
  285.     {
  286.         $this->request_uri null;
  287.         $this->uri null;
  288.         $this->xrds null;
  289.         $this->xrds_uri null;
  290.         $this->body null;
  291.         $this->content_type null;
  292.     }
  293.  
  294.     /**
  295.      * Returns the list of service objects as described by the XRDS
  296.      * document, if this yadis object represents a successful Yadis
  297.      * discovery.
  298.      *
  299.      * @return array $services An array of {@link Services_Yadis_Service}
  300.      *  objects
  301.      */
  302.     function services()
  303.     {
  304.         if ($this->xrds{
  305.             return $this->xrds->services();
  306.         }
  307.  
  308.         return null;
  309.     }
  310. }
  311.  
  312. ?>

Documentation generated on Mon, 05 Mar 2007 21:33:23 +0000 by phpDocumentor 1.3.1