]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/XML.php
Fixed max value check, improved request value fetching
[friendica.git] / src / Util / XML.php
index 9aba38910d6a7a0020e743d8f95ba30ec24690b2..e8eb4233b1a8a3040da04da6b68cb02c7aaadc50 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -268,7 +268,7 @@ class XML
                }
 
                if (!function_exists('xml_parser_create')) {
-                       Logger::log('Xml::toArray: parser function missing');
+                       Logger::notice('Xml::toArray: parser function missing');
                        return [];
                }
 
@@ -283,7 +283,7 @@ class XML
                }
 
                if (! $parser) {
-                       Logger::log('Xml::toArray: xml_parser_create: no resource');
+                       Logger::notice('Xml::toArray: xml_parser_create: no resource');
                        return [];
                }
 
@@ -295,9 +295,9 @@ class XML
                @xml_parser_free($parser);
 
                if (! $xml_values) {
-                       Logger::log('Xml::toArray: libxml: parse error: ' . $contents, Logger::DATA);
+                       Logger::debug('Xml::toArray: libxml: parse error: ' . $contents);
                        foreach (libxml_get_errors() as $err) {
-                               Logger::log('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, Logger::DATA);
+                               Logger::debug('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message);
                        }
                        libxml_clear_errors();
                        return;
@@ -433,18 +433,26 @@ class XML
                }
        }
 
-       public static function parseString($s, $strict = true)
+       /**
+        * Parse XML string
+        *
+        * @param string  $s
+        * @param boolean $suppress_log
+        * @return Object
+        */
+       public static function parseString(string $s, bool $suppress_log = false)
        {
-               // the "strict" parameter is deactivated
                libxml_use_internal_errors(true);
 
                $x = @simplexml_load_string($s);
                if (!$x) {
-                       Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
-                       foreach (libxml_get_errors() as $err) {
-                               Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ":" . $err->column, 'message' => $err->message]);
+                       if (!$suppress_log) {
+                               Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
+                               foreach (libxml_get_errors() as $err) {
+                                       Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ":" . $err->column, 'message' => $err->message]);
+                               }
+                               Logger::debug('Erroring XML string', ['xml' => $s]);
                        }
-                       Logger::debug('Erroring XML string', ['xml' => $s]);
                        libxml_clear_errors();
                }
                return $x;
@@ -480,6 +488,21 @@ class XML
                return $first_item->attributes;
        }
 
+       public static function getFirstValue($xpath, $search, $context)
+       {
+               $result = $xpath->query($search, $context);
+               if (!is_object($result)) {
+                       return '';
+               }
+
+               $first_item = $result->item(0);
+               if (!is_object($first_item)) {
+                       return '';
+               }
+
+               return $first_item->nodeValue;
+       }
+
        /**
         * escape text ($str) for XML transport
         *