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();
56 if (!$dom->loadXML($xml)) {
57 throw new Exception("Invalid XML");
59 $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
61 throw new Exception("Invalid XML, missing XRD root");
64 // Check for host-meta host
65 $host = $xrd_element->getElementsByTagName('Host')->item(0);
67 $xrd->host = $host->nodeValue;
70 // Loop through other elements
71 foreach ($xrd_element->childNodes as $node) {
72 if (!($node instanceof DOMElement)) {
75 switch ($node->tagName) {
77 $xrd->expires = $node->nodeValue;
80 $xrd->subject = $node->nodeValue;
84 $xrd->alias[] = $node->nodeValue;
88 $xrd->links[] = $xrd->parseLink($node);
92 $xrd->types[] = $xrd->parseType($node);
100 public function toXML()
102 $dom = new DOMDocument('1.0', 'UTF-8');
103 $dom->formatOutput = true;
105 $xrd_dom = $dom->createElementNS(XRD::XRD_NS, 'XRD');
106 $dom->appendChild($xrd_dom);
109 $host_dom = $dom->createElement('hm:Host', $this->host);
110 $xrd_dom->setAttributeNS(XRD::XML_NS, 'xmlns:hm', XRD::HOST_META_NS);
111 $xrd_dom->appendChild($host_dom);
114 if ($this->expires) {
115 $expires_dom = $dom->createElement('Expires', $this->expires);
116 $xrd_dom->appendChild($expires_dom);
119 if ($this->subject) {
120 $subject_dom = $dom->createElement('Subject', $this->subject);
121 $xrd_dom->appendChild($subject_dom);
124 foreach ($this->alias as $alias) {
125 $alias_dom = $dom->createElement('Alias', $alias);
126 $xrd_dom->appendChild($alias_dom);
129 foreach ($this->types as $type) {
130 $type_dom = $dom->createElement('Type', $type);
131 $xrd_dom->appendChild($type_dom);
134 foreach ($this->links as $link) {
135 $link_dom = $this->saveLink($dom, $link);
136 $xrd_dom->appendChild($link_dom);
139 return $dom->saveXML();
142 function parseType($element)
147 function parseLink($element)
150 $link['rel'] = $element->getAttribute('rel');
151 $link['type'] = $element->getAttribute('type');
152 $link['href'] = $element->getAttribute('href');
153 $link['template'] = $element->getAttribute('template');
154 foreach ($element->childNodes as $node) {
155 if ($node instanceof DOMElement) {
156 switch($node->tagName) {
158 $link['title'][] = $node->nodeValue;
166 function saveLink($doc, $link)
168 $link_element = $doc->createElement('Link');
169 if (!empty($link['rel'])) {
170 $link_element->setAttribute('rel', $link['rel']);
172 if (!empty($link['type'])) {
173 $link_element->setAttribute('type', $link['type']);
175 if (!empty($link['href'])) {
176 $link_element->setAttribute('href', $link['href']);
178 if (!empty($link['template'])) {
179 $link_element->setAttribute('template', $link['template']);
182 if (!empty($link['title']) && is_array($link['title'])) {
183 foreach($link['title'] as $title) {
184 $title = $doc->createElement('Title', $title);
185 $link_element->appendChild($title);
190 return $link_element;