]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/lib/xrd.php
* i18n/L10n update
[quix0rs-gnu-social.git] / plugins / OStatus / lib / xrd.php
index aa13ef02428faf623b5c128e0c5e38e47fdfeeab..c8cffed9cde255fd14decbb0d68c2353fcfee748 100644 (file)
  * @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,22 +42,31 @@ 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) {
-            throw new Exception("Invalid XML, missing XRD root");
+            // TRANS: Exception.
+            throw new Exception(_m('Invalid XML, missing XRD root.'));
         }
 
         // Check for host-meta host
@@ -79,7 +87,7 @@ class XRD
             case 'Subject':
                 $xrd->subject = $node->nodeValue;
                 break;
-                
+
             case 'Alias':
                 $xrd->alias[] = $node->nodeValue;
                 break;
@@ -99,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);
         }
-        
-               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();
+
+        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');
+        }
+
+        $xs->elementEnd('XRD');
+
+        return $xs->getString();
     }
 
     function parseType($element)
     {
         return array();
     }
-    
+
     function parseLink($element)
     {
         $link = array();
@@ -162,32 +169,4 @@ class XRD
 
         return $link;
     }
-
-    function saveLink($doc, $link)
-    {
-        $link_element = $doc->createElement('Link');
-        if (!empty($link['rel'])) {
-            $link_element->setAttribute('rel', $link['rel']);
-        }
-        if (!empty($link['type'])) {
-            $link_element->setAttribute('type', $link['type']);
-        }
-        if (!empty($link['href'])) {
-            $link_element->setAttribute('href', $link['href']);
-        }
-        if (!empty($link['template'])) {
-            $link_element->setAttribute('template', $link['template']);
-        }
-
-        if (!empty($link['title']) && is_array($link['title'])) {
-            foreach($link['title'] as $title) {
-                $title = $doc->createElement('Title', $title);
-                $link_element->appendChild($title);
-            }
-        }
-
-        
-        return $link_element;
-    }
 }
-