[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 (
|
---|
[387] | 90 | $GLOBALS['DO_LIBXML_PATCH'] == '1'
|
---|
| 91 | || LIBXML_DOTTED_VERSION == '2.7.0'
|
---|
[353] | 92 | || LIBXML_DOTTED_VERSION == '2.7.1'
|
---|
| 93 | || LIBXML_DOTTED_VERSION == '2.7.2'
|
---|
| 94 | || (
|
---|
| 95 | LIBXML_DOTTED_VERSION == '2.7.3'
|
---|
| 96 | && version_compare( PHP_VERSION, '5.2.9', '<' )
|
---|
| 97 | )
|
---|
| 98 | ) {
|
---|
| 99 | $data =str_replace('<','<',$data );
|
---|
| 100 | $data =str_replace('>','>',$data );
|
---|
| 101 | $data =str_replace('&','&',$data );
|
---|
[615] | 102 | $data =str_replace('"','"',$data );
|
---|
| 103 | $data =str_replace(''',''',$data );
|
---|
[353] | 104 | }
|
---|
| 105 | //end Fix
|
---|
| 106 |
|
---|
[39] | 107 | // return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; // GIJ
|
---|
| 108 | $ret = @xml_parse($this->parser, $data, true) ? $this->document : NULL;
|
---|
| 109 | return $ret ;
|
---|
[35] | 110 | }
|
---|
| 111 | function open(&$parser, $tag, $attributes){
|
---|
| 112 | $this->data = ''; #stores temporary cdata
|
---|
| 113 | $this->last_opened_tag = $tag;
|
---|
| 114 | if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
|
---|
| 115 | if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
|
---|
| 116 | #this is the third or later instance of $tag we've come across
|
---|
[42] | 117 | $key = xpress_count_numeric_items($this->parent[$tag]);
|
---|
[35] | 118 | }else{
|
---|
| 119 | #this is the second instance of $tag that we've seen. shift around
|
---|
| 120 | if(array_key_exists("$tag attr",$this->parent)){
|
---|
| 121 | $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
|
---|
| 122 | unset($this->parent["$tag attr"]);
|
---|
| 123 | }else{
|
---|
| 124 | $arr = array(&$this->parent[$tag]);
|
---|
| 125 | }
|
---|
| 126 | $this->parent[$tag] = &$arr;
|
---|
| 127 | $key = 1;
|
---|
| 128 | }
|
---|
| 129 | $this->parent = &$this->parent[$tag];
|
---|
| 130 | }else{
|
---|
| 131 | $key = $tag;
|
---|
| 132 | }
|
---|
| 133 | if($attributes) $this->parent["$key attr"] = $attributes;
|
---|
| 134 | $this->parent = &$this->parent[$key];
|
---|
| 135 | $this->stack[] = &$this->parent;
|
---|
| 136 | }
|
---|
| 137 | function data(&$parser, $data){
|
---|
| 138 | if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
|
---|
| 139 | $this->data .= $data;
|
---|
| 140 | }
|
---|
| 141 | function close(&$parser, $tag){
|
---|
| 142 | if($this->last_opened_tag == $tag){
|
---|
| 143 | $this->parent = $this->data;
|
---|
| 144 | $this->last_opened_tag = NULL;
|
---|
| 145 | }
|
---|
| 146 | array_pop($this->stack);
|
---|
| 147 | if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[40] | 150 |
|
---|
[42] | 151 | if( ! function_exists( 'xpress_count_numeric_items' ) ) : // d3pipe Used function
|
---|
| 152 | function xpress_count_numeric_items(&$array){
|
---|
[35] | 153 | return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
|
---|
| 154 | }
|
---|
[40] | 155 | endif;
|
---|
[35] | 156 | ?> |
---|