]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/extlib/libomb/omb_yadis_xrds.php
Squashed commit of the following:
[quix0rs-gnu-social.git] / plugins / OMB / extlib / libomb / omb_yadis_xrds.php
1 <?php
2 /**
3  * This file is part of libomb
4  *
5  * PHP version 5
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @package OMB
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
25  */
26
27 require_once 'Auth/Yadis/Yadis.php';
28 require_once 'unsupportedserviceexception.php';
29 require_once 'invalidyadisexception.php';
30
31 /**
32  * OMB XRDS representation
33  *
34  * This class represents a Yadis XRDS file for OMB. It adds some useful methods to
35  * Auth_Yadis_XRDS.
36  */
37 class OMB_Yadis_XRDS extends Auth_Yadis_XRDS
38 {
39
40     protected $fetcher;
41
42     /**
43      * Create an instance from URL
44      *
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.
49      *
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
53      *                                        resources
54      *
55      * @access public
56      *
57      * @return OMB_Yadis_XRDS The initialized object representing the given
58      *                        resource
59      */
60     public static function fromYadisURL($url, $fetcher)
61     {
62         /* Perform a Yadis discovery. */
63         $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
64         if ($yadis->failed) {
65             throw new OMB_InvalidYadisException($url);
66         }
67
68         /* Parse the XRDS file. */
69         $xrds = OMB_Yadis_XRDS::parseXRDS($yadis->response_text);
70         if ($xrds === null) {
71             throw new OMB_InvalidYadisException($url);
72         }
73         $xrds->fetcher = $fetcher;
74         return $xrds;
75     }
76
77     /**
78      * Get a specific service
79      *
80      * Returns the Auth_Yadis_Service object corresponding to the given service
81      * URI.
82      * Throws an OMB_UnsupportedServiceException if the service is not
83      * available.
84      *
85      * @param string $service URI specifier of the requested service
86      *
87      * @access public
88      *
89      * @return Auth_Yadis_Service The object representing the requested service
90      */
91     public function getService($service)
92     {
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);
97         }
98         return $match[0];
99     }
100
101     /**
102      * Get a specific XRD
103      *
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.
107      *
108      * @param string $uri URI specifier of the requested XRD
109      *
110      * @access public
111      *
112      * @return OMB_Yadis_XRDS The object representing the requested XRD
113      */
114     public function getXRD($uri)
115     {
116         $nexthash = strpos($uri, '#');
117         if ($nexthash === false) {
118             throw new OMB_InvalidYadisException("‘$uri’ does not specify a " .
119                                                 'valid XML node.');
120         }
121
122         if ($nexthash > 0) {
123             $cururi  = substr($uri, 0, $nexthash);
124             $nexturi = substr($uri, $nexthash);
125             return OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)
126                                                ->getXRD($nexturi);
127         }
128
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);
136             }
137         }
138         throw new OMB_UnsupportedServiceException($uri);
139     }
140
141     /**
142      * Parse an XML string containing a XRDS document
143      *
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
146      * valid.
147      * This method is just copy and paste from the parent class to select the
148      * correct constructor.
149      *
150      * @param string $xml_string   An XRDS XML string
151      * @param array  $extra_ns_map Additional namespace declarations
152      *
153      * @access public
154      *
155      * @return mixed An instance of OMB_Yadis_XRDS or null,
156      *               depending on the validity of $xml_string
157      */
158     public static function parseXRDS($xml_string, $extra_ns_map = null)
159     {
160         $_null = null;
161
162         if (!$xml_string) {
163             return $_null;
164         }
165
166         $parser = Auth_Yadis_getXMLParser();
167
168         $ns_map = Auth_Yadis_getNSMap();
169
170         if ($extra_ns_map && is_array($extra_ns_map)) {
171             $ns_map = array_merge($ns_map, $extra_ns_map);
172         }
173
174         if (!($parser && $parser->init($xml_string, $ns_map))) {
175             return $_null;
176         }
177
178         // Try to get root element.
179         $root = $parser->evalXPath('/xrds:XRDS[1]');
180         if (!$root) {
181             return $_null;
182         }
183
184         if (is_array($root)) {
185             $root = $root[0];
186         }
187
188         $attrs = $parser->attributes($root);
189
190         if (array_key_exists('xmlns:xrd', $attrs) &&
191                     $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
192             return $_null;
193         } else if (array_key_exists('xmlns', $attrs) &&
194                                      preg_match('/xri/', $attrs['xmlns']) &&
195                                      $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
196             return $_null;
197         }
198
199         // Get the last XRD node.
200         $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
201
202         if (!$xrd_nodes) {
203             return $_null;
204         }
205
206         $xrds = new OMB_Yadis_XRDS($parser, $xrd_nodes);
207         return $xrds;
208     }
209 }