PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website trường trung học phổ thông PHP/lc1/includes/phpsvnclient/xml_parser.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 427 lines | 386 code | 27 blank | 14 comment | 60 complexity | f886b3a3e5250a24ebf962fa81a9d4ea MD5 | raw file
  1. <?php
  2. /*
  3. * xml_parser.php
  4. *
  5. * @(#) $Header: /home/mlemos/cvsroot/xmlparser/xml_parser.php,v 1.19 2006/11/22 01:25:05 mlemos Exp $
  6. *
  7. */
  8. /*
  9. * Parser error numbers:
  10. *
  11. * 1 - Could not create the XML parser
  12. * 2 - Could not parse data
  13. * 3 - Could not read from input stream
  14. *
  15. */
  16. $xml_parser_handlers=array();
  17. Function xml_parser_start_element_handler($parser,$name,$attrs)
  18. {
  19. global $xml_parser_handlers;
  20. if(!strcmp($xml_parser_handlers[$parser]->error,""))
  21. $xml_parser_handlers[$parser]->StartElement($xml_parser_handlers[$parser],$name,$attrs);
  22. }
  23. Function xml_parser_end_element_handler($parser,$name)
  24. {
  25. global $xml_parser_handlers;
  26. if(!strcmp($xml_parser_handlers[$parser]->error,""))
  27. $xml_parser_handlers[$parser]->EndElement($xml_parser_handlers[$parser],$name);
  28. }
  29. Function xml_parser_character_data_handler($parser,$data)
  30. {
  31. global $xml_parser_handlers;
  32. if(!strcmp($xml_parser_handlers[$parser]->error,""))
  33. $xml_parser_handlers[$parser]->CharacterData($xml_parser_handlers[$parser],$data);
  34. }
  35. class xml_parser_handler_class
  36. {
  37. var $xml_parser;
  38. var $error_number=0;
  39. var $error="";
  40. var $error_code=0;
  41. var $error_line,$error_column,$error_byte_index;
  42. var $structure=array();
  43. var $positions=array();
  44. var $path="";
  45. var $store_positions=0;
  46. var $simplified_xml=0;
  47. var $fail_on_non_simplified_xml=0;
  48. Function SetError(&$object,$error_number,$error)
  49. {
  50. $object->error_number=$error_number;
  51. $object->error=$error;
  52. $object->error_line=xml_get_current_line_number($object->xml_parser);
  53. $object->error_column=xml_get_current_column_number($object->xml_parser);
  54. $object->error_byte_index=xml_get_current_byte_index($object->xml_parser);
  55. }
  56. Function SetElementData(&$object,$path,&$data)
  57. {
  58. $object->structure[$path]=$data;
  59. if($object->store_positions)
  60. {
  61. $object->positions[$path]=array(
  62. "Line"=>xml_get_current_line_number($object->xml_parser),
  63. "Column"=>xml_get_current_column_number($object->xml_parser),
  64. "Byte"=>xml_get_current_byte_index($object->xml_parser)
  65. );
  66. }
  67. }
  68. Function StartElement(&$object,$name,&$attrs)
  69. {
  70. if(strcmp($this->path,""))
  71. {
  72. $element=$object->structure[$this->path]["Elements"];
  73. $object->structure[$this->path]["Elements"]++;
  74. $this->path.=",$element";
  75. }
  76. else
  77. {
  78. $element=0;
  79. $this->path="0";
  80. }
  81. $data=array(
  82. "Tag"=>$name,
  83. "Elements"=>0
  84. );
  85. if($object->simplified_xml)
  86. {
  87. if($object->fail_on_non_simplified_xml
  88. && count($attrs)>0)
  89. {
  90. $this->SetError($object,2,"Simplified XML can not have attributes in tags");
  91. return;
  92. }
  93. }
  94. else
  95. $data["Attributes"]=$attrs;
  96. $this->SetElementData($object,$this->path,$data);
  97. }
  98. Function EndElement(&$object,$name)
  99. {
  100. $this->path=(($position=strrpos($this->path,",")) ? substr($this->path,0,$position) : "");
  101. }
  102. Function CharacterData(&$object,$data)
  103. {
  104. $element=$object->structure[$this->path]["Elements"];
  105. $previous=$this->path.",".strval($element-1);
  106. if($element>0
  107. && GetType($object->structure[$previous])=="string")
  108. $object->structure[$previous].=$data;
  109. else
  110. {
  111. $this->SetElementData($object,$this->path.",$element",$data);
  112. $object->structure[$this->path]["Elements"]++;
  113. }
  114. }
  115. };
  116. class xml_parser_class
  117. {
  118. var $xml_parser=0;
  119. var $parser_handler;
  120. var $error="";
  121. var $error_number=0;
  122. var $error_line=0;
  123. var $error_column=0;
  124. var $error_byte_index=0;
  125. var $error_code=0;
  126. var $stream_buffer_size=4096;
  127. var $structure=array();
  128. var $positions=array();
  129. var $store_positions=0;
  130. var $case_folding=0;
  131. var $target_encoding="ISO-8859-1";
  132. var $simplified_xml=0;
  133. var $fail_on_non_simplified_xml=0;
  134. Function xml_parser_start_element_handler($parser,$name,$attrs)
  135. {
  136. if(!strcmp($this->error,""))
  137. $this->parser_handler->StartElement($this,$name,$attrs);
  138. }
  139. Function xml_parser_end_element_handler($parser,$name)
  140. {
  141. if(!strcmp($this->error,""))
  142. $this->parser_handler->EndElement($this,$name);
  143. }
  144. Function xml_parser_character_data_handler($parser,$data)
  145. {
  146. if(!strcmp($this->error,""))
  147. $this->parser_handler->CharacterData($this,$data);
  148. }
  149. Function SetErrorPosition($error_number,$error,$line,$column,$byte_index)
  150. {
  151. $this->error_number=$error_number;
  152. $this->error=$error;
  153. $this->error_line=$line;
  154. $this->error_column=$column;
  155. $this->error_byte_index=$byte_index;
  156. }
  157. Function SetError($error_number,$error)
  158. {
  159. $this->error_number=$error_number;
  160. $this->error=$error;
  161. if($this->xml_parser)
  162. {
  163. $line=xml_get_current_line_number($this->xml_parser);
  164. $column=xml_get_current_column_number($this->xml_parser);
  165. $byte_index=xml_get_current_byte_index($this->xml_parser);
  166. }
  167. else
  168. {
  169. $line=$column=1;
  170. $byte_index=0;
  171. }
  172. $this->SetErrorPosition($error_number,$error,$line,$column,$byte_index);
  173. }
  174. Function Parse($data,$end_of_data)
  175. {
  176. global $xml_parser_handlers;
  177. if(strcmp($this->error,""))
  178. return($this->error);
  179. if(!$this->xml_parser)
  180. {
  181. if(!function_exists("xml_parser_create"))
  182. {
  183. $this->SetError(1,"XML support is not available in this PHP configuration");
  184. return($this->error);
  185. }
  186. if(!($this->xml_parser=xml_parser_create()))
  187. {
  188. $this->SetError(1,"Could not create the XML parser");
  189. return($this->error);
  190. }
  191. xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,$this->case_folding);
  192. xml_parser_set_option($this->xml_parser,XML_OPTION_TARGET_ENCODING,$this->target_encoding);
  193. if(function_exists("xml_set_object"))
  194. {
  195. xml_set_object($this->xml_parser,$this);
  196. $this->parser_handler=new xml_parser_handler_class;
  197. $this->structure=array();
  198. $this->positions=array();
  199. }
  200. else
  201. {
  202. $xml_parser_handlers[$this->xml_parser]=new xml_parser_handler_class;
  203. $xml_parser_handlers[$this->xml_parser]->xml_parser=$this->xml_parser;
  204. $xml_parser_handlers[$this->xml_parser]->store_positions=$this->store_positions;
  205. $xml_parser_handlers[$this->xml_parser]->simplified_xml=$this->simplified_xml;
  206. $xml_parser_handlers[$this->xml_parser]->fail_on_non_simplified_xml=$this->fail_on_non_simplified_xml;
  207. }
  208. xml_set_element_handler($this->xml_parser,"xml_parser_start_element_handler","xml_parser_end_element_handler");
  209. xml_set_character_data_handler($this->xml_parser,"xml_parser_character_data_handler");
  210. }
  211. $parser_ok=xml_parse($this->xml_parser,$data,$end_of_data);
  212. if(!function_exists("xml_set_object"))
  213. $this->error=$xml_parser_handlers[$this->xml_parser]->error;
  214. if(!strcmp($this->error,""))
  215. {
  216. if($parser_ok)
  217. {
  218. if($end_of_data)
  219. {
  220. if(function_exists("xml_set_object"))
  221. Unset($this->parser_handler);
  222. else
  223. {
  224. $this->structure=$xml_parser_handlers[$this->xml_parser]->structure;
  225. $this->positions=$xml_parser_handlers[$this->xml_parser]->positions;
  226. Unset($xml_parser_handlers[$this->xml_parser]);
  227. }
  228. xml_parser_free($this->xml_parser);
  229. $this->xml_parser=0;
  230. }
  231. }
  232. else
  233. $this->SetError(2,"Could not parse data: ".xml_error_string($this->error_code=xml_get_error_code($this->xml_parser)));
  234. }
  235. else
  236. {
  237. if(!function_exists("xml_set_object"))
  238. {
  239. $this->error_number=$xml_parser_handlers[$this->xml_parser]->error_number;
  240. $this->error_code=$xml_parser_handlers[$this->xml_parser]->error_code;
  241. $this->error_line=$xml_parser_handlers[$this->xml_parser]->error_line;
  242. $this->error_column=$xml_parser_handlers[$this->xml_parser]->error_column;
  243. $this->error_byte_index=$xml_parser_handlers[$this->xml_parser]->error_byte_index;
  244. }
  245. }
  246. return($this->error);
  247. }
  248. Function VerifyWhiteSpace($path)
  249. {
  250. if($this->store_positions)
  251. {
  252. $line=$parser->positions[$path]["Line"];
  253. $column=$parser->positions[$path]["Column"];
  254. $byte_index=$parser->positions[$path]["Byte"];
  255. }
  256. else
  257. {
  258. $line=$column=1;
  259. $byte_index=0;
  260. }
  261. if(!IsSet($this->structure[$path]))
  262. {
  263. $this->SetErrorPosition(2,"element path does not exist",$line,$column,$byte_index);
  264. return($this->error);
  265. }
  266. if(GetType($this->structure[$path])!="string")
  267. {
  268. $this->SetErrorPosition(2,"element is not data",$line,$column,$byte_index);
  269. return($this->error);
  270. }
  271. $data=$this->structure[$path];
  272. for($previous_return=0,$position=0;$position<strlen($data);$position++)
  273. {
  274. switch($data[$position])
  275. {
  276. case " ":
  277. case "\t":
  278. $column++;
  279. $byte_index++;
  280. $previous_return=0;
  281. break;
  282. case "\n":
  283. if(!$previous_return)
  284. $line++;
  285. $column=1;
  286. $byte_index++;
  287. $previous_return=0;
  288. break;
  289. case "\r":
  290. $line++;
  291. $column=1;
  292. $byte_index++;
  293. $previous_return=1;
  294. break;
  295. default:
  296. $this->SetErrorPosition(2,"data is not white space",$line,$column,$byte_index);
  297. return($this->error);
  298. }
  299. }
  300. return("");
  301. }
  302. Function ParseStream($stream)
  303. {
  304. if(strcmp($this->error,""))
  305. return($this->error);
  306. do
  307. {
  308. if(!($data=@fread($stream,$this->stream_buffer_size)))
  309. {
  310. if(!feof($stream))
  311. {
  312. $this->SetError(3,"Could not read from input stream".(IsSet($php_errormsg) ? ': '.$php_errormsg : ''));
  313. break;
  314. }
  315. }
  316. if(strcmp($error=$this->Parse($data,feof($stream)),""))
  317. break;
  318. }
  319. while(!feof($stream));
  320. return($this->error);
  321. }
  322. Function ParseFile($file)
  323. {
  324. if(!file_exists($file))
  325. return("the XML file to parse ($file) does not exist");
  326. if(!($definition=@fopen($file,"r")))
  327. return("could not open the XML file ($file)".(IsSet($php_errormsg) ? ': '.$php_errormsg : ''));
  328. $error=$this->ParseStream($definition);
  329. fclose($definition);
  330. return($error);
  331. }
  332. };
  333. Function XMLParseFile(&$parser,$file,$store_positions,$cache="",$case_folding=0,$target_encoding="ISO-8859-1",$simplified_xml=0,$fail_on_non_simplified_xml=0)
  334. {
  335. if(!file_exists($file))
  336. return("the XML file to parse ($file) does not exist");
  337. if(strcmp($cache,""))
  338. {
  339. if(file_exists($cache)
  340. && filemtime($file)<=filemtime($cache))
  341. {
  342. if(($cache_file=@fopen($cache,"r")))
  343. {
  344. if(function_exists("set_file_buffer"))
  345. set_file_buffer($cache_file,0);
  346. if(!($cache_contents=@fread($cache_file,filesize($cache))))
  347. $error="could not read from the XML cache file $cache".(IsSet($php_errormsg) ? ': '.$php_errormsg : '');
  348. else
  349. $error="";
  350. fclose($cache_file);
  351. if(!strcmp($error,""))
  352. {
  353. if(GetType($parser=unserialize($cache_contents))=="object"
  354. && IsSet($parser->structure))
  355. {
  356. if(!IsSet($parser->simplified_xml))
  357. $parser->simplified_xml=0;
  358. if(($simplified_xml
  359. || !$parser->simplified_xml)
  360. && (!$store_positions
  361. || $parser->store_positions))
  362. {
  363. return("");
  364. }
  365. }
  366. else
  367. $error="it was not specified a valid cache object in XML file ($cache)";
  368. }
  369. }
  370. else
  371. $error="could not open cache XML file ($cache)".(IsSet($php_errormsg) ? ': '.$php_errormsg : '');
  372. if(strcmp($error,""))
  373. return($error);
  374. }
  375. }
  376. $parser=new xml_parser_class;
  377. $parser->store_positions=$store_positions;
  378. $parser->case_folding=$case_folding;
  379. $parser->target_encoding=$target_encoding;
  380. $parser->simplified_xml=$simplified_xml;
  381. $parser->fail_on_non_simplified_xml=$fail_on_non_simplified_xml;
  382. if(!strcmp($error=$parser->ParseFile($file),"")
  383. && strcmp($cache,""))
  384. {
  385. if(($cache_file=@fopen($cache,"w")))
  386. {
  387. if(function_exists("set_file_buffer"))
  388. set_file_buffer($cache_file,0);
  389. if(!@fwrite($cache_file,serialize($parser))
  390. || !@fclose($cache_file))
  391. $error="could to write to the XML cache file ($cache)".(IsSet($php_errormsg) ? ': '.$php_errormsg : '');
  392. if(strcmp($error,""))
  393. unlink($cache);
  394. }
  395. else
  396. $error="could not open for writing to the cache file ($cache)".(IsSet($php_errormsg) ? ': '.$php_errormsg : '');
  397. }
  398. return($error);
  399. }
  400. ?>