[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/ -> class_xmlparser.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  /**
  12   * The following class is based upon code by Eric Pollman
  13   * @ http://eric.pollman.net/work/public_domain/
  14   * and is licensed under the public domain license.
  15   */
  16  
  17  class MyBBXMLParser {
  18  
  19      /**
  20       * @var string
  21       */
  22      public $data;
  23      /**
  24       * @var array
  25       */
  26      public $vals;
  27      /**
  28       * @var int
  29       */
  30      public $collapse_dups = 1;
  31      /**
  32       * @var int
  33       */
  34      public $index_numeric = 0;
  35  
  36      /**
  37       * Initialize the parser and store the XML data to be parsed.
  38       *
  39       * @param string $data
  40       */
  41  	function __construct($data)
  42      {
  43          $this->data = $data;
  44      }
  45  
  46      /**
  47       * Build a tree based structure based from the parsed data
  48       *
  49       * @return array The tree based structure
  50       */
  51  	function get_tree()
  52      {
  53          $parser = xml_parser_create();
  54          xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
  55          xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  56          if(!xml_parse_into_struct($parser, $this->data, $vals, $index))
  57          {
  58              return false;
  59          }
  60  
  61          $i = -1;
  62          return $this->get_children($vals, $i);
  63      }
  64  
  65      /**
  66       * Private: Build a completed tag by fetching all child nodes and attributes
  67       *
  68       * @param array $thisvals Array of values from the current tag
  69       * @param array $vals Array of child nodes
  70       * @param int $i Internal counter
  71       * @param string $type Type of tag. Complete is a single line tag with attributes
  72       * @return array Completed tag array
  73       */
  74  	function build_tag($thisvals, $vals, &$i, $type)
  75      {
  76          $tag = array('tag' => $thisvals['tag']);
  77  
  78          if(isset($thisvals['attributes']))
  79          {
  80              $tag['attributes'] = $thisvals['attributes'];
  81          }
  82  
  83          if($type == "complete")
  84          {
  85              if(isset($thisvals['value']))
  86              {
  87                  $tag['value'] = $thisvals['value'];
  88              }
  89          }
  90          else
  91          {
  92              $tag = array_merge($tag, $this->get_children($vals, $i));
  93          }
  94          return $tag;
  95      }
  96  
  97      /**
  98       * Fetch the children for from a specific node array
  99       *
 100       * @param array $vals Array of children
 101       * @param int $i Internal counter
 102       * @return array Array of child nodes
 103       */
 104  	function get_children($vals, &$i)
 105      {
 106          $children = array();
 107  
 108          if($i > -1 && isset($vals[$i]['value']))
 109          {
 110              $children['value'] = $vals[$i]['value'];
 111          }
 112  
 113          while(++$i < count($vals))
 114          {
 115              $type = $vals[$i]['type'];
 116              if($type == "cdata")
 117              {
 118                  $children['value'] .= $vals[$i]['value'];
 119              }
 120              elseif($type == "complete" || $type == "open")
 121              {
 122                  $tag = $this->build_tag($vals[$i], $vals, $i, $type);
 123                  if($this->index_numeric)
 124                  {
 125                      $tag['tag'] = $vals[$i]['tag'];
 126                      $children[] = $tag;
 127                  }
 128                  else
 129                  {
 130                      $children[$tag['tag']][] = $tag;
 131                  }
 132              }
 133              else if($type == "close")
 134              {
 135                  break;
 136              }
 137          }
 138          if($this->collapse_dups)
 139          {
 140              foreach($children as $key => $value)
 141              {
 142                  if(is_array($value) && (count($value) == 1))
 143                  {
 144                      $children[$key] = $value[0];
 145                  }
 146              }
 147          }
 148          return $children;
 149      }
 150  }
 151  
 152  /**
 153   * Kill off unnecessary tags and return a clean array of XML data
 154   *
 155   * @param array $array Array of parsed XML data
 156   * @return array Cleaned array of XML data
 157   */
 158  function kill_tags($array)
 159  {
 160      foreach($array as $key => $val)
 161      {
 162          if($key == "tag" || $key == "value")
 163          {
 164              unset($array[$key]);
 165          }
 166          else if(is_array($val))
 167          {
 168              // kill any nested tag or value indexes
 169              $array[$key] = kill_tags($val);
 170  
 171              // if the array no longer has any key/val sets
 172              // and therefore is at the deepest level, then
 173              // store the string value
 174              if(is_array($array[$key]) && count($array[$key]) <= 0)
 175              {
 176                  $array[$key] = $val['value'];
 177              }
 178          }
 179      }
 180  
 181      return $array;
 182  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref