| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- define('ATTR_SINGLEVALUE', 1);
- define('ATTR_MULTIVALUE', 2);
- /////////
- // XML //
- /////////
- class xml2Array
- {
- var $stack=array();
- var $stack_ref;
- var $arrOutput = array();
- var $resParser;
- var $strXmlData;
- function push_pos(&$pos)
- {
- $this->stack[count($this->stack)]=&$pos;
- $this->stack_ref=&$pos;
- }
- function pop_pos()
- {
- unset($this->stack[count($this->stack)-1]);
- $this->stack_ref=&$this->stack[count($this->stack)-1];
- }
- function parse($strInputXML)
- {
- $this->resParser = xml_parser_create ();
- xml_set_object($this->resParser,$this);
- xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
- xml_set_character_data_handler($this->resParser, "tagData");
- $this->push_pos($this->arrOutput);
- $this->strXmlData = xml_parse($this->resParser,$strInputXML );
- if(!$this->strXmlData)
- {
- die(sprintf("XML error: %s at line %d",
- xml_error_string(xml_get_error_code($this->resParser)),
- xml_get_current_line_number($this->resParser)));
- }
- xml_parser_free($this->resParser);
- return $this->arrOutput;
- }
- function tagOpen($parser, $name, $attrs)
- {
- if (isset($this->stack_ref[$name]))
- {
- if (!isset($this->stack_ref[$name][0]))
- {
- $tmp=$this->stack_ref[$name];
- unset($this->stack_ref[$name]);
- $this->stack_ref[$name][0]=$tmp;
- }
- $cnt=count($this->stack_ref[$name]);
- $this->stack_ref[$name][$cnt]=array();
- if (isset($attrs))
- $this->stack_ref[$name][$cnt]=$attrs;
- $this->push_pos($this->stack_ref[$name][$cnt]);
- }
- else
- {
- $this->stack_ref[$name]=array();
- if (isset($attrs))
- $this->stack_ref[$name]=$attrs;
- $this->push_pos($this->stack_ref[$name]);
- }
- }
- function tagData($parser, $tagData)
- {
- if(trim($tagData))
- {
- if(isset($this->stack_ref['DATA']))
- $this->stack_ref['DATA'] .= $tagData;
- else
- $this->stack_ref['DATA'] = $tagData;
- }
- }
- function tagClosed($parser, $name)
- {
- $this->pop_pos();
- }
- }
- function getSoapAttribute($allAttrs, $attrName, $multisingle=ATTR_SINGLEVALUE)
- {
- $attrs = array ();
- foreach ($allAttrs as $a) {
- if ($a['N'] == $attrName){
- $attrs[] = $a['DATA'];
- if ($multisingle == ATTR_SINGLEVALUE) break;
- }
- }
- if ($multisingle == ATTR_MULTIVALUE)
- return $attrs;
- else
- return $attrs[0];
- }
- ////////////////
- // Exceptions //
- ////////////////
- function print_exception($ex)
- {
- if (PHP_SAPI != "cli") {
- $nl = "<br/>";
- $pre1 = "<pre>";
- $pre2 = "</pre>";
- } else {
- $nl = "\n";
- $pre1 = $nl.$nl;
- $pre2 = $nl.$nl;
- }
- echo "Exception caught!...".$nl.$nl."EXCEPTION START <<<<<<<<<<< ";
- echo $pre1 . $ex . $pre2;
- echo ">>>>>>>>>>>> EXCEPTION END".$nl;
- }
- ///////////////
- // Variables //
- ///////////////
- function print_var($var, $titre = "")
- {
- if (PHP_SAPI != "cli") {
- $nl = "<br/>";
- $pre1 = "<pre>";
- $pre2 = "</pre>";
- if ($titre) {
- $sep = "<hr>";
- $title = "<h1>".$titre."</h1>";
- }
- } else {
- $nl = "\n";
- $pre1 = $nl.$nl;
- $pre2 = $nl.$nl;
- if ($titre) {
- $sep = str_repeat("-", 80).$nl;
- $title = "\033[1m"."--- ".$titre." ---"."\033[0m";
- }
- }
- echo $title;
- echo $pre1;
- print_r($var);
- echo $pre2;
- echo $sep;
- }
- function parse_args($argv){
- array_shift($argv);
- $out = array();
- foreach ($argv as $arg){
- if (substr($arg,0,2) == '--'){
- $eqPos = strpos($arg,'=');
- if ($eqPos === false){
- $key = substr($arg,2);
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- } else {
- $key = substr($arg,2,$eqPos-2);
- $out[$key] = substr($arg,$eqPos+1);
- }
- } else if (substr($arg,0,1) == '-'){
- if (substr($arg,2,1) == '='){
- $key = substr($arg,1,1);
- $out[$key] = substr($arg,3);
- } else {
- $chars = str_split(substr($arg,1));
- foreach ($chars as $char){
- $key = $char;
- $out[$key] = isset($out[$key]) ? $out[$key] : true;
- }
- }
- } else {
- $out[] = $arg;
- }
- }
- return $out;
- }
- /////////////
- // Account //
- /////////////
- function isAccountId($str)
- {
- $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function isAccountName($str)
- {
- $syntaxe = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function getAccountType($str)
- {
- $outputType = null;
- if(isAccountName($str))
- $outputType = "name";
- else if (isAccountId($str))
- $outputType = "id";
- else
- echo "Unknown AccountType";
- return $outputType;
- }
- ////////////
- // Domain //
- ////////////
- function isDomainId($str)
- {
- $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function isDomainName($str)
- {
- $syntaxe = '#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function getDomainType($str)
- {
- $outputType = null;
- if(isDomainName($str))
- $outputType = "name";
- else if (isDomainId($str))
- $outputType = "id";
- else
- echo "Unknown DomainType";
- return $outputType;
- }
- ////////////
- // Server //
- ////////////
- function isServerId($str)
- {
- $syntaxe = '#[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function isServerName($str)
- {
- $syntaxe = '#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}#';
- if(preg_match($syntaxe,$str))
- return true;
- else
- return false;
- }
- function getServerType($str)
- {
- $outputType = null;
- if(isServerName($str))
- $outputType = "name";
- else if (isServerId($str))
- $outputType = "id";
- else
- echo "Unknown ServerType";
- return $outputType;
- }
- ?>
|