3 require_once 'Auth/Yadis/Yadis.php';
4 require_once 'unsupportedserviceexception.php';
5 require_once 'invalidyadisexception.php';
8 * OMB XRDS representation
10 * This class represents a Yadis XRDS file for OMB. It adds some useful methods to
15 * LICENSE: This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Affero General Public License for more details.
25 * You should have received a copy of the GNU Affero General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * @author Adrian Lang <mail@adrianlang.de>
30 * @copyright 2009 Adrian Lang
31 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
34 class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
39 * Create an instance from URL
41 * Constructs an OMB_Yadis_XRDS object from a given URL. A full Yadis
42 * discovery is performed on the URL and the XRDS is parsed.
43 * Throws an OMB_InvalidYadisException when no Yadis is discovered or the
44 * detected XRDS file is broken.
46 * @param string $url The URL on which Yadis discovery
47 * should be performed on
48 * @param Auth_Yadis_HTTPFetcher $fetcher A fetcher used to get HTTP
53 * @return OMB_Yadis_XRDS The initialized object representing the given
56 public static function fromYadisURL($url, $fetcher) {
57 /* Perform a Yadis discovery. */
58 $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
60 throw new OMB_InvalidYadisException($url);
63 /* Parse the XRDS file. */
64 $xrds = OMB_Yadis_XRDS::parseXRDS($yadis->response_text);
66 throw new OMB_InvalidYadisException($url);
68 $xrds->fetcher = $fetcher;
73 * Get a specific service
75 * Returns the Auth_Yadis_Service object corresponding to the given service
77 * Throws an OMB_UnsupportedServiceException if the service is not available.
79 * @param string $service URI specifier of the requested service
83 * @return Auth_Yadis_Service The object representing the requested service
85 public function getService($service) {
86 $match = $this->services(array( create_function('$s',
87 "return in_array('$service', \$s->getTypes());")));
88 if ($match === array()) {
89 throw new OMB_UnsupportedServiceException($service);
97 * Returns the OMB_Yadis_XRDS object corresponding to the given URI.
98 * Throws an OMB_UnsupportedServiceException if the XRD is not available.
99 * Note that getXRD tries to resolve external XRD parts as well.
101 * @param string $uri URI specifier of the requested XRD
105 * @return OMB_Yadis_XRDS The object representing the requested XRD
107 public function getXRD($uri) {
108 $nexthash = strpos($uri, '#');
109 if ($nexthash !== 0) {
110 if ($nexthash !== false) {
111 $cururi = substr($uri, 0, $nexthash);
112 $nexturi = substr($uri, $nexthash);
115 OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)->getXRD($nexturi);
118 $id = substr($uri, 1);
119 foreach ($this->allXrdNodes as $node) {
120 $attrs = $this->parser->attributes($node);
121 if (array_key_exists('xml:id', $attrs) && $attrs['xml:id'] == $id) {
122 /* Trick the constructor into thinking this is the only node. */
123 $bogus_nodes = array($node);
124 return new OMB_Yadis_XRDS($this->parser, $bogus_nodes);
127 throw new OMB_UnsupportedServiceException($uri);
131 * Parse an XML string containing a XRDS document
133 * Parse an XML string (XRDS document) and return either a
134 * Auth_Yadis_XRDS object or null, depending on whether the
136 * Copy and paste from parent to select correct constructor.
138 * @param string $xml_string An XRDS XML string.
142 * @return mixed An instance of OMB_Yadis_XRDS or null,
143 * depending on the validity of $xml_string
146 public function &parseXRDS($xml_string, $extra_ns_map = null) {
153 $parser = Auth_Yadis_getXMLParser();
155 $ns_map = Auth_Yadis_getNSMap();
157 if ($extra_ns_map && is_array($extra_ns_map)) {
158 $ns_map = array_merge($ns_map, $extra_ns_map);
161 if (!($parser && $parser->init($xml_string, $ns_map))) {
165 // Try to get root element.
166 $root = $parser->evalXPath('/xrds:XRDS[1]');
171 if (is_array($root)) {
175 $attrs = $parser->attributes($root);
177 if (array_key_exists('xmlns:xrd', $attrs) &&
178 $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
180 } else if (array_key_exists('xmlns', $attrs) &&
181 preg_match('/xri/', $attrs['xmlns']) &&
182 $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
186 // Get the last XRD node.
187 $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
193 $xrds = new OMB_Yadis_XRDS($parser, $xrd_nodes);