00001 <?php
00025 require_once dirname(__FILE__).'/class.ilBMFBase.php';
00026 require_once dirname(__FILE__).'/class.ilBMFValue.php';
00027
00040 class ilBMFParser extends ilBMFBase
00041 {
00042 var $status = '';
00043 var $position = 0;
00044 var $pos_stat = 0;
00045 var $depth = 0;
00046 var $default_namespace = '';
00047 var $message = array();
00048 var $depth_array = array();
00049 var $previous_element = '';
00050 var $soapresponse = null;
00051 var $soapheaders = null;
00052 var $parent = 0;
00053 var $root_struct_name = array();
00054 var $header_struct_name = array();
00055 var $curent_root_struct_name = '';
00056 var $entities = array ( '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"' );
00057 var $root_struct = array();
00058 var $header_struct = array();
00059 var $curent_root_struct = 0;
00060 var $references = array();
00061 var $need_references = array();
00062 var $XMLSchemaVersion;
00063 var $bodyDepth;
00064
00071 function ilBMFParser(&$xml, $encoding = SOAP_DEFAULT_ENCODING, $attachments = null)
00072 {
00073 parent::ilBMFBase('Parser');
00074 $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
00075
00076 $this->attachments = $attachments;
00077
00078
00079 if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/', $xml, $m)) {
00080 $encoding = strtoupper($m[2] ? $m[2] : $m[3]);
00081 }
00082
00083
00084
00085
00086 if (!empty($xml)) {
00087
00088 $parser = xml_parser_create($encoding);
00089 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
00090 xml_set_object($parser, $this);
00091 xml_set_element_handler($parser, 'startElement', 'endElement');
00092 xml_set_character_data_handler($parser, 'characterData');
00093
00094
00095
00096 if ($xml[strlen($xml) - 1] == 0) {
00097 $xml = trim($xml);
00098 }
00099
00100
00101 if (!xml_parse($parser, $xml, true)) {
00102 $err = sprintf('XML error on line %d col %d byte %d %s',
00103 xml_get_current_line_number($parser),
00104 xml_get_current_column_number($parser),
00105 xml_get_current_byte_index($parser),
00106 xml_error_string(xml_get_error_code($parser)));
00107 $this->_raiseSoapFault($err,htmlspecialchars($xml));
00108 }
00109 xml_parser_free($parser);
00110 }
00111 }
00112
00113
00120 function domulti($d, &$ar, &$r, &$v, $ad=0)
00121 {
00122 if ($d) {
00123 $this->domulti($d-1, $ar, $r[$ar[$ad]], $v, $ad+1);
00124 } else {
00125 $r = $v;
00126 }
00127 }
00128
00137 function &buildResponse($pos)
00138 {
00139 $response = null;
00140
00141 if (isset($this->message[$pos]['children'])) {
00142 $children = explode('|', $this->message[$pos]['children']);
00143
00144 foreach ($children as $c => $child_pos) {
00145 if ($this->message[$child_pos]['type'] != null) {
00146 $response[] =& $this->buildResponse($child_pos);
00147 }
00148 }
00149 if (array_key_exists('arraySize', $this->message[$pos])) {
00150 $ardepth = count($this->message[$pos]['arraySize']);
00151 if ($ardepth > 1) {
00152 $ar = array_pad(array(), $ardepth, 0);
00153 if (array_key_exists('arrayOffset', $this->message[$pos])) {
00154 for ($i = 0; $i < $ardepth; $i++) {
00155 $ar[$i] += $this->message[$pos]['arrayOffset'][$i];
00156 }
00157 }
00158 $elc = count($response);
00159 for ($i = 0; $i < $elc; $i++) {
00160
00161 $this->domulti($ardepth, $ar, $newresp, $response[$i]);
00162
00163
00164 $ad = $ardepth - 1;
00165 $ar[$ad]++;
00166 while ($ad > 0 && $ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) {
00167 $ar[$ad] = 0;
00168 $ad--;
00169 $ar[$ad]++;
00170 }
00171 }
00172 $response = $newresp;
00173 } elseif (isset($this->message[$pos]['arrayOffset']) &&
00174 $this->message[$pos]['arrayOffset'][0] > 0) {
00175
00176 $pad = $this->message[$pos]['arrayOffset'][0] + count($response) * -1;
00177 $response = array_pad($response, $pad, null);
00178 }
00179 }
00180 }
00181
00182
00183 $attrs = array();
00184 foreach ($this->message[$pos]['attrs'] as $atn => $atv) {
00185 if (!strstr($atn, 'xmlns') &&
00186 !strpos($atn, ':')) {
00187 $attrs[$atn] = $atv;
00188 }
00189 }
00190
00191
00192 if ($response) {
00193 $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
00194 $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
00195 $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $response, $attrs);
00196 if (isset($this->message[$pos]['arrayType'])) {
00197 $response->arrayType = $this->message[$pos]['arrayType'];
00198 }
00199 } else {
00200 $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
00201 $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
00202 $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $this->message[$pos]['cdata'], $attrs);
00203 }
00204
00205
00206 if (array_key_exists('actor', $this->message[$pos])) {
00207 $response->actor = $this->message[$pos]['actor'];
00208 }
00209 if (array_key_exists('mustUnderstand', $this->message[$pos])) {
00210 $response->mustunderstand = $this->message[$pos]['mustUnderstand'];
00211 }
00212 return $response;
00213 }
00214
00221 function startElement($parser, $name, $attrs)
00222 {
00223
00224
00225 $pos = $this->position++;
00226
00227
00228 $this->message[$pos] = array();
00229 $this->message[$pos]['type'] = '';
00230 $this->message[$pos]['type_namespace'] = '';
00231 $this->message[$pos]['cdata'] = '';
00232 $this->message[$pos]['pos'] = $pos;
00233 $this->message[$pos]['id'] = '';
00234
00235
00236
00237
00238
00239 $this->message[$pos]['depth'] = $this->depth++;
00240
00241
00242 if ($pos != 0) {
00243 if (isset($this->message[$this->parent]['children']))
00244 $this->message[$this->parent]['children'] .= "|$pos";
00245 else
00246 $this->message[$this->parent]['children'] = $pos;
00247 }
00248
00249
00250 $this->message[$pos]['parent'] = $this->parent;
00251
00252
00253 $this->depth_array[$this->depth] = $pos;
00254
00255 $this->parent = $pos;
00256 $qname =& new QName($name);
00257
00258 if (strcasecmp('envelope', $qname->name) == 0) {
00259 $this->status = 'envelope';
00260 } elseif (strcasecmp('header', $qname->name) == 0) {
00261 $this->status = 'header';
00262 $this->header_struct_name[] = $this->curent_root_struct_name = $qname->name;
00263 $this->header_struct[] = $this->curent_root_struct = $pos;
00264 $this->message[$pos]['type'] = 'Struct';
00265 } elseif (strcasecmp('body', $qname->name) == 0) {
00266 $this->status = 'body';
00267 $this->bodyDepth = $this->depth;
00268
00269
00270 } elseif ($this->status == 'body') {
00271
00272
00273 $can_root = $this->depth == $this->bodyDepth + 1;
00274 if ($can_root) {
00275 foreach ($attrs as $key => $value) {
00276 if (stristr($key, ':root') && !$value) {
00277 $can_root = FALSE;
00278 }
00279 }
00280 }
00281
00282 if ($can_root) {
00283 $this->status = 'method';
00284 $this->root_struct_name[] = $this->curent_root_struct_name = $qname->name;
00285 $this->root_struct[] = $this->curent_root_struct = $pos;
00286 $this->message[$pos]['type'] = 'Struct';
00287 }
00288 }
00289
00290
00291 $this->message[$pos]['status'] = $this->status;
00292
00293
00294 $this->message[$pos]['name'] = htmlspecialchars($qname->name);
00295
00296
00297 $this->message[$pos]['attrs'] = $attrs;
00298
00299
00300 foreach ($attrs as $key => $value) {
00301
00302
00303 $kqn =& new QName($key);
00304 if ($kqn->ns == 'xmlns') {
00305 $prefix = $kqn->name;
00306
00307 if (in_array($value, $this->_XMLSchema)) {
00308 $this->_setSchemaVersion($value);
00309 }
00310
00311 $this->_namespaces[$value] = $prefix;
00312
00313
00314 } elseif ($key == 'xmlns') {
00315 $qname->ns = $this->_getNamespacePrefix($value);
00316 $qname->namespace = $value;
00317 } elseif ($kqn->name == 'actor') {
00318 $this->message[$pos]['actor'] = $value;
00319 } elseif ($kqn->name == 'mustUnderstand') {
00320 $this->message[$pos]['mustUnderstand'] = $value;
00321
00322
00323 } elseif ($kqn->name == 'type') {
00324 $vqn =& new QName($value);
00325 $this->message[$pos]['type'] = $vqn->name;
00326 $this->message[$pos]['type_namespace'] = $this->_getNamespaceForPrefix($vqn->ns);
00327
00328
00329
00330 } elseif ($kqn->name == 'arrayType') {
00331 $vqn =& new QName($value);
00332 $this->message[$pos]['type'] = 'Array';
00333 if (isset($vqn->arraySize)) {
00334 $this->message[$pos]['arraySize'] = $vqn->arraySize;
00335 }
00336 $this->message[$pos]['arrayType'] = $vqn->name;
00337
00338 } elseif ($kqn->name == 'offset') {
00339 $this->message[$pos]['arrayOffset'] = split(',', substr($value, 1, strlen($value) - 2));
00340
00341 } elseif ($kqn->name == 'id') {
00342
00343 $this->references[$value] = $pos;
00344 $this->message[$pos]['id'] = $value;
00345
00346 } elseif ($kqn->name == 'href') {
00347 if ($value[0] == '#') {
00348 $ref = substr($value, 1);
00349 if (isset($this->references[$ref])) {
00350
00351 $ref_pos = $this->references[$ref];
00352 $this->message[$pos]['children'] = &$this->message[$ref_pos]['children'];
00353 $this->message[$pos]['cdata'] = &$this->message[$ref_pos]['cdata'];
00354 $this->message[$pos]['type'] = &$this->message[$ref_pos]['type'];
00355 $this->message[$pos]['arraySize'] = &$this->message[$ref_pos]['arraySize'];
00356 $this->message[$pos]['arrayType'] = &$this->message[$ref_pos]['arrayType'];
00357 } else {
00358
00359 if (!isset($this->need_references[$ref])) {
00360 $this->need_references[$ref] = array();
00361 }
00362 $this->need_references[$ref][] = $pos;
00363 }
00364 } elseif (isset($this->attachments[$value])) {
00365 $this->message[$pos]['cdata'] = $this->attachments[$value];
00366 }
00367 }
00368 }
00369
00370 if (array_key_exists('xmlns:' . $qname->ns, $attrs)) {
00371 $namespace = $attrs['xmlns:' . $qname->ns];
00372 } elseif ($qname->ns && !$qname->namespace) {
00373 $namespace = $this->_getNamespaceForPrefix($qname->ns);
00374 } else {
00375
00376 $namespace = $qname->namespace ? $qname->namespace : $this->default_namespace;
00377 }
00378 $this->message[$pos]['namespace'] = $namespace;
00379 $this->default_namespace = $namespace;
00380 }
00381
00388 function endElement($parser, $name)
00389 {
00390
00391
00392 $pos = $this->depth_array[$this->depth];
00393
00394
00395 $this->depth--;
00396 $qname =& new QName($name);
00397
00398
00399
00400 if ($this->message[$pos]['type'] == '') {
00401 if (isset($this->message[$pos]['children'])) {
00402
00403
00404
00405
00406
00407
00408
00409
00410 $this->message[$pos]['type'] = 'Struct';
00411 } else {
00412 $parent = $this->message[$pos]['parent'];
00413 if ($this->message[$parent]['type'] == 'Array' &&
00414 array_key_exists('arrayType', $this->message[$parent])) {
00415 $this->message[$pos]['type'] = $this->message[$parent]['arrayType'];
00416 } else {
00417 $this->message[$pos]['type'] = 'string';
00418 }
00419 }
00420 }
00421
00422
00423 if ($pos == $this->curent_root_struct) {
00424 $this->status = 'body';
00425 } elseif ($qname->name == 'Body' || $qname->name == 'Header') {
00426 $this->status = 'envelope';
00427 }
00428
00429
00430 $this->parent = $this->message[$pos]['parent'];
00431
00432
00433 $idref = $this->message[$pos]['id'];
00434
00435 if ($idref != '' && array_key_exists($idref, $this->need_references)) {
00436 foreach ($this->need_references[$idref] as $ref_pos) {
00437
00438 $this->message[$ref_pos]['children'] = &$this->message[$pos]['children'];
00439 $this->message[$ref_pos]['cdata'] = &$this->message[$pos]['cdata'];
00440 $this->message[$ref_pos]['type'] = &$this->message[$pos]['type'];
00441 $this->message[$ref_pos]['arraySize'] = &$this->message[$pos]['arraySize'];
00442 $this->message[$ref_pos]['arrayType'] = &$this->message[$pos]['arrayType'];
00443 }
00444 }
00445 }
00446
00453 function characterData($parser, $data)
00454 {
00455 $pos = $this->depth_array[$this->depth];
00456 if (isset($this->message[$pos]['cdata'])) {
00457 $this->message[$pos]['cdata'] .= $data;
00458 } else {
00459 $this->message[$pos]['cdata'] = $data;
00460 }
00461 }
00462
00471 function &getResponse()
00472 {
00473 if (isset($this->root_struct[0]) &&
00474 $this->root_struct[0]) {
00475 $response =& $this->buildResponse($this->root_struct[0]);
00476 } else {
00477 $response =& $this->_raiseSoapFault("couldn't build response");
00478 }
00479 return $response;
00480 }
00481
00490 function &getHeaders()
00491 {
00492 if (isset($this->header_struct[0]) &&
00493 $this->header_struct[0]) {
00494 $response = &$this->buildResponse($this->header_struct[0]);
00495 } else {
00496
00497
00498 $response = null;
00499 }
00500 return $response;
00501 }
00502
00512 function decodeEntities($text)
00513 {
00514 $trans_tbl = array_flip($this->entities);
00515 return strtr($text, $trans_tbl);
00516 }
00517
00518 }