]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/xrd.php
* i18n/L10n update
[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 class XRD
31 {
32     const XML_NS = 'http://www.w3.org/2000/xmlns/';
33
34     const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
35
36     const HOST_META_NS = 'http://host-meta.net/xrd/1.0';
37
38     public $expires;
39
40     public $subject;
41
42     public $host;
43
44     public $alias = array();
45
46     public $types = array();
47
48     public $links = array();
49
50     public static function parse($xml)
51     {
52         $xrd = new XRD();
53
54         $dom = new DOMDocument();
55
56         // Don't spew XML warnings to output
57         $old = error_reporting();
58         error_reporting($old & ~E_WARNING);
59         $ok = $dom->loadXML($xml);
60         error_reporting($old);
61
62         if (!$ok) {
63             // TRANS: Exception.
64             throw new Exception(_m('Invalid XML.'));
65         }
66         $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
67         if (!$xrd_element) {
68             // TRANS: Exception.
69             throw new Exception(_m('Invalid XML, missing XRD root.'));
70         }
71
72         // Check for host-meta host
73         $host = $xrd_element->getElementsByTagName('Host')->item(0);
74         if ($host) {
75             $xrd->host = $host->nodeValue;
76         }
77
78         // Loop through other elements
79         foreach ($xrd_element->childNodes as $node) {
80             if (!($node instanceof DOMElement)) {
81                 continue;
82             }
83             switch ($node->tagName) {
84             case 'Expires':
85                 $xrd->expires = $node->nodeValue;
86                 break;
87             case 'Subject':
88                 $xrd->subject = $node->nodeValue;
89                 break;
90
91             case 'Alias':
92                 $xrd->alias[] = $node->nodeValue;
93                 break;
94
95             case 'Link':
96                 $xrd->links[] = $xrd->parseLink($node);
97                 break;
98
99             case 'Type':
100                 $xrd->types[] = $xrd->parseType($node);
101                 break;
102
103             }
104         }
105         return $xrd;
106     }
107
108     public function toXML()
109     {
110         $xs = new XMLStringer();
111
112         $xs->startXML();
113         $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS));
114
115         if ($this->host) {
116             $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host);
117         }
118
119         if ($this->expires) {
120             $xs->element('Expires', null, $this->expires);
121         }
122
123         if ($this->subject) {
124             $xs->element('Subject', null, $this->subject);
125         }
126
127         foreach ($this->alias as $alias) {
128             $xs->element('Alias', null, $alias);
129         }
130
131         foreach ($this->links as $link) {
132             $titles = array();
133             if (isset($link['title'])) {
134                 $titles = $link['title'];
135                 unset($link['title']);
136             }
137             $xs->elementStart('Link', $link);
138             foreach ($titles as $title) {
139                 $xs->element('Title', null, $title);
140             }
141             $xs->elementEnd('Link');
142         }
143
144         $xs->elementEnd('XRD');
145
146         return $xs->getString();
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 }