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