Advertisement
Guest User

Untitled

a guest
Mar 27th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.99 KB | None | 0 0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////
  3. //
  4. //      File:
  5. //          xml.php
  6. //      Description:
  7. //          This file parses and compiles XML.
  8. //      Actions:
  9. //          1) compile
  10. //          2) parse
  11. //      Date:
  12. //          Created June 3rd, 2010
  13. //      Version:
  14. //          2.3
  15. //      Copyright:
  16. //          Copyright (c) 2010 Matthew Praetzel.
  17. //      License:
  18. //          This software is licensed under the terms of the End User License Agreement (EULA)
  19. //          provided with this software. In the event the EULA is not present with this software
  20. //          or you have not read it, please visit: http://www.ternstyle.us/license.txt
  21. //
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////
  23.  
  24. /****************************************Commence Script*******************************************/
  25.  
  26. if(!class_exists('ternXML')) {
  27. //
  28. class ternXML {
  29.    
  30.     var $init = false;
  31.     var $a = array(
  32.         'root'      =>  'root',
  33.         'data'      =>  array(),
  34.         'default'   =>  'item',
  35.         'cdata'     =>  array()
  36.     );
  37.     var $xml;
  38.     var $parsed = array();
  39.     var $open = array();
  40.     var $index = 0;
  41.     var $root = false;
  42.    
  43.     function compile($a) {
  44.         $this->a = array_merge($this->a,$a);
  45.         $this->init = true;
  46.        
  47.         $this->head();
  48.         $this->body();
  49.        
  50.         return $this->xml;
  51.     }
  52.     function head() {
  53.         $this->xml .= '<?xml version="1.0" encoding="utf-8"?>';
  54.     }
  55.     function body() {
  56.         $this->generate($this->a['data']);
  57.     }
  58.     function generate($a) {
  59.    
  60.         if(is_array($a)) {
  61.             foreach($a as $k => $this->item) {
  62.                
  63.                 //set id
  64.                 $this->id = uniqid();
  65.                
  66.                 //set attributes
  67.                 $this->set_attributes();
  68.                 //fix offset by array key 'value'
  69.                 if(is_array($this->item) and isset($this->item['value'])) {
  70.                     $this->item = $this->item['value'];
  71.                 }
  72.                
  73.                 //add cdata
  74.                 if(((!is_array($this->a['cdata']) and $this->a['cdata']) or in_array($k,$this->a['cdata'])) and !is_array($this->item)) {
  75.                     $this->item = '<![CDATA['.$this->item.']]>';
  76.                 }
  77.                
  78.                 //add to array
  79.                 $c = count($this->open);
  80.                 $this->open[$c] = array();
  81.                 $this->open[$c] = array(
  82.                     'id'        =>  $this->id,
  83.                     'name'      =>  $k,
  84.                     //'depth'       =>  $this->parent_is_a_list() ? $this->get_parent_value('depth') : count($this->open)-1,
  85.                     'index'     =>  0,
  86.                     'is_list'   =>  $this->is_a_list(),
  87.                     'count'     =>  count($this->item),
  88.                     'item'      =>  $this->item,
  89.                     'parent'    =>  $this->open[$c-1]
  90.                 );
  91.                
  92.                 //add xml
  93.                 if($this->is_a_list()) {
  94.                     $this->generate($this->item);
  95.                 }
  96.                 elseif(is_array($this->item)) {
  97.                     $this->open_item();
  98.                     $this->generate($this->item);
  99.                     $this->close_item();
  100.                 }
  101.                 else {
  102.                     $this->open_item();
  103.                     $this->item();
  104.                     $this->close_item();
  105.                 }
  106.                
  107.             }
  108.         }
  109.  
  110.     }
  111.     function open_item() {
  112.         $this->add_indent(1);
  113.         $this->xml .= '<';
  114.         $this->xml .= $this->parent_is_a_list() ? $this->get_parent_value('name') : $this->get_item_value('name');
  115.         $this->last = $this->open[count($this->open)-1];
  116.         $this->add_attributes();
  117.         $this->xml .= '>';
  118.         $this->increment_index();
  119.     }
  120.     function item() {
  121.         $this->xml .= $this->item;
  122.     }
  123.     function close_item() {
  124.    
  125.        
  126.        
  127.         $this->add_indent(0);
  128.         $this->xml .= '</';
  129.         if($this->parent_is_a_list()) {
  130.             $this->xml .= $this->get_parent_value('name');
  131.         }
  132.         else {
  133.             $this->xml .= $this->get_item_value('name');
  134.         }
  135.         $this->xml .= '>';
  136.        
  137.         if($this->parent_is_a_list() and $this->get_parent_value('index') == $this->get_parent_value('count')) {
  138.             array_pop($this->open);
  139.         }
  140.         //if(!$this->parent_is_a_list()) {
  141.             array_pop($this->open);
  142.         //}
  143.  
  144.     }
  145.     function add_attributes() {
  146.         if(is_array($this->attr)) {
  147.             foreach((array)$this->attr as $k => $v) {
  148.                 $this->xml .= ' '.$k.'="'.$v.'"';
  149.             }
  150.         }
  151.     }
  152.     function set_attributes() {
  153.         if(is_array($this->item['attributes'])) {
  154.             $this->attr = $this->item['attributes'];
  155.             unset($this->item['attributes']);
  156.         }
  157.         else {
  158.            
  159.             $this->attr = false;
  160.         }
  161.     }
  162.     function index_in_parent() {
  163.         if($this->get_parent_value('is_list')) {
  164.             return $this->get_parent_value['index'];
  165.         }
  166.         return 0;
  167.     }
  168.     function increment_index() {
  169.         if($this->get_parent_value('is_list')) {
  170.             $this->open[count($this->open)-2]['index']++;
  171.         }
  172.     }
  173.     function get_parent_value($v) {
  174.         return $this->open[count($this->open)-2][$v];
  175.     }
  176.     function get_item_value($v) {
  177.         return $this->open[count($this->open)-1][$v];
  178.     }
  179.     function get_item() {
  180.         return $this->open[count($this->open)-1][$v];
  181.     }
  182.     function is_a_list() {
  183.         if(is_array($this->item)) {
  184.             $a = is_array($this->item['attributes']) ? $this->item['value'] : $this->item;
  185.             if(count($a) == 0) {
  186.                 return false;
  187.             }
  188.             foreach($a as $k => $v) {
  189.                 if(!is_numeric($k)) {
  190.                     return false;
  191.                 }
  192.             }
  193.             return true;
  194.         }
  195.         return false;
  196.     }
  197.     function parent_is_a_list() {
  198.         if($this->get_parent_value('is_list')) {
  199.             return true;
  200.         }
  201.         return false;
  202.     }
  203.     function add_indent($b=0) {
  204.        
  205.         if(!$b and $this->get_parent_value('is_list') and $this->last['parent']['id'] == $this->get_item_value('id')) {
  206.             $this->indent();
  207.             return;
  208.         }
  209.         elseif(!$b and $this->last['id'] == $this->get_item_value('id')) {
  210.             return;
  211.         }
  212.         $this->indent();
  213.        
  214.     }
  215.     function indent() {
  216.         $this->xml .= "\n";
  217.         for($i=0;$i<count($this->open)-1;$i++) {
  218.             if(!$this->open[$i]['is_list']) {
  219.                 $this->xml .= "\t";
  220.             }
  221.         }
  222.     }
  223.  
  224.  
  225.     function parse($x,$v=true) {
  226.  
  227.         //$x = preg_replace("/[\r\n]*/",'',$x);
  228.  
  229.         $this->value = $v;
  230.         $this->open = array();
  231.         $this->parsed = array();
  232.         $this->opened = array();
  233.        
  234.         $this->parser = xml_parser_create();
  235.         xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
  236.         xml_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING,'utf-8');
  237.         xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,0);
  238.         xml_set_object($this->parser,$this);
  239.         xml_set_element_handler($this->parser,'parse_open_item','parse_close_item');
  240.         xml_set_character_data_handler($this->parser,'parse_item');
  241.         xml_parse($this->parser,$x,true);
  242.         xml_parser_free($this->parser);
  243.        
  244.         $this->clean_parsed($this->parsed);
  245.         return $this->parsed;
  246.     }
  247.    
  248.     function parse_open_item($p,$n,$a) {
  249.         $this->name = $n;
  250.        
  251.         $this->attr = count($a) > 0 ? $a : false;
  252.  
  253.         $this->is_list = false;
  254.         if($this->crawl_items('exists')) {
  255.             $this->is_list = true;
  256.             $this->crawl_items('fix');
  257.             $this->crawl_items('add');
  258.             $this->open[] =  array('name'=>$this->name,'is_list'=>true);
  259.         }
  260.         else {
  261.             $this->crawl_items('add');
  262.             $this->open[] =  array('name'=>$this->name,'is_list'=>false);
  263.         }
  264.     }
  265.     function parse_item($p,$v) {
  266.        
  267.         $this->parsed_value = strval(ltrim(rtrim($v,"\t\r\n"),"\t\r\n"));
  268.         if($this->parsed_value === '' or (empty($this->parsed_value) and $this->parsed_value !== 0 and $this->parsed_value !== '0') or preg_match("/^[\s]+$/",$this->parsed_value)) {
  269.             return;
  270.         }
  271.         $this->crawl_items('value');
  272.     }
  273.     function parse_close_item($p,$n) {
  274.         array_pop($this->open);
  275.     }
  276.    
  277.     function crawl_items($n) {
  278.         $this->count[$n] = 0;
  279.         return $this->walk_items($this->parsed,$n);
  280.     }
  281.     function walk_items(&$item,$n) {
  282.         $c = $this->count[$n];
  283.         $name = $this->open[$c]['name'];
  284.        
  285.         if($this->open[$c]['is_list']) {
  286.             $this->count[$n]++;
  287.             return $this->walk_items($item[$name]['value'][count($item[$name]['value'])-1],$n);
  288.         }
  289.        
  290.         //stop walking
  291.         if(empty($this->open[$c])) {
  292.             return $this->perform_on_item($item,$n);
  293.         }
  294.        
  295.         $this->count[$n]++;
  296.         return $this->walk_items($item[$name],$n);
  297.     }
  298.     function perform_on_item(&$item,$n) {
  299.         if($n == 'exists') {
  300.             return $this->item_exists($item);
  301.         }
  302.         if($n == 'fix') {
  303.             return $this->fix_item($item);
  304.         }
  305.         if($n == 'add') {
  306.             return $this->add_item($item);
  307.         }
  308.         if($n == 'value') {
  309.             return $this->add_value($item);
  310.         }
  311.     }
  312.    
  313.     function item_exists(&$item) {
  314.         if(is_array($item) and array_key_exists($this->name,$item)) {
  315.             return true;
  316.         }
  317.         return false;
  318.     }
  319.     function fix_item(&$item) {
  320.         if($this->is_list and ((is_array($item[$this->name]) and !$item[$this->name]['attributes']['list']) or !is_array($item[$this->name]))) {
  321.             $a = array();
  322.             /*if(is_array($item[$this->name]) and $item[$this->name]['attributes']) {
  323.                 $v = $item[$this->name]['value'];
  324.                 $a = $item[$this->name]['attributes'];
  325.             }
  326.             else {*/
  327.                 $v = $item[$this->name];
  328.                 $a = array();
  329.             //}
  330.  
  331.             $item[$this->name] = array(
  332.                 'attributes'    =>  array_merge($a,array(
  333.                     'list'  =>  true
  334.                 )),
  335.                 'value'         =>  array($v)
  336.             );
  337.         }
  338.         return true;
  339.     }
  340.     function add_item(&$item) {
  341.         if($this->attr) {
  342.             $v = array('attributes'=>$this->attr,'value'=>array());
  343.         }
  344.         else {
  345.             $v = array();
  346.         }
  347.  
  348.         if(is_array($item)) {
  349.             if($this->is_list) {
  350.                 $item[$this->name]['value'][count($item[$this->name]['value'])] = $v;
  351.             }
  352.             else {
  353.                 $item[$this->name] = $v;
  354.             }
  355.         }
  356.         else {
  357.             $this->parsed[$this->name] = $v;
  358.         }
  359.         return true;
  360.     }
  361.     function add_value(&$item) {
  362.         if(is_array($item['value'])) {
  363.             $item['value'] = is_scalar($item['value']) ? $item['value'].$this->parsed_value : $this->parsed_value;
  364.         }
  365.         else {
  366.             if(is_scalar($item)) {
  367.                 $item .= $this->parsed_value;
  368.             }
  369.             else {
  370.                 $item = $this->parsed_value;
  371.             }
  372.         }
  373.     }
  374.     function clean_parsed(&$a) {
  375.         if(is_array($a)) {
  376.             foreach($a as $k => &$v) {
  377.                 if($v['attributes']) {
  378.                     $this->clean_attributes($a[$k]);
  379.                 }
  380.                 if((is_array($v) and count($v) < 1) or (is_string($v) and strlen($v) < 1)) {
  381.                     unset($a[$k]);
  382.                 }
  383.                 $this->clean_parsed($a[$k]);
  384.             }
  385.         }
  386.     }
  387.     function clean_attributes(&$a) {
  388.         if(is_array($a) and $a['attributes']) {
  389.             foreach($a['attributes'] as $k => $v) {
  390.                 if($k == 'list') {
  391.                     unset($a['attributes'][$k]);
  392.                 }
  393.             }
  394.             if(count($a['attributes']) < 1) {
  395.                 $this->remove_attributes($a);
  396.             }
  397.         }
  398.     }
  399.     function remove_attributes(&$a) {
  400.         unset($a['attributes']);
  401.         $a = $a['value'];
  402.     }
  403.     function clean_value(&$a) {
  404.         unset($a);
  405.     }
  406.  
  407. }
  408.  
  409. }
  410.    
  411. /****************************************Terminate Script******************************************/
  412. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement