]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/libomb/omb_yadis_xrds.php
89921203b0a377c07d98ababa7ff4ad5b7befed6
[quix0rs-gnu-social.git] / extlib / libomb / omb_yadis_xrds.php
1 <?php
2
3 require_once 'Auth/Yadis/Yadis.php';
4 require_once 'unsupportedserviceexception.php';
5 require_once 'invalidyadisexception.php';
6
7 /**
8  * OMB XRDS representation
9  *
10  * This class represents a Yadis XRDS file for OMB. It adds some useful methods to
11  * Auth_Yadis_XRDS.
12  *
13  * PHP version 5
14  *
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.
19  *
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.
24  *
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/>.
27  *
28  * @package   OMB
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
32  **/
33
34 class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
35
36   protected $fetcher;
37
38   /**
39    * Create an instance from URL
40    *
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.
45    *
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
49    *                                        resources
50    *
51    * @access public
52    *
53    * @return OMB_Yadis_XRDS The initialized object representing the given
54    *                        resource
55    **/
56   public static function fromYadisURL($url, $fetcher) {
57     /* Perform a Yadis discovery. */
58     $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
59     if ($yadis->failed) {
60       throw new OMB_InvalidYadisException($url);
61     }
62
63     /* Parse the XRDS file. */
64     $xrds = OMB_Yadis_XRDS::parseXRDS($yadis->response_text);
65     if ($xrds === null) {
66       throw new OMB_InvalidYadisException($url);
67     }
68     $xrds->fetcher = $fetcher;
69     return $xrds;
70   }
71
72   /**
73    * Get a specific service
74    *
75    * Returns the Auth_Yadis_Service object corresponding to the given service
76    * URI.
77    * Throws an OMB_UnsupportedServiceException if the service is not available.
78    *
79    * @param string $service URI specifier of the requested service
80    *
81    * @access public
82    *
83    * @return Auth_Yadis_Service The object representing the requested service
84    **/
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);
90     }
91     return $match[0];
92   }
93
94   /**
95    * Get a specific XRD
96    *
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.
100    *
101    * @param string $uri URI specifier of the requested XRD
102    *
103    * @access public
104    *
105    * @return OMB_Yadis_XRDS The object representing the requested XRD
106    **/
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);
113       }
114       return
115         OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)->getXRD($nexturi);
116     }
117
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);
125       }
126     }
127     throw new OMB_UnsupportedServiceException($uri);
128   }
129
130   /**
131    * Parse an XML string containing a XRDS document
132    *
133    * Parse an XML string (XRDS document) and return either a
134    * Auth_Yadis_XRDS object or null, depending on whether the
135    * XRDS XML is valid.
136    * Copy and paste from parent to select correct constructor.
137    *
138    * @param string $xml_string An XRDS XML string.
139    *
140    * @access public
141    *
142    * @return mixed An instance of OMB_Yadis_XRDS or null,
143    *               depending on the validity of $xml_string
144    **/
145
146   public function &parseXRDS($xml_string, $extra_ns_map = null) {
147     $_null = null;
148
149     if (!$xml_string) {
150       return $_null;
151     }
152
153     $parser = Auth_Yadis_getXMLParser();
154
155     $ns_map = Auth_Yadis_getNSMap();
156
157     if ($extra_ns_map && is_array($extra_ns_map)) {
158       $ns_map = array_merge($ns_map, $extra_ns_map);
159     }
160
161     if (!($parser && $parser->init($xml_string, $ns_map))) {
162       return $_null;
163     }
164
165     // Try to get root element.
166     $root = $parser->evalXPath('/xrds:XRDS[1]');
167     if (!$root) {
168       return $_null;
169     }
170
171     if (is_array($root)) {
172       $root = $root[0];
173     }
174
175     $attrs = $parser->attributes($root);
176
177     if (array_key_exists('xmlns:xrd', $attrs) &&
178           $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
179       return $_null;
180     } else if (array_key_exists('xmlns', $attrs) &&
181                    preg_match('/xri/', $attrs['xmlns']) &&
182                    $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
183       return $_null;
184     }
185
186     // Get the last XRD node.
187     $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
188
189     if (!$xrd_nodes) {
190       return $_null;
191     }
192
193     $xrds = new OMB_Yadis_XRDS($parser, $xrd_nodes);
194     return $xrds;
195   }
196 }