X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Flib%2Fxrd.php;h=c8cffed9cde255fd14decbb0d68c2353fcfee748;hb=1652ded48c9c62c40157a5142e5231adbc574ddb;hp=1de065db9fa643e9fc32bd7eee3fdf8fb0c8534b;hpb=8da1b71d6957b4fc2c9032a10e5be0613bafbde7;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/lib/xrd.php b/plugins/OStatus/lib/xrd.php index 1de065db9f..c8cffed9cd 100644 --- a/plugins/OStatus/lib/xrd.php +++ b/plugins/OStatus/lib/xrd.php @@ -27,15 +27,14 @@ * @link http://status.net/ */ - class XRD { const XML_NS = 'http://www.w3.org/2000/xmlns/'; - + const XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0'; const HOST_META_NS = 'http://host-meta.net/xrd/1.0'; - + public $expires; public $subject; @@ -43,20 +42,32 @@ class XRD public $host; public $alias = array(); - + public $types = array(); - + public $links = array(); - + public static function parse($xml) { $xrd = new XRD(); $dom = new DOMDocument(); - if (!$dom->loadXML($xml)) { - throw new Exception("Invalid XML"); + + // Don't spew XML warnings to output + $old = error_reporting(); + error_reporting($old & ~E_WARNING); + $ok = $dom->loadXML($xml); + error_reporting($old); + + if (!$ok) { + // TRANS: Exception. + throw new Exception(_m('Invalid XML.')); } $xrd_element = $dom->getElementsByTagName('XRD')->item(0); + if (!$xrd_element) { + // TRANS: Exception. + throw new Exception(_m('Invalid XML, missing XRD root.')); + } // Check for host-meta host $host = $xrd_element->getElementsByTagName('Host')->item(0); @@ -76,7 +87,7 @@ class XRD case 'Subject': $xrd->subject = $node->nodeValue; break; - + case 'Alias': $xrd->alias[] = $node->nodeValue; break; @@ -96,51 +107,50 @@ class XRD public function toXML() { - $dom = new DOMDocument('1.0', 'UTF-8'); - $dom->formatOutput = true; - - $xrd_dom = $dom->createElementNS(XRD::XRD_NS, 'XRD'); - $dom->appendChild($xrd_dom); + $xs = new XMLStringer(); + + $xs->startXML(); + $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS)); if ($this->host) { - $host_dom = $dom->createElement('hm:Host', $this->host); - $xrd_dom->setAttributeNS(XRD::XML_NS, 'xmlns:hm', XRD::HOST_META_NS); - $xrd_dom->appendChild($host_dom); + $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host); + } + + if ($this->expires) { + $xs->element('Expires', null, $this->expires); + } + + if ($this->subject) { + $xs->element('Subject', null, $this->subject); + } + + foreach ($this->alias as $alias) { + $xs->element('Alias', null, $alias); + } + + foreach ($this->links as $link) { + $titles = array(); + if (isset($link['title'])) { + $titles = $link['title']; + unset($link['title']); + } + $xs->elementStart('Link', $link); + foreach ($titles as $title) { + $xs->element('Title', null, $title); + } + $xs->elementEnd('Link'); } - - if ($this->expires) { - $expires_dom = $dom->createElement('Expires', $this->expires); - $xrd_dom->appendChild($expires_dom); - } - - if ($this->subject) { - $subject_dom = $dom->createElement('Subject', $this->subject); - $xrd_dom->appendChild($subject_dom); - } - - foreach ($this->alias as $alias) { - $alias_dom = $dom->createElement('Alias', $alias); - $xrd_dom->appendChild($alias_dom); - } - - foreach ($this->types as $type) { - $type_dom = $dom->createElement('Type', $type); - $xrd_dom->appendChild($type_dom); - } - - foreach ($this->links as $link) { - $link_dom = $this->saveLink($dom, $link); - $xrd_dom->appendChild($link_dom); - } - - return $dom->saveXML(); + + $xs->elementEnd('XRD'); + + return $xs->getString(); } function parseType($element) { return array(); } - + function parseLink($element) { $link = array(); @@ -149,40 +159,14 @@ class XRD $link['href'] = $element->getAttribute('href'); $link['template'] = $element->getAttribute('template'); foreach ($element->childNodes as $node) { - switch($node->tagName) { - case 'Title': - $link['title'][] = $node->nodeValue; + if ($node instanceof DOMElement) { + switch($node->tagName) { + case 'Title': + $link['title'][] = $node->nodeValue; + } } } return $link; } - - function saveLink($doc, $link) - { - $link_element = $doc->createElement('Link'); - if ($link['rel']) { - $link_element->setAttribute('rel', $link['rel']); - } - if ($link['type']) { - $link_element->setAttribute('type', $link['type']); - } - if ($link['href']) { - $link_element->setAttribute('href', $link['href']); - } - if ($link['template']) { - $link_element->setAttribute('template', $link['template']); - } - - if (is_array($link['title'])) { - foreach($link['title'] as $title) { - $title = $doc->createElement('Title', $title); - $link_element->appendChild($title); - } - } - - - return $link_element; - } } -