]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/xrd.php
85df26c54cb752605c9babb4bccd4ef65f690085
[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         if (!$dom->loadXML($xml)) {
57             throw new Exception("Invalid XML");
58         }
59         $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
60
61         // Check for host-meta host
62         $host = $xrd_element->getElementsByTagName('Host')->item(0);
63         if ($host) {
64             $xrd->host = $host->nodeValue;
65         }
66
67         // Loop through other elements
68         foreach ($xrd_element->childNodes as $node) {
69             if (!($node instanceof DOMElement)) {
70                 continue;
71             }
72             switch ($node->tagName) {
73             case 'Expires':
74                 $xrd->expires = $node->nodeValue;
75                 break;
76             case 'Subject':
77                 $xrd->subject = $node->nodeValue;
78                 break;
79                 
80             case 'Alias':
81                 $xrd->alias[] = $node->nodeValue;
82                 break;
83
84             case 'Link':
85                 $xrd->links[] = $xrd->parseLink($node);
86                 break;
87
88             case 'Type':
89                 $xrd->types[] = $xrd->parseType($node);
90                 break;
91
92             }
93         }
94         return $xrd;
95     }
96
97     public function toXML()
98     {
99         $dom = new DOMDocument('1.0', 'UTF-8');
100         $dom->formatOutput = true;
101         
102         $xrd_dom = $dom->createElementNS(XRD::XRD_NS, 'XRD');
103         $dom->appendChild($xrd_dom);
104
105         if ($this->host) {
106             $host_dom = $dom->createElement('hm:Host', $this->host);
107             $xrd_dom->setAttributeNS(XRD::XML_NS, 'xmlns:hm', XRD::HOST_META_NS);
108             $xrd_dom->appendChild($host_dom);
109         }
110         
111                 if ($this->expires) {
112                         $expires_dom = $dom->createElement('Expires', $this->expires);
113                         $xrd_dom->appendChild($expires_dom);
114                 }
115
116                 if ($this->subject) {
117                         $subject_dom = $dom->createElement('Subject', $this->subject);
118                         $xrd_dom->appendChild($subject_dom);
119                 }
120
121                 foreach ($this->alias as $alias) {
122                         $alias_dom = $dom->createElement('Alias', $alias);
123                         $xrd_dom->appendChild($alias_dom);
124                 }
125
126                 foreach ($this->types as $type) {
127                         $type_dom = $dom->createElement('Type', $type);
128                         $xrd_dom->appendChild($type_dom);
129                 }
130
131                 foreach ($this->links as $link) {
132                         $link_dom = $this->saveLink($dom, $link);
133                         $xrd_dom->appendChild($link_dom);
134                 }
135
136         return $dom->saveXML();
137     }
138
139     function parseType($element)
140     {
141         return array();
142     }
143     
144     function parseLink($element)
145     {
146         $link = array();
147         $link['rel'] = $element->getAttribute('rel');
148         $link['type'] = $element->getAttribute('type');
149         $link['href'] = $element->getAttribute('href');
150         $link['template'] = $element->getAttribute('template');
151         foreach ($element->childNodes as $node) {
152             switch($node->tagName) {
153             case 'Title':
154                 $link['title'][] = $node->nodeValue;
155             }
156         }
157
158         return $link;
159     }
160
161     function saveLink($doc, $link)
162     {
163         $link_element = $doc->createElement('Link');
164         if (!empty($link['rel'])) {
165             $link_element->setAttribute('rel', $link['rel']);
166         }
167         if (!empty($link['type'])) {
168             $link_element->setAttribute('type', $link['type']);
169         }
170         if (!empty($link['href'])) {
171             $link_element->setAttribute('href', $link['href']);
172         }
173         if (!empty($link['template'])) {
174             $link_element->setAttribute('template', $link['template']);
175         }
176
177         if (!empty($link['title']) && is_array($link['title'])) {
178             foreach($link['title'] as $title) {
179                 $title = $doc->createElement('Title', $title);
180                 $link_element->appendChild($title);
181             }
182         }
183
184         
185         return $link_element;
186     }
187 }
188