PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/applications/pm/libraries/SimpleXML.php

https://bitbucket.org/amitholkar/zenfile-18-05
PHP | 260 lines | 206 code | 26 blank | 28 comment | 65 complexity | 7fcd62ef9b81e3ba53833721e4811e41 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. //**************************************************
  3. // This class provides an alternative implementation of the SimpleXML API
  4. // that works under PHP 4, so applications that need to run under environments
  5. // on which only PHP 4 is available can take advantage of the SimpleXML API benefit
  6. // the original class was created by Taha Paksu
  7. // http://www.phpclasses.org/browse/package/4484.html
  8. // the original class was modified by Chris Brainard
  9. // floweringmind@gmail.com
  10. // so that it would work with codeigniter
  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. function xml_parse($data){
  18. $php_errormsg="";
  19. $this->result="";
  20. $this->evalCode="";
  21. $values="";
  22. $encoding = 'UTF-8';
  23. $parser = xml_parser_create($encoding);
  24. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  25. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  26. $ok = xml_parse_into_struct($parser, $data, $values);
  27. if (!$ok) {
  28. $errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
  29. xml_get_error_code($parser),
  30. xml_error_string(xml_get_error_code($parser)),
  31. xml_get_current_line_number($parser),
  32. xml_get_current_column_number($parser),
  33. xml_get_current_byte_index($parser));
  34. }
  35. xml_parser_free($parser);
  36. return $this->xml_reorganize($values);
  37. }
  38. function xml_reorganize($array)
  39. {
  40. $count = count($array);
  41. $repeat = $this->xml_tags($array);
  42. $repeatedone = false;
  43. $tags = array();
  44. $k = 0;
  45. for ($i = 0; $i < $count; $i++) {
  46. switch ($array[$i]['type']) {
  47. case 'open':
  48. array_push($tags, $array[$i]['tag']);
  49. if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))
  50. $k++;
  51. if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
  52. array_push($tags, '@content');
  53. $this->array_insert(count($tags), $tags, $array[$i]['value'], "open");
  54. array_pop($tags);
  55. }
  56. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  57. if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {
  58. array_push($tags, strval($k++));
  59. } else {
  60. $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
  61. array_push($tags, strval($k));
  62. }
  63. }
  64. if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {
  65. array_push($tags, '@attributes');
  66. foreach ($array[$i]['attributes'] as $attrkey => $attr) {
  67. array_push($tags, $attrkey);
  68. $this->array_insert(count($tags), $tags, $attr, "open");
  69. array_pop($tags);
  70. }
  71. array_pop($tags);
  72. }
  73. break;
  74. case 'close':
  75. array_pop($tags);
  76. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  77. if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {
  78. array_pop($tags);
  79. } else {
  80. $repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];
  81. array_pop($tags);
  82. }
  83. }
  84. break;
  85. case 'complete':
  86. array_push($tags, $array[$i]['tag']);
  87. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  88. if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {
  89. array_push($tags, strval($k));
  90. } else {
  91. $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
  92. array_push($tags, strval($k));
  93. }
  94. }
  95. if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
  96. if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
  97. array_push($tags, '@content');
  98. $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
  99. array_pop($tags);
  100. } else {
  101. $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
  102. }
  103. }
  104. if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
  105. array_push($tags, '@attributes');
  106. foreach ($array[$i]['attributes'] as $attrkey => $attr) {
  107. array_push($tags, $attrkey);
  108. $this->array_insert(count($tags), $tags, $attr, "complete");
  109. array_pop($tags);
  110. }
  111. array_pop($tags);
  112. }
  113. if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
  114. array_pop($tags);
  115. $k++;
  116. }
  117. array_pop($tags);
  118. break;
  119. }
  120. }
  121. eval($this->evalCode);
  122. $last = $this->array_reindex($this->result);
  123. return $last;
  124. }
  125. function array_insert($level, $tags, $value, $type)
  126. {
  127. $temp = '';
  128. for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {
  129. if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {
  130. if (is_numeric($tags[$c])) {
  131. $temp .= '[' . $tags[$c] . ']';
  132. } else {
  133. $temp .= '["' . $tags[$c] . '"]';
  134. }
  135. }
  136. }
  137. $this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";
  138. #echo $code. "\n";
  139. }
  140. /**
  141. * Define the repeated tags in XML file so we can set an index
  142. *
  143. * @param array $array
  144. * @return array
  145. */
  146. function xml_tags($array)
  147. { $repeats_temp = array();
  148. $repeats_count = array();
  149. $repeats = array();
  150. if (is_array($array)) {
  151. $n = count($array) - 1;
  152. for ($i = 0; $i < $n; $i++) {
  153. $idn = $array[$i]['tag'].$array[$i]['level'];
  154. if(in_array($idn,$repeats_temp)){
  155. $repeats_count[array_search($idn,$repeats_temp)]+=1;
  156. }else{
  157. array_push($repeats_temp,$idn);
  158. $repeats_count[array_search($idn,$repeats_temp)]=1;
  159. }
  160. }
  161. }
  162. $n = count($repeats_count);
  163. for($i=0;$i<$n;$i++){
  164. if($repeats_count[$i]>1){
  165. array_push($repeats,$repeats_temp[$i]);
  166. }
  167. }
  168. unset($repeats_temp);
  169. unset($repeats_count);
  170. return array_unique($repeats);
  171. }
  172. /**
  173. * Converts Array Variable to Object Variable
  174. *
  175. * @param array $arg_array
  176. * @return $tmp
  177. */
  178. function array2object ($arg_array)
  179. {
  180. if (is_array($arg_array)) {
  181. $keys = array_keys($arg_array);
  182. if(!is_numeric($keys[0])) $tmp = new Xml;
  183. foreach ($keys as $key) {
  184. if (is_numeric($key)) $has_number = true;
  185. if (is_string($key)) $has_string = true;
  186. }
  187. if (isset($has_number) and !isset($has_string)) {
  188. foreach ($arg_array as $key => $value) {
  189. $tmp[] = $this->array2object($value);
  190. }
  191. } elseif (isset($has_string)) {
  192. foreach ($arg_array as $key => $value) {
  193. if (is_string($key))
  194. $tmp->$key = $this->array2object($value);
  195. }
  196. }
  197. } elseif (is_object($arg_array)) {
  198. foreach ($arg_array as $key => $value) {
  199. if (is_array($value) or is_object($value))
  200. $tmp->$key = $this->array2object($value);
  201. else
  202. $tmp->$key = $value;
  203. }
  204. } else {
  205. $tmp = $arg_array;
  206. }
  207. return $tmp; //return the object
  208. }
  209. /**
  210. * Reindexes the whole array with ascending numbers
  211. *
  212. * @param array $array
  213. * @return array
  214. */
  215. function array_reindex($array)
  216. {
  217. if (is_array($array)) {
  218. if(count($array) == 1 && isset($array[0])) {
  219. return $this->array_reindex($array[0]);
  220. }else{
  221. foreach($array as $keys => $items) {
  222. if (is_array($items)) {
  223. if (is_numeric($keys)) {
  224. $array[$keys] = $this->array_reindex($items);
  225. } else {
  226. $array[$keys] = $this->array_reindex(array_merge(array(), $items));
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return $array;
  233. }
  234. }