utils.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. define('ATTR_SINGLEVALUE', 1);
  3. define('ATTR_MULTIVALUE', 2);
  4. /////////
  5. // XML //
  6. /////////
  7. class xml2Array
  8. {
  9. var $stack=array();
  10. var $stack_ref;
  11. var $arrOutput = array();
  12. var $resParser;
  13. var $strXmlData;
  14. function push_pos(&$pos)
  15. {
  16. $this->stack[count($this->stack)]=&$pos;
  17. $this->stack_ref=&$pos;
  18. }
  19. function pop_pos()
  20. {
  21. unset($this->stack[count($this->stack)-1]);
  22. $this->stack_ref=&$this->stack[count($this->stack)-1];
  23. }
  24. function parse($strInputXML)
  25. {
  26. $this->resParser = xml_parser_create ();
  27. xml_set_object($this->resParser,$this);
  28. xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
  29. xml_set_character_data_handler($this->resParser, "tagData");
  30. $this->push_pos($this->arrOutput);
  31. $this->strXmlData = xml_parse($this->resParser,$strInputXML );
  32. if(!$this->strXmlData)
  33. {
  34. die(sprintf("XML error: %s at line %d",
  35. xml_error_string(xml_get_error_code($this->resParser)),
  36. xml_get_current_line_number($this->resParser)));
  37. }
  38. xml_parser_free($this->resParser);
  39. return $this->arrOutput;
  40. }
  41. function tagOpen($parser, $name, $attrs)
  42. {
  43. if (isset($this->stack_ref[$name]))
  44. {
  45. if (!isset($this->stack_ref[$name][0]))
  46. {
  47. $tmp=$this->stack_ref[$name];
  48. unset($this->stack_ref[$name]);
  49. $this->stack_ref[$name][0]=$tmp;
  50. }
  51. $cnt=count($this->stack_ref[$name]);
  52. $this->stack_ref[$name][$cnt]=array();
  53. if (isset($attrs))
  54. $this->stack_ref[$name][$cnt]=$attrs;
  55. $this->push_pos($this->stack_ref[$name][$cnt]);
  56. }
  57. else
  58. {
  59. $this->stack_ref[$name]=array();
  60. if (isset($attrs))
  61. $this->stack_ref[$name]=$attrs;
  62. $this->push_pos($this->stack_ref[$name]);
  63. }
  64. }
  65. function tagData($parser, $tagData)
  66. {
  67. if(trim($tagData))
  68. {
  69. if(isset($this->stack_ref['DATA']))
  70. $this->stack_ref['DATA'] .= $tagData;
  71. else
  72. $this->stack_ref['DATA'] = $tagData;
  73. }
  74. }
  75. function tagClosed($parser, $name)
  76. {
  77. $this->pop_pos();
  78. }
  79. }
  80. function getSoapAttribute($allAttrs, $attrName, $multisingle=ATTR_SINGLEVALUE)
  81. {
  82. $attrs = array ();
  83. foreach ($allAttrs as $a) {
  84. if ($a['N'] == $attrName){
  85. $attrs[] = $a['DATA'];
  86. if ($multisingle == ATTR_SINGLEVALUE) break;
  87. }
  88. }
  89. if ($multisingle == ATTR_MULTIVALUE)
  90. return $attrs;
  91. else
  92. return $attrs[0];
  93. }
  94. ////////////////
  95. // Exceptions //
  96. ////////////////
  97. function print_exception($ex)
  98. {
  99. if (PHP_SAPI != "cli") {
  100. $nl = "<br/>";
  101. $pre1 = "<pre>";
  102. $pre2 = "</pre>";
  103. } else {
  104. $nl = "\n";
  105. $pre1 = $nl.$nl;
  106. $pre2 = $nl.$nl;
  107. }
  108. echo "Exception caught!...".$nl.$nl."EXCEPTION START <<<<<<<<<<< ";
  109. echo $pre1 . $ex . $pre2;
  110. echo ">>>>>>>>>>>> EXCEPTION END".$nl;
  111. }
  112. ///////////////
  113. // Variables //
  114. ///////////////
  115. function print_var($var, $titre = "")
  116. {
  117. if (PHP_SAPI != "cli") {
  118. $nl = "<br/>";
  119. $pre1 = "<pre>";
  120. $pre2 = "</pre>";
  121. if ($titre) {
  122. $sep = "<hr>";
  123. $title = "<h1>".$titre."</h1>";
  124. }
  125. } else {
  126. $nl = "\n";
  127. $pre1 = $nl.$nl;
  128. $pre2 = $nl.$nl;
  129. if ($titre) {
  130. $sep = str_repeat("-", 80).$nl;
  131. $title = "\033[1m"."--- ".$titre." ---"."\033[0m";
  132. }
  133. }
  134. echo $title;
  135. echo $pre1;
  136. print_r($var);
  137. echo $pre2;
  138. echo $sep;
  139. }
  140. function parse_args($argv){
  141. array_shift($argv);
  142. $out = array();
  143. foreach ($argv as $arg){
  144. if (substr($arg,0,2) == '--'){
  145. $eqPos = strpos($arg,'=');
  146. if ($eqPos === false){
  147. $key = substr($arg,2);
  148. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  149. } else {
  150. $key = substr($arg,2,$eqPos-2);
  151. $out[$key] = substr($arg,$eqPos+1);
  152. }
  153. } else if (substr($arg,0,1) == '-'){
  154. if (substr($arg,2,1) == '='){
  155. $key = substr($arg,1,1);
  156. $out[$key] = substr($arg,3);
  157. } else {
  158. $chars = str_split(substr($arg,1));
  159. foreach ($chars as $char){
  160. $key = $char;
  161. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  162. }
  163. }
  164. } else {
  165. $out[] = $arg;
  166. }
  167. }
  168. return $out;
  169. }
  170. /////////////
  171. // Account //
  172. /////////////
  173. function isAccountId($str)
  174. {
  175. $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
  176. if(preg_match($syntaxe,$str))
  177. return true;
  178. else
  179. return false;
  180. }
  181. function isAccountName($str)
  182. {
  183. $syntaxe = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#';
  184. if(preg_match($syntaxe,$str))
  185. return true;
  186. else
  187. return false;
  188. }
  189. function getAccountType($str)
  190. {
  191. $outputType = null;
  192. if(isAccountName($str))
  193. $outputType = "name";
  194. else if (isAccountId($str))
  195. $outputType = "id";
  196. else
  197. echo "Unknown AccountType";
  198. return $outputType;
  199. }
  200. ////////////
  201. // Domain //
  202. ////////////
  203. function isDomainId($str)
  204. {
  205. $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
  206. if(preg_match($syntaxe,$str))
  207. return true;
  208. else
  209. return false;
  210. }
  211. function isDomainName($str)
  212. {
  213. $syntaxe = '#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}#';
  214. if(preg_match($syntaxe,$str))
  215. return true;
  216. else
  217. return false;
  218. }
  219. function getDomainType($str)
  220. {
  221. $outputType = null;
  222. if(isDomainName($str))
  223. $outputType = "name";
  224. else if (isDomainId($str))
  225. $outputType = "id";
  226. else
  227. echo "Unknown DomainType";
  228. return $outputType;
  229. }
  230. ////////////
  231. // Server //
  232. ////////////
  233. function isServerId($str)
  234. {
  235. $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
  236. if(preg_match($syntaxe,$str))
  237. return true;
  238. else
  239. return false;
  240. }
  241. function isServerName($str)
  242. {
  243. $syntaxe = '#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}#';
  244. if(preg_match($syntaxe,$str))
  245. return true;
  246. else
  247. return false;
  248. }
  249. function getServerType($str)
  250. {
  251. $outputType = null;
  252. if(isServerName($str))
  253. $outputType = "name";
  254. else if (isServerId($str))
  255. $outputType = "id";
  256. else
  257. echo "Unknown ServerType";
  258. return $outputType;
  259. }
  260. ?>