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/
33 const XML_NS = 'http://www.w3.org/2000/xmlns/';
35 const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
37 const HOST_META_NS = 'http://host-meta.net/xrd/1.0';
45 public $alias = array();
47 public $types = array();
49 public $links = array();
51 public static function parse($xml)
55 $dom = new DOMDocument();
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);
64 throw new Exception("Invalid XML");
66 $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
68 throw new Exception("Invalid XML, missing XRD root");
71 // Check for host-meta host
72 $host = $xrd_element->getElementsByTagName('Host')->item(0);
74 $xrd->host = $host->nodeValue;
77 // Loop through other elements
78 foreach ($xrd_element->childNodes as $node) {
79 if (!($node instanceof DOMElement)) {
82 switch ($node->tagName) {
84 $xrd->expires = $node->nodeValue;
87 $xrd->subject = $node->nodeValue;
91 $xrd->alias[] = $node->nodeValue;
95 $xrd->links[] = $xrd->parseLink($node);
99 $xrd->types[] = $xrd->parseType($node);
107 public function toXML()
109 $xs = new XMLStringer();
112 $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS));
115 $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host);
118 if ($this->expires) {
119 $xs->element('Expires', null, $this->expires);
122 if ($this->subject) {
123 $xs->element('Subject', null, $this->subject);
126 foreach ($this->alias as $alias) {
127 $xs->element('Alias', null, $alias);
130 foreach ($this->links as $link) {
132 if (isset($link['title'])) {
133 $titles = $link['title'];
134 unset($link['title']);
136 $xs->elementStart('Link', $link);
137 foreach ($titles as $title) {
138 $xs->element('Title', null, $title);
140 $xs->elementEnd('Link');
143 $xs->elementEnd('XRD');
145 return $xs->getString();
148 function parseType($element)
153 function parseLink($element)
156 $link['rel'] = $element->getAttribute('rel');
157 $link['type'] = $element->getAttribute('type');
158 $link['href'] = $element->getAttribute('href');
159 $link['template'] = $element->getAttribute('template');
160 foreach ($element->childNodes as $node) {
161 if ($node instanceof DOMElement) {
162 switch($node->tagName) {
164 $link['title'][] = $node->nodeValue;