]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/xrd.php
34b28790b7452b9bea3e74e70ed2425e8f80c4fe
[quix0rs-gnu-social.git] / plugins / OStatus / lib / xrd.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A sample module to show best practices for StatusNet plugins
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @package   StatusNet
24  * @author    James Walker <james@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  * @link      http://status.net/
28  */
29
30
31 class XRD
32 {
33     const XML_NS = 'http://www.w3.org/2000/xmlns/';
34     
35     const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
36
37     const HOST_META_NS = 'http://host-meta.net/xrd/1.0';
38     
39     public $expires;
40
41     public $subject;
42
43     public $host;
44
45     public $alias = array();
46     
47     public $types = array();
48     
49     public $links = array();
50     
51     public static function parse($xml)
52     {
53         $xrd = new XRD();
54
55         $dom = new DOMDocument();
56
57         // Don't spew XML warnings to output
58         $old = error_reporting();
59         error_reporting($old & ~E_WARNING);
60         $ok = $dom->loadXML($xml);
61         error_reporting($old);
62
63         if (!$ok) {
64             throw new Exception("Invalid XML");
65         }
66         $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
67         if (!$xrd_element) {
68             throw new Exception("Invalid XML, missing XRD root");
69         }
70
71         // Check for host-meta host
72         $host = $xrd_element->getElementsByTagName('Host')->item(0);
73         if ($host) {
74             $xrd->host = $host->nodeValue;
75         }
76
77         // Loop through other elements
78         foreach ($xrd_element->childNodes as $node) {
79             if (!($node instanceof DOMElement)) {
80                 continue;
81             }
82             switch ($node->tagName) {
83             case 'Expires':
84                 $xrd->expires = $node->nodeValue;
85                 break;
86             case 'Subject':
87                 $xrd->subject = $node->nodeValue;
88                 break;
89                 
90             case 'Alias':
91                 $xrd->alias[] = $node->nodeValue;
92                 break;
93
94             case 'Link':
95                 $xrd->links[] = $xrd->parseLink($node);
96                 break;
97
98             case 'Type':
99                 $xrd->types[] = $xrd->parseType($node);
100                 break;
101
102             }
103         }
104         return $xrd;
105     }
106
107     public function toXML()
108     {
109         $dom = new DOMDocument('1.0', 'UTF-8');
110         $dom->formatOutput = true;
111         
112         $xrd_dom = $dom->createElementNS(XRD::XRD_NS, 'XRD');
113         $dom->appendChild($xrd_dom);
114
115         if ($this->host) {
116             $host_dom = $dom->createElement('hm:Host', $this->host);
117             $xrd_dom->setAttributeNS(XRD::XML_NS, 'xmlns:hm', XRD::HOST_META_NS);
118             $xrd_dom->appendChild($host_dom);
119         }
120         
121                 if ($this->expires) {
122                         $expires_dom = $dom->createElement('Expires', $this->expires);
123                         $xrd_dom->appendChild($expires_dom);
124                 }
125
126                 if ($this->subject) {
127                         $subject_dom = $dom->createElement('Subject', $this->subject);
128                         $xrd_dom->appendChild($subject_dom);
129                 }
130
131                 foreach ($this->alias as $alias) {
132                         $alias_dom = $dom->createElement('Alias', $alias);
133                         $xrd_dom->appendChild($alias_dom);
134                 }
135
136                 foreach ($this->types as $type) {
137                         $type_dom = $dom->createElement('Type', $type);
138                         $xrd_dom->appendChild($type_dom);
139                 }
140
141                 foreach ($this->links as $link) {
142                         $link_dom = $this->saveLink($dom, $link);
143                         $xrd_dom->appendChild($link_dom);
144                 }
145
146         return $dom->saveXML();
147     }
148
149     function parseType($element)
150     {
151         return array();
152     }
153     
154     function parseLink($element)
155     {
156         $link = array();
157         $link['rel'] = $element->getAttribute('rel');
158         $link['type'] = $element->getAttribute('type');
159         $link['href'] = $element->getAttribute('href');
160         $link['template'] = $element->getAttribute('template');
161         foreach ($element->childNodes as $node) {
162             if ($node instanceof DOMElement) {
163                 switch($node->tagName) {
164                 case 'Title':
165                     $link['title'][] = $node->nodeValue;
166                 }
167             }
168         }
169
170         return $link;
171     }
172
173     function saveLink($doc, $link)
174     {
175         $link_element = $doc->createElement('Link');
176         if (!empty($link['rel'])) {
177             $link_element->setAttribute('rel', $link['rel']);
178         }
179         if (!empty($link['type'])) {
180             $link_element->setAttribute('type', $link['type']);
181         }
182         if (!empty($link['href'])) {
183             $link_element->setAttribute('href', $link['href']);
184         }
185         if (!empty($link['template'])) {
186             $link_element->setAttribute('template', $link['template']);
187         }
188
189         if (!empty($link['title']) && is_array($link['title'])) {
190             foreach($link['title'] as $title) {
191                 $title = $doc->createElement('Title', $title);
192                 $link_element->appendChild($title);
193             }
194         }
195
196         
197         return $link_element;
198     }
199 }
200