Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

XmlUtils.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 7.43 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Config\Util;
013   
014  /**
015   * XMLUtils is a bunch of utility methods to XML operations.
016   *
017   * This class contains static methods only and is not meant to be instantiated.
018   *
019   * @author Fabien Potencier <fabien@symfony.com>
020   * @author Martin Hasoň <martin.hason@gmail.com>
021   */
022  class XmlUtils
023  {
024      /**
025       * This class should not be instantiated
026       */
027      private function __construct()
028      {
029      }
030   
031      /**
032       * Loads an XML file.
033       *
034       * @param string          $file             An XML file path
035       * @param string|callable $schemaOrCallable An XSD schema file path or callable
036       *
037       * @return \DOMDocument
038       *
039       * @throws \InvalidArgumentException When loading of XML file returns error
040       */
041      public static function loadFile($file, $schemaOrCallable = null)
042      {
043          $content = @file_get_contents($file);
044          if ('' === trim($content)) {
045              throw new \InvalidArgumentException(sprintf('File %s does not contain valid XML, it is empty.', $file));
046          }
047   
048          $internalErrors = libxml_use_internal_errors(true);
049          $disableEntities = libxml_disable_entity_loader(true);
050          libxml_clear_errors();
051   
052          $dom = new \DOMDocument();
053          $dom->validateOnParse = true;
054          if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
055              libxml_disable_entity_loader($disableEntities);
056   
057              throw new \InvalidArgumentException(implode("\n", static::getXmlErrors($internalErrors)));
058          }
059   
060          $dom->normalizeDocument();
061   
062          libxml_use_internal_errors($internalErrors);
063          libxml_disable_entity_loader($disableEntities);
064   
065          foreach ($dom->childNodes as $child) {
066              if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
067                  throw new \InvalidArgumentException('Document types are not allowed.');
068              }
069          }
070   
071          if (null !== $schemaOrCallable) {
072              $internalErrors = libxml_use_internal_errors(true);
073              libxml_clear_errors();
074   
075              $e = null;
076              if (is_callable($schemaOrCallable)) {
077                  try {
078                      $valid = call_user_func($schemaOrCallable, $dom, $internalErrors);
079                  } catch (\Exception $e) {
080                      $valid = false;
081                  }
082              } elseif (!is_array($schemaOrCallable) && is_file((string) $schemaOrCallable)) {
083                  $schemaSource = file_get_contents((string) $schemaOrCallable);
084                  $valid = @$dom->schemaValidateSource($schemaSource);
085              } else {
086                  libxml_use_internal_errors($internalErrors);
087   
088                  throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
089              }
090   
091              if (!$valid) {
092                  $messages = static::getXmlErrors($internalErrors);
093                  if (empty($messages)) {
094                      $messages = array(sprintf('The XML file "%s" is not valid.', $file));
095                  }
096                  throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
097              }
098          }
099   
100          libxml_clear_errors();
101          libxml_use_internal_errors($internalErrors);
102   
103          return $dom;
104      }
105   
106      /**
107       * Converts a \DomElement object to a PHP array.
108       *
109       * The following rules applies during the conversion:
110       *
111       *  * Each tag is converted to a key value or an array
112       *    if there is more than one "value"
113       *
114       *  * The content of a tag is set under a "value" key (<foo>bar</foo>)
115       *    if the tag also has some nested tags
116       *
117       *  * The attributes are converted to keys (<foo foo="bar"/>)
118       *
119       *  * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
120       *
121       * @param \DomElement $element     A \DomElement instance
122       * @param bool        $checkPrefix Check prefix in an element or an attribute name
123       *
124       * @return array A PHP array
125       */
126      public static function convertDomElementToArray(\DomElement $element, $checkPrefix = true)
127      {
128          $prefix = (string) $element->prefix;
129          $empty = true;
130          $config = array();
131          foreach ($element->attributes as $name => $node) {
132              if ($checkPrefix && !in_array((string) $node->prefix, array('', $prefix), true)) {
133                  continue;
134              }
135              $config[$name] = static::phpize($node->value);
136              $empty = false;
137          }
138   
139          $nodeValue = false;
140          foreach ($element->childNodes as $node) {
141              if ($node instanceof \DOMText) {
142                  if ('' !== trim($node->nodeValue)) {
143                      $nodeValue = trim($node->nodeValue);
144                      $empty = false;
145                  }
146              } elseif ($checkPrefix && $prefix != (string) $node->prefix) {
147                  continue;
148              } elseif (!$node instanceof \DOMComment) {
149                  $value = static::convertDomElementToArray($node, $checkPrefix);
150   
151                  $key = $node->localName;
152                  if (isset($config[$key])) {
153                      if (!is_array($config[$key]) || !is_int(key($config[$key]))) {
154                          $config[$key] = array($config[$key]);
155                      }
156                      $config[$key][] = $value;
157                  } else {
158                      $config[$key] = $value;
159                  }
160   
161                  $empty = false;
162              }
163          }
164   
165          if (false !== $nodeValue) {
166              $value = static::phpize($nodeValue);
167              if (count($config)) {
168                  $config['value'] = $value;
169              } else {
170                  $config = $value;
171              }
172          }
173   
174          return !$empty ? $config : null;
175      }
176   
177      /**
178       * Converts an xml value to a PHP type.
179       *
180       * @param mixed $value
181       *
182       * @return mixed
183       */
184      public static function phpize($value)
185      {
186          $value = (string) $value;
187          $lowercaseValue = strtolower($value);
188   
189          switch (true) {
190              case 'null' === $lowercaseValue:
191                  return;
192              case ctype_digit($value):
193                  $raw = $value;
194                  $cast = intval($value);
195   
196                  return '0' == $value[0] ? octdec($value) : (((string) $raw == (string) $cast) ? $cast : $raw);
197              case 'true' === $lowercaseValue:
198                  return true;
199              case 'false' === $lowercaseValue:
200                  return false;
201              case is_numeric($value):
202                  return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value);
203              case preg_match('/^(-|\+)?[0-9]+(\.[0-9]+)?$/', $value):
204                  return floatval($value);
205              default:
206                  return $value;
207          }
208      }
209   
210      protected static function getXmlErrors($internalErrors)
211      {
212          $errors = array();
213          foreach (libxml_get_errors() as $error) {
214              $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
215                  LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
216                  $error->code,
217                  trim($error->message),
218                  $error->file ? $error->file : 'n/a',
219                  $error->line,
220                  $error->column
221              );
222          }
223   
224          libxml_clear_errors();
225          libxml_use_internal_errors($internalErrors);
226   
227          return $errors;
228      }
229  }
230