Improved Code Sniffing (#928)
[openemr.git] / library / xmltoarray_parser_htmlfix.php
blobefada493d4c2dc6beeb95636df1a1cde359d1270
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Vinish K <vinish@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 * Proper usage for xmltoarray_parser_htmlfix class:
29 * $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
30 * $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE, 1); //set options same as xml_parser_set_option
31 * $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING, 0);
32 * $xmltoarray->xmlparser_fix_into_struct($xmlstring); //fixes html values for XML
33 * $array = $xmltoarray->createArray(); //creates an array with fixed html values
34 * foreach($array as $key => $value){
35 * $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
36 * }
38 class xmltoarray_parser_htmlfix{
39 var $values;
40 var $index;
41 var $thearray;
42 var $parser;
44 /**
45 * Default constructor for xmltoarray_parser_htmlfix.
47 function xmltoarray_parser_htmlfix()
49 $this->values = array();
50 $this->index = array();
51 $this->thearray = array();
52 $this->parser = xml_parser_create();
55 /**
56 * xmlparser_setoption sets XML options based on xml_parser_set_option options.
57 * @param $optionName - The name of the option from the xml_parser_set_option list.
58 * @param $value - The value to set for the option.
60 function xmlparser_setoption($optionName, $value)
62 xml_parser_set_option($this->parser, $optionName, $value);
65 /**
66 * xmlparser_fix_into_struct fixes the XML and passes the XML into the struct parser.
67 * @param $xml - A string XML value.
69 function xmlparser_fix_into_struct($xml)
71 $trans_table = get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
72 $keys = array();
73 foreach($trans_table as $key=>$value) {
74 if($key != "<" && $key != ">" && $key != "&" && $key != "\"" && $key != "'" && $key != " "){
75 $keys[$key] = $value;
78 foreach($keys as $key=>$value){
79 $xml = preg_replace("/".$key."/",$value,$xml);
81 $xml = str_replace("&","%and%",$xml);
83 xml_parse_into_struct($this->parser, $xml, $this->values, $this->index);
84 xml_parser_free($this->parser);
87 /**
88 * createArray creates and returns the array.
89 * @return The associative XML array.
91 function createArray()
93 $i = 0;
94 $name = isset($this->values[$i]['tag']) ? $this->values[$i]['tag']: '';
95 $this->thearray[$name] = isset($this->values[$i]['attributes']) ? $this->values[$i]['attributes'] : '';
96 $this->thearray[$name] = $this->_struct_to_array($this->values, $i);
97 return $this->thearray;
98 }//createArray
101 * _struct_to_array is a recursive function that takes the values and creates the array.
102 * @param $values - The values of the XML
103 * @param &$i - The index value
104 * @return The child
106 function _struct_to_array($values, &$i)
108 $child = array();
109 if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
111 while ($i++ < count($values)) {
112 if(isset($values[$i])){
113 switch ($values[$i]['type']) {
114 case 'cdata':
115 array_push($child, $values[$i]['value']);
116 break;
118 case 'complete':
119 $name = $values[$i]['tag'];
120 if(!empty($name)){
121 $child[$name]= (isset($values[$i]['value']))?($values[$i]['value']):'';
122 if(isset($values[$i]['attributes'])) {
123 $child[$name] = $values[$i]['attributes'];
126 break;
128 case 'open':
129 $name = $values[$i]['tag'];
130 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
131 $child[$name][$size] = $this->_struct_to_array($values, $i);
132 break;
134 case 'close':
135 return $child;
136 break;
140 return $child;
141 }//_struct_to_array
144 * fix_html_entities replaces all instances of '%and%' with '&', since the xml_parser can't handle '&'.
145 * @param $string - A string value.
146 * @return A fixed string with & instead of %and%.
148 function fix_html_entities($string)
150 $string = str_replace("%and%","&",$string);
151 return $string;