]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xrd.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / 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 class XRD
30 {
31     const XML_NS = 'http://www.w3.org/2000/xmlns/';
32
33     const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
34
35     const HOST_META_NS = 'http://host-meta.net/xrd/1.0';
36
37     public $expires;
38
39     public $subject;
40
41     public $host;
42
43     public $alias = array();
44
45     public $types = array();
46
47     public $links = array();
48
49     public static function parse($xml)
50     {
51         $xrd = new XRD();
52
53         $dom = new DOMDocument();
54
55         // Don't spew XML warnings to output
56         $old = error_reporting();
57         error_reporting($old & ~E_WARNING);
58         $ok = $dom->loadXML($xml);
59         error_reporting($old);
60
61         if (!$ok) {
62             // TRANS: Exception.
63             throw new Exception(_('Invalid XML.'));
64         }
65         $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
66         if (!$xrd_element) {
67             // TRANS: Exception.
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         $xs = new XMLStringer();
110
111         $xs->startXML();
112         $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS));
113
114         if ($this->host) {
115             $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host);
116         }
117
118         if ($this->expires) {
119             $xs->element('Expires', null, $this->expires);
120         }
121
122         if ($this->subject) {
123             $xs->element('Subject', null, $this->subject);
124         }
125
126         foreach ($this->alias as $alias) {
127             $xs->element('Alias', null, $alias);
128         }
129
130         foreach ($this->links as $link) {
131             $titles = array();
132             $properties = array();
133             if (isset($link['title'])) {
134                 $titles = $link['title'];
135                 unset($link['title']);
136             }
137             if (isset($link['property'])) {
138                 $properties = $link['property'];
139                 unset($link['property']);
140             }
141             $xs->elementStart('Link', $link);
142             foreach ($titles as $title) {
143                 $xs->element('Title', null, $title);
144             }
145             foreach ($properties as $property) {
146                 $xs->element('Property',
147                              array('type' => $property['type']),
148                              $property['value']);
149             }
150             $xs->elementEnd('Link');
151         }
152
153         $xs->elementEnd('XRD');
154
155         return $xs->getString();
156     }
157
158     function parseType($element)
159     {
160         return array();
161     }
162
163     function parseLink($element)
164     {
165         $link = array();
166         $link['rel'] = $element->getAttribute('rel');
167         $link['type'] = $element->getAttribute('type');
168         $link['href'] = $element->getAttribute('href');
169         $link['template'] = $element->getAttribute('template');
170         foreach ($element->childNodes as $node) {
171             if ($node instanceof DOMElement) {
172                 switch($node->tagName) {
173                 case 'Title':
174                     $link['title'][] = $node->nodeValue;
175                     break;
176                 case 'Property':
177                     $link['property'][] = array('type' => $node->getAttribute('type'),
178                                                 'value' => $node->nodeValue);
179                     break;
180                 default:
181                     common_log(LOG_NOTICE, "Unexpected tag name {$node->tagName} found in XRD file.");
182                 }
183             }
184         }
185
186         return $link;
187     }
188 }