• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

payment/bmf/lib/SOAP/class.ilBMFParser.php

Go to the documentation of this file.
00001 <?php
00002 //
00003 // +----------------------------------------------------------------------+
00004 // | PHP Version 4                                                        |
00005 // +----------------------------------------------------------------------+
00006 // | Copyright (c) 1997-2003 The PHP Group                                |
00007 // +----------------------------------------------------------------------+
00008 // | This source file is subject to version 2.02 of the PHP license,      |
00009 // | that is bundled with this package in the file LICENSE, and is        |
00010 // | available at through the world-wide-web at                           |
00011 // | http://www.php.net/license/2_02.txt.                                 |
00012 // | If you did not receive a copy of the PHP license and are unable to   |
00013 // | obtain it through the world-wide-web, please send a note to          |
00014 // | license@php.net so we can mail you a copy immediately.               |
00015 // +----------------------------------------------------------------------+
00016 // | Authors: Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more   |
00017 // | Authors: Dietrich Ayala <dietrich@ganx4.com> Original Author         |
00018 // +----------------------------------------------------------------------+
00019 //
00020 // $Id: class.ilBMFParser.php 11401 2006-07-12 10:40:54Z jconze $
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 ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', "'" => '&apos;', '"' => '&quot;' );
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; // used to handle non-root elements before root body element
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         // check the xml tag for encoding
00077         if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/',$xml,$m)) {
00078             $encoding = strtoupper($m[2]?$m[2]:$m[3]);
00079         }
00080         
00081         // determines where in the message we are (envelope,header,body,method)
00082         // Check whether content has been read.
00083         if (!empty($xml)) {
00084             // prepare the xml parser
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             // some lame soap implementations add null bytes at the
00092             // end of the soap stream, and expat choaks on that
00093             if ($xml[strlen($xml)-1]==0)
00094                 $xml = trim($xml);
00095             
00096             // Parse the XML file.
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                         // recurse to build a multi-dim array
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         // build attributes
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         // add current node's value
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         // handle header attribute that we need
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         // position in a total number of elements, starting from 0
00213         // update class level pos
00214         $pos = $this->position++;
00215 
00216         // and set mine
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         // parent/child/depth determinations
00225         
00226         // depth = how many levels removed from root?
00227         // set mine as current global depth and increment global depth value
00228         $this->message[$pos]['depth'] = $this->depth++;
00229         
00230         // else add self as child to whoever the current parent is
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         // set my parent
00239         $this->message[$pos]['parent'] = $this->parent;
00240 
00241         // set self as current value for this depth
00242         $this->depth_array[$this->depth] = $pos;
00243         // set self as current parent
00244         $this->parent = $pos;
00245         $qname =& new ilBMFQName($name);
00246         // set status
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         // set method
00258         } elseif ($this->status == 'body') {
00259             // is this element allowed to be a root?
00260             // XXX this needs to be optimized, we loop through attrs twice now
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         // set my status
00279         $this->message[$pos]['status'] = $this->status;
00280         
00281         // set name
00282         $this->message[$pos]['name'] = htmlspecialchars($qname->name);
00283 
00284         // set attrs
00285         $this->message[$pos]['attrs'] = $attrs;
00286 
00287         // loop through atts, logging ns and type declarations
00288         foreach ($attrs as $key => $value) {
00289             // if ns declarations, add to class level array of valid namespaces
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                 // set method namespace
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             // if it's a type declaration, set type
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                 // should do something here with the namespace of specified type?
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         // see if namespace is defined in tag
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         // get namespace
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         // position of current element is equal to the last value left in depth_array for my depth
00385         $pos = $this->depth_array[$this->depth];
00386         // bring depth down a notch
00387         $this->depth--;
00388         $qname =& new ilBMFQName($name);
00389         
00390         // get type if not explicitly declared in an xsi:type attribute
00391         // XXX check on integrating wsdl validation here
00392         if ($this->message[$pos]['type'] == '') {
00393             if (isset($this->message[$pos]['children'])) {
00394                 /* this is slow, need to look at some faster method
00395                 $children = explode('|',$this->message[$pos]['children']);
00396                 if (count($children) > 2 &&
00397                     $this->message[$children[1]]['name'] == $this->message[$children[2]]['name']) {
00398                     $this->message[$pos]['type'] = 'Array';
00399                 } else { 
00400                     $this->message[$pos]['type'] = 'Struct';
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         // if tag we are currently closing is the method wrapper
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         // set parent back to my parent
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         // we don't fault if there are no headers
00494         // that can be handled by the app if necessary
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 ?>

Generated on Fri Dec 13 2013 11:57:58 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1