📄 parser.php.tmp
字号:
<?php/** * This file contains the code for the SOAP message parser. * * PHP versions 4 and 5 * * 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. * * @category Web Services * @package SOAP * @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';/** * SOAP Parser * * 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 * * @access public * @package SOAP * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates * @author Dietrich Ayala <dietrich@ganx4.com> Original Author */class SOAP_Parser extends SOAP_Base{ var $status = ''; var $position = 0; var $pos_stat = 0; var $depth = 0; var $default_namespace = ''; var $message = array(); var $depth_array = array(); var $previous_element = ''; var $soapresponse = null; var $soapheaders = null; var $parent = 0; var $root_struct_name = array(); var $header_struct_name = array(); var $curent_root_struct_name = ''; var $entities = array ( '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"' ); var $root_struct = array(); var $header_struct = array(); var $curent_root_struct = 0; var $references = array(); var $need_references = array(); var $XMLSchemaVersion; var $bodyDepth; // used to handle non-root elements before root body element /** * SOAP_Parser constructor * * @param string xml content * @param string xml character encoding, defaults to 'UTF-8' */ function SOAP_Parser(&$xml, $encoding = SOAP_DEFAULT_ENCODING, $attachments = null) { parent::SOAP_Base('Parser'); $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION); $this->attachments = $attachments; // Check the xml tag for encoding. if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/', $xml, $m)) { $encoding = strtoupper($m[2] ? $m[2] : $m[3]); } // Determines where in the message we are // (envelope,header,body,method). Check whether content has // been read. if (!empty($xml)) { // Prepare the xml parser. $parser = xml_parser_create($encoding); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_set_object($parser, $this); xml_set_element_handler($parser, 'startElement', 'endElement'); xml_set_character_data_handler($parser, 'characterData'); // Some lame soap implementations add null bytes at the // end of the soap stream, and expat choaks on that. if ($xml[strlen($xml) - 1] == 0) { $xml = trim($xml); } // Parse the XML file. if (!xml_parse($parser, $xml, true)) { $err = sprintf('XML error on line %d col %d byte %d %s', xml_get_current_line_number($parser), xml_get_current_column_number($parser), xml_get_current_byte_index($parser), xml_error_string(xml_get_error_code($parser))); $this->_raiseSoapFault($err,htmlspecialchars($xml)); } xml_parser_free($parser); } } /** * domulti * recurse to build a multi-dim array, used by buildResponse * * @access private */ function domulti($d, &$ar, &$r, &$v, $ad=0) { if ($d) { $this->domulti($d-1, $ar, $r[$ar[$ad]], $v, $ad+1); } else { $r = $v; } } /** * buildResponse * loop through msg, building response structures * * @param int position * @return SOAP_Value * @access private */ function &buildResponse($pos) { $response = null; if (isset($this->message[$pos]['children'])) { $children = explode('|', $this->message[$pos]['children']); foreach ($children as $c => $child_pos) { if ($this->message[$child_pos]['type'] != null) { $response[] =& $this->buildResponse($child_pos); } } if (array_key_exists('arraySize', $this->message[$pos])) { $ardepth = count($this->message[$pos]['arraySize']); if ($ardepth > 1) { $ar = array_pad(array(), $ardepth, 0); if (array_key_exists('arrayOffset', $this->message[$pos])) { for ($i = 0; $i < $ardepth; $i++) { $ar[$i] += $this->message[$pos]['arrayOffset'][$i]; } } $elc = count($response); for ($i = 0; $i < $elc; $i++) { // recurse to build a multi-dimensional array $this->domulti($ardepth, $ar, $newresp, $response[$i]); // increment our array pointers $ad = $ardepth - 1; $ar[$ad]++; while ($ad > 0 && $ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) { $ar[$ad] = 0; $ad--; $ar[$ad]++; } } $response = $newresp; } elseif (isset($this->message[$pos]['arrayOffset']) && $this->message[$pos]['arrayOffset'][0] > 0) { // check for padding $pad = $this->message[$pos]['arrayOffset'][0] + count($response) * -1; $response = array_pad($response, $pad, null); } } } // Build attributes. $attrs = array(); foreach ($this->message[$pos]['attrs'] as $atn => $atv) { if (!strstr($atn, 'xmlns') && !strpos($atn, ':')) { $attrs[$atn] = $atv; } } // Add current node's value. if ($response) { $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']); $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']); $response =& new SOAP_Value($nqn->fqn(), $tqn->fqn(), $response, $attrs); if (isset($this->message[$pos]['arrayType'])) { $response->arrayType = $this->message[$pos]['arrayType']; } } else { $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']); $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']); $response =& new SOAP_Value($nqn->fqn(), $tqn->fqn(), $this->message[$pos]['cdata'], $attrs); } // handle header attribute that we need if (array_key_exists('actor', $this->message[$pos])) { $response->actor = $this->message[$pos]['actor']; } if (array_key_exists('mustUnderstand', $this->message[$pos])) { $response->mustunderstand = $this->message[$pos]['mustUnderstand']; } return $response; } /** * startElement * start-element handler used with xml parser * * @access private */ function startElement($parser, $name, $attrs) { // position in a total number of elements, starting from 0 // update class level pos $pos = $this->position++; // and set mine $this->message[$pos] = array(); $this->message[$pos]['type'] = ''; $this->message[$pos]['type_namespace'] = ''; $this->message[$pos]['cdata'] = ''; $this->message[$pos]['pos'] = $pos; $this->message[$pos]['id'] = ''; // parent/child/depth determinations // depth = how many levels removed from root? // set mine as current global depth and increment global depth value $this->message[$pos]['depth'] = $this->depth++; // else add self as child to whoever the current parent is if ($pos != 0) { if (isset($this->message[$this->parent]['children'])) $this->message[$this->parent]['children'] .= "|$pos"; else $this->message[$this->parent]['children'] = $pos; } // set my parent $this->message[$pos]['parent'] = $this->parent; // set self as current value for this depth $this->depth_array[$this->depth] = $pos; // set self as current parent $this->parent = $pos; $qname =& new QName($name); // set status if (strcasecmp('envelope', $qname->name) == 0) { $this->status = 'envelope'; } elseif (strcasecmp('header', $qname->name) == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -