PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.2.10
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.2.10
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / libraries / WpaeXmlProcessor.php
wp-all-export / libraries Last commit date
VariableProductTitle 4 years ago .gitkeep 4 years ago WpaeInvalidPhpException.php 4 years ago WpaeInvalidStringException.php 4 years ago WpaeMethodNotFoundException.php 4 years ago WpaePhpInterpreterErrorHandler.php 4 years ago WpaeString.php 4 years ago WpaeTooMuchRecursionException.php 4 years ago WpaeXmlProcessor.php 4 years ago XmlCsvExport.php 4 years ago XmlExportACF.php 4 years ago XmlExportComment.php 4 years ago XmlExportCpt.php 4 years ago XmlExportEngine.php 4 years ago XmlExportFiltering.php 4 years ago XmlExportMediaGallery.php 4 years ago XmlExportTaxonomy.php 4 years ago XmlExportUser.php 4 years ago XmlExportWooCommerce.php 4 years ago XmlExportWooCommerceCoupon.php 4 years ago XmlExportWooCommerceOrder.php 4 years ago XmlGoogleMerchants.php 4 years ago XmlSpec.php 4 years ago
WpaeXmlProcessor.php
683 lines
1 <?php
2
3 class WpaeXmlProcessor
4 {
5 const SNIPPET_DELIMITER = '*SNIPPET*';
6
7 /** @var array */
8 protected $tags;
9
10 /** @var string */
11 protected $xml;
12
13 /** @var DOMDocument */
14 private $dom;
15
16 private $step = 0;
17
18 /** @var WpaeString */
19 private $wpaeString;
20
21 public function __construct(WpaeString $wpaeString)
22 {
23 add_filter('wp_all_export_post_process_xml', array($this, 'wp_all_export_post_process_xml'), 10, 1);
24
25 $this->wpaeString = $wpaeString;
26 }
27
28 public function process($xml)
29 {
30 $this->step = 0;
31
32 $xml = $this->preprocessXml($xml);
33 $xml = $this->handleSimpleSnippets($xml);
34
35 // Add a snippet to trigger a process
36 $snippetCount = count($this->parseSnippetsInString($xml));
37
38 if($snippetCount == 0 ) {
39 $xml .="<filler>[str_replace('a','b','c')]</filler>";
40 }
41
42 // While we have snippets
43 if ($snippetCount = count($this->parseSnippetsInString($xml))) {
44
45 // $this->step++;
46 $xml = '<root>' . $xml . '</root>';
47 $this->initVariables($xml);
48 $root = $this->dom->getElementsByTagName("root");
49 $this->parseElement($root->item(0));
50 $response = $this->dom->saveXML($this->dom);
51
52 $xml = $this->cleanResponse($response);
53
54 // if ($this->step > 8) {
55 // throw new WpaeTooMuchRecursionException('Too much recursion');
56 // }
57 }
58
59 $xml = $this->postProcessXml($xml);
60 $xml = $this->decodeSpecialCharacters($xml);
61 $xml = $this->encodeSpecialCharsInAttributes($xml);
62
63 $xml = str_replace('**OPENSHORTCODE**', '[', $xml);
64 $xml = str_replace('**CLOSESHORTCODE**', ']', $xml);
65
66 return $this->pretify($xml);
67 }
68
69 /**
70 * @param $xml
71 * @return mixed
72 */
73 public function pretify($xml)
74 {
75 $xml = '<root>' . $xml . '</root>';
76 $this->initVariables($xml);
77 // $root = $this->dom->getElementsByTagName("root");
78 // $this->preprocess_attributes($root->item(0));
79
80 return "\n ".$this->cleanResponse($this->dom->saveXML($this->dom));
81 }
82
83 private function preprocess_attributes(DOMNode $element){
84 if($element->hasAttributes()){
85 for ($i = 0; $i < $element->attributes->length; $i++) {
86 $element->attributes->item($i)->nodeValue = $this->sanitizeAttribute($element->attributes->item($i)->nodeValue);
87 }
88 }
89 if ($element->hasChildNodes()) {
90 for ($i = 0; $i < $element->childNodes->length; $i++) {
91 $this->preprocess_attributes($element->childNodes->item($i));
92 }
93 }
94 }
95
96 private function parseElement(DOMNode $element)
97 {
98 if($element->hasAttributes() && $element->nodeValue == '') {
99
100 if ($element->hasChildNodes()) {
101 $has_text_elements = false;
102 for ($i = 0; $i < $element->childNodes->length; $i++) {
103 if ( $element->childNodes->item($i)->nodeType == XML_TEXT_NODE ){
104 $has_text_elements = true;
105 break;
106 }
107 }
108 if ( ! $has_text_elements ){
109 $nodeAttributes = $this->getNodeAttributes($element);
110 $snippets = $this->parseSnippetsInString($nodeAttributes);
111 if (!empty($snippets)){
112 $tagValues = array();
113 foreach ($snippets as $snippet) {
114 $wholeValue = str_replace("\n", '', $nodeAttributes);
115 $isInFunction = $this->wpaeString->isBetween($wholeValue, $snippet, '[',']');
116 $snippetValue = $this->processSnippet($snippet,$isInFunction);
117 $tagValues[$snippet] = $snippetValue;
118 }
119
120 // Doing this to replace multiple snippet in the same tag (not to treat them as array and
121 // replace the snippet with the first letter of the string
122 foreach ($snippets as $snippet) {
123 if(isset($tagValues[$snippet])){
124 //$element->nodeValue = str_replace($snippet, $tagValues[$snippet], $element->nodeValue);
125 $this->replaceSnippetInAttributes($element, $snippet, $tagValues[$snippet]);
126 }
127 }
128 }
129 }
130 for ($i = 0; $i < $element->childNodes->length; $i++) {
131 $this->parseElement($element->childNodes->item($i));
132 }
133 }
134 $textNode = new DOMText('##FILLER##');
135 $element->appendChild($textNode);
136 return $this->parseElement($textNode);
137 }
138 if ($element->nodeType === XML_TEXT_NODE) {
139 $nodeAttributes = $this->getNodeAttributes($element->parentNode);
140
141 $snippets = $this->parseSnippetsInString($element->nodeValue . $nodeAttributes);
142
143 $maxTagValues = 0;
144 $tagValues = array();
145
146 if (count($snippets) > 0) {
147 if (count($snippets) == 1) {
148 $snippet = $snippets[0];
149 $isInFunction = $this->wpaeString->isBetween($nodeAttributes.$element->nodeValue, $snippet, '[',']');
150
151 $snippetValues = $this->processSnippet($snippet, $isInFunction);
152
153 if (!is_array($snippetValues)) {
154 $element->nodeValue =
155 str_replace(
156 $snippet,
157 $snippetValues,
158 $element->nodeValue
159 );
160 $nodeXML = $this->cloneNode($element->parentNode, $snippet, $snippetValues);
161 $f = $this->dom->createDocumentFragment();
162 $f->appendXML($nodeXML);
163 $this->parseElement($f);
164 if($element->parentNode->parentNode) {
165 $element->parentNode->parentNode->replaceChild($f, $element->parentNode);
166 }
167 } else {
168 foreach ($snippetValues as $snippetValue) {
169 $newValueNode = $element->parentNode->cloneNode(true);
170 $newValueNode->nodeValue = str_replace($snippet, $snippetValue, $newValueNode->nodeValue);
171 $this->replaceSnippetInAttributes($newValueNode, $snippet, $snippetValue);
172 $this->elementCdata($newValueNode);
173 $element->parentNode->parentNode->insertBefore($newValueNode, $element->parentNode);
174 }
175 $element->parentNode->parentNode->removeChild($element->parentNode);
176 }
177 } else if (count($snippets) > 1) {
178 foreach ($snippets as $snippet) {
179 $wholeValue = $nodeAttributes.$element->nodeValue;
180 $wholeValue = str_replace("\n", '', $wholeValue);
181 $isInFunction = $this->wpaeString->isBetween($wholeValue, $snippet, '[',']');
182 $snippetValue = $this->processSnippet($snippet,$isInFunction);
183
184 $tagValues[$snippet] = $snippetValue;
185
186
187 if (count($tagValues[$snippet]) > $maxTagValues) {
188 $maxTagValues = count($tagValues[$snippet]);
189 }
190 }
191 //We have arrays
192 if ($maxTagValues > 1) {
193 for ($i = 0; $i < $maxTagValues; $i++) {
194 $elementClone = $element->parentNode->cloneNode(true);
195 $elementValue = $elementClone->nodeValue;
196
197 foreach ($snippets as $snippet) {
198 // We might have the case that
199 // there are arrays but also implodes in the same tag
200 if(is_array($tagValues[$snippet])) {
201 if (isset($tagValues[$snippet][$i])) {
202 $elementValue = str_replace($snippet, $tagValues[$snippet][$i], $elementValue);
203 $this->replaceSnippetInAttributes($elementClone, $snippet, $tagValues[$snippet][$i]);
204
205
206 } else {
207 $elementValue = str_replace($snippet, "", $elementValue);
208 $this->replaceSnippetInAttributes($elementClone, $snippet, "");
209 }
210 } else {
211 $elementValue = str_replace($snippet, $tagValues[$snippet], $elementValue);
212 $this->replaceSnippetInAttributes($elementClone, $snippet, $tagValues[$snippet]);
213 }
214 }
215 $elementClone->nodeValue = $elementValue;
216 $this->elementCdata($elementClone);
217 $element->parentNode->parentNode->insertBefore($elementClone, $element->parentNode);
218 }
219 $element->parentNode->parentNode->removeChild($element->parentNode);
220 } else {
221 // Doing this to replace multiple snippet in the same tag (not to treat them as array and
222 // replace the snippet with the first letter of the string
223 foreach ($snippets as $snippet) {
224 if(isset($tagValues[$snippet])){
225 $element->nodeValue = str_replace($snippet, $tagValues[$snippet], $element->nodeValue);
226 $this->replaceSnippetInAttributes($element->parentNode, $snippet, $tagValues[$snippet]);
227 }
228 }
229 }
230 }
231 }
232 $this->elementCdata($element);
233 } else {
234 if ($element->hasChildNodes()) {
235 $has_text_elements = false;
236 for ($i = 0; $i < $element->childNodes->length; $i++) {
237 if ( $element->childNodes->item($i)->nodeType == XML_TEXT_NODE ){
238 $has_text_elements = true;
239 break;
240 }
241 }
242 if ( ! $has_text_elements ){
243 $nodeAttributes = $this->getNodeAttributes($element);
244 $snippets = $this->parseSnippetsInString($nodeAttributes);
245
246 if (!empty($snippets)){
247 $tagValues = array();
248 foreach ($snippets as $snippet) {
249 $wholeValue = str_replace("\n", '', $nodeAttributes);
250 $isInFunction = $this->wpaeString->isBetween($wholeValue, $snippet, '[',']');
251 $snippetValue = $this->processSnippet($snippet,$isInFunction);
252 $tagValues[$snippet] = $snippetValue;
253 }
254
255 // Doing this to replace multiple snippet in the same tag (not to treat them as array and
256 // replace the snippet with the first letter of the string
257 foreach ($snippets as $snippet) {
258 if(isset($tagValues[$snippet])){
259 //$element->nodeValue = str_replace($snippet, $tagValues[$snippet], $element->nodeValue);
260 $this->replaceSnippetInAttributes($element, $snippet, $tagValues[$snippet]);
261 }
262 }
263 }
264 }
265 for ($i = 0; $i < $element->childNodes->length; $i++) {
266 $this->parseElement($element->childNodes->item($i));
267 }
268 }
269 }
270 }
271
272 /**
273 * @param $filtered
274 * @return mixed
275 */
276 private function sanitizeFunctionName($filtered)
277 {
278 $functionName = str_replace('array(','(', substr($filtered, 0, strpos($filtered, "(")));
279 return $functionName;
280 }
281
282 /**
283 * @param $originalTag
284 * @return array
285 */
286 private function parseSnippetsInString($originalTag)
287 {
288 $results = array();
289 $matches = array();
290 preg_match_all("%(\[[^\]\[]*\])%", $originalTag, $matches);
291
292 $snippets = empty($matches) ? array() : array_unique($matches[0]);
293
294 foreach ($snippets as $snippet) {
295 $isCdataString = '<![CDATA' . $snippet;
296
297 if (strpos($this->xml, $isCdataString) === false) {
298 $results[] = $snippet;
299 }
300 }
301
302 return $results;
303 }
304
305 /**
306 * @param $v
307 * @return string
308 */
309 private function maybe_cdata($v, $hasSnippets = false)
310 {
311 if (XmlExportEngine::$is_preview) {
312 $v = str_replace('&amp;', '&', $v);
313 $v = htmlspecialchars($v);
314 $v = str_replace('##lt##','&lt;', $v);
315 $v = str_replace('##gt##','&gt;', $v);
316 }
317
318 if (XmlExportEngine::$is_preview && !XmlExportEngine::$exportOptions['show_cdata_in_preview']) {
319 return $v;
320 }
321
322 $cdataStrategyFactory = new CdataStrategyFactory();
323
324 if (!isset(XmlExportEngine::$exportOptions['custom_xml_cdata_logic'])) {
325 XmlExportEngine::$exportOptions['custom_xml_cdata_logic'] = 'auto';
326 }
327 $cdataStrategy = $cdataStrategyFactory->create_strategy(XmlExportEngine::$exportOptions['custom_xml_cdata_logic']);
328 $is_wrap_into_cdata = $cdataStrategy->should_cdata_be_applied($this->decodeSpecialCharacters($v), $hasSnippets);
329
330 if ($is_wrap_into_cdata === false) {
331 return $v;
332 } else {
333 return 'CDATABEGIN' . $v . 'CDATACLOSE';
334 }
335 }
336
337 /**
338 * @param $filtered
339 * @param $functionName
340 * @throws WpaeInvalidStringException
341 */
342 private function checkCorrectNumberOfQuotes($filtered, $functionName)
343 {
344 $numberOfSingleQuotes = substr_count($filtered, "'");
345 $numberOfDoubleQuotes = substr_count($filtered, "\"");
346
347 if ($numberOfSingleQuotes % 2 || $numberOfDoubleQuotes % 2) {
348 throw new WpaeInvalidStringException($functionName);
349 }
350 }
351
352 /**
353 * @param $filtered
354 * @return mixed
355 */
356 private function sanitizeAttribute($filtered)
357 {
358 $filtered = str_replace('&amp;', '&', $filtered);
359 $filtered = str_replace('&', '&amp;', $filtered);
360 $filtered = str_replace("'", '&#x27;', $filtered);
361 $filtered = str_replace('"', '&quot;', $filtered);
362 $filtered = str_replace('<', '&lt;', $filtered);
363 $filtered = str_replace('>', '&gt;', $filtered);
364
365 return $filtered;
366 }
367
368 /**
369 * @param $functionName
370 * @throws WpaeMethodNotFoundException
371 */
372 private function checkIfFunctionExists($functionName)
373 {
374 if (!function_exists($functionName) && $functionName != 'array') {
375 throw new WpaeMethodNotFoundException($functionName);
376 }
377 }
378
379 /**
380 * @param $snippet
381 * @return mixed
382 */
383 private function sanitizeSnippet($snippet)
384 {
385 $sanitizedSnippet = str_replace(array('[', ']'), '', $snippet);
386 $sanitizedSnippet = str_replace('\'', '"', $sanitizedSnippet);
387
388 return $sanitizedSnippet;
389 }
390
391 /**
392 * @param $xml
393 *
394 * @return mixed
395 */
396 private function handleSimpleSnippets($xml)
397 {
398 preg_match_all("%(\[[^\]\[]*\])%", $xml, $matches);
399 $snippets = empty($matches) ? array() : array_unique($matches[0]);
400
401 $simple_snipets = array();
402 preg_match_all("%(\{[^\}\{]*\})%", $xml, $matches);
403 $xpaths = array_unique($matches[0]);
404
405 if (!empty($xpaths)) {
406 foreach ($xpaths as $xpath) {
407 if (!in_array($xpath, $snippets)) $simple_snipets[] = $xpath;
408 }
409 }
410
411 if (!empty($simple_snipets)) {
412 foreach ($simple_snipets as $snippet) {
413
414 $filtered = preg_replace("%[\{\}]%", "", $snippet);
415
416 //Encode data in attributes
417 if (strpos($xml, "\"$snippet\"") !== false || strpos($xml, "'$snippet'") !== false) {
418 $attributeValue = str_replace('&amp;', '&', $filtered);
419 $attributeValue = str_replace('&', '&amp;', $attributeValue);
420 $attributeValue = str_replace('\'', '&#x27;', $attributeValue);
421 $attributeValue = str_replace('"', '&quot;', $attributeValue);
422 $attributeValue = str_replace('<', '&lt;', $attributeValue);
423 $attributeValue = str_replace('>', '&gt;', $attributeValue);
424
425 $xml = str_replace("\"".$snippet."\"", "\"".$attributeValue."\"", $xml);
426 $xml = str_replace("'".$snippet."'", "\"".$attributeValue."\"", $xml);
427 }
428
429 $filteredEncoded = $this->encodeSpecialCharacters($filtered);
430
431 $xml = str_replace($snippet, self::SNIPPET_DELIMITER.$filteredEncoded.self::SNIPPET_DELIMITER, $xml);
432 }
433 }
434 return $xml;
435 }
436
437 /**
438 * @param $xml
439 *
440 * @return mixed
441 */
442 public function encodeSpecialCharsInAttributes($xml)
443 {
444 preg_match_all('/<.*?=["\'](.*?)["\'].*?>/', $xml, $attributes);
445 $attributes = $attributes[1];
446
447 foreach ($attributes as $attribute) {
448
449 $attribute = trim($attribute, "'\"");
450
451 if (!$this->wpaeString->isBetween($xml, $attribute, '<![CDATA[', ']]>')) {
452
453 $xml = str_replace(array('\'' . $attribute . '\'', '"' . $attribute . '"'), '"' . $attribute . '"', $xml);
454 }
455 }
456
457 return $xml;
458 }
459
460 /**
461 * @param $xml
462 * @return DOMDocument
463 */
464 private function initVariables($xml)
465 {
466 $this->xml = $xml;
467
468 $dom = new DOMDocument();
469 $dom->recover = true;
470 $dom->preserveWhiteSpace = false;
471 $dom->substituteEntities = false;
472 $dom->resolveExternals = false;
473 $dom->formatOutput = true;
474
475 $dom->loadXML($xml);
476 $this->dom = $dom;
477 }
478
479 /**
480 * @param $snippet
481 * @param bool $isInFunction
482 *
483 * @return mixed
484 * @throws WpaeInvalidStringException
485 * @throws WpaeMethodNotFoundException
486 */
487 private function processSnippet($snippet, $isInFunction = false)
488 {
489
490 $sanitizedSnippet = $this->sanitizeSnippet($snippet);
491
492 $sanitizedSnippet = str_replace(WpaeXmlProcessor::SNIPPET_DELIMITER, '"', $sanitizedSnippet);
493 $functionName = $this->sanitizeFunctionName($sanitizedSnippet);
494
495 $this->checkCorrectNumberOfQuotes($sanitizedSnippet, $functionName);
496 $this->checkIfFunctionExists($functionName);
497
498 $argsStr = preg_replace("%^".$functionName."\((.*)\)$%", "$1", $sanitizedSnippet);
499 preg_match_all("%(\"[^\"]*\")%", $argsStr, $matches);
500 if (!empty($matches[0])){
501 $args = $matches[0];
502 foreach ($args as $k => $arg){
503 $sanitizedSnippet = str_replace($arg, 'apply_filters("wp_all_export_post_process_xml", '. $arg .')' ,$sanitizedSnippet);
504 }
505 }
506
507 // Clean empty strings
508 $sanitizedSnippet = str_replace(array(', ,',',,'), ',"",', $sanitizedSnippet);
509
510 $snippetValue = eval('return ' . $sanitizedSnippet . ';');
511 $snippetValue = $this->encodeSpecialCharacters($snippetValue);
512
513 if(strpos($snippet, 'explode') !== false && $isInFunction) {
514 $snippetValue = 'array('."'" . implode("','", $snippetValue) . "'".')';
515 }
516
517 return $snippetValue;
518 }
519
520 public function wp_all_export_post_process_xml($value){
521 return $this->postProcessXml($this->decodeSpecialCharacters(str_replace('"','', $value)));
522 }
523
524 public function getNodeAttributes(DOMNode $dom)
525 {
526 $result = "";
527 if ($dom->hasAttributes()) {
528 for ($i = 0; $i < $dom->attributes->length; $i++)
529 $result .= $dom->attributes->item($i)->nodeValue;
530 }
531
532 return $result;
533 }
534
535 /**
536 * @param DOMNode $newValueNode
537 * @param $snippet
538 * @param $snippetValue
539 * @internal param $snippetValues
540 */
541 private function replaceSnippetInAttributes(DOMNode $newValueNode, $snippet, $snippetValue)
542 {
543 $snippetValue = $this->sanitizeAttribute($snippetValue);
544 if ($newValueNode->hasAttributes()) {
545 for ($i = 0; $i < $newValueNode->attributes->length; $i++) {
546 $newValueNode->attributes->item($i)->nodeValue =
547 str_replace(
548 $snippet,
549 $snippetValue,
550 $newValueNode->attributes->item($i)->nodeValue
551 );
552 }
553 }
554 }
555
556 /**
557 * @param DOMNode $element
558 */
559 private function elementCdata(DOMNode $element)
560 {
561 $hasSnippets = $this->parseSnippetsInString($element->nodeValue);
562
563 if (strpos($element->nodeValue, '<![CDATA[') === false && strpos($element->nodeValue, 'CDATABEGIN') === false) {
564 $element->nodeValue = $this->maybe_cdata($element->nodeValue, $hasSnippets);
565 }
566 }
567
568 private function encodeSpecialCharacters($text)
569 {
570 $text = str_replace('&amp;', '&', $text);
571 $text = str_replace('&', '##amp##', $text);
572 $text = str_replace("'", '##x27##', $text);
573 $text = str_replace('"', '##quot##', $text);
574 $text = str_replace('<', '##lt##', $text);
575 $text = str_replace('>', '##gt##', $text);
576
577 return $text;
578 }
579
580 private function decodeSpecialCharacters($text)
581 {
582 $text = str_replace('##amp##', '&', $text);
583 $text = str_replace('##x27##', "'", $text);
584 $text = str_replace('##quot##', '"', $text);
585 $text = str_replace('##lt##', '<', $text);
586 $text = str_replace('##gt##', '>', $text);
587
588 return $text;
589 }
590
591 /**
592 * @param $xml
593 * @return mixed
594 */
595 private function postProcessXml($xml)
596 {
597 $xml = str_replace('CDATABEGIN', '<![CDATA[', $xml);
598 $xml = str_replace('CDATACLOSE', ']]>', $xml);
599
600 $xml = str_replace('CLOSEBRAKET', ']', str_replace('OPENBRAKET', '[', $xml));
601 $xml = str_replace('CLOSECURVE', '}', str_replace('OPENCURVE', '{', $xml));
602 $xml = str_replace('CLOSECIRCLE', ')', str_replace('OPENCIRCLE', '(', $xml));
603
604 $xml = str_replace('**SINGLEQUOT**', "'", $xml);
605 $xml = str_replace('**DOUBLEQUOT**', "\"", $xml);
606
607 $xml = str_replace('**GT**', ">", $xml);
608 $xml = str_replace('**LT**', "<", $xml);
609
610 $xml = str_replace('##FILLER##', '', $xml);
611 $xml = str_replace('<filler>c</filler>', '', $xml);
612 $xml = str_replace('<filler><![CDATA[c]]></filler>', '', $xml);
613 $xml = str_replace('<filler>CDATABEGINcCDATACLOSE</filler>', '', $xml);
614
615 $xml = str_replace('<commentTempNode>', '<!--', $xml);
616 $xml = str_replace('</commentTempNode>', '-->', $xml);
617
618 $xml = str_replace(self::SNIPPET_DELIMITER, '', $xml);
619
620 $xml = trim($xml);
621 return $xml;
622 }
623
624 /**
625 * @param $xml
626 * @return mixed
627 */
628 private function preprocessXml($xml)
629 {
630 $xml = str_replace('<!--', '<commentTempNode>', $xml);
631 $xml = str_replace('-->', '</commentTempNode>', $xml);
632 $xml = str_replace("\"{}\"", '""', $xml);
633
634 preg_replace('%(\[.*)({})(.*\])%', "$1\"\"$2", $xml);
635
636 $xml = str_replace(">\"\"<", '><', $xml);
637 $xml = str_replace("[implode(',',{})]", "", $xml);
638 return $xml;
639 }
640
641 /**
642 * @param $xml
643 * @param $response
644 * @return mixed
645 */
646 private function cleanResponse($response)
647 {
648 $response = str_replace('<root>', '', $response);
649 $response = str_replace('</root>', '', $response);
650 $response = str_replace('<root/>', '', $response);
651 $xml = str_replace("<?xml version=\"1.0\"?>", '', $response);
652 $xml = str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "", $xml);
653
654 return trim($xml);
655 }
656
657 /**
658 *
659 * Cloning DOMNode with including child DOMNode elements
660 *
661 * @param $node
662 * @return \DOMNode*
663 */
664 private function cloneNode(DOMNode $node, $snippet, $snippetValues){
665
666 $Document = new DOMDocument('1.0', 'UTF-8');
667 $Document->preserveWhiteSpace = false;
668 $Document->formatOutput = true;
669 $newElement = $Document->importNode($node, true);
670
671 $this->replaceSnippetInAttributes($newElement, $snippet, $snippetValues);
672
673 foreach ($newElement->childNodes as $child){
674 if ($child->nodeType === XML_TEXT_NODE) {
675 $this->elementCdata($child);
676 }
677 }
678
679 $Document->appendChild($newElement);
680 return trim(str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","",$Document->saveXML()));
681
682 }
683 }