zimbra-api / soap-api

Zimbra SOAP client in PHP language
BSD 3-Clause "New" or "Revised" License
62 stars 48 forks source link

Search with & caracter fails #38

Closed maxxer closed 3 years ago

maxxer commented 5 years ago

The library doesn't encode in CDATA the search element, so for example searching for a message ID which contains a & character the call fails

maxxer commented 5 years ago

This patch works. Probably not the best approach but it does.

diff --git a/src/Zimbra/Common/SimpleXML.php b/src/Zimbra/Common/SimpleXML.php
index cd262dd..c4b1d17 100644
--- a/src/Zimbra/Common/SimpleXML.php
+++ b/src/Zimbra/Common/SimpleXML.php
@@ -208,4 +208,34 @@ class SimpleXML extends SimpleXMLElement
         }
         return $this;
     }
+
+    // https://stackoverflow.com/a/6260295/738852
+    /**
+      * Adds a child with $value inside CDATA
+      * @param unknown $name
+      * @param unknown $value
+      */
+    public function addChildWithCDATA($name, $value = NULL) {
+        $new_child = $this->addChild($name);
+
+        if ($new_child !== NULL) {
+            $node = dom_import_simplexml($new_child);
+            $no   = $node->ownerDocument;
+            $node->appendChild($no->createCDATASection($value));
+        }
+
+        return $new_child;
+    }
+
+    /**
+     * override to use cdata, when necessary
+     */
+    public function addChild($name, $val = NULL, $ns = NULL)
+    {
+        if (preg_match('/.*[\&\>\<].*/', $val) == 1) {
+            return $this->addChildWithCDATA($name, $val);
+        } else {
+            return parent::addChild($name, $val, $ns);
+        }
+    }