/simplexml.class.php

https://github.com/shafiqissani/BottomToolbar · PHP · 300 lines · 224 code · 26 blank · 50 comment · 68 complexity · 9e392289721632727505e8d90d8d0633 MD5 · raw file

  1. <?php
  2. /**
  3. * If the result will be an object, this container class is used.
  4. *
  5. */
  6. class SimpleXMLObject{
  7. function attributes(){
  8. $container = get_object_vars($this);
  9. return (object) $container["@attributes"];
  10. }
  11. function content(){
  12. $container = get_object_vars($this);
  13. return (object) $container["@content"];
  14. }
  15. }
  16. /**
  17. * The Main XML Parser Class
  18. *
  19. */
  20. class simplexml {
  21. var $result = array();
  22. var $ignore_level = 0;
  23. var $skip_empty_values = false;
  24. var $php_errormsg;
  25. var $evalCode="";
  26. /**
  27. * Adds Items to Array
  28. *
  29. * @param int $level
  30. * @param array $tags
  31. * @param $value
  32. * @param string $type
  33. */
  34. function array_insert($level, $tags, $value, $type)
  35. {
  36. $temp = '';
  37. for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {
  38. if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {
  39. if (is_numeric($tags[$c])) {
  40. $temp .= '[' . $tags[$c] . ']';
  41. } else {
  42. $temp .= '["' . $tags[$c] . '"]';
  43. }
  44. }
  45. }
  46. $this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";
  47. #echo $code. "\n";
  48. }
  49. /**
  50. * Define the repeated tags in XML file so we can set an index
  51. *
  52. * @param array $array
  53. * @return array
  54. */
  55. function xml_tags($array)
  56. { $repeats_temp = array();
  57. $repeats_count = array();
  58. $repeats = array();
  59. if (is_array($array)) {
  60. $n = count($array) - 1;
  61. for ($i = 0; $i < $n; $i++) {
  62. $idn = $array[$i]['tag'].$array[$i]['level'];
  63. if(in_array($idn,$repeats_temp)){
  64. $repeats_count[array_search($idn,$repeats_temp)]+=1;
  65. }else{
  66. array_push($repeats_temp,$idn);
  67. $repeats_count[array_search($idn,$repeats_temp)]=1;
  68. }
  69. }
  70. }
  71. $n = count($repeats_count);
  72. for($i=0;$i<$n;$i++){
  73. if($repeats_count[$i]>1){
  74. array_push($repeats,$repeats_temp[$i]);
  75. }
  76. }
  77. unset($repeats_temp);
  78. unset($repeats_count);
  79. return array_unique($repeats);
  80. }
  81. /**
  82. * Converts Array Variable to Object Variable
  83. *
  84. * @param array $arg_array
  85. * @return $tmp
  86. */
  87. function array2object ($arg_array)
  88. {
  89. if (is_array($arg_array)) {
  90. $keys = array_keys($arg_array);
  91. if(!is_numeric($keys[0])) $tmp = new SimpleXMLObject;
  92. foreach ($keys as $key) {
  93. if (is_numeric($key)) $has_number = true;
  94. if (is_string($key)) $has_string = true;
  95. }
  96. if (isset($has_number) and !isset($has_string)) {
  97. foreach ($arg_array as $key => $value) {
  98. $tmp[] = $this->array2object($value);
  99. }
  100. } elseif (isset($has_string)) {
  101. foreach ($arg_array as $key => $value) {
  102. if (is_string($key))
  103. $tmp->$key = $this->array2object($value);
  104. }
  105. }
  106. } elseif (is_object($arg_array)) {
  107. foreach ($arg_array as $key => $value) {
  108. if (is_array($value) or is_object($value))
  109. $tmp->$key = $this->array2object($value);
  110. else
  111. $tmp->$key = $value;
  112. }
  113. } else {
  114. $tmp = $arg_array;
  115. }
  116. return $tmp; //return the object
  117. }
  118. /**
  119. * Reindexes the whole array with ascending numbers
  120. *
  121. * @param array $array
  122. * @return array
  123. */
  124. function array_reindex($array)
  125. {
  126. if (is_array($array)) {
  127. if(count($array) == 1 && $array[0]){
  128. return $this->array_reindex($array[0]);
  129. }else{
  130. foreach($array as $keys => $items) {
  131. if (is_array($items)) {
  132. if (is_numeric($keys)) {
  133. $array[$keys] = $this->array_reindex($items);
  134. } else {
  135. $array[$keys] = $this->array_reindex(array_merge(array(), $items));
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return $array;
  142. }
  143. /**
  144. * Parse the XML generation to array object
  145. *
  146. * @param array $array
  147. * @return array
  148. */
  149. function xml_reorganize($array)
  150. {
  151. $count = count($array);
  152. $repeat = $this->xml_tags($array);
  153. $repeatedone = false;
  154. $tags = array();
  155. $k = 0;
  156. for ($i = 0; $i < $count; $i++) {
  157. switch ($array[$i]['type']) {
  158. case 'open':
  159. array_push($tags, $array[$i]['tag']);
  160. if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))
  161. $k++;
  162. if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
  163. array_push($tags, '@content');
  164. $this->array_insert(count($tags), $tags, $array[$i]['value'], "open");
  165. array_pop($tags);
  166. }
  167. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  168. if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {
  169. array_push($tags, strval($k++));
  170. } else {
  171. $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
  172. array_push($tags, strval($k));
  173. }
  174. }
  175. if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {
  176. array_push($tags, '@attributes');
  177. foreach ($array[$i]['attributes'] as $attrkey => $attr) {
  178. array_push($tags, $attrkey);
  179. $this->array_insert(count($tags), $tags, $attr, "open");
  180. array_pop($tags);
  181. }
  182. array_pop($tags);
  183. }
  184. break;
  185. case 'close':
  186. array_pop($tags);
  187. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  188. if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {
  189. array_pop($tags);
  190. } else {
  191. $repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];
  192. array_pop($tags);
  193. }
  194. }
  195. break;
  196. case 'complete':
  197. array_push($tags, $array[$i]['tag']);
  198. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  199. if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {
  200. array_push($tags, strval($k));
  201. } else {
  202. $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
  203. array_push($tags, strval($k));
  204. }
  205. }
  206. if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
  207. if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
  208. array_push($tags, '@content');
  209. $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
  210. array_pop($tags);
  211. } else {
  212. $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
  213. }
  214. }
  215. if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
  216. array_push($tags, '@attributes');
  217. foreach ($array[$i]['attributes'] as $attrkey => $attr) {
  218. array_push($tags, $attrkey);
  219. $this->array_insert(count($tags), $tags, $attr, "complete");
  220. array_pop($tags);
  221. }
  222. array_pop($tags);
  223. }
  224. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  225. array_pop($tags);
  226. $k++;
  227. }
  228. array_pop($tags);
  229. break;
  230. }
  231. }
  232. eval($this->evalCode);
  233. $last = $this->array_reindex($this->result);
  234. return $last;
  235. }
  236. /**
  237. * Get the XML contents and parse like SimpleXML
  238. *
  239. * @param string $file
  240. * @param string $resulttype
  241. * @param string $encoding
  242. * @return array/object
  243. */
  244. function xml_load_file($file, $resulttype = 'object', $encoding = 'UTF-8')
  245. {
  246. $php_errormsg="";
  247. $this->result="";
  248. $this->evalCode="";
  249. $values="";
  250. $data = file_get_contents($file);
  251. if (!$data)
  252. return 'Cannot open xml document: ' . (isset($php_errormsg) ? $php_errormsg : $file);
  253. $parser = xml_parser_create($encoding);
  254. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  255. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  256. $ok = xml_parse_into_struct($parser, $data, $values);
  257. if (!$ok) {
  258. $errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
  259. xml_get_error_code($parser),
  260. xml_error_string(xml_get_error_code($parser)),
  261. xml_get_current_line_number($parser),
  262. xml_get_current_column_number($parser),
  263. xml_get_current_byte_index($parser));
  264. }
  265. xml_parser_free($parser);
  266. if (!$ok)
  267. return $errmsg;
  268. if ($resulttype == 'array')
  269. return $this->xml_reorganize($values);
  270. // default $resulttype is 'object'
  271. return $this->array2object($this->xml_reorganize($values));
  272. }
  273. }
  274. ?>