PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/includes/class_simplexml.php

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