[35] | 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 | ###################################################################################
|
---|
[42] | 16 | if( ! function_exists( 'xpress_XML_unserialize' ) ) : // d3pipe Used function
|
---|
| 17 | function & xpress_XML_unserialize(&$xml){
|
---|
[836] | 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 | }
|
---|
[35] | 23 | $data = &$xml_parser->parse($xml);
|
---|
| 24 | $xml_parser->destruct();
|
---|
| 25 | return $data;
|
---|
| 26 | }
|
---|
[39] | 27 | endif;
|
---|
[35] | 28 | ###################################################################################
|
---|
| 29 | # XML_serialize: serializes any PHP data structure into XML
|
---|
| 30 | # Takes one parameter: the data to serialize. Must be an array.
|
---|
| 31 | ###################################################################################
|
---|
[42] | 32 | if( ! function_exists( 'xpress_XML_serialize' ) ) : // d3pipe Used function
|
---|
| 33 | function & xpress_XML_serialize(&$data, $level = 0, $prior_key = NULL){
|
---|
[35] | 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)){
|
---|
[42] | 41 | xpress_XML_serialize($value, $level, $key);
|
---|
[35] | 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";
|
---|
[42] | 53 | else echo ">\n",xpress_XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
|
---|
[35] | 54 | }
|
---|
| 55 | reset($data);
|
---|
| 56 | if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
|
---|
| 57 | }
|
---|
[39] | 58 | endif;
|
---|
[35] | 59 | ###################################################################################
|
---|
| 60 | # XML class: utility class to be used with PHP's XML handling functions
|
---|
| 61 | ###################################################################################
|
---|
[42] | 62 | class xpress_XML{
|
---|
[35] | 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 |
|
---|
[42] | 69 | function xpress_XML(){
|
---|
[39] | 70 | // $this->parser = &xml_parser_create();
|
---|
| 71 | $this->parser = xml_parser_create(); // GIJ
|
---|
| 72 | /*
|
---|
[35] | 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');
|
---|
[39] | 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');
|
---|
[35] | 82 | }
|
---|
[39] | 83 | function destruct(){
|
---|
| 84 | // xml_parser_free(&$this->parser); // GIJ
|
---|
| 85 | xml_parser_free($this->parser);
|
---|
| 86 | }
|
---|
[35] | 87 | function & parse(&$data){
|
---|
| 88 | $this->document = array();
|
---|
| 89 | $this->stack = array();
|
---|
| 90 | $this->parent = &$this->document;
|
---|
[353] | 91 |
|
---|
| 92 | //libxml2 ver.2.7.0 -2.7.2 stripping leading angle brackets bug patch
|
---|
| 93 | if (
|
---|
[387] | 94 | $GLOBALS['DO_LIBXML_PATCH'] == '1'
|
---|
| 95 | || LIBXML_DOTTED_VERSION == '2.7.0'
|
---|
[353] | 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('<','<',$data );
|
---|
| 104 | $data =str_replace('>','>',$data );
|
---|
| 105 | $data =str_replace('&','&',$data );
|
---|
[615] | 106 | $data =str_replace('"','"',$data );
|
---|
| 107 | $data =str_replace(''',''',$data );
|
---|
[353] | 108 | }
|
---|
| 109 | //end Fix
|
---|
| 110 |
|
---|
[39] | 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 ;
|
---|
[35] | 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
|
---|
[42] | 121 | $key = xpress_count_numeric_items($this->parent[$tag]);
|
---|
[35] | 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 | }
|
---|
[40] | 154 |
|
---|
[42] | 155 | if( ! function_exists( 'xpress_count_numeric_items' ) ) : // d3pipe Used function
|
---|
| 156 | function xpress_count_numeric_items(&$array){
|
---|
[35] | 157 | return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
|
---|
| 158 | }
|
---|
[40] | 159 | endif;
|
---|
[35] | 160 | ?> |
---|