Source for file Parser.php
Documentation is available at Parser.php
* This file contains the code for the SOAP message parser.
* LICENSE: This source file is subject to version 2.02 of the PHP license,
* that is bundled with this package in the file LICENSE, and is available at
* through the world-wide-web at http://www.php.net/license/2_02.txt. If you
* did not receive a copy of the PHP license and are unable to obtain it
* through the world-wide-web, please send a note to license@php.net so we can
* mail you a copy immediately.
* @author Dietrich Ayala <dietrich@ganx4.com> Original Author
* @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more
* @author Chuck Hagenbuch <chuck@horde.org> Maintenance
* @author Jan Schneider <jan@horde.org> Maintenance
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/2_02.txt PHP License 2.02
* @link http://pear.php.net/package/SOAP
require_once 'SOAP/Base.php';
require_once 'SOAP/Value.php';
* This class is used by SOAP::Message and SOAP::Server to parse soap
* packets. Originally based on SOAPx4 by Dietrich Ayala
* http://dietrich.ganx4.com/soapx4
* @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
* @author Dietrich Ayala <dietrich@ganx4.com> Original Author
* Used to handle non-root elements before root body element.
* @param string $xml XML content.
* @param string $encoding Character set encoding, defaults to 'UTF-8'.
* @param array $attachments List of attachments.
function SOAP_Parser($xml, $encoding = SOAP_DEFAULT_ENCODING ,
$this->attachments = $attachments;
// Check the XML tag for encoding.
if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/', $xml, $m)) {
// Determine where in the message we are (envelope, header, body,
// method). Check whether content has been read.
// Prepare the XML parser.
// Some lame SOAP implementations add nul bytes at the end of the
// SOAP stream, and expat chokes on that.
if ($xml[strlen($xml) - 1 ] == 0 ) {
$err = sprintf('XML error on line %d col %d byte %d %s',
* Returns an array of responses.
* After parsing a SOAP message, use this to get the response.
return $this->_raiseSoapFault ('Cannot build response');
* Returns an array of header responses.
* After parsing a SOAP message, use this to get the response.
// We don't fault if there are no headers; that can be handled by
// the application if necessary.
* Recurses to build a multi dimensional array.
function _domulti ($d, &$ar, &$r, &$v, $ad = 0 )
$this->_domulti ($d - 1 , $ar, $r[$ar[$ad]], $v, $ad + 1 );
* Loops through the message, building response structures.
* @param integer $pos Position.
function _buildResponse ($pos)
if (isset ($this->message[$pos]['children'])) {
foreach ($children as $c => $child_pos) {
if ($this->message[$child_pos]['type'] != null ) {
$response[] = $this->_buildResponse ($child_pos);
if (isset ($this->message[$pos]['arraySize'])) {
if (isset ($this->message[$pos]['arrayOffset'])) {
for ($i = 0; $i < $ardepth; $i++ ) {
$ar[$i] += $this->message[$pos]['arrayOffset'][$i];
for ($i = 0; $i < $elc; $i++ ) {
// Recurse to build a multi dimensional array.
$this->_domulti ($ardepth, $ar, $newresp, $response[$i]);
// Increment our array pointers.
$ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) {
} elseif (isset ($this->message[$pos]['arrayOffset']) &&
$this->message[$pos]['arrayOffset'][0 ] > 0 ) {
$pad = $this->message[$pos]['arrayOffset'][0 ] + count($response) * -1;
$response = array_pad($response, $pad, null );
foreach ($this->message[$pos]['attrs'] as $atn => $atv) {
// Add current node's value.
$this->message[$pos]['namespace']);
$this->message[$pos]['type_namespace']);
$response = new SOAP_Value($nqn->fqn (), $tqn->fqn (), $response,
if (isset ($this->message[$pos]['arrayType'])) {
$response->arrayType = $this->message[$pos]['arrayType'];
// Check if value is an empty array
if ($tqn->name == 'Array') {
$response = new SOAP_Value($nqn->fqn (), $tqn->fqn (), array (),
//if ($pos == 4) var_dump($this->message[$pos], $response);
$response = new SOAP_Value($nqn->fqn (), $tqn->fqn (),
// Handle header attribute that we need.
$response->actor = $this->message[$pos]['actor'];
$response->mustunderstand = $this->message[$pos]['mustUnderstand'];
* Start element handler used with the XML parser.
function _startElement ($parser, $name, $attrs)
// Position in a total number of elements, starting from 0.
// Update class level position.
// Parent/child/depth determinations.
// depth = How many levels removed from root?
// Set mine as current global depth and increment global depth value.
// Else add self as child to whoever the current parent is.
// Set self as current value for this depth.
// Set self as current parent.
$qname = new QName($name);
} elseif (strcasecmp('header', $qname->name ) == 0 ) {
$this->message[$pos]['type'] = 'Struct';
} elseif (strcasecmp('body', $qname->name ) == 0 ) {
} elseif ($this->status == 'body') {
// Is this element allowed to be a root?
// TODO: this needs to be optimized, we loop through $attrs twice
foreach ($attrs as $key => $value) {
if (stristr($key, ':root') && !$value) {
$this->message[$pos]['type'] = 'Struct';
$this->message[$pos]['attrs'] = $attrs;
// Loop through attributes, logging ns and type declarations.
foreach ($attrs as $key => $value) {
// If ns declarations, add to class level array of valid
if ($kqn->ns == 'xmlns') {
if (in_array($value, $this->_XMLSchema)) {
$this->_setSchemaVersion ($value);
$this->_namespaces[$value] = $prefix;
} elseif ($key == 'xmlns') {
$qname->ns = $this->_getNamespacePrefix ($value);
$qname->namespace = $value;
} elseif ($kqn->name == 'actor') {
$this->message[$pos]['actor'] = $value;
} elseif ($kqn->name == 'mustUnderstand') {
$this->message[$pos]['mustUnderstand'] = $value;
// If it's a type declaration, set type.
} elseif ($kqn->name == 'type') {
$vqn = new QName($value);
$this->message[$pos]['type'] = $vqn->name;
$this->message[$pos]['type_namespace'] = $this->_getNamespaceForPrefix ($vqn->ns );
// Should do something here with the namespace of specified
} elseif ($kqn->name == 'arrayType') {
$vqn = new QName($value);
$this->message[$pos]['type'] = 'Array';
if (isset ($vqn->arraySize )) {
$this->message[$pos]['arraySize'] = $vqn->arraySize;
$this->message[$pos]['arrayType'] = $vqn->name;
} elseif ($kqn->name == 'offset') {
} elseif ($kqn->name == 'id') {
// Save id to reference array.
$this->message[$pos]['id'] = $value;
} elseif ($kqn->name == 'href') {
$this->message[$pos]['children'] = &$this->message[$ref_pos]['children'];
$this->message[$pos]['arraySize'] = &$this->message[$ref_pos]['arraySize'];
$this->message[$pos]['arrayType'] = &$this->message[$ref_pos]['arrayType'];
// Reverse reference, store in 'need reference'.
} elseif (isset ($this->attachments[$value])) {
$this->message[$pos]['cdata'] = $this->attachments[$value];
// See if namespace is defined in tag.
if (isset ($attrs['xmlns:' . $qname->ns ])) {
$namespace = $attrs['xmlns:' . $qname->ns ];
} elseif ($qname->ns && !$qname->namespace ) {
$namespace = $this->_getNamespaceForPrefix ($qname->ns );
$this->message[$pos]['namespace'] = $namespace;
* End element handler used with the XML parser.
function _endElement ($parser, $name)
// Position of current element is equal to the last value left in
// depth_array for my depth.
// Bring depth down a notch.
$qname = new QName($name);
// Get type if not explicitly declared in an xsi:type attribute.
// TODO: check on integrating WSDL validation here.
if ($this->message[$pos]['type'] == '') {
if (isset ($this->message[$pos]['children'])) {
/* this is slow, need to look at some faster method
$children = explode('|', $this->message[$pos]['children']);
if (count($children) > 2 &&
$this->message[$children[1]]['name'] == $this->message[$children[2]]['name']) {
$this->message[$pos]['type'] = 'Array';
$this->message[$pos]['type'] = 'Struct';
$this->message[$pos]['type'] = 'Struct';
$parent = $this->message[$pos]['parent'];
if ($this->message[$parent]['type'] == 'Array' &&
isset ($this->message[$parent]['arrayType'])) {
$this->message[$pos]['type'] = 'string';
// If tag we are currently closing is the method wrapper.
} elseif ($qname->name == 'Body' || $qname->name == 'Header') {
// Set parent back to my parent.
// Handle any reverse references now.
$idref = $this->message[$pos]['id'];
// XXX is this stuff there already?
$this->message[$ref_pos]['children'] = &$this->message[$pos]['children'];
$this->message[$ref_pos]['arraySize'] = &$this->message[$pos]['arraySize'];
$this->message[$ref_pos]['arrayType'] = &$this->message[$pos]['arrayType'];
* Element content handler used with the XML parser.
function _characterData ($parser, $data)
if (isset ($this->message[$pos]['cdata'])) {
$this->message[$pos]['cdata'] .= $data;
$this->message[$pos]['cdata'] = $data;
Documentation generated on Mon, 04 Aug 2008 20:00:27 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|