ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBMFParser.php
Go to the documentation of this file.
1 <?php
25 require_once dirname(__FILE__).'/class.ilBMFBase.php';
26 require_once dirname(__FILE__).'/class.ilBMFValue.php';
27 
40 class ilBMFParser extends ilBMFBase
41 {
42  var $status = '';
43  var $position = 0;
44  var $pos_stat = 0;
45  var $depth = 0;
47  var $message = array();
48  var $depth_array = array();
50  var $soapresponse = null;
51  var $soapheaders = null;
52  var $parent = 0;
53  var $root_struct_name = array();
54  var $header_struct_name = array();
56  var $entities = array ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', "'" => '&apos;', '"' => '&quot;' );
57  var $root_struct = array();
58  var $header_struct = array();
60  var $references = array();
61  var $need_references = array();
63  var $bodyDepth; // used to handle non-root elements before root body element
64 
71  function ilBMFParser(&$xml, $encoding = SOAP_DEFAULT_ENCODING, $attachments = null)
72  {
73  parent::ilBMFBase('Parser');
75 
76  $this->attachments = $attachments;
77 
78  // Check the xml tag for encoding.
79  if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/', $xml, $m)) {
80  $encoding = strtoupper($m[2] ? $m[2] : $m[3]);
81  }
82 
83  // Determines where in the message we are
84  // (envelope,header,body,method). Check whether content has
85  // been read.
86  if (!empty($xml)) {
87  // Prepare the xml parser.
88  $parser = xml_parser_create($encoding);
89  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
90  xml_set_object($parser, $this);
91  xml_set_element_handler($parser, 'startElement', 'endElement');
92  xml_set_character_data_handler($parser, 'characterData');
93 
94  // Some lame soap implementations add null bytes at the
95  // end of the soap stream, and expat choaks on that.
96  if ($xml[strlen($xml) - 1] == 0) {
97  $xml = trim($xml);
98  }
99 
100  // Parse the XML file.
101  if (!xml_parse($parser, $xml, true)) {
102  $err = sprintf('XML error on line %d col %d byte %d %s',
103  xml_get_current_line_number($parser),
104  xml_get_current_column_number($parser),
105  xml_get_current_byte_index($parser),
106  xml_error_string(xml_get_error_code($parser)));
107  $this->_raiseSoapFault($err,htmlspecialchars($xml));
108  }
109  xml_parser_free($parser);
110  }
111  }
112 
113 
120  function domulti($d, &$ar, &$r, &$v, $ad=0)
121  {
122  if ($d) {
123  $this->domulti($d-1, $ar, $r[$ar[$ad]], $v, $ad+1);
124  } else {
125  $r = $v;
126  }
127  }
128 
137  function &buildResponse($pos)
138  {
139  $response = null;
140 
141  if (isset($this->message[$pos]['children'])) {
142  $children = explode('|', $this->message[$pos]['children']);
143 
144  foreach ($children as $c => $child_pos) {
145  if ($this->message[$child_pos]['type'] != null) {
146  $response[] =& $this->buildResponse($child_pos);
147  }
148  }
149  if (array_key_exists('arraySize', $this->message[$pos])) {
150  $ardepth = count($this->message[$pos]['arraySize']);
151  if ($ardepth > 1) {
152  $ar = array_pad(array(), $ardepth, 0);
153  if (array_key_exists('arrayOffset', $this->message[$pos])) {
154  for ($i = 0; $i < $ardepth; $i++) {
155  $ar[$i] += $this->message[$pos]['arrayOffset'][$i];
156  }
157  }
158  $elc = count($response);
159  for ($i = 0; $i < $elc; $i++) {
160  // recurse to build a multi-dimensional array
161  $this->domulti($ardepth, $ar, $newresp, $response[$i]);
162 
163  // increment our array pointers
164  $ad = $ardepth - 1;
165  $ar[$ad]++;
166  while ($ad > 0 && $ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) {
167  $ar[$ad] = 0;
168  $ad--;
169  $ar[$ad]++;
170  }
171  }
172  $response = $newresp;
173  } elseif (isset($this->message[$pos]['arrayOffset']) &&
174  $this->message[$pos]['arrayOffset'][0] > 0) {
175  // check for padding
176  $pad = $this->message[$pos]['arrayOffset'][0] + count($response) * -1;
177  $response = array_pad($response, $pad, null);
178  }
179  }
180  }
181 
182  // Build attributes.
183  $attrs = array();
184  foreach ($this->message[$pos]['attrs'] as $atn => $atv) {
185  if (!strstr($atn, 'xmlns') &&
186  !strpos($atn, ':')) {
187  $attrs[$atn] = $atv;
188  }
189  }
190 
191  // Add current node's value.
192  if ($response) {
193  $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
194  $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
195  $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $response, $attrs);
196  if (isset($this->message[$pos]['arrayType'])) {
197  $response->arrayType = $this->message[$pos]['arrayType'];
198  }
199  } else {
200  $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
201  $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
202  $response =& new ilBMFValue($nqn->fqn(), $tqn->fqn(), $this->message[$pos]['cdata'], $attrs);
203  }
204 
205  // handle header attribute that we need
206  if (array_key_exists('actor', $this->message[$pos])) {
207  $response->actor = $this->message[$pos]['actor'];
208  }
209  if (array_key_exists('mustUnderstand', $this->message[$pos])) {
210  $response->mustunderstand = $this->message[$pos]['mustUnderstand'];
211  }
212  return $response;
213  }
214 
221  function startElement($parser, $name, $attrs)
222  {
223  // position in a total number of elements, starting from 0
224  // update class level pos
225  $pos = $this->position++;
226 
227  // and set mine
228  $this->message[$pos] = array();
229  $this->message[$pos]['type'] = '';
230  $this->message[$pos]['type_namespace'] = '';
231  $this->message[$pos]['cdata'] = '';
232  $this->message[$pos]['pos'] = $pos;
233  $this->message[$pos]['id'] = '';
234 
235  // parent/child/depth determinations
236 
237  // depth = how many levels removed from root?
238  // set mine as current global depth and increment global depth value
239  $this->message[$pos]['depth'] = $this->depth++;
240 
241  // else add self as child to whoever the current parent is
242  if ($pos != 0) {
243  if (isset($this->message[$this->parent]['children']))
244  $this->message[$this->parent]['children'] .= "|$pos";
245  else
246  $this->message[$this->parent]['children'] = $pos;
247  }
248 
249  // set my parent
250  $this->message[$pos]['parent'] = $this->parent;
251 
252  // set self as current value for this depth
253  $this->depth_array[$this->depth] = $pos;
254  // set self as current parent
255  $this->parent = $pos;
256  $qname =& new QName($name);
257  // set status
258  if (strcasecmp('envelope', $qname->name) == 0) {
259  $this->status = 'envelope';
260  } elseif (strcasecmp('header', $qname->name) == 0) {
261  $this->status = 'header';
262  $this->header_struct_name[] = $this->curent_root_struct_name = $qname->name;
263  $this->header_struct[] = $this->curent_root_struct = $pos;
264  $this->message[$pos]['type'] = 'Struct';
265  } elseif (strcasecmp('body', $qname->name) == 0) {
266  $this->status = 'body';
267  $this->bodyDepth = $this->depth;
268 
269  // Set method
270  } elseif ($this->status == 'body') {
271  // Is this element allowed to be a root?
272  // XXX this needs to be optimized, we loop through attrs twice now.
273  $can_root = $this->depth == $this->bodyDepth + 1;
274  if ($can_root) {
275  foreach ($attrs as $key => $value) {
276  if (stristr($key, ':root') && !$value) {
277  $can_root = FALSE;
278  }
279  }
280  }
281 
282  if ($can_root) {
283  $this->status = 'method';
284  $this->root_struct_name[] = $this->curent_root_struct_name = $qname->name;
285  $this->root_struct[] = $this->curent_root_struct = $pos;
286  $this->message[$pos]['type'] = 'Struct';
287  }
288  }
289 
290  // Set my status.
291  $this->message[$pos]['status'] = $this->status;
292 
293  // Set name.
294  $this->message[$pos]['name'] = htmlspecialchars($qname->name);
295 
296  // Set attributes.
297  $this->message[$pos]['attrs'] = $attrs;
298 
299  // Loop through attributes, logging ns and type declarations.
300  foreach ($attrs as $key => $value) {
301  // If ns declarations, add to class level array of valid
302  // namespaces.
303  $kqn =& new QName($key);
304  if ($kqn->ns == 'xmlns') {
305  $prefix = $kqn->name;
306 
307  if (in_array($value, $this->_XMLSchema)) {
308  $this->_setSchemaVersion($value);
309  }
310 
311  $this->_namespaces[$value] = $prefix;
312 
313  // Set method namespace.
314  } elseif ($key == 'xmlns') {
315  $qname->ns = $this->_getNamespacePrefix($value);
316  $qname->namespace = $value;
317  } elseif ($kqn->name == 'actor') {
318  $this->message[$pos]['actor'] = $value;
319  } elseif ($kqn->name == 'mustUnderstand') {
320  $this->message[$pos]['mustUnderstand'] = $value;
321 
322  // If it's a type declaration, set type.
323  } elseif ($kqn->name == 'type') {
324  $vqn =& new QName($value);
325  $this->message[$pos]['type'] = $vqn->name;
326  $this->message[$pos]['type_namespace'] = $this->_getNamespaceForPrefix($vqn->ns);
327  // Should do something here with the namespace of
328  // specified type?
329 
330  } elseif ($kqn->name == 'arrayType') {
331  $vqn =& new QName($value);
332  $this->message[$pos]['type'] = 'Array';
333  if (isset($vqn->arraySize)) {
334  $this->message[$pos]['arraySize'] = $vqn->arraySize;
335  }
336  $this->message[$pos]['arrayType'] = $vqn->name;
337 
338  } elseif ($kqn->name == 'offset') {
339  $this->message[$pos]['arrayOffset'] = split(',', substr($value, 1, strlen($value) - 2));
340 
341  } elseif ($kqn->name == 'id') {
342  // Save id to reference array.
343  $this->references[$value] = $pos;
344  $this->message[$pos]['id'] = $value;
345 
346  } elseif ($kqn->name == 'href') {
347  if ($value[0] == '#') {
348  $ref = substr($value, 1);
349  if (isset($this->references[$ref])) {
350  // cdata, type, inval.
351  $ref_pos = $this->references[$ref];
352  $this->message[$pos]['children'] = &$this->message[$ref_pos]['children'];
353  $this->message[$pos]['cdata'] = &$this->message[$ref_pos]['cdata'];
354  $this->message[$pos]['type'] = &$this->message[$ref_pos]['type'];
355  $this->message[$pos]['arraySize'] = &$this->message[$ref_pos]['arraySize'];
356  $this->message[$pos]['arrayType'] = &$this->message[$ref_pos]['arrayType'];
357  } else {
358  // Reverse reference, store in 'need reference'.
359  if (!isset($this->need_references[$ref])) {
360  $this->need_references[$ref] = array();
361  }
362  $this->need_references[$ref][] = $pos;
363  }
364  } elseif (isset($this->attachments[$value])) {
365  $this->message[$pos]['cdata'] = $this->attachments[$value];
366  }
367  }
368  }
369  // See if namespace is defined in tag.
370  if (array_key_exists('xmlns:' . $qname->ns, $attrs)) {
371  $namespace = $attrs['xmlns:' . $qname->ns];
372  } elseif ($qname->ns && !$qname->namespace) {
373  $namespace = $this->_getNamespaceForPrefix($qname->ns);
374  } else {
375  // Get namespace.
376  $namespace = $qname->namespace ? $qname->namespace : $this->default_namespace;
377  }
378  $this->message[$pos]['namespace'] = $namespace;
379  $this->default_namespace = $namespace;
380  }
381 
388  function endElement($parser, $name)
389  {
390  // Position of current element is equal to the last value left
391  // in depth_array for my depth.
392  $pos = $this->depth_array[$this->depth];
393 
394  // Bring depth down a notch.
395  $this->depth--;
396  $qname =& new QName($name);
397 
398  // Get type if not explicitly declared in an xsi:type attribute.
399  // XXX check on integrating wsdl validation here
400  if ($this->message[$pos]['type'] == '') {
401  if (isset($this->message[$pos]['children'])) {
402  /* this is slow, need to look at some faster method
403  $children = explode('|', $this->message[$pos]['children']);
404  if (count($children) > 2 &&
405  $this->message[$children[1]]['name'] == $this->message[$children[2]]['name']) {
406  $this->message[$pos]['type'] = 'Array';
407  } else {
408  $this->message[$pos]['type'] = 'Struct';
409  }*/
410  $this->message[$pos]['type'] = 'Struct';
411  } else {
412  $parent = $this->message[$pos]['parent'];
413  if ($this->message[$parent]['type'] == 'Array' &&
414  array_key_exists('arrayType', $this->message[$parent])) {
415  $this->message[$pos]['type'] = $this->message[$parent]['arrayType'];
416  } else {
417  $this->message[$pos]['type'] = 'string';
418  }
419  }
420  }
421 
422  // If tag we are currently closing is the method wrapper.
423  if ($pos == $this->curent_root_struct) {
424  $this->status = 'body';
425  } elseif ($qname->name == 'Body' || $qname->name == 'Header') {
426  $this->status = 'envelope';
427  }
428 
429  // Set parent back to my parent.
430  $this->parent = $this->message[$pos]['parent'];
431 
432  // Handle any reverse references now.
433  $idref = $this->message[$pos]['id'];
434 
435  if ($idref != '' && array_key_exists($idref, $this->need_references)) {
436  foreach ($this->need_references[$idref] as $ref_pos) {
437  // XXX is this stuff there already?
438  $this->message[$ref_pos]['children'] = &$this->message[$pos]['children'];
439  $this->message[$ref_pos]['cdata'] = &$this->message[$pos]['cdata'];
440  $this->message[$ref_pos]['type'] = &$this->message[$pos]['type'];
441  $this->message[$ref_pos]['arraySize'] = &$this->message[$pos]['arraySize'];
442  $this->message[$ref_pos]['arrayType'] = &$this->message[$pos]['arrayType'];
443  }
444  }
445  }
446 
453  function characterData($parser, $data)
454  {
455  $pos = $this->depth_array[$this->depth];
456  if (isset($this->message[$pos]['cdata'])) {
457  $this->message[$pos]['cdata'] .= $data;
458  } else {
459  $this->message[$pos]['cdata'] = $data;
460  }
461  }
462 
471  function &getResponse()
472  {
473  if (isset($this->root_struct[0]) &&
474  $this->root_struct[0]) {
475  $response =& $this->buildResponse($this->root_struct[0]);
476  } else {
477  $response =& $this->_raiseSoapFault("couldn't build response");
478  }
479  return $response;
480  }
481 
490  function &getHeaders()
491  {
492  if (isset($this->header_struct[0]) &&
493  $this->header_struct[0]) {
494  $response = &$this->buildResponse($this->header_struct[0]);
495  } else {
496  // We don't fault if there are no headers; that can be handled by
497  // the application if necessary.
498  $response = null;
499  }
500  return $response;
501  }
502 
512  function decodeEntities($text)
513  {
514  $trans_tbl = array_flip($this->entities);
515  return strtr($text, $trans_tbl);
516  }
517 
518 }