3 * This file is part of libomb
7 * LICENSE: This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @author Adrian Lang <mail@adrianlang.de>
22 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
23 * @version 0.1a-20090828
24 * @link http://adrianlang.de/libomb
27 require_once 'Auth/Yadis/Yadis.php';
28 require_once 'unsupportedserviceexception.php';
29 require_once 'invalidyadisexception.php';
32 * OMB XRDS representation
34 * This class represents a Yadis XRDS file for OMB. It adds some useful methods to
37 class OMB_Yadis_XRDS extends Auth_Yadis_XRDS
43 * Create an instance from URL
45 * Constructs an OMB_Yadis_XRDS object from a given URL. A full Yadis
46 * discovery is performed on the URL and the XRDS is parsed.
47 * Throws an OMB_InvalidYadisException when no Yadis is discovered or the
48 * detected XRDS file is broken.
50 * @param string $url The URL on which Yadis discovery
51 * should be performed on
52 * @param Auth_Yadis_HTTPFetcher $fetcher A fetcher used to get HTTP
57 * @return OMB_Yadis_XRDS The initialized object representing the given
60 public static function fromYadisURL($url, $fetcher)
62 /* Perform a Yadis discovery. */
63 $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
65 throw new OMB_InvalidYadisException($url);
68 /* Parse the XRDS file. */
69 $xrds = OMB_Yadis_XRDS::parseXRDS($yadis->response_text);
71 throw new OMB_InvalidYadisException($url);
73 $xrds->fetcher = $fetcher;
78 * Get a specific service
80 * Returns the Auth_Yadis_Service object corresponding to the given service
82 * Throws an OMB_UnsupportedServiceException if the service is not
85 * @param string $service URI specifier of the requested service
89 * @return Auth_Yadis_Service The object representing the requested service
91 public function getService($service)
93 $match = $this->services(array(create_function('$s',
94 "return in_array('$service', \$s->getTypes());")));
95 if ($match === array()) {
96 throw new OMB_UnsupportedServiceException($service);
104 * Returns the OMB_Yadis_XRDS object corresponding to the given URI.
105 * Throws an OMB_UnsupportedServiceException if the XRD is not available.
106 * Note that getXRD tries to resolve external XRD parts as well.
108 * @param string $uri URI specifier of the requested XRD
112 * @return OMB_Yadis_XRDS The object representing the requested XRD
114 public function getXRD($uri)
116 $nexthash = strpos($uri, '#');
117 if ($nexthash === false) {
118 throw new OMB_InvalidYadisException("‘$uri’ does not specify a " .
123 $cururi = substr($uri, 0, $nexthash);
124 $nexturi = substr($uri, $nexthash);
125 return OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)
129 $id = substr($uri, 1);
130 foreach ($this->allXrdNodes as $node) {
131 $attrs = $this->parser->attributes($node);
132 if (array_key_exists('xml:id', $attrs) && $attrs['xml:id'] == $id) {
133 /* Trick the constructor into thinking this is the only node. */
134 $bogus_nodes = array($node);
135 return new OMB_Yadis_XRDS($this->parser, $bogus_nodes);
138 throw new OMB_UnsupportedServiceException($uri);
142 * Parse an XML string containing a XRDS document
144 * Parses an XML string (XRDS document) and returns either an
145 * Auth_Yadis_XRDS object or null, depending on whether the XRDS XML is
147 * This method is just copy and paste from the parent class to select the
148 * correct constructor.
150 * @param string $xml_string An XRDS XML string
151 * @param array $extra_ns_map Additional namespace declarations
155 * @return mixed An instance of OMB_Yadis_XRDS or null,
156 * depending on the validity of $xml_string
158 public static function parseXRDS($xml_string, $extra_ns_map = null)
166 $parser = Auth_Yadis_getXMLParser();
168 $ns_map = Auth_Yadis_getNSMap();
170 if ($extra_ns_map && is_array($extra_ns_map)) {
171 $ns_map = array_merge($ns_map, $extra_ns_map);
174 if (!($parser && $parser->init($xml_string, $ns_map))) {
178 // Try to get root element.
179 $root = $parser->evalXPath('/xrds:XRDS[1]');
184 if (is_array($root)) {
188 $attrs = $parser->attributes($root);
190 if (array_key_exists('xmlns:xrd', $attrs) &&
191 $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
193 } else if (array_key_exists('xmlns', $attrs) &&
194 preg_match('/xri/', $attrs['xmlns']) &&
195 $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
199 // Get the last XRD node.
200 $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
206 $xrds = new OMB_Yadis_XRDS($parser, $xrd_nodes);