[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /* $Id: class.soap_parser.inc.php 11430 2003-08-28 14:31:11Z ralfbecker $ */ 3 4 class soap_parser 5 { 6 function soap_parser($xml='',$encoding='UTF-8') 7 { 8 global $soapTypes; 9 10 $this->soapTypes = $soapTypes; 11 $this->xml = $xml; 12 $this->xml_encoding = $encoding; 13 $this->root_struct = ""; 14 // options: envelope,header,body,method 15 // determines where in the message we are (envelope,header,body,method) 16 $this->status = ''; 17 $this->position = 0; 18 $this->pos_stat = 0; 19 $this->depth = 0; 20 $this->default_namespace = ''; 21 $this->namespaces = array(); 22 $this->message = array(); 23 $this->fault = false; 24 $this->fault_code = ''; 25 $this->fault_str = ''; 26 $this->fault_detail = ''; 27 $this->eval_str = ''; 28 $this->depth_array = array(); 29 $this->debug_flag = True; 30 $this->debug_str = ''; 31 $this->previous_element = ''; 32 33 $this->entities = array ( 34 '&' => '&', 35 '<' => '<', 36 '>' => '>', 37 "'" => ''', 38 '"' => '"' 39 ); 40 41 // Check whether content has been read. 42 if(!empty($xml)) 43 { 44 $this->debug('Entering soap_parser()'); 45 //$this->debug("DATA DUMP:\n\n$xml"); 46 // Create an XML parser. 47 $this->parser = xml_parser_create($this->xml_encoding); 48 // Set the options for parsing the XML data. 49 //xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 50 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); 51 // Set the object for the parser. 52 xml_set_object($this->parser, &$this); 53 // Set the element handlers for the parser. 54 xml_set_element_handler($this->parser, 'start_element','end_element'); 55 xml_set_character_data_handler($this->parser,'character_data'); 56 xml_set_default_handler($this->parser, 'default_handler'); 57 58 // Parse the XML file. 59 if(!xml_parse($this->parser,$xml,true)) 60 { 61 // Display an error message. 62 $this->debug(sprintf("XML error on line %d: %s", 63 xml_get_current_line_number($this->parser), 64 xml_error_string(xml_get_error_code($this->parser)))); 65 $this->fault = true; 66 } 67 else 68 { 69 // get final eval string 70 $this->eval_str = "\$response = ".trim($this->build_eval($this->root_struct)).";"; 71 } 72 xml_parser_free($this->parser); 73 } 74 else 75 { 76 $this->debug("xml was empty, didn't parse!"); 77 } 78 } 79 80 // loop through msg, building eval_str 81 function build_eval($pos) 82 { 83 $this->debug("inside build_eval() for $pos: ".$this->message[$pos]["name"]); 84 $eval_str = $this->message[$pos]['eval_str']; 85 // loop through children, building... 86 if($this->message[$pos]['children'] != '') 87 { 88 $this->debug('children string = '.$this->message[$pos]['children']); 89 $children = explode('|',$this->message[$pos]['children']); 90 $this->debug('it has '.count($children).' children'); 91 @reset($children); 92 while(list($c,$child_pos) = @each($children)) 93 /* foreach($children as $c => $child_pos) */ 94 { 95 //$this->debug("child pos $child_pos: ".$this->message[$child_pos]["name"]); 96 if($this->message[$child_pos]['eval_str'] != '') 97 { 98 $this->debug('entering build_eval() for '.$this->message[$child_pos]['name'].", array pos $c, pos: $child_pos"); 99 $eval_str .= $this->build_eval($child_pos).', '; 100 } 101 } 102 $eval_str = substr($eval_str,0,strlen($eval_str)-2); 103 } 104 // add current node's eval_str 105 $eval_str .= $this->message[$pos]['end_eval_str']; 106 return $eval_str; 107 } 108 109 // start-element handler 110 function start_element($parser, $name, $attrs) 111 { 112 // position in a total number of elements, starting from 0 113 // update class level pos 114 $pos = $this->position++; 115 // and set mine 116 $this->message[$pos]['pos'] = $pos; 117 118 // parent/child/depth determinations 119 120 // depth = how many levels removed from root? 121 // set mine as current global depth and increment global depth value 122 $this->message[$pos]['depth'] = $this->depth++; 123 124 // else add self as child to whoever the current parent is 125 if($pos != 0) 126 { 127 $this->message[$this->parent]['children'] .= "|$pos"; 128 } 129 // set my parent 130 $this->message[$pos]['parent'] = $this->parent; 131 // set self as current value for this depth 132 $this->depth_array[$this->depth] = $pos; 133 // set self as current parent 134 $this->parent = $pos; 135 136 // set status 137 if(ereg(":Envelope$",$name)) 138 { 139 $this->status = 'envelope'; 140 } 141 elseif(ereg(":Header$",$name)) 142 { 143 $this->status = 'header'; 144 } 145 elseif(ereg(":Body$",$name)) 146 { 147 $this->status = 'body'; 148 // set method 149 } 150 elseif($this->status == 'body') 151 { 152 $this->status = 'method'; 153 if(ereg(':',$name)) 154 { 155 $this->root_struct_name = substr(strrchr($name,':'),1); 156 } 157 else 158 { 159 $this->root_struct_name = $name; 160 } 161 $this->root_struct = $pos; 162 $this->message[$pos]['type'] = 'struct'; 163 } 164 // set my status 165 $this->message[$pos]['status'] = $this->status; 166 167 // set name 168 $this->message[$pos]['name'] = htmlspecialchars($name); 169 // set attrs 170 $this->message[$pos]['attrs'] = $attrs; 171 // get namespace 172 if(ereg(":",$name)) 173 { 174 $namespace = substr($name,0,strpos($name,':')); 175 $this->message[$pos]['namespace'] = $namespace; 176 $this->default_namespace = $namespace; 177 } 178 else 179 { 180 $this->message[$pos]['namespace'] = $this->default_namespace; 181 } 182 // loop through atts, logging ns and type declarations 183 @reset($attrs); 184 while (list($key,$value) = @each($attrs)) 185 /* foreach($attrs as $key => $value) */ 186 { 187 // if ns declarations, add to class level array of valid namespaces 188 if(ereg('xmlns:',$key)) 189 { 190 $namespaces[substr(strrchr($key,':'),1)] = $value; 191 if($name == $this->root_struct_name) 192 { 193 $this->methodNamespace = $value; 194 } 195 } 196 // if it's a type declaration, set type 197 elseif($key == 'xsi:type') 198 { 199 // then get attname and set $type 200 $type = substr(strrchr($value,':'),1); 201 } 202 } 203 204 // set type if available 205 if($type) 206 { 207 $this->message[$pos]['type'] = $type; 208 } 209 210 // debug 211 //$this->debug("parsed $name start, eval = '".$this->message[$pos]["eval_str"]."'"); 212 } 213 214 // end-element handler 215 function end_element($parser, $name) 216 { 217 // position of current element is equal to the last value left in depth_array for my depth 218 $pos = $this->depth_array[$this->depth]; 219 // bring depth down a notch 220 $this->depth--; 221 222 // get type if not set already 223 if($this->message[$pos]['type'] == '') 224 { 225 // if($this->message[$pos]['cdata'] == '' && $this->message[$pos]['children'] != '') 226 if($this->message[$pos]['children'] != '') 227 { 228 $this->message[$pos]['type'] = 'SOAPStruct'; 229 } 230 else 231 { 232 $this->message[$pos]['type'] = 'string'; 233 } 234 } 235 236 // set eval str start if it has a valid type and is inside the method 237 if($pos >= $this->root_struct) 238 { 239 $this->message[$pos]['eval_str'] .= "\n CreateObject(\"phpgwapi.soapval\",\"".htmlspecialchars($name)."\", \"".$this->message[$pos]["type"]."\" "; 240 $this->message[$pos]['end_eval_str'] = ')'; 241 $this->message[$pos]['inval'] = 'true'; 242 /* 243 if($this->message[$pos]["name"] == $this->root_struct_name){ 244 $this->message[$pos]["end_eval_str"] .= " ,\"$this->methodNamespace\""; 245 } 246 */ 247 if($this->message[$pos]['children'] != '') 248 { 249 $this->message[$pos]['eval_str'] .= ', array( '; 250 $this->message[$pos]['end_eval_str'] .= ' )'; 251 } 252 } 253 254 // if i have no children and have cdata...then i must be a scalar value, so add my data to the eval_str 255 if($this->status == 'method' && $this->message[$pos]['children'] == '') 256 { 257 // add cdata w/ no quotes if only int/float/dbl 258 if($this->message[$pos]['type'] == 'string') 259 { 260 $this->message[$pos]['eval_str'] .= ", \"".$this->message[$pos]['cdata']."\""; 261 } 262 elseif($this->message[$pos]['type'] == 'int' || $this->message[$pos]['type'] == 'float' || $this->message[$pos]['type'] == 'double') 263 { 264 //$this->debug("adding cdata w/o quotes"); 265 $this->message[$pos]['eval_str'] .= ', '.trim($this->message[$pos]['cdata']); 266 } 267 elseif(is_string($this->message[$pos]['cdata'])) 268 { 269 //$this->debug("adding cdata w/ quotes"); 270 $this->message[$pos]['eval_str'] .= ", \"".$this->message[$pos]['cdata']."\""; 271 } 272 } 273 // if in the process of making a soap_val, close the parentheses and move on... 274 if($this->message[$pos]['inval'] == 'true') 275 { 276 $this->message[$pos]['inval'] == 'false'; 277 } 278 // if tag we are currently closing is the method wrapper 279 if($pos == $this->root_struct) 280 { 281 $this->status = 'body'; 282 } 283 elseif(ereg(':Body',$name)) 284 { 285 $this->status = 'header'; 286 } 287 elseif(ereg(':Header',$name)) 288 { 289 $this->status = 'envelope'; 290 } 291 // set parent back to my parent 292 $this->parent = $this->message[$pos]['parent']; 293 $this->debug("parsed $name end, type '".$this->message[$pos]['type']."'eval_str = '".trim($this->message[$pos]['eval_str'])."' and children = ".$this->message[$pos]['children']); 294 } 295 296 // element content handler 297 function character_data($parser, $data) 298 { 299 $pos = $this->depth_array[$this->depth]; 300 $this->message[$pos]['cdata'] .= $data; 301 //$this->debug("parsed ".$this->message[$pos]["name"]." cdata, eval = '$this->eval_str'"); 302 } 303 304 // default handler 305 function default_handler($parser, $data) 306 { 307 //$this->debug("DEFAULT HANDLER: $data"); 308 } 309 310 // function to get fault code 311 function fault() 312 { 313 if($this->fault) 314 { 315 return true; 316 } 317 else 318 { 319 return false; 320 } 321 } 322 323 // have this return a soap_val object 324 function get_response() 325 { 326 $this->debug("eval()ing eval_str: $this->eval_str"); 327 @eval("$this->eval_str"); 328 if($response) 329 { 330 $this->debug("successfully eval'd msg"); 331 return $response; 332 } 333 else 334 { 335 $this->debug('ERROR: did not successfully eval the msg'); 336 $this->fault = true; 337 return CreateObject('phpgwapi.soapval', 338 'Fault', 339 'struct', 340 array( 341 CreateObject('phpgwapi.soapval', 342 'faultcode', 343 'string', 344 'SOAP-ENV:Server' 345 ), 346 CreateObject('phpgwapi.soapval', 347 'faultstring', 348 'string', 349 "couldn't eval \"$this->eval_str\"" 350 ) 351 ) 352 ); 353 } 354 } 355 356 function debug($string) 357 { 358 if($this->debug_flag) 359 { 360 $this->debug_str .= "$string\n"; 361 } 362 } 363 364 function decode_entities($text) 365 { 366 @reset($this->entities); 367 while(list($entity,$encoded) = @each($this->entities)) 368 /* foreach($this->entities as $entity => $encoded) */ 369 { 370 $text = str_replace($encoded,$entity,$text); 371 } 372 return $text; 373 } 374 } 375 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |