home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / SOAP / Parser.php < prev    next >
Encoding:
PHP Script  |  2006-04-07  |  19.0 KB  |  518 lines

  1. <?php
  2. /**
  3.  * This file contains the code for the SOAP message parser.
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 2.02 of the PHP license,
  8.  * that is bundled with this package in the file LICENSE, and is available at
  9.  * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
  10.  * did not receive a copy of the PHP license and are unable to obtain it
  11.  * through the world-wide-web, please send a note to license@php.net so we can
  12.  * mail you a copy immediately.
  13.  *
  14.  * @category   Web Services
  15.  * @package    SOAP
  16.  * @author     Dietrich Ayala <dietrich@ganx4.com> Original Author
  17.  * @author     Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more
  18.  * @author     Chuck Hagenbuch <chuck@horde.org>   Maintenance
  19.  * @author     Jan Schneider <jan@horde.org>       Maintenance
  20.  * @copyright  2003-2005 The PHP Group
  21.  * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
  22.  * @link       http://pear.php.net/package/SOAP
  23.  */
  24.  
  25. require_once 'SOAP/Base.php';
  26. require_once 'SOAP/Value.php';
  27.  
  28. /**
  29.  * SOAP Parser
  30.  *
  31.  * This class is used by SOAP::Message and SOAP::Server to parse soap
  32.  * packets. Originally based on SOAPx4 by Dietrich Ayala
  33.  * http://dietrich.ganx4.com/soapx4
  34.  *
  35.  * @access public
  36.  * @package SOAP
  37.  * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  38.  * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
  39.  */
  40. class SOAP_Parser extends SOAP_Base
  41. {
  42.     var $status = '';
  43.     var $position = 0;
  44.     var $pos_stat = 0;
  45.     var $depth = 0;
  46.     var $default_namespace = '';
  47.     var $message = array();
  48.     var $depth_array = array();
  49.     var $previous_element = '';
  50.     var $soapresponse = null;
  51.     var $soapheaders = null;
  52.     var $parent = 0;
  53.     var $root_struct_name = array();
  54.     var $header_struct_name = array();
  55.     var $curent_root_struct_name = '';
  56.     var $entities = array ( '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"' );
  57.     var $root_struct = array();
  58.     var $header_struct = array();
  59.     var $curent_root_struct = 0;
  60.     var $references = array();
  61.     var $need_references = array();
  62.     var $XMLSchemaVersion;
  63.     var $bodyDepth; // used to handle non-root elements before root body element
  64.  
  65.     /**
  66.      * SOAP_Parser constructor
  67.      *
  68.      * @param string xml content
  69.      * @param string xml character encoding, defaults to 'UTF-8'
  70.      */
  71.     function SOAP_Parser(&$xml, $encoding = SOAP_DEFAULT_ENCODING, $attachments = null)
  72.     {
  73.         parent::SOAP_Base('Parser');
  74.         $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
  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.  
  114.     /**
  115.      * domulti
  116.      * recurse to build a multi-dim array, used by buildResponse
  117.      *
  118.      * @access private
  119.      */
  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.  
  129.     /**
  130.      * buildResponse
  131.      * loop through msg, building response structures
  132.      *
  133.      * @param int position
  134.      * @return SOAP_Value
  135.      * @access private
  136.      */
  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 SOAP_Value($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 SOAP_Value($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.  
  215.     /**
  216.      * startElement
  217.      * start-element handler used with xml parser
  218.      *
  219.      * @access private
  220.      */
  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.  
  382.     /**
  383.      * endElement
  384.      * end-element handler used with xml parser
  385.      *
  386.      * @access private
  387.      */
  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.  
  447.     /**
  448.      * characterData
  449.      * element content handler used with xml parser
  450.      *
  451.      * @access private
  452.      */
  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.  
  463.     /**
  464.      * getResponse
  465.      *
  466.      * returns an array of responses
  467.      * after parsing a soap message, use this to get the response
  468.      *
  469.      * @return   array
  470.      * @access public
  471.      */
  472.     function &getResponse()
  473.     {
  474.         if (isset($this->root_struct[0]) &&
  475.             $this->root_struct[0]) {
  476.             return $this->buildResponse($this->root_struct[0]);
  477.         }
  478.         return $this->_raiseSoapFault("couldn't build response");
  479.     }
  480.  
  481.     /**
  482.      * getHeaders
  483.      *
  484.      * returns an array of header responses
  485.      * after parsing a soap message, use this to get the response
  486.      *
  487.      * @return   array
  488.      * @access public
  489.      */
  490.     function &getHeaders()
  491.     {
  492.         if (isset($this->header_struct[0]) &&
  493.             $this->header_struct[0]) {
  494.             return $this->buildResponse($this->header_struct[0]);
  495.         }
  496.  
  497.         // We don't fault if there are no headers that can be handled
  498.         // by the app if necessary.
  499.         return null;
  500.     }
  501.  
  502.     /**
  503.      * decodeEntities
  504.      *
  505.      * removes entities from text
  506.      *
  507.      * @param string
  508.      * @return   string
  509.      * @access private
  510.      */
  511.     function decodeEntities($text)
  512.     {
  513.         $trans_tbl = array_flip($this->entities);
  514.         return strtr($text, $trans_tbl);
  515.     }
  516.  
  517. }
  518.