home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / SOAP / Parser.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  19.4 KB  |  538 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.  
  57.     /**
  58.      * XML entities.
  59.      *
  60.      * @var array
  61.      */
  62.     var $entities = array('&' => '&',
  63.                           '<' => '<',
  64.                           '>' => '>',
  65.                           "'" => ''',
  66.                           '"' => '"');
  67.  
  68.     var $root_struct = array();
  69.     var $header_struct = array();
  70.     var $curent_root_struct = 0;
  71.     var $references = array();
  72.     var $need_references = array();
  73.     var $XMLSchemaVersion;
  74.  
  75.     /**
  76.      * Used to handle non-root elements before root body element.
  77.      *
  78.      * @var integer
  79.      */
  80.     var $bodyDepth;
  81.  
  82.     /**
  83.      * Constructor.
  84.      *
  85.      * @param string $xml         XML content.
  86.      * @param string $encoding    Character set encoding, defaults to 'UTF-8'.
  87.      * @param array $attachments  List of attachments.
  88.      */
  89.     function SOAP_Parser(&$xml, $encoding = SOAP_DEFAULT_ENCODING,
  90.                          $attachments = null)
  91.     {
  92.         parent::SOAP_Base('Parser');
  93.         $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
  94.  
  95.         $this->attachments = $attachments;
  96.  
  97.         // Check the xml tag for encoding.
  98.         if (preg_match('/<\?xml[^>]+encoding\s*?=\s*?(\'([^\']*)\'|"([^"]*)")[^>]*?[\?]>/', $xml, $m)) {
  99.             $encoding = strtoupper($m[2] ? $m[2] : $m[3]);
  100.         }
  101.  
  102.         // Determines where in the message we are
  103.         // (envelope,header,body,method). Check whether content has
  104.         // been read.
  105.         if (!empty($xml)) {
  106.             // Prepare the xml parser.
  107.             $parser = xml_parser_create($encoding);
  108.             xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  109.             xml_set_object($parser, $this);
  110.             xml_set_element_handler($parser, 'startElement', 'endElement');
  111.             xml_set_character_data_handler($parser, 'characterData');
  112.  
  113.             // Some lame soap implementations add null bytes at the
  114.             // end of the soap stream, and expat choaks on that.
  115.             if ($xml[strlen($xml) - 1] == 0) {
  116.                 $xml = trim($xml);
  117.             }
  118.  
  119.             // Parse the XML file.
  120.             if (!xml_parse($parser, $xml, true)) {
  121.                 $err = sprintf('XML error on line %d col %d byte %d %s',
  122.                     xml_get_current_line_number($parser),
  123.                     xml_get_current_column_number($parser),
  124.                     xml_get_current_byte_index($parser),
  125.                     xml_error_string(xml_get_error_code($parser)));
  126.                 $this->_raiseSoapFault($err,htmlspecialchars($xml));
  127.             }
  128.             xml_parser_free($parser);
  129.         }
  130.     }
  131.  
  132.  
  133.     /**
  134.      * domulti
  135.      * recurse to build a multi-dim array, used by buildResponse
  136.      *
  137.      * @access private
  138.      */
  139.     function domulti($d, &$ar, &$r, &$v, $ad=0)
  140.     {
  141.         if ($d) {
  142.             $this->domulti($d-1, $ar, $r[$ar[$ad]], $v, $ad+1);
  143.         } else {
  144.             $r = $v;
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * buildResponse
  150.      * loop through msg, building response structures
  151.      *
  152.      * @param int position
  153.      * @return SOAP_Value
  154.      * @access private
  155.      */
  156.     function &buildResponse($pos)
  157.     {
  158.         $response = null;
  159.  
  160.         if (isset($this->message[$pos]['children'])) {
  161.             $children = explode('|', $this->message[$pos]['children']);
  162.  
  163.             foreach ($children as $c => $child_pos) {
  164.                 if ($this->message[$child_pos]['type'] != null) {
  165.                     $response[] =& $this->buildResponse($child_pos);
  166.                 }
  167.             }
  168.             if (array_key_exists('arraySize', $this->message[$pos])) {
  169.                 $ardepth = count($this->message[$pos]['arraySize']);
  170.                 if ($ardepth > 1) {
  171.                     $ar = array_pad(array(), $ardepth, 0);
  172.                     if (array_key_exists('arrayOffset', $this->message[$pos])) {
  173.                         for ($i = 0; $i < $ardepth; $i++) {
  174.                             $ar[$i] += $this->message[$pos]['arrayOffset'][$i];
  175.                         }
  176.                     }
  177.                     $elc = count($response);
  178.                     for ($i = 0; $i < $elc; $i++) {
  179.                         // recurse to build a multi-dimensional array
  180.                         $this->domulti($ardepth, $ar, $newresp, $response[$i]);
  181.  
  182.                         // increment our array pointers
  183.                         $ad = $ardepth - 1;
  184.                         $ar[$ad]++;
  185.                         while ($ad > 0 && $ar[$ad] >= $this->message[$pos]['arraySize'][$ad]) {
  186.                             $ar[$ad] = 0;
  187.                             $ad--;
  188.                             $ar[$ad]++;
  189.                         }
  190.                     }
  191.                     $response = $newresp;
  192.                 } elseif (isset($this->message[$pos]['arrayOffset']) &&
  193.                           $this->message[$pos]['arrayOffset'][0] > 0) {
  194.                     // check for padding
  195.                     $pad = $this->message[$pos]['arrayOffset'][0] + count($response) * -1;
  196.                     $response = array_pad($response, $pad, null);
  197.                 }
  198.             }
  199.         }
  200.  
  201.         // Build attributes.
  202.         $attrs = array();
  203.         foreach ($this->message[$pos]['attrs'] as $atn => $atv) {
  204.             if (!strstr($atn, 'xmlns') &&
  205.                 !strpos($atn, ':')) {
  206.                 $attrs[$atn] = $atv;
  207.             }
  208.         }
  209.  
  210.         // Add current node's value.
  211.         if ($response) {
  212.             $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
  213.             $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
  214.             $response =& new SOAP_Value($nqn->fqn(), $tqn->fqn(), $response, $attrs);
  215.             if (isset($this->message[$pos]['arrayType'])) {
  216.                 $response->arrayType = $this->message[$pos]['arrayType'];
  217.             }
  218.         } else {
  219.             $nqn =& new Qname($this->message[$pos]['name'], $this->message[$pos]['namespace']);
  220.             $tqn =& new Qname($this->message[$pos]['type'], $this->message[$pos]['type_namespace']);
  221.             $response =& new SOAP_Value($nqn->fqn(), $tqn->fqn(), $this->message[$pos]['cdata'], $attrs);
  222.         }
  223.  
  224.         // handle header attribute that we need
  225.         if (array_key_exists('actor', $this->message[$pos])) {
  226.             $response->actor = $this->message[$pos]['actor'];
  227.         }
  228.         if (array_key_exists('mustUnderstand', $this->message[$pos])) {
  229.             $response->mustunderstand = $this->message[$pos]['mustUnderstand'];
  230.         }
  231.         return $response;
  232.     }
  233.  
  234.     /**
  235.      * startElement
  236.      * start-element handler used with xml parser
  237.      *
  238.      * @access private
  239.      */
  240.     function startElement($parser, $name, $attrs)
  241.     {
  242.         // position in a total number of elements, starting from 0
  243.         // update class level pos
  244.         $pos = $this->position++;
  245.  
  246.         // and set mine
  247.         $this->message[$pos] = array();
  248.         $this->message[$pos]['type'] = '';
  249.         $this->message[$pos]['type_namespace'] = '';
  250.         $this->message[$pos]['cdata'] = '';
  251.         $this->message[$pos]['pos'] = $pos;
  252.         $this->message[$pos]['id'] = '';
  253.  
  254.         // parent/child/depth determinations
  255.  
  256.         // depth = how many levels removed from root?
  257.         // set mine as current global depth and increment global depth value
  258.         $this->message[$pos]['depth'] = $this->depth++;
  259.  
  260.         // else add self as child to whoever the current parent is
  261.         if ($pos != 0) {
  262.             if (isset($this->message[$this->parent]['children']))
  263.                 $this->message[$this->parent]['children'] .= "|$pos";
  264.             else
  265.                 $this->message[$this->parent]['children'] = $pos;
  266.         }
  267.  
  268.         // set my parent
  269.         $this->message[$pos]['parent'] = $this->parent;
  270.  
  271.         // set self as current value for this depth
  272.         $this->depth_array[$this->depth] = $pos;
  273.         // set self as current parent
  274.         $this->parent = $pos;
  275.         $qname =& new QName($name);
  276.         // set status
  277.         if (strcasecmp('envelope', $qname->name) == 0) {
  278.             $this->status = 'envelope';
  279.         } elseif (strcasecmp('header', $qname->name) == 0) {
  280.             $this->status = 'header';
  281.             $this->header_struct_name[] = $this->curent_root_struct_name = $qname->name;
  282.             $this->header_struct[] = $this->curent_root_struct = $pos;
  283.             $this->message[$pos]['type'] = 'Struct';
  284.         } elseif (strcasecmp('body', $qname->name) == 0) {
  285.             $this->status = 'body';
  286.             $this->bodyDepth = $this->depth;
  287.  
  288.         // Set method
  289.         } elseif ($this->status == 'body') {
  290.             // Is this element allowed to be a root?
  291.             // XXX this needs to be optimized, we loop through attrs twice now.
  292.             $can_root = $this->depth == $this->bodyDepth + 1;
  293.             if ($can_root) {
  294.                 foreach ($attrs as $key => $value) {
  295.                     if (stristr($key, ':root') && !$value) {
  296.                         $can_root = FALSE;
  297.                     }
  298.                 }
  299.             }
  300.  
  301.             if ($can_root) {
  302.                 $this->status = 'method';
  303.                 $this->root_struct_name[] = $this->curent_root_struct_name = $qname->name;
  304.                 $this->root_struct[] = $this->curent_root_struct = $pos;
  305.                 $this->message[$pos]['type'] = 'Struct';
  306.             }
  307.         }
  308.  
  309.         // Set my status.
  310.         $this->message[$pos]['status'] = $this->status;
  311.  
  312.         // Set name.
  313.         $this->message[$pos]['name'] = htmlspecialchars($qname->name);
  314.  
  315.         // Set attributes.
  316.         $this->message[$pos]['attrs'] = $attrs;
  317.  
  318.         // Loop through attributes, logging ns and type declarations.
  319.         foreach ($attrs as $key => $value) {
  320.             // If ns declarations, add to class level array of valid
  321.             // namespaces.
  322.             $kqn =& new QName($key);
  323.             if ($kqn->ns == 'xmlns') {
  324.                 $prefix = $kqn->name;
  325.  
  326.                 if (in_array($value, $this->_XMLSchema)) {
  327.                     $this->_setSchemaVersion($value);
  328.                 }
  329.  
  330.                 $this->_namespaces[$value] = $prefix;
  331.  
  332.             // Set method namespace.
  333.             } elseif ($key == 'xmlns') {
  334.                 $qname->ns = $this->_getNamespacePrefix($value);
  335.                 $qname->namespace = $value;
  336.             } elseif ($kqn->name == 'actor') {
  337.                 $this->message[$pos]['actor'] = $value;
  338.             } elseif ($kqn->name == 'mustUnderstand') {
  339.                 $this->message[$pos]['mustUnderstand'] = $value;
  340.  
  341.             // If it's a type declaration, set type.
  342.             } elseif ($kqn->name == 'type') {
  343.                 $vqn =& new QName($value);
  344.                 $this->message[$pos]['type'] = $vqn->name;
  345.                 $this->message[$pos]['type_namespace'] = $this->_getNamespaceForPrefix($vqn->ns);
  346.                 // Should do something here with the namespace of
  347.                 // specified type?
  348.  
  349.             } elseif ($kqn->name == 'arrayType') {
  350.                 $vqn =& new QName($value);
  351.                 $this->message[$pos]['type'] = 'Array';
  352.                 if (isset($vqn->arraySize)) {
  353.                     $this->message[$pos]['arraySize'] = $vqn->arraySize;
  354.                 }
  355.                 $this->message[$pos]['arrayType'] = $vqn->name;
  356.  
  357.             } elseif ($kqn->name == 'offset') {
  358.                 $this->message[$pos]['arrayOffset'] = split(',', substr($value, 1, strlen($value) - 2));
  359.  
  360.             } elseif ($kqn->name == 'id') {
  361.                 // Save id to reference array.
  362.                 $this->references[$value] = $pos;
  363.                 $this->message[$pos]['id'] = $value;
  364.  
  365.             } elseif ($kqn->name == 'href') {
  366.                 if ($value[0] == '#') {
  367.                     $ref = substr($value, 1);
  368.                     if (isset($this->references[$ref])) {
  369.                         // cdata, type, inval.
  370.                         $ref_pos = $this->references[$ref];
  371.                         $this->message[$pos]['children'] = &$this->message[$ref_pos]['children'];
  372.                         $this->message[$pos]['cdata'] = &$this->message[$ref_pos]['cdata'];
  373.                         $this->message[$pos]['type'] = &$this->message[$ref_pos]['type'];
  374.                         $this->message[$pos]['arraySize'] = &$this->message[$ref_pos]['arraySize'];
  375.                         $this->message[$pos]['arrayType'] = &$this->message[$ref_pos]['arrayType'];
  376.                     } else {
  377.                         // Reverse reference, store in 'need reference'.
  378.                         if (!isset($this->need_references[$ref])) {
  379.                             $this->need_references[$ref] = array();
  380.                         }
  381.                         $this->need_references[$ref][] = $pos;
  382.                     }
  383.                 } elseif (isset($this->attachments[$value])) {
  384.                     $this->message[$pos]['cdata'] = $this->attachments[$value];
  385.                 }
  386.             }
  387.         }
  388.         // See if namespace is defined in tag.
  389.         if (array_key_exists('xmlns:' . $qname->ns, $attrs)) {
  390.             $namespace = $attrs['xmlns:' . $qname->ns];
  391.         } elseif ($qname->ns && !$qname->namespace) {
  392.             $namespace = $this->_getNamespaceForPrefix($qname->ns);
  393.         } else {
  394.             // Get namespace.
  395.             $namespace = $qname->namespace ? $qname->namespace : $this->default_namespace;
  396.         }
  397.         $this->message[$pos]['namespace'] = $namespace;
  398.         $this->default_namespace = $namespace;
  399.     }
  400.  
  401.     /**
  402.      * endElement
  403.      * end-element handler used with xml parser
  404.      *
  405.      * @access private
  406.      */
  407.     function endElement($parser, $name)
  408.     {
  409.         // Position of current element is equal to the last value left
  410.         // in depth_array for my depth.
  411.         $pos = $this->depth_array[$this->depth];
  412.  
  413.         // Bring depth down a notch.
  414.         $this->depth--;
  415.         $qname =& new QName($name);
  416.  
  417.         // Get type if not explicitly declared in an xsi:type attribute.
  418.         // XXX check on integrating wsdl validation here
  419.         if ($this->message[$pos]['type'] == '') {
  420.             if (isset($this->message[$pos]['children'])) {
  421.                 /* this is slow, need to look at some faster method
  422.                 $children = explode('|', $this->message[$pos]['children']);
  423.                 if (count($children) > 2 &&
  424.                     $this->message[$children[1]]['name'] == $this->message[$children[2]]['name']) {
  425.                     $this->message[$pos]['type'] = 'Array';
  426.                 } else {
  427.                     $this->message[$pos]['type'] = 'Struct';
  428.                 }*/
  429.                 $this->message[$pos]['type'] = 'Struct';
  430.             } else {
  431.                 $parent = $this->message[$pos]['parent'];
  432.                 if ($this->message[$parent]['type'] == 'Array' &&
  433.                   array_key_exists('arrayType', $this->message[$parent])) {
  434.                     $this->message[$pos]['type'] = $this->message[$parent]['arrayType'];
  435.                 } else {
  436.                     $this->message[$pos]['type'] = 'string';
  437.                 }
  438.             }
  439.         }
  440.  
  441.         // If tag we are currently closing is the method wrapper.
  442.         if ($pos == $this->curent_root_struct) {
  443.             $this->status = 'body';
  444.         } elseif ($qname->name == 'Body' || $qname->name == 'Header') {
  445.             $this->status = 'envelope';
  446.         }
  447.  
  448.         // Set parent back to my parent.
  449.         $this->parent = $this->message[$pos]['parent'];
  450.  
  451.         // Handle any reverse references now.
  452.         $idref = $this->message[$pos]['id'];
  453.  
  454.         if ($idref != '' && array_key_exists($idref, $this->need_references)) {
  455.             foreach ($this->need_references[$idref] as $ref_pos) {
  456.                 // XXX is this stuff there already?
  457.                 $this->message[$ref_pos]['children'] = &$this->message[$pos]['children'];
  458.                 $this->message[$ref_pos]['cdata'] = &$this->message[$pos]['cdata'];
  459.                 $this->message[$ref_pos]['type'] = &$this->message[$pos]['type'];
  460.                 $this->message[$ref_pos]['arraySize'] = &$this->message[$pos]['arraySize'];
  461.                 $this->message[$ref_pos]['arrayType'] = &$this->message[$pos]['arrayType'];
  462.             }
  463.         }
  464.     }
  465.  
  466.     /**
  467.      * characterData
  468.      * element content handler used with xml parser
  469.      *
  470.      * @access private
  471.      */
  472.     function characterData($parser, $data)
  473.     {
  474.         $pos = $this->depth_array[$this->depth];
  475.         if (isset($this->message[$pos]['cdata'])) {
  476.             $this->message[$pos]['cdata'] .= $data;
  477.         } else {
  478.             $this->message[$pos]['cdata'] = $data;
  479.         }
  480.     }
  481.  
  482.     /**
  483.      * Returns an array of responses.
  484.      *
  485.      * After parsing a SOAP message, use this to get the response.
  486.      *
  487.      * @return array
  488.      * @access public
  489.      */
  490.     function &getResponse()
  491.     {
  492.         if (isset($this->root_struct[0]) &&
  493.             $this->root_struct[0]) {
  494.             $response =& $this->buildResponse($this->root_struct[0]);
  495.         } else {
  496.             $response =& $this->_raiseSoapFault("couldn't build response");
  497.         }
  498.         return $response;
  499.     }
  500.  
  501.     /**
  502.      * Returns an array of header responses.
  503.      *
  504.      * After parsing a SOAP message, use this to get the response.
  505.      *
  506.      * @return array
  507.      * @access public
  508.      */
  509.     function &getHeaders()
  510.     {
  511.         if (isset($this->header_struct[0]) &&
  512.             $this->header_struct[0]) {
  513.             $response = &$this->buildResponse($this->header_struct[0]);
  514.         } else {
  515.             // We don't fault if there are no headers; that can be handled by
  516.             // the application if necessary.
  517.             $response = null;
  518.         }
  519.         return $response;
  520.     }
  521.  
  522.     /**
  523.      * decodeEntities
  524.      *
  525.      * removes entities from text
  526.      *
  527.      * @param string
  528.      * @return   string
  529.      * @access private
  530.      */
  531.     function decodeEntities($text)
  532.     {
  533.         $trans_tbl = array_flip($this->entities);
  534.         return strtr($text, $trans_tbl);
  535.     }
  536.  
  537. }
  538.