00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 require_once dirname(__FILE__).'/class.ilBMFBase.php';
00024 require_once dirname(__FILE__).'/class.ilBMFValue.php';
00025
00038 class ilBMFParser extends ilBMFBase
00039 {
00040 var $status = '';
00041 var $position = 0;
00042 var $pos_stat = 0;
00043 var $depth = 0;
00044 var $default_namespace = '';
00045 var $message = array();
00046 var $depth_array = array();
00047 var $previous_element = '';
00048 var $soapresponse = NULL;
00049 var $soapheaders = NULL;
00050 var $parent = 0;
00051 var $root_struct_name = array();
00052 var $header_struct_name = array();
00053 var $curent_root_struct_name = '';
00054 var $entities = array ( '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"' );
00055 var $root_struct = array();
00056 var $header_struct = array();
00057 var $curent_root_struct = 0;
00058 var $references = array();
00059 var $need_references = array();
00060 var $XMLSchemaVersion;
00061 var $bodyDepth;
00062
00069 function ilBMFParser(&$xml, $encoding = SOAP_DEFAULT_ENCODING, $attachments=NULL)
00070 {
00071 parent::ilBMFBase('Parser');
00072 $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
00073
00074 $this->attachments = $attachments;
00075
00076
00077 if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/',$xml,$m)) {
00078 $encoding = strtoupper($m[2]?$m[2]:$m[3]);
00079 }
00080
00081
00082
00083 if (!empty($xml)) {
00084
00085 $parser = xml_parser_create($encoding);
00086 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
00087 xml_set_object($parser, $this);
00088 xml_set_element_handler($parser, 'startElement','endElement');
00089 xml_set_character_data_handler($parser,'characterData');
00090
00091
00092
00093 if ($xml[strlen($xml)-1]==0)
00094 $xml = trim($xml);
00095
00096
00097 if (!xml_parse($parser,$xml,true)) {
00098 $err = sprintf('XML error on line %d col %d byte %d %s',
00099 xml_get_current_line_number($parser),
00100 xml_get_current_column_number($parser),
00101 xml_get_current_byte_index($parser),
00102 xml_error_string(xml_get_error_code($parser)));
00103 $this->_raiseSoapFault($err,htmlspecialchars($xml));
00104 }
00105 xml_parser_free($parser);
00106 }
00107 }
00108
00109
00116 function domulti($d, &$ar, &$r, &$v, $ad=0)
00117 {
00118 if ($d) {
00119 $this->domulti($d-1, $ar, $r[$ar[$ad]], $v, $ad+1);
00120 } else {
00121 $r = $v;
00122 }
00123 }
00124
00133 function &buildResponse($pos)
00134 {
00135 $response = NULL;
00136
00137 if (isset($this->message[$pos]['children'])) {
00138 $children = explode('|',$this->message[$pos]['children']);
00139
00140 foreach ($children as $c => $child_pos) {
00141 if ($this->message[$child_pos]['type'] != NULL) {
00142 $response[] =& $this->buildResponse($child_pos);
00143 }
00144 }
00145 if (array_key_exists('arraySize',$this->message[$pos])) {
00146 $ardepth = count($this->message[$pos]['arraySize']);
00147 if ($ardepth > 1) {
00148 $ar = array_pad(array(), $ardepth, 0);
00149 if (array_key_exists('arrayOffset',$this->message[$pos])) {
00150 for ($i = 0; $i < $ardepth; $i++) {
00151 $ar[$i] += $this->message[$pos]['arrayOffset'][$i];
00152 }
00153 }
00154 $elc = count($response);
00155 for ($i = 0; $i < $elc; $i++) {
00156
00157 $this->domulti($ardepth, $ar, $newresp, $response[$i]);
00158
00159 # increment our array pointers
00160 $ad = $ardepth - 1;
00161 $ar[$ad]++;
00162 while ($ad > 0 && $ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) {
00163 $ar[$ad] = 0;
00164 $ad--;
00165 $ar[$ad]++;
00166 }
00167 }
00168 $response = $newresp;
00169 } else if (isset($this->message[$pos]['arrayOffset']) &&
00170 $this->message[$pos]['arrayOffset'][0] > 0) {
00171 # check for padding
00172 $pad = $this->message[$pos]['arrayOffset'][0]+count($response)*-1;
00173 $response = array_pad($response,$pad,NULL);
00174 }
00175 }
00176 }
00177
00178 $attrs = array();
00179 foreach ($this->message[$pos]['attrs'] as $atn => $atv) {
00180 if (!strstr($atn, 'xmlns') &&
00181 !strpos($atn, ':')) $attrs[$atn]=$atv;
00182 }
00183
00184 if ($response) {
00185 $nqn =& new ilBMFQname($this->message[$pos]['name'],$this->message[$pos]['namespace']);
00186 $tqn =& new ilBMFQname($this->message[$pos]['type'],$this->message[$pos]['type_namespace']);
00187 $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $response, $attrs);
00188 if (isset($this->message[$pos]['arrayType'])) $response->arrayType = $this->message[$pos]['arrayType'];
00189 } else {
00190 $nqn =& new ilBMFQname($this->message[$pos]['name'],$this->message[$pos]['namespace']);
00191 $tqn =& new ilBMFQname($this->message[$pos]['type'],$this->message[$pos]['type_namespace']);
00192 $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $this->message[$pos]['cdata'], $attrs);
00193 }
00194
00195 if (array_key_exists('actor',$this->message[$pos])) {
00196 $response->actor = $this->message[$pos]['actor'];
00197 }
00198 if (array_key_exists('mustUnderstand',$this->message[$pos])) {
00199 $response->mustunderstand = $this->message[$pos]['mustUnderstand'];
00200 }
00201 return $response;
00202 }
00203
00210 function startElement($parser, $name, $attrs)
00211 {
00212
00213
00214 $pos = $this->position++;
00215
00216
00217 $this->message[$pos] = array();
00218 $this->message[$pos]['type'] = '';
00219 $this->message[$pos]['type_namespace'] = '';
00220 $this->message[$pos]['cdata'] = '';
00221 $this->message[$pos]['pos'] = $pos;
00222 $this->message[$pos]['id'] = '';
00223
00224
00225
00226
00227
00228 $this->message[$pos]['depth'] = $this->depth++;
00229
00230
00231 if ($pos != 0) {
00232 if (isset($this->message[$this->parent]['children']))
00233 $this->message[$this->parent]['children'] .= "|$pos";
00234 else
00235 $this->message[$this->parent]['children'] = $pos;
00236 }
00237
00238
00239 $this->message[$pos]['parent'] = $this->parent;
00240
00241
00242 $this->depth_array[$this->depth] = $pos;
00243
00244 $this->parent = $pos;
00245 $qname =& new ilBMFQName($name);
00246
00247 if (strcasecmp('envelope',$qname->name)==0) {
00248 $this->status = 'envelope';
00249 } elseif (strcasecmp('header',$qname->name)==0) {
00250 $this->status = 'header';
00251 $this->header_struct_name[] = $this->curent_root_struct_name = $qname->name;
00252 $this->header_struct[] = $this->curent_root_struct = $pos;
00253 $this->message[$pos]['type'] = 'Struct';
00254 } elseif (strcasecmp('body',$qname->name)==0) {
00255 $this->status = 'body';
00256 $this->bodyDepth = $this->depth;
00257
00258 } elseif ($this->status == 'body') {
00259
00260
00261 $can_root = $this->depth == $this->bodyDepth + 1;
00262 if ($can_root) {
00263 foreach ($attrs as $key => $value) {
00264 if (stristr($key, ':root') && !$value) {
00265 $can_root = FALSE;
00266 }
00267 }
00268 }
00269
00270 if ($can_root) {
00271 $this->status = 'method';
00272 $this->root_struct_name[] = $this->curent_root_struct_name = $qname->name;
00273 $this->root_struct[] = $this->curent_root_struct = $pos;
00274 $this->message[$pos]['type'] = 'Struct';
00275 }
00276 }
00277
00278
00279 $this->message[$pos]['status'] = $this->status;
00280
00281
00282 $this->message[$pos]['name'] = htmlspecialchars($qname->name);
00283
00284
00285 $this->message[$pos]['attrs'] = $attrs;
00286
00287
00288 foreach ($attrs as $key => $value) {
00289
00290 $kqn =& new ilBMFQName($key);
00291 if ($kqn->ns == 'xmlns') {
00292 $prefix = $kqn->name;
00293
00294 if (in_array($value, $this->_XMLSchema)) {
00295 $this->_setSchemaVersion($value);
00296 }
00297
00298 $this->_namespaces[$value] = $prefix;
00299
00300
00301 # XXX unused???
00302 #if ($name == $this->curent_root_struct_name) {
00303 # $this->methodNamespace = $value;
00304 #}
00305 } elseif ($key == 'xmlns') {
00306 $qname->ns = $this->_getNamespacePrefix($value);
00307 $qname->namespace = $value;
00308 } elseif ($kqn->name == 'actor') {
00309 $this->message[$pos]['actor'] = $value;
00310 } elseif ($kqn->name == 'mustUnderstand') {
00311 $this->message[$pos]['mustUnderstand'] = $value;
00312
00313
00314 } elseif ($kqn->name == 'type') {
00315 $vqn =& new ilBMFQName($value);
00316 $this->message[$pos]['type'] = $vqn->name;
00317 $this->message[$pos]['type_namespace'] = $this->_getNamespaceForPrefix($vqn->ns);
00318 #print "set type for {$this->message[$pos]['name']} to {$this->message[$pos]['type']}\n";
00319
00320
00321 } elseif ($kqn->name == 'arrayType') {
00322 $vqn =& new ilBMFQName($value);
00323 $this->message[$pos]['type'] = 'Array';
00324 #$type = $vqn->name;
00325 if (isset($vqn->arraySize))
00326 $this->message[$pos]['arraySize'] = $vqn->arraySize;
00327 #$sa = strpos($type,'[');
00328 #if ($sa > 0) {
00329 # $this->message[$pos]['arraySize'] = split(',',substr($type,$sa+1, strlen($type)-$sa-2));
00330 # $type = substr($type, 0, $sa);
00331 #}
00332 $this->message[$pos]['arrayType'] = $vqn->name;
00333
00334 } elseif ($kqn->name == 'offset') {
00335 $this->message[$pos]['arrayOffset'] = split(',',substr($value, 1, strlen($value)-2));
00336
00337 } elseif ($kqn->name == 'id') {
00338 # save id to reference array
00339 $this->references[$value] = $pos;
00340 $this->message[$pos]['id'] = $value;
00341
00342 } elseif ($kqn->name == 'href') {
00343 if ($value[0] == '#') {
00344 $ref = substr($value, 1);
00345 if (array_key_exists($ref,$this->references)) {
00346 # cdata, type, inval
00347 $ref_pos = $this->references[$ref];
00348 $this->message[$pos]['children'] = &$this->message[$ref_pos]['children'];
00349 $this->message[$pos]['cdata'] = &$this->message[$ref_pos]['cdata'];
00350 $this->message[$pos]['type'] = &$this->message[$ref_pos]['type'];
00351 $this->message[$pos]['arraySize'] = &$this->message[$ref_pos]['arraySize'];
00352 $this->message[$pos]['arrayType'] = &$this->message[$ref_pos]['arrayType'];
00353 } else {
00354 # reverse reference, store in 'need reference'
00355 if (!isset($this->need_references[$ref])) $this->need_references[$ref] = array();
00356 $this->need_references[$ref][] = $pos;
00357 }
00358 } else if (isset($this->attachments[$value])) {
00359 $this->message[$pos]['cdata'] = $this->attachments[$value];
00360 }
00361 }
00362 }
00363
00364 if (array_key_exists('xmlns:'.$qname->ns,$attrs)) {
00365 $namespace = $attrs['xmlns:'.$qname->ns];
00366 } else if ($qname->ns && !$qname->namespace) {
00367 $namespace = $this->_getNamespaceForPrefix($qname->ns);
00368 } else {
00369
00370 $namespace = $qname->namespace?$qname->namespace:$this->default_namespace;
00371 }
00372 $this->message[$pos]['namespace'] = $namespace;
00373 $this->default_namespace = $namespace;
00374 }
00375
00382 function endElement($parser, $name)
00383 {
00384
00385 $pos = $this->depth_array[$this->depth];
00386
00387 $this->depth--;
00388 $qname =& new ilBMFQName($name);
00389
00390
00391
00392 if ($this->message[$pos]['type'] == '') {
00393 if (isset($this->message[$pos]['children'])) {
00394
00395
00396
00397
00398
00399
00400
00401
00402 $this->message[$pos]['type'] = 'Struct';
00403 } else {
00404 $parent = $this->message[$pos]['parent'];
00405 if ($this->message[$parent]['type'] == 'Array' &&
00406 array_key_exists('arrayType', $this->message[$parent])) {
00407 $this->message[$pos]['type'] = $this->message[$parent]['arrayType'];
00408 } else {
00409 $this->message[$pos]['type'] = 'string';
00410 }
00411 }
00412 }
00413
00414
00415 if ($pos == $this->curent_root_struct) {
00416 $this->status = 'body';
00417 } elseif ($qname->name == 'Body' || $qname->name == 'Header') {
00418 $this->status = 'envelope';
00419 }
00420
00421
00422 $this->parent = $this->message[$pos]['parent'];
00423
00424 # handle any reverse references now
00425 $idref = $this->message[$pos]['id'];
00426
00427 if ($idref != '' && array_key_exists($idref,$this->need_references)) {
00428 foreach ($this->need_references[$idref] as $ref_pos) {
00429 #XXX is this stuff there already?
00430 $this->message[$ref_pos]['children'] = &$this->message[$pos]['children'];
00431 $this->message[$ref_pos]['cdata'] = &$this->message[$pos]['cdata'];
00432 $this->message[$ref_pos]['type'] = &$this->message[$pos]['type'];
00433 $this->message[$ref_pos]['arraySize'] = &$this->message[$pos]['arraySize'];
00434 $this->message[$ref_pos]['arrayType'] = &$this->message[$pos]['arrayType'];
00435 }
00436 # wipe out our waiting list
00437 # $this->need_references[$idref] = array();
00438 }
00439 }
00440
00447 function characterData($parser, $data)
00448 {
00449 #$fp = fopen('payment.log','a+');
00450 #fwrite($fp,$data);
00451 #fclose($fp);
00452
00453 $pos = $this->depth_array[$this->depth];
00454 if (isset($this->message[$pos]['cdata']))
00455 $this->message[$pos]['cdata'] .= $data;
00456 else
00457 $this->message[$pos]['cdata'] = $data;
00458 }
00459
00469 function &getResponse()
00470 {
00471 if (isset($this->root_struct[0]) &&
00472 $this->root_struct[0]) {
00473 return $this->buildResponse($this->root_struct[0]);
00474 }
00475 return $this->_raiseSoapFault("couldn't build response");
00476 }
00477
00487 function &getHeaders()
00488 {
00489 if (isset($this->header_struct[0]) &&
00490 $this->header_struct[0]) {
00491 return $this->buildResponse($this->header_struct[0]);
00492 }
00493
00494
00495 return NULL;
00496 }
00497
00507 function decodeEntities($text)
00508 {
00509 $trans_tbl = array_flip($this->entities);
00510 return strtr($text, $trans_tbl);
00511 }
00512 }
00513
00514 ?>