From 7827bfc5a37695a51d907f542fd8b5ac6dc61d93 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Mon, 20 Apr 2015 13:26:15 +0200 Subject: [PATCH] Move SimpleSAML_Utilities::formatDOMElement() to SimpleSAML\Utils\XML::formatDOMElement(). Deprecate the former. --- lib/SimpleSAML/Metadata/SAMLBuilder.php | 2 +- lib/SimpleSAML/Utilities.php | 84 ++-------------------- lib/SimpleSAML/Utils/XML.php | 96 +++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 81 deletions(-) create mode 100644 lib/SimpleSAML/Utils/XML.php diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php index 52f71c2ae..d79619f4b 100644 --- a/lib/SimpleSAML/Metadata/SAMLBuilder.php +++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php @@ -78,7 +78,7 @@ class SimpleSAML_Metadata_SAMLBuilder { $xml = $this->getEntityDescriptor(); if ($formatted) { - SimpleSAML_Utilities::formatDOMElement($xml); + SimpleSAML\Utils\XML::formatDOMElement($xml); } return $xml->ownerDocument->saveXML(); diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 98819475d..b46c2903e 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1218,86 +1218,10 @@ class SimpleSAML_Utilities { /** - * Format a DOM element. - * - * This function takes in a DOM element, and inserts whitespace to make it more - * readable. Note that whitespace added previously will be removed. - * - * @param DOMElement $root The root element which should be formatted. - * @param string $indentBase The indentation this element should be assumed to - * have. Default is an empty string. + * @deprecated This function will be removed in SSP 2.0. Please use SimpleSAML\Utils\XML::formatDOMElement() instead. */ public static function formatDOMElement(DOMElement $root, $indentBase = '') { - assert(is_string($indentBase)); - - /* Check what this element contains. */ - $fullText = ''; /* All text in this element. */ - $textNodes = array(); /* Text nodes which should be deleted. */ - $childNodes = array(); /* Other child nodes. */ - for ($i = 0; $i < $root->childNodes->length; $i++) { - $child = $root->childNodes->item($i); - - if($child instanceof DOMText) { - $textNodes[] = $child; - $fullText .= $child->wholeText; - - } elseif ($child instanceof DOMComment || $child instanceof DOMElement) { - $childNodes[] = $child; - - } else { - /* Unknown node type. We don't know how to format this. */ - return; - } - } - - $fullText = trim($fullText); - if (strlen($fullText) > 0) { - /* We contain text. */ - $hasText = TRUE; - } else { - $hasText = FALSE; - } - - $hasChildNode = (count($childNodes) > 0); - - if ($hasText && $hasChildNode) { - /* Element contains both text and child nodes - we don't know how to format this one. */ - return; - } - - /* Remove text nodes. */ - foreach ($textNodes as $node) { - $root->removeChild($node); - } - - if ($hasText) { - /* Only text - add a single text node to the element with the full text. */ - $root->appendChild(new DOMText($fullText)); - return; - - } - - if (!$hasChildNode) { - /* Empty node. Nothing to do. */ - return; - } - - /* Element contains only child nodes - add indentation before each one, and - * format child elements. - */ - $childIndentation = $indentBase . ' '; - foreach ($childNodes as $node) { - /* Add indentation before node. */ - $root->insertBefore(new DOMText("\n" . $childIndentation), $node); - - /* Format child elements. */ - if ($node instanceof DOMElement) { - self::formatDOMElement($node, $childIndentation); - } - } - - /* Add indentation before closing tag. */ - $root->appendChild(new DOMText("\n" . $indentBase)); + return SimpleSAML\Utils\XML::formatDOMElement($root, $indentBase); } @@ -1321,7 +1245,7 @@ class SimpleSAML_Utilities { } $root = $doc->firstChild; - self::formatDOMElement($root); + SimpleSAML\Utils\XML::formatDOMElement($root); return $doc->saveXML($root); } @@ -1624,7 +1548,7 @@ class SimpleSAML_Utilities { * Disable the loading of external entities in XML documents to prevent local and * remote file inclusion attacks. This is in most cases already disabled by default * in system libraries, but to be safe we explicitly disable it also. - * @deprecated This function will be removed in SSP 2.0. Please use libxml_disable_entity_loader() instead. + * @deprecated This function will be removed in SSP 2.0. Please use libxml_disable_entity_loader() instead. */ public static function disableXMLEntityLoader() { /* Function only present in PHP >= 5.2.11 while we support 5.2+ */ diff --git a/lib/SimpleSAML/Utils/XML.php b/lib/SimpleSAML/Utils/XML.php new file mode 100644 index 000000000..faec37b5d --- /dev/null +++ b/lib/SimpleSAML/Utils/XML.php @@ -0,0 +1,96 @@ +<?php +/** + * Utility class for XML and DOM manipulation. + * + * @package SimpleSAMLphp + */ + +namespace SimpleSAML\Utils; + + +class XML +{ + + /** + * Format a DOM element. + * + * This function takes in a DOM element, and inserts whitespace to make it more + * readable. Note that whitespace added previously will be removed. + * + * @param DOMElement $root The root element which should be formatted. + * @param string $indentBase The indentation this element should be assumed to + * have. Default is an empty string. + * + * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + */ + public static function formatDOMElement(DOMElement $root, $indentBase = '') + { + assert(is_string($indentBase)); + + // check what this element contains + $fullText = ''; // all text in this element + $textNodes = array(); // text nodes which should be deleted + $childNodes = array(); // other child nodes + for ($i = 0; $i < $root->childNodes->length; $i++) { + $child = $root->childNodes->item($i); + + if ($child instanceof DOMText) { + $textNodes[] = $child; + $fullText .= $child->wholeText; + } elseif ($child instanceof DOMComment || $child instanceof DOMElement) { + $childNodes[] = $child; + } else { + // unknown node type. We don't know how to format this + return; + } + } + + $fullText = trim($fullText); + if (strlen($fullText) > 0) { + // we contain text + $hasText = true; + } else { + $hasText = false; + } + + $hasChildNode = (count($childNodes) > 0); + + if ($hasText && $hasChildNode) { + // element contains both text and child nodes - we don't know how to format this one + return; + } + + // remove text nodes + foreach ($textNodes as $node) { + $root->removeChild($node); + } + + if ($hasText) { + // only text - add a single text node to the element with the full text + $root->appendChild(new DOMText($fullText)); + return; + } + + if (!$hasChildNode) { + // empty node. Nothing to do + return; + } + + /* Element contains only child nodes - add indentation before each one, and + * format child elements. + */ + $childIndentation = $indentBase.' '; + foreach ($childNodes as $node) { + // add indentation before node + $root->insertBefore(new DOMText("\n".$childIndentation), $node); + + // format child elements + if ($node instanceof DOMElement) { + self::formatDOMElement($node, $childIndentation); + } + } + + // add indentation before closing tag + $root->appendChild(new DOMText("\n".$indentBase)); + } +} -- GitLab