3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
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.
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.
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/>.
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/
32 const XML_NS = 'http://www.w3.org/2000/xmlns/';
34 const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
36 const HOST_META_NS = 'http://host-meta.net/xrd/1.0';
44 public $alias = array();
46 public $types = array();
48 public $links = array();
50 public static function parse($xml)
54 $dom = new DOMDocument();
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);
64 throw new Exception(_m('Invalid XML.'));
66 $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
69 throw new Exception(_m('Invalid XML, missing XRD root.'));
72 // Check for host-meta host
73 $host = $xrd_element->getElementsByTagName('Host')->item(0);
75 $xrd->host = $host->nodeValue;
78 // Loop through other elements
79 foreach ($xrd_element->childNodes as $node) {
80 if (!($node instanceof DOMElement)) {
83 switch ($node->tagName) {
85 $xrd->expires = $node->nodeValue;
88 $xrd->subject = $node->nodeValue;
92 $xrd->alias[] = $node->nodeValue;
96 $xrd->links[] = $xrd->parseLink($node);
100 $xrd->types[] = $xrd->parseType($node);
108 public function toXML()
110 $xs = new XMLStringer();
113 $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS));
116 $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host);
119 if ($this->expires) {
120 $xs->element('Expires', null, $this->expires);
123 if ($this->subject) {
124 $xs->element('Subject', null, $this->subject);
127 foreach ($this->alias as $alias) {
128 $xs->element('Alias', null, $alias);
131 foreach ($this->links as $link) {
133 if (isset($link['title'])) {
134 $titles = $link['title'];
135 unset($link['title']);
137 $xs->elementStart('Link', $link);
138 foreach ($titles as $title) {
139 $xs->element('Title', null, $title);
141 $xs->elementEnd('Link');
144 $xs->elementEnd('XRD');
146 return $xs->getString();
149 function parseType($element)
154 function parseLink($element)
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) {
165 $link['title'][] = $node->nodeValue;