]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LRDD/extlib/XML/XRD.php
207d0ae3c5146d24e673594ff6b2040de5466e1d
[quix0rs-gnu-social.git] / plugins / LRDD / extlib / XML / XRD.php
1 <?php
2 /**
3  * Part of XML_XRD
4  *
5  * PHP version 5
6  *
7  * @category XML
8  * @package  XML_XRD
9  * @author   Christian Weiske <cweiske@php.net>
10  * @license  http://www.gnu.org/copyleft/lesser.html LGPL
11  * @link     http://pear.php.net/package/XML_XRD
12  */
13
14 require_once 'XML/XRD/PropertyAccess.php';
15 require_once 'XML/XRD/Element/Link.php';
16 require_once 'XML/XRD/Loader.php';
17 require_once 'XML/XRD/Serializer.php';
18
19 /**
20  * Main class used to load XRD documents from string or file.
21  *
22  * After loading the file, access to links is possible with get() and getAll(),
23  * as well as foreach-iterating over the XML_XRD object.
24  *
25  * Property access is possible with getProperties() and array access (foreach)
26  * on the XML_XRD object.
27  *
28  * Verification that the subject/aliases match the requested URL can be done with
29  * describes().
30  *
31  * @category XML
32  * @package  XML_XRD
33  * @author   Christian Weiske <cweiske@php.net>
34  * @license  http://www.gnu.org/copyleft/lesser.html LGPL
35  * @version  Release: @package_version@
36  * @link     http://pear.php.net/package/XML_XRD
37  */
38 class XML_XRD extends XML_XRD_PropertyAccess implements IteratorAggregate
39 {
40     /**
41      * XRD file/string loading dispatcher
42      *
43      * @var XML_XRD_Loader
44      */
45     public $loader;
46
47     /**
48      * XRD serializing dispatcher
49      *
50      * @var XML_XRD_Serializer
51      */
52     public $serializer;
53
54     /**
55      * XRD subject
56      *
57      * @var string
58      */
59     public $subject;
60
61     /**
62      * Array of subject alias strings
63      *
64      * @var array
65      */
66     public $aliases = array();
67
68     /**
69      * Array of link objects
70      *
71      * @var array
72      */
73     public $links = array();
74
75     /**
76      * Unix timestamp when the document expires.
77      * NULL when no expiry date set.
78      *
79      * @var integer|null
80      */
81     public $expires;
82
83     /**
84      * xml:id of the XRD document
85      *
86      * @var string|null
87      */
88     public $id;
89
90
91
92     /**
93      * Loads the contents of the given file.
94      *
95      * Note: Only use file type auto-detection for local files.
96      * Do not use it on remote files as the file gets requested several times.
97      *
98      * @param string $file Path to an XRD file
99      * @param string $type File type: xml or json, NULL for auto-detection
100      *
101      * @return void
102      *
103      * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
104      *                                   loaded
105      */
106     public function loadFile($file, $type = null)
107     {
108         if (!isset($this->loader)) {
109             $this->loader = new XML_XRD_Loader($this);
110         }
111         return $this->loader->loadFile($file, $type);
112     }
113
114     /**
115      * Loads the contents of the given string
116      *
117      * @param string $str  XRD string
118      * @param string $type File type: xml or json, NULL for auto-detection
119      *
120      * @return void
121      *
122      * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
123      *                                   loaded
124      */
125     public function loadString($str, $type = null)
126     {
127         if (!isset($this->loader)) {
128             $this->loader = new XML_XRD_Loader($this);
129         }
130         return $this->loader->loadString($str, $type);
131     }
132
133     /**
134      * Checks if the XRD document describes the given URI.
135      *
136      * This should always be used to make sure the XRD file
137      * is the correct one for e.g. the given host, and not a copycat.
138      *
139      * Checks against the subject and aliases
140      *
141      * @param string $uri An URI that the document is expected to describe
142      *
143      * @return boolean True or false
144      */
145     public function describes($uri)
146     {
147         if ($this->subject == $uri) {
148             return true;
149         }
150         foreach ($this->aliases as $alias) {
151             if ($alias == $uri) {
152                 return true;
153             }
154         }
155
156         return false;
157     }
158
159     /**
160      * Get the link with highest priority for the given relation and type.
161      *
162      * @param string  $rel          Relation name
163      * @param string  $type         MIME Type
164      * @param boolean $typeFallback When true and no link with the given type
165      *                              could be found, the best link without a
166      *                              type will be returned
167      *
168      * @return XML_XRD_Element_Link Link object or NULL if none found
169      */
170     public function get($rel, $type = null, $typeFallback = true)
171     {
172         $links = $this->getAll($rel, $type, $typeFallback);
173         if (count($links) == 0) {
174             return null;
175         }
176
177         return $links[0];
178     }
179
180
181     /**
182      * Get all links with the given relation and type, highest priority first.
183      *
184      * @param string  $rel          Relation name
185      * @param string  $type         MIME Type
186      * @param boolean $typeFallback When true and no link with the given type
187      *                              could be found, the best link without a
188      *                              type will be returned
189      *
190      * @return array Array of XML_XRD_Element_Link objects
191      */
192     public function getAll($rel, $type = null, $typeFallback = true)
193     {
194         $links = array();
195         $exactType = false;
196         foreach ($this->links as $link) {
197             if ($link->rel == $rel
198                 && ($type === null || $link->type == $type
199                 || $typeFallback && $link->type === null)
200             ) {
201                 $links[]    = $link;
202                 $exactType |= $typeFallback && $type !== null
203                     && $link->type == $type;
204             }
205         }
206         if ($exactType) {
207             //remove all links without type
208             $exactlinks = array();
209             foreach ($links as $link) {
210                 if ($link->type !== null) {
211                     $exactlinks[] = $link;
212                 }
213             }
214             $links = $exactlinks;
215         }
216         return $links;
217     }
218
219     /**
220      * Return the iterator object to loop over the links
221      *
222      * Part of the IteratorAggregate interface
223      *
224      * @return Traversable Iterator for the links
225      */
226     public function getIterator()
227     {
228         return new ArrayIterator($this->links);
229     }
230
231     /**
232      * Converts this XRD object to XML or JSON.
233      *
234      * @param string $type Serialization type: xml or json
235      *
236      * @return string Generated content
237      */
238     public function to($type)
239     {
240         if (!isset($this->serializer)) {
241             $this->serializer = new XML_XRD_Serializer($this);
242         }
243         return $this->serializer->to($type);
244     }
245
246     /**
247      * Converts this XRD object to XML.
248      *
249      * @return string Generated XML
250      *
251      * @deprecated use to('xml')
252      */
253     public function toXML()
254     {
255         return $this->to('xml');
256     }
257 }
258 ?>