PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/xml2json.php

https://github.com/mx504/Provisioner
PHP | 374 lines | 328 code | 12 blank | 34 comment | 78 complexity | f7560e2c5b8cf2e64b66214876b38fc4 MD5 | raw file
  1. <?php
  2. define("PROVISIONER_PATH", "");
  3. $request = isset($_REQUEST['request']) ? $_REQUEST['request'] : '';
  4. $brand = isset($_REQUEST['brand']) ? $_REQUEST['brand'] : '';
  5. $product = isset($_REQUEST['product']) ? $_REQUEST['product'] : '';
  6. $model = isset($_REQUEST['model']) ? $_REQUEST['model'] : '';
  7. $show_array = isset($_REQUEST['show_array']) ? TRUE : FALSE;
  8. $hide_loops = isset($_REQUEST['hide_loops']) ? TRUE : FALSE;
  9. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  10. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  11. switch($request) {
  12. case 'list':
  13. $list_array = array();
  14. if(file_exists(PROVISIONER_PATH."endpoint/master.xml")) {
  15. $master = xml2array(PROVISIONER_PATH."endpoint/master.xml");
  16. foreach($master['data']['brands'] as $brand_list) {
  17. if(file_exists(PROVISIONER_PATH."endpoint/".$brand_list['directory']."/brand_data.xml")) {
  18. $brand_data = array();
  19. $brand_data = xml2array(PROVISIONER_PATH."endpoint/".$brand_list['directory']."/brand_data.xml");
  20. $family_list_array = fix_single_array_keys($brand_data['data']['brands']['family_list']['family']);
  21. foreach($family_list_array as $family_list) {
  22. if(file_exists(PROVISIONER_PATH."endpoint/".$brand_list['directory']."/".$family_list['directory']."/family_data.xml")) {
  23. $family_data = array();
  24. $family_data = xml2array(PROVISIONER_PATH."endpoint/".$brand_list['directory']."/".$family_list['directory']."/family_data.xml");
  25. $model_list_array = fix_single_array_keys($family_data['data']['model_list']);
  26. foreach($model_list_array as $model_list) {
  27. $list_array[$brand_list['directory']][$family_list['directory']][] = $model_list['model'];
  28. }
  29. }
  30. }
  31. }
  32. }
  33. if($show_array) {
  34. echo "<pre>";
  35. print_r($list_array);
  36. } else {
  37. echo json_encode($list_array);
  38. }
  39. } else {
  40. die('cant find master.xml');
  41. }
  42. break;
  43. case 'data':
  44. if(!empty($brand) && empty($product) && empty($model)) {
  45. if(file_exists(PROVISIONER_PATH."endpoint/".$brand."/brand_data.xml")) {
  46. $temp = xml2array(PROVISIONER_PATH."endpoint/".$brand."/brand_data.xml");
  47. if($show_array) {
  48. echo "<pre>";
  49. print_r($temp['data']['brands']);
  50. } else {
  51. echo json_encode($temp['data']['brands']);
  52. }
  53. } else {
  54. die('No brand_data.xml for '.$brand);
  55. }
  56. } elseif(!empty($brand) && !empty($product) && empty($model)) {
  57. if(file_exists(PROVISIONER_PATH."endpoint/".$brand."/".$product."/family_data.xml")) {
  58. $temp = xml2array(PROVISIONER_PATH."endpoint/".$brand."/".$product."/family_data.xml");
  59. if($show_array) {
  60. echo "<pre>";
  61. print_r($temp['data']);
  62. } else {
  63. echo json_encode($temp['data']);
  64. }
  65. } else {
  66. die('No family_data.xml for '.$product);
  67. }
  68. } elseif(!empty($brand) && !empty($product) && !empty($model)) {
  69. if(file_exists(PROVISIONER_PATH."endpoint/".$brand."/".$product."/family_data.xml")) {
  70. $data = array();
  71. $temp = xml2array(PROVISIONER_PATH."endpoint/".$brand."/".$product."/family_data.xml");
  72. $test = arraysearchrecursive($model,$temp,'model');
  73. if(!$test) {
  74. die('cant find model');
  75. }
  76. $data['phone_data']['brand'] = $brand;
  77. $data['phone_data']['product'] = $product;
  78. $data['phone_data']['model'] = $model;
  79. $data['lines'] = $temp['data']['model_list'][$test[2]]['lines'];
  80. $files = fix_single_array_keys($temp['data']['model_list'][$test[2]]['template_data']['files']);
  81. if(!is_array($files)) {
  82. $file[0] = $files;
  83. $files = $file;
  84. }
  85. foreach($files as $files_data) {
  86. if(file_exists(PROVISIONER_PATH."endpoint/".$brand."/".$product."/".$files_data)) {
  87. $temp_files_data = xml2array(PROVISIONER_PATH."endpoint/".$brand."/".$product."/".$files_data);
  88. $categories = fix_single_array_keys($temp_files_data['template_data']['category']);
  89. foreach($categories as $cat_data) {
  90. $subcategories = fix_single_array_keys($cat_data['subcategory']);
  91. foreach($subcategories as $subcat_data) {
  92. $items = fix_single_array_keys($subcat_data['item']);
  93. $items_loop = array();
  94. foreach($items as $key => $loop_data) {
  95. if($loop_data['type'] == 'loop') {
  96. for($i = $loop_data['loop_start']; $i <= $loop_data['loop_end']; $i++) {
  97. $name = explode("_",$loop_data['data']['item'][0]['variable']);
  98. $var_nam = "loop|".str_replace("\$","",$name[0])."_".$i;
  99. foreach($loop_data['data']['item'] as $item_loop) {
  100. if($item_loop['type'] != 'break') {
  101. $z_tmp = explode("_",$item_loop['variable']);
  102. $z = $z_tmp[1];
  103. $items_loop[$var_nam][$z] = $item_loop;
  104. $items_loop[$var_nam][$z]['description'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['description']);
  105. $items_loop[$var_nam][$z]['variable'] = str_replace('_', '_'.$i.'_', $items_loop[$var_nam][$z]['variable']);
  106. $items_loop[$var_nam][$z]['default_value'] = isset($items_loop[$var_nam][$z]['default_value']) ? $items_loop[$var_nam][$z]['default_value'] : '';
  107. $items_loop[$var_nam][$z]['loop'] = TRUE;
  108. $items_loop[$var_nam][$z]['loop_count'] = $i;
  109. }
  110. }
  111. }
  112. $items = array();
  113. $items = $items_loop;
  114. } elseif($loop_data['type'] == 'loop_line_options') {
  115. for($i = 1; $i <= $data['lines']; $i++) {
  116. $var_nam = "lineloop|line_".$i;
  117. foreach($loop_data['data']['item'] as $item_loop) {
  118. if($item_loop['type'] != 'break') {
  119. $z = str_replace("\$","",$item_loop['variable']);
  120. $items_loop[$var_nam][$z] = $item_loop;
  121. $items_loop[$var_nam][$z]['description'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['description']);
  122. //$items_loop[$var_nam][$z]['variable'] = str_replace('_', '_'.$i.'_', $items_loop[$var_nam][$z]['variable']);
  123. $items_loop[$var_nam][$z]['default_value'] = fix_single_array_keys($items_loop[$var_nam][$z]['default_value']);
  124. $items_loop[$var_nam][$z]['default_value'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['default_value']);
  125. $items_loop[$var_nam][$z]['line_loop'] = TRUE;
  126. $items_loop[$var_nam][$z]['line_count'] = $i;
  127. }
  128. }
  129. }
  130. $items = array();
  131. $items = $items_loop;
  132. } elseif($loop_data['type'] == 'break') {
  133. unset($items[$key]);
  134. } else {
  135. unset($items[$key]);
  136. $var_nam = "option|".str_replace("\$","",$loop_data['variable']);
  137. $items[$var_nam] = $loop_data;
  138. }
  139. }
  140. if(array_key_exists($subcat_data['name'],$data['data'][$cat_data['name']])) {
  141. $oldsub = $data['data'][$cat_data['name']][$subcat_data['name']];
  142. $data['data'][$cat_data['name']][$subcat_data['name']] = array_merge($oldsub,$items);
  143. } else {
  144. $data['data'][$cat_data['name']][$subcat_data['name']] = $items;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if($show_array) {
  151. echo "<pre>";
  152. print_r($data);
  153. } else {
  154. echo json_encode($data);
  155. }
  156. } else {
  157. die('No family_data.xml for '.$product);
  158. }
  159. }
  160. break;
  161. default:
  162. die('nothing defined');
  163. }
  164. //FUNCTIONS BELOW------------------
  165. /**
  166. * Search Recursively through an array
  167. * @param string $Needle
  168. * @param array $Haystack
  169. * @param string $NeedleKey
  170. * @param boolean $Strict
  171. * @param array $Path
  172. * @return array
  173. */
  174. function arraysearchrecursive($Needle, $Haystack, $NeedleKey="", $Strict=false, $Path=array()) {
  175. if (!is_array($Haystack))
  176. return false;
  177. foreach ($Haystack as $Key => $Val) {
  178. if (is_array($Val) &&
  179. $SubPath = arraysearchrecursive($Needle, $Val, $NeedleKey, $Strict, $Path)) {
  180. $Path = array_merge($Path, Array($Key), $SubPath);
  181. return $Path;
  182. } elseif ((!$Strict && $Val == $Needle &&
  183. $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key)) ||
  184. ($Strict && $Val === $Needle &&
  185. $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key))) {
  186. $Path[] = $Key;
  187. return $Path;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Function xml2array has a bad habit of returning blank xml values as empty arrays.
  194. * Also if the xml children only loops once then the array is put into a normal array (array[variable]).
  195. * However if it loops more than once then it is put into a counted array (array[0][variable])
  196. * We fix that issue here by returning blank values on empty arrays or always returning array[0]
  197. * @param array $array
  198. * @return mixed
  199. * @author Karl Anderson
  200. */
  201. function fix_single_array_keys($array) {
  202. if (!is_array($array)) {
  203. return $array;
  204. }
  205. if((empty($array[0])) AND (!empty($array))) {
  206. $array_n[0] = $array;
  207. return($array_n);
  208. }
  209. return empty($array) ? '' : $array;
  210. /*
  211. if((empty($array[0])) AND (!empty($array))) {
  212. $array_n[0] = $array;
  213. return($array_n);
  214. } elseif(!empty($array)) {
  215. return($array);
  216. //This is so stupid?! PHP gets confused.
  217. } elseif($array == '0') {
  218. return($array);
  219. } else {
  220. return("");
  221. }
  222. * *
  223. */
  224. }
  225. function xml2array($url, $get_attributes = 1, $priority = 'tag')
  226. {
  227. $contents = "";
  228. if (!function_exists('xml_parser_create'))
  229. {
  230. return array ();
  231. }
  232. $parser = xml_parser_create('');
  233. if (!($fp = @ fopen($url, 'rb')))
  234. {
  235. return array ();
  236. }
  237. while (!feof($fp))
  238. {
  239. $contents .= fread($fp, 8192);
  240. }
  241. fclose($fp);
  242. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  243. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  244. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  245. xml_parse_into_struct($parser, trim($contents), $xml_values);
  246. xml_parser_free($parser);
  247. if (!$xml_values)
  248. return; //Hmm...
  249. $xml_array = array ();
  250. $parents = array ();
  251. $opened_tags = array ();
  252. $arr = array ();
  253. $current = & $xml_array;
  254. $repeated_tag_index = array ();
  255. foreach ($xml_values as $data)
  256. {
  257. unset ($attributes, $value);
  258. extract($data);
  259. $result = array ();
  260. $attributes_data = array ();
  261. if (isset ($value))
  262. {
  263. if ($priority == 'tag')
  264. $result = $value;
  265. else
  266. $result['value'] = $value;
  267. }
  268. if (isset ($attributes) and $get_attributes)
  269. {
  270. foreach ($attributes as $attr => $val)
  271. {
  272. if ($priority == 'tag')
  273. $attributes_data[$attr] = $val;
  274. else
  275. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  276. }
  277. }
  278. if ($type == "open")
  279. {
  280. $parent[$level -1] = & $current;
  281. if (!is_array($current) or (!in_array($tag, array_keys($current))))
  282. {
  283. $current[$tag] = $result;
  284. if ($attributes_data)
  285. $current[$tag . '_attr'] = $attributes_data;
  286. $repeated_tag_index[$tag . '_' . $level] = 1;
  287. $current = & $current[$tag];
  288. }
  289. else
  290. {
  291. if (isset ($current[$tag][0]))
  292. {
  293. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  294. $repeated_tag_index[$tag . '_' . $level]++;
  295. }
  296. else
  297. {
  298. $current[$tag] = array (
  299. $current[$tag],
  300. $result
  301. );
  302. $repeated_tag_index[$tag . '_' . $level] = 2;
  303. if (isset ($current[$tag . '_attr']))
  304. {
  305. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  306. unset ($current[$tag . '_attr']);
  307. }
  308. }
  309. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  310. $current = & $current[$tag][$last_item_index];
  311. }
  312. }
  313. elseif ($type == "complete")
  314. {
  315. if (!isset ($current[$tag]))
  316. {
  317. $current[$tag] = $result;
  318. $repeated_tag_index[$tag . '_' . $level] = 1;
  319. if ($priority == 'tag' and $attributes_data)
  320. $current[$tag . '_attr'] = $attributes_data;
  321. }
  322. else
  323. {
  324. if (isset ($current[$tag][0]) and is_array($current[$tag]))
  325. {
  326. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  327. if ($priority == 'tag' and $get_attributes and $attributes_data)
  328. {
  329. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  330. }
  331. $repeated_tag_index[$tag . '_' . $level]++;
  332. }
  333. else
  334. {
  335. $current[$tag] = array (
  336. $current[$tag],
  337. $result
  338. );
  339. $repeated_tag_index[$tag . '_' . $level] = 1;
  340. if ($priority == 'tag' and $get_attributes)
  341. {
  342. if (isset ($current[$tag . '_attr']))
  343. {
  344. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  345. unset ($current[$tag . '_attr']);
  346. }
  347. if ($attributes_data)
  348. {
  349. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  350. }
  351. }
  352. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  353. }
  354. }
  355. }
  356. elseif ($type == 'close')
  357. {
  358. $current = & $parent[$level -1];
  359. }
  360. }
  361. return ($xml_array);
  362. }