cvs: pear /Services_Yadis Yadis.php generate_package_xml.php package.xml test.php /Services_Yadis/Yadis Exception.php Service.php Xrds.php Xri.php
/Services_Yadis/Yadis/Xrds Namespace.php Service.php

From: Date: Sat, 29 Sep 2007 12:14:46 +0000
Subject: cvs: pear /Services_Yadis Yadis.php generate_package_xml.php package.xml test.php /Services_Yadis/Yadis Exception.php Service.php Xrds.php Xri.php
/Services_Yadis/Yadis/Xrds Namespace.php Service.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-48700@lists.php.net to get a copy of this message
padraic Sat Sep 29 12:14:46 2007 UTC Added files: /pear/Services_Yadis Yadis.php generate_package_xml.php package.xml test.php /pear/Services_Yadis/Yadis Exception.php Service.php Xrds.php Xri.php /pear/Services_Yadis/Yadis/Xrds Namespace.php Service.php Log: Services_Yadis initial commit for OpenID ancillary support

http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis.php +++ pear/Services_Yadis/Yadis.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Yadis.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** Services_Yadis_Xrds_Service */ require_once 'Services/Yadis/Xrds/Service.php'; /** Services_Yadis_Xrds_Namespace */ require_once 'Services/Yadis/Xrds/Namespace.php'; /** HTTP_Request */ require_once 'HTTP/Request.php'; /** Validate */ require_once 'Validate.php'; /** * Services_Yadis class * * Services_Yadis will provide a method of Service Discovery implemented * in accordance with the Yadis Specification 1.0. This describes a protocol * for locating an XRD document which details Services available. The XRD is * typically specific to a single user, identified by their Yadis ID. * Services_Yadis_XRDS will be a wrapper which is responsible for parsing * and presenting an iterable list of Services_Yadis_Service objects * holding the data for each specific Service discovered. * * Note that class comments cannot substitute for a full understanding of the * rules and nuances required to implement the Yadis protocol. Where doubt * exists, refer to the Yadis Specification 1.0 at: * http://yadis.org/papers/yadis-v1.0.pdf * Departures from the specification should be regarded as bugs ;). * * Example usage: * * Example 1: OpenID Service Discovery * * $openid = 'http://padraic.astrumfutura.com'; * $yadis = new Services_Yadis($openid); * $yadis->addNamespace('openid', 'http://openid.net/xmlns/1.0'); * $serviceList = $yadis->discover(); * * foreach ($serviceList as $service) { * $types = $service->getTypes(); * echo $types[0], ' at ', implode(', ', $service->getUris()), PHP_EOL; * echo 'Priority is ', $service->->getPriority(), PHP_EOL; * } * * Possible Result @index[0] (indicates we may send Auth 2.0 requests for OpenID): * * http://specs.openid.net/auth/2.0/server at http://www.myopenid.com/server * Priority is 0 * * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis { /** * Constants referring to Yadis response types */ const XRDS_META_HTTP_EQUIV = 2; const XRDS_LOCATION_HEADER = 4; const XRDS_CONTENT_TYPE = 8; /** * The current Yadis ID; this is the raw form initially submitted prior * to any transformation/validation as an URL. This *may* allow IRI support * in the future given IRIs map to URIs and adoption of the IRI standard * and are entering common use internationally. * * @var string */ protected $_yadisId = ''; /** * The current Yadis URL; this is a URL either validated or transformed * from the initial Yadis ID. This URL is used to make the initial HTTP * GET request during Service Discovery. * * @var string */ protected $_yadisUrl = ''; /** * Holds the first response received during Service Discovery. * * This is required to allow certain Service specific fallback methods. * For example, OpenID allows a Yadis fallback which relies on seeking a * set of appropriate <link> elements. * * @var string */ protected $_metaHttpEquivResponse = null; /** * A URL parsed from a HTML document's <meta> element inserted in * accordance with the Yadis Specification and which points to a Yadis * XRD document. * * @var string */ protected $_metaHttpEquivUrl = ''; /** * A URI parsed from an X-XRDS-Location response-header. This value must * point to a Yadis XRD document otherwise the Yadis discovery process * should be considered to have failed. * * @var string */ protected $_xrdsLocationHeaderUrl = ''; /** * Instance of Services_Yadis_Xrds_Namespace for managing namespaces * associated with an XRDS document. * * @var Services_Yadis_Xrds_Namespace */ protected $_namespace = null; /** * Array of valid HTML Content-Types. Required since Yadis states agents * must parse a document if received as the first response and with an * MIME type indicating HTML or XHTML. Listed in order of priority, with * HTML taking priority over XHTML. * * @link http://www.w3.org/International/articles/serving-xhtml/Overview.en.php * @var array */ protected $_validHtmlContentTypes = array( 'text/html', 'application/xhtml+xml', 'application/xml', 'text/xml' ); /* * Array of characters which if found at the 0 index of a Yadis ID string * may indicate the use of an XRI. * * @var array */ protected $_xriIdentifiers = array( '=', '$', '!', '@', '+' ); protected $_httpRequestOptions = null; /** * HTTP_Request object utilised by this class if externally set * * @var HTTP_Request */ protected $_httpRequest = null; /** * Class Constructor * * Allows settings of the initial Yadis ID (an OpenID URL for example) and * an optional list of additional namespaces. For example, OpenID uses a * namespace such as: xmlns:openid="http://openid.net/xmlns/1.0" * Namespaces are assigned to a Services_Yadis_Xrds_Namespace container * object to be passed more easily to other objects being * * @param string $yadisId * @param array $namespaces */ public function __construct($yadisId = null, array $namespaces = null) { $this->_namespace = new Services_Yadis_Xrds_Namespace; if (isset($namespaces) && empty($namespaces)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('$namespaces array is empty'); } if (!is_null($namespaces)) { $this->addNamespaces($namespaces); } if (isset($yadisId)) { $this->setYadisId($yadisId); } } /** * Set options to be passed to the PEAR HTTP_Request constructor * * @param array $options * @return void */ public function setHttpRequestOptions(array $options) { $this->_httpRequestOptions = $options; } /** * Get options to be passed to the PEAR HTTP_Request constructor * * @return array */ public function getHttpRequestOptions() { return $this->_httpRequestOptions; } /** * A Yadis ID is usually an URL, but can also include an IRI, or XRI i-name. * The initial version will support URLs as standard before examining options * for supporting alternatives (IRI,XRI,i-name) since they require additional * validation and conversion steps (e.g. Punycode for IRI) before use. * * Note: The current Validate classes currently do not have complete IDNA * validation support for Internationalised Domain Names. To be addressed. * * @param string $yadisId */ public function setYadisId($yadisId) { $this->_yadisId = $yadisId; $this->setYadisUrl($yadisId); } /** * Returns the original Yadis ID string set for this class. * * @returns string */ public function getYadisId() { if (!isset($this->_yadisId)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('No Yadis ID has been set on this object yet'); } return $this->_yadisId; } /** * Attempts to create a valid URI based on the value of the parameter * which would typically be the Yadis ID. * Note: This currently only supports XRI transformations. * * @param string $yadisId * @return Services_Yadis * @throws Services_Yadis_Exception */ public function setYadisUrl($yadisId) { /** * This step should validate IDNs (see ZF-881) */ if (Validate::uri($yadisId)) { $this->_yadisUrl = $yadisId; return $this; } /** * Check if the Yadis ID is an XRI */ if (stripos($yadisId, 'xri://') == 0 || in_array($yadisId[0], $this->_xriIdentifiers)) { require_once 'Services/Yadis/Xri.php'; $xri = Services_Yadis_Xri::getInstance() $this->_yadisUrl = $xri->setHttpRequestOptions($this->getHttpRequestOptions()) ->setNamespace($this->_namespace) ->toUri($yadisId); $cid = Services_Yadis_Xri::getInstance()->getCanonicalId(); exit(__LINE__ .' '. 'Services/Yadis.php' . '\nNot implemented yet'); // not cool but it's a future enhancement return $this; } /** * The use of IRIs (International Resource Identifiers) is governed by * RFC 3490-3495. Not yet available for validation in PEAR. */ require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Unable to validate a Yadis ID as a URI, or to transform a Yadis ID into a valid URI.'); } /** * Returns the Yadis URL. This will usually be identical to the Yadis ID, * unless the Yadis ID (in the future) was one of IRI, XRI or i-name which * required transformation to a valid URI. * * @returns string */ public function getYadisUrl() { if (!isset($this->_yadisUrl)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('No Yadis ID/URL has been set on this object yet'); } return $this->_yadisUrl; } /** * Add a list (array) of additional namespaces to be utilised by the XML * parser when it receives a valid XRD document. * * @param array $namespaces * @return Services_Yadis */ public function addNamespaces(array $namespaces) { $this->_namespace->addNamespaces($namespaces); return $this; } /** * Add a single namespace to be utilised by the XML parser when it receives * a valid XRD document. * * @param string $namespace * @param string $namespaceUrl * @return Services_Yadis */ public function addNamespace($namespace, $namespaceUrl) { $this->_namespace->addNamespace($namespace, $namespaceUrl); return $this; } /** * Return the value of a specific namespace. * * @return string|null */ public function getNamespace($namespace) { return $this->_namespace->getNamespace($namespace); } /** * Returns an array of all currently set namespaces. * * @return array */ public function getNamespaces() { return $this->_namespace->getNamespaces(); } /** * Performs Service Discovery, i.e. the requesting and parsing of a valid * Yadis (XRD) document into a list of Services and Service Data. The * return value will be an instance of Services_Yadis_Xrds which will * implement SeekableIterator. Returns FALSE on failure. * * @return Services_Yadis_Xrds|boolean * @throws Services_Yadis_Exception */ public function discover() { $currentUri = $this->getYadisUrl(); $xrdsDocument = null; $request = null; $xrdStatus = false; while($xrdsDocument === null) { $request = $this->_get($currentUri); if (!$this->_metaHttpEquivResponse) { $this->_metaHttpEquivResponse = $request->getResponseBody(); } $responseType = $this->_getResponseType($request); /** * If prior response type was a location header, or a http-equiv * content value, then it should have contained a valid URI to * an XRD document. Each of these when detected would set the * xrdStatus flag to true. */ if (!$responseType == self::XRDS_CONTENT_TYPE && $xrdStatus == true) { // true or false??? require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Yadis protocol could not locate a valid XRD document'); } /** * The Yadis Spec 1.0 specifies that we must use a valid response * header in preference to other responses. So even if we receive * an XRDS Content-Type, if it also includes an X-XRDS-Location * header we must request the Location URI and ignore the response * body. */ switch($responseType) { case self::XRDS_LOCATION_HEADER: $xrdStatus = true; $currentUri = $this->_xrdsLocationHeaderUrl; break; case self::XRDS_META_HTTP_EQUIV: $xrdStatus = true; $currentUri = $this->_metaHttpEquivUrl; break; case self::XRDS_CONTENT_TYPE: $xrdsDocument = $request->getResponseBody(); break; default: require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Yadis protocol could not locate a valid XRD document'); } } try { $serviceList = $this->_parseXrds($xrdsDocument); } catch (PEAR_Exception $e) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('XRD Document could not be parsed with the following message: ' . $e->getMessage(), $e->getCode()); } return $serviceList; } /** * Return the very first response received when using a valid Yadis URL. * This is important for Services, like OpenID, which can attempt a * fallback solution in case Yadis fails, and the response came from a * user's personal URL acting as an alias. * * @return string|boolean */ public function getUserResponse() { if ($this->_metaHttpEquivResponse instanceof HTTP_Request) { return $this->_metaHttpEquivResponse->getResponseBody(); } return false; } /** * Setter for custom HTTP_Request type object * * @param HTTP_Request $request * @return void */ public function setHttpRequest(HTTP_Request $request) { $this->_httpRequest = $request; } /** * Setter for custom HTTP_Request type object * * @param HTTP_Request $request * @return void */ public function getHttpRequest() { return $this->_httpRequest; } /** * Run any instance of HTTP_Request through a set of filters to * determine the Yadis Response type which in turns determines how the * response should be reacted to or dealt with. * * @param HTTP_Request $request * @return integer */ protected function _getResponseType(HTTP_Request $request) { if ($this->_isXrdsLocationHeader($request)) { return self::XRDS_LOCATION_HEADER; } elseif ($this->_isXrdsContentType($request)) { return self::XRDS_CONTENT_TYPE; } elseif ($this->_isMetaHttpEquiv($request)) { return self::XRDS_META_HTTP_EQUIV; } return false; } /** * Use the HTTP_Request to issue an HTTP GET request carrying the * "Accept" header value of "application/xrds+xml". This can allow * servers to quickly respond with a valid XRD document rather than * forcing the client to follow the X-XRDS-Location bread crumb trail. * * @param string $url * @return HTTP_Request */ protected function _get($url) { if ($this->_getHttpRequest() === null) { $request = new HTTP_Request($url, $this->getHttpRequestOptions()); } else { $request = $this->_getHttpRequest(); } $request->setMethod(HTTP_REQUEST_METHOD_GET); $request->addHeader('Accept', 'application/xrds+xml'); $response = $request->sendRequest(); if (PEAR::isError($response)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid response to Yadis protocol received: ' . $request->getResponseCode() . ' ' . $request->getResponseBody()); } return $request; } /** * Checks whether the Response contains headers which detail where * we can find the XRDS resource for this user. If exists, the value * is set to the private $_xrdsLocationHeaderUrl property. * * @param HTTP_Request $request * @return boolean */ protected function _isXrdsLocationHeader(HTTP_Request $request) { if ($request->getResponseHeader('x-xrds-location')) { $location = $request->getResponseHeader('x-xrds-location'); } elseif ($request->getResponseHeader('x-yadis-location')) { $location = $request->getResponseHeader('x-yadis-location'); } if (empty($location)) { return false; } elseif (!Validate::uri($location)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid URI found during Discovery for location of XRDS document:' . htmlentities($location, ENT_QUOTES, 'utf-8')); } $this->_xrdsLocationHeaderUrl = $location; return true; } /** * Checks whether the Response contains the XRDS resource. It should, per * the specifications always be served as application/xrds+xml * * @param HTTP_Request $request * @return boolean */ protected function _isXrdsContentType(HTTP_Request $request) { if (!$request->getResponseHeader('Content-Type') || stripos($request->getResponseHeader('Content-Type'), 'application/xrds+xml') === false) { return false; } return true; } /** * Assuming this user is hosting a third party sourced identity under an * alias personal URL, we'll need to check if the website's HTML body * has a http-equiv meta element with a content attribute pointing to where * we can fetch the XRD document. * * @param HTTP_Request * @return boolean * @throws Services_Yadis_Exception */ protected function _isMetaHttpEquiv(HTTP_Request $request) { $location = null; if (!in_array($request->getResponseHeader('Content-Type'), $this->_validHtmlContentTypes)) { return false; } /** * Find a match for a relevant <meta> element, then iterate through the * results to see if a valid http-equiv value and matching content URI * exist. */ $html = new DOMDocument(); $html->loadHTML( $request->getResponseBody() ); $head = $html->getElementsByTagName('head'); if ($head->length > 0) { $metas = $head->item(0)->getElementsByTagName('meta'); if ($metas->length > 0) { foreach ($metas as $meta) { if (strtolower($meta->getAttribute('http-equiv')) == 'x-xrds-location' || strtolower($meta->getAttribute('http-equiv')) == 'x-yadis-location') { $location = $meta->getAttribute('content'); } } } } if (is_null($location)) { return false; } elseif (!Validate::uri($location)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('The URI parsed from the HTML Alias document appears to be invalid, or could not be found: ' . htmlentities($location, ENT_QUOTES, 'utf-8')); } /** * Should now contain the content value of the http-equiv type pointing * to an XRDS resource for the user's Identity Provider, as found by * passing the meta regex across the response body. */ $this->_metaHttpEquivUrl = $location; return true; } /** * Creates a new Services_Yadis_Xrds object which uses SimpleXML to * parse the XML into a list of Iterable Services_Yadis_Service * objects. * * @param string $xrdsDocument * @return Services_Yadis_Xrds|boolean */ protected function _parseXrds($xrdsDocument) { $xrds = new SimpleXMLElement($xrdsDocument); $serviceSet = new Services_Yadis_Xrds_Service($xrds, $this->_namespace); return $serviceSet; } } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/generate_package_xml.php?view=markup&rev=1.1 Index: pear/Services_Yadis/generate_package_xml.php +++ pear/Services_Yadis/generate_package_xml.php <?php require_once('PEAR/PackageFileManager2.php'); PEAR::setErrorHandling(PEAR_ERROR_DIE); $packagexml = new PEAR_PackageFileManager2; $e = $packagexml->setOptions( array('baseinstalldir' => 'Services', 'packagedirectory' => 'D:/xampp/htdocs/projects/pear/trunk/Services_Yadis', 'filelistgenerator' => 'file', 'dir_roles' => array('docs' => 'doc', 'tests' => 'test'), 'ignore' => array('generate_package_xml.php', '.svn') ) ); $packagexml->setPackage('Services_Yadis'); $packagexml->setSummary('Implementation of Yadis Specification 1.0 protocol for PHP5'); $packagexml->setDescription("Implementation of the Yadis Specification 1.0 protocol allowing a client to discover a list of Services a Yadis Identity Provider offers."); $packagexml->setChannel('pear.php.net'); $packagexml->setAPIVersion('0.1.0'); $packagexml->setReleaseVersion('0.1.0a3'); $packagexml->setReleaseStability('alpha'); $packagexml->setAPIStability('alpha'); $packagexml->setNotes("* Added package dependencies for HTTP_Request and Validate"); $packagexml->setPackageType('php'); $packagexml->setPhpDep('5.1.4'); $packagexml->setPearinstallerDep('1.4.0'); $packagexml->addPackageDepWithChannel('required', 'HTTP_Request', 'pear.php.net'); $packagexml->addPackageDepWithChannel('required', 'Validate', 'pear.php.net'); $packagexml->addMaintainer('lead', 'padraic', 'Pádraic Brady', 'padraic.brady@yahoo.com'); $packagexml->setLicense('New BSD License', 'http://opensource.org/licenses/bsd-license.php'); $packagexml->generateContents(); if (isset($_GET['make']) || (isset($_SERVER['argv']) && @$_SERVER['argv'][1] == 'make')) { $packagexml->writePackageFile(); } else { $packagexml->debugPackageFile(); } ?> http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/package.xml?view=markup&rev=1.1 Index: pear/Services_Yadis/package.xml +++ pear/Services_Yadis/package.xml <?xml version="1.0" encoding="UTF-8"?> <package packagerversion="1.4.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Services_Yadis</name> <channel>pear.php.net</channel> <summary>Implementation of Yadis Specification 1.0 protocol for PHP5</summary> <description>Implementation of the Yadis Specification 1.0 protocol allowing a client to discover a list of Services a Yadis Identity Provider offers.</description> <lead> <name>Pádraic Brady</name> <user>padraic</user> <email>padraic.brady@yahoo.com</email> <active>yes</active> </lead> <date>2007-07-20</date> <time>13:36:39</time> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://opensource.org/licenses/bsd-license.php">New BSD License</license> <notes>* Added package dependencies for HTTP_Request and Validate</notes> <contents> <dir baseinstalldir="Services" name="/"> <file baseinstalldir="Services" md5sum="b29c85371999eb6b5fcad5ebfc47fe09" name="Yadis/Exception.php" role="php" /> <file baseinstalldir="Services" md5sum="75d58d429c1e0b35d8cff9e972ef22fc" name="Yadis/Service.php" role="php" /> <file baseinstalldir="Services" md5sum="a3bcd90b8670f35ecaa78bce0493c073" name="Yadis/Xrds.php" role="php" /> <file baseinstalldir="Services" md5sum="8e988048ec99e5b32053fe5cd0f5038e" name="Yadis/Xri.php" role="php" /> <file baseinstalldir="Services" md5sum="96a324e6951bfb5846c44980167a4142" name="Yadis/Xrds/Namespace.php" role="php" /> <file baseinstalldir="Services" md5sum="b148ae3e5b1a1ed5a30e87debfd34caa" name="Yadis/Xrds/Service.php" role="php" /> <file baseinstalldir="Services" md5sum="5da1b3a0748ab873f78408da96256702" name="Yadis.php" role="php" /> </dir> </contents> <dependencies> <required> <php> <min>5.1.4</min> </php> <pearinstaller> <min>1.4.0</min> </pearinstaller> <package> <name>HTTP_Request</name> <channel>pear.php.net</channel> </package> <package> <name>Validate</name> <channel>pear.php.net</channel> </package> </required> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.1.0a1</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-12</date> <license>New BSD License</license> <notes>* Initial build for PEAR</notes> </release> <release> <version> <release>0.1.0a2</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-13</date> <license>New BSD License</license> <notes>* Updated source code for Proposal comments. * Patched several operation breaking bugs and typos</notes> </release> <release> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-20</date> <license>New BSD License</license> <notes>* Added package dependencies for HTTP_Request and Validate</notes> </release> </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/test.php?view=markup&rev=1.1 Index: pear/Services_Yadis/test.php +++ pear/Services_Yadis/test.php <?php set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path()); require_once 'Services/Yadis.php'; $yadis = new Services_Yadis('http://padraic.astrumfutura.com'); $serviceList = $yadis->discover(); foreach ($serviceList as $service) { $types = $service->getTypes(); echo $types[0], ' at ', implode(', ', $service->getUris()), PHP_EOL; echo 'Priority is ', $service->getPriority(), PHP_EOL; } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Exception.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Exception.php +++ pear/Services_Yadis/Yadis/Exception.php <?php /** * Exception subclass of PEAR_Exception for Services_Yadis. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007, Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Exception.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ * @link http:// */ /** * PEAR_Exception */ require_once 'PEAR/Exception.php'; /** * Services_Yadis_Exception * * @category Services * @package Services_Yadis * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http:// * @version @package_version@ * @access public */ class Services_Yadis_Exception extends PEAR_Exception {} http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Service.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Service.php +++ pear/Services_Yadis/Yadis/Service.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Service.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** * The Services_Yadis_Service class represents a service parsed from the * XRD node of a Yadis 1.0 XRDS document. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis_Service { /** * Holds the Service node parsed from a Yadis XRDS document as a * SimpleXMLElement object. * * @var SimpleXMLElement */ protected $_serviceNode = null; /** * Holds the Service node parsed from a Yadis XRDS document as a * SimpleXMLElement object. * * @var SimpleXMLElement */ protected $_namespace = null; /** * Class constructor; initialise the object with a Service node from the * XRDS document, and the current Services_Yadis_Xrds_Namespace * object to provide the current namespaces for using XPath queries. * * @param SimpleXMLElement $serviceNode * @param Services_Yadis_Xrds_Namespace $namespace */ public function __construct(SimpleXMLElement $serviceNode, Services_Yadis_Xrds_Namespace $namespace) { $this->_serviceNode = $serviceNode; $this->_namespace = $namespace; $this->_namespace->registerXpathNamespaces($this->_serviceNode); } /** * Return an array of Service Type URI strings. This method will NOT * validate the resulting URIs. * * @return array */ public function getTypes() { return $this->getElements('xrd:Type'); } /** * Return an array of Service Type URI strings. This method will NOT * validate the resulting URIs. URI values in the array will have key * values matching their priority, and be ordered based on their * priority values - highest (i.e. lowest integer) priority first. * * @return array|boolean */ public function getUris() { return $this->getElements('xrd:URI'); } /** * Returns the Priority integer of this Service node. * * @return integer|boolean */ public function getPriority() { $attributes = $this->_serviceNode->attributes(); foreach($attributes as $attribute=>$value) { if($attribute == 'priority') { return intval($value); } } return false; } /** * Return the current XRDS Service node as a SimpleXMLElement object. * * @return SimpleXMLElement */ public function getSimpleXmlObject() { return $this->_serviceNode; } /** * Return the current XRDS Service node as a DOMDocument object. * This is just a simple transfer by loading the XML output from * the SimpleXMLElement object into a new DOMDocument instance. * * @return DOMDocument */ public function getDomObject() { return dom_import_simplexml($this->serviceNode); } /** * Return the current Services_Yadis_Xrds_Namespace object. * * @return Services_Yadis_Xrds_Namespace */ public function getNamespaceObject() { return $this->_namespace; } /** * Return an array of the current XRDS namespaces for working with any * XPath queries on the Service node. * * @return array */ public function getNamespaces() { return $this->_namespace->getNamespaces(); } /** * Retrieve Elements of the current Service node by their name, and return * as a SimpleXMLElement object. The Elements should be direct children of * the Service node. This method basically just passes the $element string * as an XPath query so it's open to other uses despite the assumed use * case. * * @param string $name * @return array|boolean */ public function getElements($name) { $return = array(); $elements = $this->_serviceNode->xpath($name); if (!is_array($elements) || count($elements) < 1) { return false; } foreach ($elements as $element) { $string = strval($element); if(!empty($string)) { $return[] = $string; } } return $return; } } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Xrds.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Xrds.php +++ pear/Services_Yadis/Yadis/Xrds.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Xrds.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** Services_Yadis_Xrds_Namespace */ require_once 'Services/Yadis/Xrds/Namespace.php'; /** * The Services_Yadis_Xrds class is a wrapper for elements of an * XRD document which is parsed using SimpleXML, and contains methods for * retrieving data about the document. The concrete aspects of retrieving * specific data elements is left to a concrete subclass. * * @category Services * @package Services_Yadis * @subpackage Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis_Xrds { /** * Current key/pointer for the Iterator * * @var integer */ protected $_currentKey = 0; /** * Contains the valid xrd:XRD nodes parsed from the XRD document. * * @var SimpleXMLElement */ protected $_xrdNodes = null; /** * Instance of Services_Yadis_Xrds_Namespace for managing namespaces * associated with an XRDS document. * * @var Services_Yadis_Xrds_Namespace */ protected $_namespace = null; /** * Constructor; parses and validates an XRD document. All access to * the data held in the XML is left to a concrete subclass specific to * expected XRD format and data types. * Cannot be directly instantiated; must call from subclass. * * @param SimpleXMLElement $xrds * @param Services_Yadis_Xrds_Namespace $namespace */ protected function __construct(SimpleXMLElement $xrds, Services_Yadis_Xrds_Namespace $namespace) { $this->_namespace = $namespace; $xrdNodes = $this->_getValidXrdNodes($xrds); if (!$xrdNodes) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('The XRD document was found to be invalid'); } $this->_xrdNodes = $xrdNodes; } /** * Add a list (array) of additional namespaces to be utilised by the XML * parser when it receives a valid XRD document. * * @param array $namespaces * @return Services_Yadis */ public function addNamespaces(array $namespaces) { $this->_namespace->addNamespaces($namespaces); return $this; } /** * Add a single namespace to be utilised by the XML parser when it receives * a valid XRD document. * * @param string $namespace * @param string $namespaceUrl * @return Services_Yadis */ public function addNamespace($namespace, $namespaceUrl) { $this->_namespace->addNamespace($namespace, $namespaceUrl); return $this; } /** * Return the value of a specific namespace. * * @return string|boolean */ public function getNamespace($namespace) { return $this->_namespace->getNamespace($namespace); } /** * Returns an array of all currently set namespaces. * * @return array */ public function getNamespaces() { return $this->_namespace->getNamespaces(); } /** * Returns an array of all xrd elements located in the XRD document. * * @param SimpleXMLElement * @return array */ protected function _getValidXrdNodes(SimpleXMLElement $xrds) { /** * Register all namespaces to this SimpleXMLElement. */ $this->_namespace->registerXpathNamespaces($xrds); /** * Verify the XRDS resource has a root element called "xrds:XRDS". */ $root = $xrds->xpath('/xrds:XRDS[1]'); if (count($root) == 0) { return null; } /** * Check namespace urls of standard xmlns (no suffix) or xmlns:xrd * (if present and of priority) for validity. * No loss if neither exists, but they really should be. */ $namespaces = $xrds->getDocNamespaces(); if (array_key_exists('xrd', $namespaces) && $namespaces['xrd'] != 'xri://$xrd*($v*2.0)') { return null; } elseif (array_key_exists('', $namespaces) && $namespaces[''] != 'xri://$xrd*($v*2.0)') { return null; } /** * Grab the XRD elements which contains details of the service provider's * Server url, service types, and other details. Concrete subclass may * have additional requirements concerning node priority or valid position * in relation to other nodes. E.g. Yadis requires only using the *last* * node. */ $xrdNodes = $xrds->xpath('/xrds:XRDS[1]/xrd:XRD'); if (!$xrdNodes) { return null; } return $xrdNodes; } /** * Order an array of elements by priority. This assumes an array form of: * $array[$priority] = <array of elements with equal priority> * Where multiple elements are assigned to a priority, their order in the * priority array should be made random. After ordering, the array is * flattened to a single array of elements for iteration. * * @param array $unsorted * @return array */ public static function sortByPriority(array $unsorted) { ksort($unsorted); $flattened = array(); foreach ($unsorted as $priority) { if (count($priority) > 1) { shuffle($priority); $flattened = array_merge($flattened, $priority); } else { $flattened[] = $priority[0]; } } return $flattened; } } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Xri.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Xri.php +++ pear/Services_Yadis/Yadis/Xri.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Xri.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** HTTP_Request */ require_once 'HTTP/Request.php'; /** Validate */ require_once 'Validate.php'; /** * Provides methods for translating an XRI into a URI. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis_Xri { /** * Hold an instance of this object per the Singleton Pattern. * * @var Services_Yadis_Xri */ protected static $_instance = null; /* * Array of characters which if found at the 0 index of a Yadis ID string * may indicate the use of an XRI. * * @var array */ protected $_xriIdentifiers = array( '=', '$', '!', '@', '+' ); /** * Default proxy to append XRI identifier to when forming a valid URI. * * @var string */ protected $_proxy = 'http://xri.net/'; /** * Instance of Services_Yadis_Xrds_Namespace for managing namespaces * associated with an XRDS document. * * @var Services_Yadis_Xrds_Namespace */ protected $_namespace = null; /** * The XRI string. * * @var string */ protected $_xri = null; /** * The URI as translated from an XRI and appended to a Proxy. * * @var string */ protected $_uri = null; /** * A Canonical ID if requested, and parsed from the XRDS document found * by requesting the URI created from a valid XRI. * * @var string */ protected $_canonicalId = null; protected $_httpRequestOptions = null; /** * Constructor; protected since this class is a singleton. */ protected function __construct() {} /** * Return a singleton instance of this class. * * @return Services_Yadis_Xri */ public static function getInstance() { if (is_null(self::$_instance)) { self::$_instance = new self; } return self::$_instance; } /** * Set a Namespace object which contains all relevant namespaces * for XPath queries on this Yadis resource. * * @param Services_Yadis_Xrds_Namespace * @return Services_Yadis_Xri */ public function setNamespace(Services_Yadis_Xrds_Namespace $namespace) { $this->_namespace = $namespace; return $this; } /** * Set an XRI proxy URI. A default of "http://xri.net/" is available. * * @param string $proxy * @return Services_Yadis_Xri * @throws Services_Yadis_Exception */ public function setProxy($proxy) { if (!Validate::uri($proxy)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid URI; unable to set as an XRI proxy'); } $this->_proxy = $proxy; return $this; } /** * Return the URI of the current proxy. * * @param string $proxy */ public function getProxy() { return $this->_proxy; } /** * Set an XRI to be translated to a URI. * * @param string $url * @return Services_Yadis_Xri * @throws Services_Yadis_Exception */ public function setXri($xri) { /** * Check if the passed string is a likely XRI. */ if (stripos($xri, 'xri://') === false && !in_array($xri[0], $this->_xriIdentifiers)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid XRI string submitted'); } $this->_xri = $xri; return $this; } /** * Return the original XRI string. * * @return string */ public function getXri() { return $this->_xri; } /** * Attempts to convert an XRI into a URI. In simple terms this involves * removing the "xri://" prefix and appending the remainder to the URI of * an XRI proxy such as "http://xri.net/". * * @param string $xri * @return string * @throws Services_Yadis_Exception * @uses Validate */ public function toUri($xri = null, $serviceType = null) { if (!is_null($serviceType)) { $this->_serviceType = (string) $serviceType; } if (isset($xri)) { $this->setXri($xri); } /** * Get rid of the xri:// prefix before assembling the URI * including any IP or DNS wildcards */ if (stripos($this->_xri, 'xri://') == 0) { if (stripos($this->_xri, 'xri://$ip*') == 0) { $iname = substr($xri, 10); } elseif (stripos($this->_xri, 'xri://$dns*') == 0) { $iname = substr($xri, 11); } else { $iname = substr($xri, 6); } } else { $iname = $xri; } $uri = $this->getProxy() . $iname; if (!Validate::uri($uri)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Unable to translate XRI to a valid URI using proxy: ' . $this->getProxy()); } $this->_uri = $uri; return $uri; } /** * Based on an XRI, will request the XRD document located at the proxy * prefixed URI and parse in search of the XRI Canonical Id. This is * a flexible requirement. OpenID 2.0 requires the use of the Canonical * ID instead of the raw i-name. 2idi.com, on the other hand, does not. * * @todo Imcomplete; requires interface from Yadis main class * @param string $xri * @return string * @throws Services_Yadis_Exception */ public function toCanonicalId($xri = null) { if (!isset($xri) && !isset($this->_uri)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('No XRI passed as parameter as required unless called after Services_Yadis_Xri:toUri'); } elseif (isset($xri)) { $uri = $this->toUri($xri); } else { $uri = $this->_uri; } $request = $this->_get($uri, $this->getHttpRequestOptions()); if (stripos($request->getResponseHeader('Content-Type'), 'application/xrds+xml') === false) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('The response header indicates the response body is not an XRDS document'); } $xrds = new SimpleXMLElement($request->getResponseBody()); $this->_namespace->registerXpathNamespaces($xrds); $this->_canonicalId = $xrds->xpath('/xrd:CanonicalID[last()]'); if (!$this->_canonicalId) { return false; } throw new Exception('Not yet implemented'); //var_dump($canonicalIds . __FILE__.__LINE__); exit; return $this->_canonicalId; } public function getCanonicalId() { if (!is_null($this->_canonicalId)) { return $this->_canonicalId; } if (is_null($this->_xri)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Unable to get a Canonical Id since no XRI value has been set'); } return $this->toCanonicalId($this->_xri); } /** * Set options to be passed to the PEAR HTTP_Request constructor * * @param array $options * @return void */ public function setHttpRequestOptions(array $options) { $this->_httpRequestOptions = $options; } /** * Get options to be passed to the PEAR HTTP_Request constructor * * @return array */ public function getHttpRequestOptions() { return $this->_httpRequestOptions; } /** * Required to request the root i-name (XRI) XRD which will provide an * error message that the i-name does not exist, or else return a valid * XRD document containing the i-name's Canonical ID. * * @param string $uri * @return HTTP_Request * @todo Finish this a bit better using the QXRI rules. */ protected function _get($url, $serviceType = null, array $options = null) { $request = new HTTP_Request($url, $options); $request->setMethod(HTTP_REQUEST_METHOD_GET); $request->addHeader('Accept', 'application/xrds+xml'); if ($serviceType) { $request->addQueryString('_xrd_r', 'application/xrds+xml'); $request->addQueryString('_xrd_t', $serviceType); } else { $request->addQueryString('_xrd_r', 'application/xrds+xml;sep=false'); } $response = $request->sendRequest(); if (PEAR::isError($response)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid response to Yadis protocol received: ' . $request->getResponseCode() . ' ' . $request->getResponseBody()); } return $request; } } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Xrds/Namespace.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Xrds/Namespace.php +++ pear/Services_Yadis/Yadis/Xrds/Namespace.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Namespace.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** Validate */ require_once 'Validate.php'; /** * The Services_Yadis_Xrds_Namespace class is a container for namespaces * which need to be registered to an XML parser in order to correctly consume * an XRDS document using the parser's XPath functionality. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis_Xrds_Namespace { /** * Default XRDS namespaces which should always be registered. * * @var array */ protected $_namespaces = array( 'xrds' => 'xri://$xrds', 'xrd' => 'xri://$xrd*($v*2.0)' ); /** * Add a list (array) of additional namespaces to be utilised by the XML * parser when it receives a valid XRD document. * * @param array $namespaces */ public function addNamespaces(array $namespaces) { foreach($namespaces as $namespaceKey=>$namespaceUrl) { $this->addNamespace($namespaceKey, $namespaceUrl); } } /** * Add a single namespace to be utilised by the XML parser when it receives * a valid XRD document. * * @param string $namespaceKey * @param string $namespaceUrl * @return void */ public function addNamespace($namespaceKey, $namespaceUrl) { if (!isset($namespaceKey) || !isset($namespaceUrl) || empty($namespaceKey) || empty($namespaceUrl)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Parameters must be non-empty strings'); } elseif (!Validate::uri($namespaceUrl)) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('Invalid namespace URI: ' . htmlentities($namespaceUrl, ENT_QUOTES, 'utf-8')); } elseif (array_key_exists($namespaceKey, $this->getNamespaces())) { require_once 'Services/Yadis/Exception.php'; throw new Services_Yadis_Exception('You may not redefine the "xrds" or "xrd" XML Namespaces'); } $this->_namespaces[$namespaceKey] = $namespaceUrl; } /** * Return the value of a specific namespace, or FALSE if not found. * * @param string $namespaceKey * @return string|boolean */ public function getNamespace($namespaceKey) { if (array_key_exists($namespaceKey, $this->_namespaces)) { return $this->_namespaces[$namespaceKey]; } return false; } /** * Returns an array of all currently set namespaces. * * @return array */ public function getNamespaces() { return $this->_namespaces; } /** * Register all stored namespaces to the parameter SimpleXMLElement object. * * @param SimpleXMLElement * @return void */ public function registerXpathNamespaces(SimpleXMLElement $element) { foreach ($this->_namespaces as $namespaceKey=>$namespaceUrl) { $element->registerXPathNamespace($namespaceKey, $namespaceUrl); } } } http://cvs.php.net/viewvc.cgi/pear/Services_Yadis/Yadis/Xrds/Service.php?view=markup&rev=1.1 Index: pear/Services_Yadis/Yadis/Xrds/Service.php +++ pear/Services_Yadis/Yadis/Xrds/Service.php <?php /** * Implementation of the Yadis Specification 1.0 protocol for service * discovery from an Identity URI/XRI or other. * * PHP version 5 * * LICENSE: * * Copyright (c) 2007 Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Service.php,v 1.1 2007/09/29 12:14:45 padraic Exp $ */ /** Services_Yadis_Xrds */ require_once 'Services/Yadis/Xrds.php'; /** Services_Yadis_Service */ require_once 'Services/Yadis/Service.php'; /** * The Services_Yadis_Xrds_Service class is a wrapper for Service elements * of an XRD document which is parsed using SimpleXML, and contains methods for * retrieving data about each Service, including Type, Url and other arbitrary * data added in a separate namespace, e.g. openid:Delegate for OpenID 1.1. * * This class extends the basic Services_Yadis_Xrds wrapper to implement a * Service object specific to the Yadis Specification 1.0. XRDS itself is not * an XML format ruled by Yadis, but by an OASIS proposal. * * @category Services * @package Services_Yadis * @author Pádraic Brady (http://blog.astrumfutura.com) * @license http://opensource.org/licenses/bsd-license.php New BSD License */ class Services_Yadis_Xrds_Service extends Services_Yadis_Xrds implements Iterator { /** * Establish a lowest priority integer; we'll take the upper 2^31 * integer limit. * Highest priority is 0. */ const SERVICE_LOWEST_PRIORITY = 2147483647; /** * Holds the last XRD node of the XRD document as required by Yadis 1.0. * * @var SimpleXMLElement */ protected $_xrdNode = null; /** * The Yadis Services resultset * * @var array */ protected $_services = array(); /** * Flag holding whether or not the array endpoint has been reached. * * @var boolean */ protected $_valid = true; /** * Constructor; Accepts an XRD document for parsing. * Parses the XRD document by <xrd:Service> element to construct an array * of Services_Yadis_Service objects ordered by their priority. * * @param SimpleXMLElement $xrds * @param Services_Yadis_Xrds_Namespace $namespace */ public function __construct(SimpleXMLElement $xrds, Services_Yadis_Xrds_Namespace $namespace) { parent::__construct($xrds, $namespace); /** * The Yadis Specification requires we only use the last xrd node. The * rest being ignored (if present for whatever reason). Important to * note when writing an XRD document for multiple services - put * the authentication service XRD node last. */ $this->_xrdNode = $this->_xrdNodes[count($this->_xrdNodes) - 1]; $this->_namespace->registerXpathNamespaces($this->_xrdNode); $services = $this->_xrdNode->xpath('xrd:Service'); foreach ($services as $service) { $serviceObj = new Services_Yadis_Service($service, $this->_namespace); $this->_addService($serviceObj); } $this->_services = Services_Yadis_Xrds::sortByPriority($this->_services); } /** * Implements Iterator::current() * * Return the current element. * * @return Services_Yadis_Service */ public function current() { return current($this->_services); } /** * Implements Iterator::key() * * Return the key of the current element. * * @return integer */ public function key() { return key($this->_services); } /** * Implements Iterator::next() * * Increments pointer to next Service object. * * @return void */ public function next() { $this->_valid = (false !== next($this->_services)); } /** * Implements Iterator::rewind() * * Rewinds the Iterator to the first Service object * * @return boolean */ public function rewind() { $this->_valid = (false !== reset($this->_services)); } /** * Implement Iterator::valid() * * @return boolean */ public function valid() { return $this->_valid; } /** * Add a service to the Service list indexed by priority. Assumes * a missing or invalid priority should be shuffled to the bottom * of the priority order. * * @param Services_Yadis_Service $service */ protected function _addService(Services_Yadis_Service $service) { $servicePriority = $service->getPriority(); if(is_null($servicePriority) || !is_numeric($servicePriority)) { $servicePriority = self::SERVICE_LOWEST_PRIORITY; } if (!array_key_exists($servicePriority, $this->_services)){ $this->_services[$servicePriority] = array(); } $this->_services[$servicePriority][] = $service; } }
« previous php.pear.cvs (#48700) next »