]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/XML.php
Move to XML
[friendica.git] / src / Util / XML.php
index c866475c1b40d69e1c057de9e739e6c1e9c3e17f..9a3108ca9894c49fa2dcea38213b7b692ba2617e 100644 (file)
@@ -4,6 +4,7 @@
  */
 namespace Friendica\Util;
 
+use Friendica\Core\Logger;
 use DOMXPath;
 use SimpleXMLElement;
 
@@ -52,6 +53,7 @@ class XML
                        }
                }
 
+               $element = null;
                foreach ($array as $key => $value) {
                        if (!isset($element) && isset($xml)) {
                                $element = $xml;
@@ -185,12 +187,13 @@ class XML
                        return(null);
                }
 
+               $xml_element_copy = '';
                if (!is_string($xml_element)
                        && !is_array($xml_element)
                        && (get_class($xml_element) == 'SimpleXMLElement')
                ) {
-                               $xml_element_copy = $xml_element;
-                               $xml_element = get_object_vars($xml_element);
+                       $xml_element_copy = $xml_element;
+                       $xml_element = get_object_vars($xml_element);
                }
 
                if (is_array($xml_element)) {
@@ -246,7 +249,7 @@ class XML
                }
 
                if (!function_exists('xml_parser_create')) {
-                       logger('Xml::toArray: parser function missing');
+                       Logger::log('Xml::toArray: parser function missing');
                        return [];
                }
 
@@ -261,7 +264,7 @@ class XML
                }
 
                if (! $parser) {
-                       logger('Xml::toArray: xml_parser_create: no resource');
+                       Logger::log('Xml::toArray: xml_parser_create: no resource');
                        return [];
                }
 
@@ -273,9 +276,9 @@ class XML
                @xml_parser_free($parser);
 
                if (! $xml_values) {
-                       logger('Xml::toArray: libxml: parse error: ' . $contents, LOGGER_DATA);
+                       Logger::log('Xml::toArray: libxml: parse error: ' . $contents, Logger::DATA);
                        foreach (libxml_get_errors() as $err) {
-                               logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
+                               Logger::log('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, Logger::DATA);
                        }
                        libxml_clear_errors();
                        return;
@@ -413,4 +416,89 @@ class XML
                        $child->parentNode->removeChild($child);
                }
        }
+
+       public static function parseString($s, $strict = true)
+       {
+               // the "strict" parameter is deactivated
+               libxml_use_internal_errors(true);
+
+               $x = @simplexml_load_string($s);
+               if (!$x) {
+                       Logger::log('libxml: parse: error: ' . $s, Logger::DATA);
+                       foreach (libxml_get_errors() as $err) {
+                               Logger::log('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, Logger::DATA);
+                       }
+                       libxml_clear_errors();
+               }
+               return $x;
+       }
+
+       public static function getFirstNodeValue($xpath, $element, $context = null)
+       {
+               $result = $xpath->evaluate($element, $context);
+               if (!is_object($result)) {
+                       return '';
+               }
+
+               $first_item = $result->item(0);
+               if (!is_object($first_item)) {
+                       return '';
+               }
+
+               return $first_item->nodeValue;
+       }
+
+       public static function getFirstAttributes($xpath, $element, $context = null)
+       {
+               $result = $xpath->query($element, $context);
+               if (!is_object($result)) {
+                       return false;
+               }
+
+               $first_item = $result->item(0);
+               if (!is_object($first_item)) {
+                       return false;
+               }
+
+               return $first_item->attributes;
+       }
+
+       /**
+        * escape text ($str) for XML transport
+        * @param string $str
+        * @return string Escaped text.
+        */
+       public static function xmlify($str)
+       {
+               $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
+               $buffer = trim($buffer);
+
+               return $buffer;
+       }
+
+       /**
+        * undo an xmlify
+        * @param string $s xml escaped text
+        * @return string unescaped text
+        */
+       public static function unxmlify($s)
+       {
+               $ret = htmlspecialchars_decode($s, ENT_QUOTES);
+               return $ret;
+       }
+
+       /**
+        * apply xmlify() to all values of array $val, recursively
+        * @param array $val
+        * @return array
+        */
+       public static function arrayXmlify($val)
+       {
+               if (is_bool($val)) {
+                       return $val?"true":"false";
+               } elseif (is_array($val)) {
+                       return array_map('XML::arrayXmlify', $val);
+               }
+               return self::xmlify((string) $val);
+       }
 }