]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LRDD/extlib/XML/XRD/Loader/XML.php
Implemented WebFinger and replaced our XRD with PEAR XML_XRD
[quix0rs-gnu-social.git] / plugins / LRDD / extlib / XML / XRD / Loader / XML.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 /**
15  * Loads XRD data from an XML file
16  *
17  * @category XML
18  * @package  XML_XRD
19  * @author   Christian Weiske <cweiske@php.net>
20  * @license  http://www.gnu.org/copyleft/lesser.html LGPL
21  * @version  Release: @package_version@
22  * @link     http://pear.php.net/package/XML_XRD
23  */
24 class XML_XRD_Loader_XML
25 {
26     /**
27      * Data storage the XML data get loaded into
28      *
29      * @var XML_XRD
30      */
31     protected $xrd;
32
33     /**
34      * XRD 1.0 namespace
35      */
36     const NS_XRD = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
37
38
39
40     /**
41      * Init object with xrd object
42      *
43      * @param XML_XRD $xrd Data storage the XML data get loaded into
44      */
45     public function __construct(XML_XRD $xrd)
46     {
47         $this->xrd = $xrd;
48     }
49
50     /**
51      * Loads the contents of the given file
52      *
53      * @param string $file Path to an XRD file
54      *
55      * @return void
56      *
57      * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
58      *                                   loaded
59      */
60     public function loadFile($file)
61     {
62         $old = libxml_use_internal_errors(true);
63         $x = simplexml_load_file($file);
64         libxml_use_internal_errors($old);
65         if ($x === false) {
66             throw new XML_XRD_Loader_Exception(
67                 'Error loading XML file: ' . libxml_get_last_error()->message,
68                 XML_XRD_Loader_Exception::LOAD
69             );
70         }
71         return $this->load($x);
72     }
73
74     /**
75      * Loads the contents of the given string
76      *
77      * @param string $xml XML string
78      *
79      * @return void
80      *
81      * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
82      *                                   loaded
83      */
84     public function loadString($xml)
85     {
86         if ($xml == '') {
87             throw new XML_XRD_Loader_Exception(
88                 'Error loading XML string: string empty',
89                 XML_XRD_Loader_Exception::LOAD
90             );
91         }
92         $old = libxml_use_internal_errors(true);
93         $x = simplexml_load_string($xml);
94         libxml_use_internal_errors($old);
95         if ($x === false) {
96             throw new XML_XRD_Loader_Exception(
97                 'Error loading XML string: ' . libxml_get_last_error()->message,
98                 XML_XRD_Loader_Exception::LOAD
99             );
100         }
101         return $this->load($x);
102     }
103
104     /**
105      * Loads the XML element into the classes' data structures
106      *
107      * @param object $x XML element containing the whole XRD document
108      *
109      * @return void
110      *
111      * @throws XML_XRD_Loader_Exception When the XML is invalid
112      */
113     public function load(SimpleXMLElement $x)
114     {
115         $ns = $x->getDocNamespaces();
116         if ($ns[''] !== self::NS_XRD) {
117             throw new XML_XRD_Loader_Exception(
118                 'Wrong document namespace', XML_XRD_Loader_Exception::DOC_NS
119             );
120         }
121         if ($x->getName() != 'XRD') {
122             throw new XML_XRD_Loader_Exception(
123                 'XML root element is not "XRD"', XML_XRD_Loader_Exception::DOC_ROOT
124             );
125         }
126
127         if (isset($x->Subject)) {
128             $this->xrd->subject = (string)$x->Subject;
129         }
130         foreach ($x->Alias as $xAlias) {
131             $this->xrd->aliases[] = (string)$xAlias;
132         }
133
134         foreach ($x->Link as $xLink) {
135             $this->xrd->links[] = $this->loadLink($xLink);
136         }
137
138         $this->loadProperties($this->xrd, $x);
139
140         if (isset($x->Expires)) {
141             $this->xrd->expires = strtotime($x->Expires);
142         }
143
144         $xmlAttrs = $x->attributes('http://www.w3.org/XML/1998/namespace');
145         if (isset($xmlAttrs['id'])) {
146             $this->xrd->id = (string)$xmlAttrs['id'];
147         }
148     }
149
150     /**
151      * Loads the Property elements from XML
152      *
153      * @param object $store Data store where the properties get stored
154      * @param object $x     XML element
155      *
156      * @return boolean True when all went well
157      */
158     protected function loadProperties(
159         XML_XRD_PropertyAccess $store, SimpleXMLElement $x
160     ) {
161         foreach ($x->Property as $xProp) {
162             $store->properties[] = $this->loadProperty($xProp);
163         }
164     }
165
166     /**
167      * Create a link element object from XML element
168      *
169      * @param object $x XML link element
170      *
171      * @return XML_XRD_Element_Link Created link object
172      */
173     protected function loadLink(SimpleXMLElement $x)
174     {
175         $link = new XML_XRD_Element_Link();
176         foreach (array('rel', 'type', 'href', 'template') as $var) {
177             if (isset($x[$var])) {
178                 $link->$var = (string)$x[$var];
179             }
180         }
181
182         foreach ($x->Title as $xTitle) {
183             $xmlAttrs = $xTitle->attributes('http://www.w3.org/XML/1998/namespace');
184             $lang = '';
185             if (isset($xmlAttrs['lang'])) {
186                 $lang = (string)$xmlAttrs['lang'];
187             }
188             if (!isset($link->titles[$lang])) {
189                 $link->titles[$lang] = (string)$xTitle;
190             }
191         }
192         $this->loadProperties($link, $x);
193
194         return $link;
195     }
196
197     /**
198      * Create a property element object from XML element
199      *
200      * @param object $x XML property element
201      *
202      * @return XML_XRD_Element_Property Created link object
203      */
204     protected function loadProperty(SimpleXMLElement $x)
205     {
206         $prop = new XML_XRD_Element_Property();
207         if (isset($x['type'])) {
208             $prop->type = (string)$x['type'];
209         }
210         $s = (string)$x;
211         if ($s != '') {
212             $prop->value = $s;
213         }
214
215         return $prop;
216     }
217 }
218 ?>