XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/include/xml.php @ 852

Last change on this file since 852 was 836, checked in by toemon, 11 years ago

PHP5.4 Fatal error: Call-time pass-by-reference fixed #424

File size: 6.4 KB
Line 
1<?php
2###################################################################################
3#
4# XML Library, by Keith Devens, version 1.2b
5# http://keithdevens.com/software/phpxml
6#
7# This code is Open Source, released under terms similar to the Artistic License.
8# Read the license at http://keithdevens.com/software/license
9#
10###################################################################################
11
12###################################################################################
13# XML_unserialize: takes raw XML as a parameter (a string)
14# and returns an equivalent PHP data structure
15###################################################################################
16if( ! function_exists( 'xpress_XML_unserialize' ) ) : // d3pipe Used function
17function & xpress_XML_unserialize(&$xml){
18        if ( version_compare(PHP_VERSION, '5.0.0', '>')) {
19                eval('$xml_parser = new xpress_XML();');
20        }else {
21                eval('$xml_parser = &new xpress_XML();');
22        }
23        $data = &$xml_parser->parse($xml);
24        $xml_parser->destruct();
25        return $data;
26}
27endif;
28###################################################################################
29# XML_serialize: serializes any PHP data structure into XML
30# Takes one parameter: the data to serialize. Must be an array.
31###################################################################################
32if( ! function_exists( 'xpress_XML_serialize' ) ) : // d3pipe Used function
33function & xpress_XML_serialize(&$data, $level = 0, $prior_key = NULL){
34        if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
35        while(list($key, $value) = each($data))
36                if(!strpos($key, ' attr')) #if it's not an attribute
37                        #we don't treat attributes by themselves, so for an empty element
38                        # that has attributes you still need to set the element to NULL
39
40                        if(is_array($value) and array_key_exists(0, $value)){
41                                xpress_XML_serialize($value, $level, $key);
42                        }else{
43                                $tag = $prior_key ? $prior_key : $key;
44                                echo str_repeat("\t", $level),'<',$tag;
45                                if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
46                                        while(list($attr_name, $attr_value) = each($data["$key attr"]))
47                                                echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
48                                        reset($data["$key attr"]);
49                                }
50
51                                if(is_null($value)) echo " />\n";
52                                elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
53                                else echo ">\n",xpress_XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
54                        }
55        reset($data);
56        if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
57}
58endif;
59###################################################################################
60# XML class: utility class to be used with PHP's XML handling functions
61###################################################################################
62class xpress_XML{
63        var $parser;   #a reference to the XML parser
64        var $document; #the entire XML structure built up so far
65        var $parent;   #a pointer to the current parent - the parent will be an array
66        var $stack;    #a stack of the most recent parent at each nesting level
67        var $last_opened_tag; #keeps track of the last tag opened.
68
69        function xpress_XML(){
70                // $this->parser = &xml_parser_create();
71                $this->parser = xml_parser_create(); // GIJ
72                /*
73                xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
74                xml_set_object(&$this->parser, &$this);
75                xml_set_element_handler(&$this->parser, 'open','close');
76                xml_set_character_data_handler(&$this->parser, 'data');
77                */
78                xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
79                xml_set_object($this->parser, $this);
80                xml_set_element_handler($this->parser, 'open','close');
81                xml_set_character_data_handler($this->parser, 'data');
82        }
83        function destruct(){
84                // xml_parser_free(&$this->parser); // GIJ
85                xml_parser_free($this->parser);
86        }
87        function & parse(&$data){
88                $this->document = array();
89                $this->stack    = array();
90                $this->parent   = &$this->document;
91               
92                //libxml2 ver.2.7.0 -2.7.2 stripping leading angle brackets bug patch
93                if (
94                        $GLOBALS['DO_LIBXML_PATCH'] == '1'
95                        || LIBXML_DOTTED_VERSION == '2.7.0'
96                        || LIBXML_DOTTED_VERSION == '2.7.1'
97                        || LIBXML_DOTTED_VERSION == '2.7.2'
98                        || (
99                                LIBXML_DOTTED_VERSION == '2.7.3'
100                                && version_compare( PHP_VERSION, '5.2.9', '<' )
101                        )
102                ) {
103                        $data =str_replace('&lt;','&#60;',$data );
104                        $data =str_replace('&gt;','&#62;',$data );
105                        $data =str_replace('&amp;','&#38;',$data );
106                        $data =str_replace('&quot;','&#34;',$data );
107                        $data =str_replace('&apos;','&#39;',$data );
108                }
109                //end Fix
110               
111                // return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; // GIJ
112                $ret = @xml_parse($this->parser, $data, true) ? $this->document : NULL;
113                return $ret ;
114        }
115        function open(&$parser, $tag, $attributes){
116                $this->data = ''; #stores temporary cdata
117                $this->last_opened_tag = $tag;
118                if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
119                        if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
120                                #this is the third or later instance of $tag we've come across
121                                $key = xpress_count_numeric_items($this->parent[$tag]);
122                        }else{
123                                #this is the second instance of $tag that we've seen. shift around
124                                if(array_key_exists("$tag attr",$this->parent)){
125                                        $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
126                                        unset($this->parent["$tag attr"]);
127                                }else{
128                                        $arr = array(&$this->parent[$tag]);
129                                }
130                                $this->parent[$tag] = &$arr;
131                                $key = 1;
132                        }
133                        $this->parent = &$this->parent[$tag];
134                }else{
135                        $key = $tag;
136                }
137                if($attributes) $this->parent["$key attr"] = $attributes;
138                $this->parent  = &$this->parent[$key];
139                $this->stack[] = &$this->parent;
140        }
141        function data(&$parser, $data){
142                if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
143                        $this->data .= $data;
144        }
145        function close(&$parser, $tag){
146                if($this->last_opened_tag == $tag){
147                        $this->parent = $this->data;
148                        $this->last_opened_tag = NULL;
149                }
150                array_pop($this->stack);
151                if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
152        }
153}
154
155if( ! function_exists( 'xpress_count_numeric_items' ) ) : // d3pipe Used function
156function xpress_count_numeric_items(&$array){
157        return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
158}
159endif;
160?>
Note: See TracBrowser for help on using the repository browser.