[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezsoap/classes/ -> ezsoapresponse.php (source)

   1  <?php
   2  //
   3  // $Id$
   4  //
   5  // Definition of eZSOAPResponse class
   6  //
   7  // Bård Farstad <bf@ez.no>
   8  // Created on: <19-Feb-2002 16:51:10 bf>
   9  //
  10  // SOFTWARE NAME: eZ publish
  11  // SOFTWARE RELEASE: 3.9.0
  12  // BUILD VERSION: 17785
  13  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  14  // SOFTWARE LICENSE: GNU General Public License v2.0
  15  // NOTICE: >
  16  //   This program is free software; you can redistribute it and/or
  17  //   modify it under the terms of version 2.0  of the GNU General
  18  //   Public License as published by the Free Software Foundation.
  19  //
  20  //   This program is distributed in the hope that it will be useful,
  21  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  22  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23  //   GNU General Public License for more details.
  24  //
  25  //   You should have received a copy of version 2.0 of the GNU General
  26  //   Public License along with this program; if not, write to the Free
  27  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  28  //   MA 02110-1301, USA.
  29  //
  30  //
  31  
  32  /*!
  33    \class eZSOAPResponse ezsoapresponse.php
  34    \ingroup eZSOAP
  35    \brief eZSOAPResponse handles SOAP response packages
  36  
  37  */
  38  
  39  include_once ( "lib/ezutils/classes/ezdebug.php" );
  40  include_once ( 'lib/ezsoap/classes/ezsoapcodec.php' );
  41  include_once ( "lib/ezxml/classes/ezxml.php" );
  42  include_once ( "lib/ezsoap/classes/ezsoapenvelope.php" );
  43  
  44  class eZSOAPResponse extends eZSOAPEnvelope
  45  {
  46      /*!
  47        Constructs a new SOAP response
  48      */
  49      function eZSOAPResponse( $name="", $namespace="" )
  50      {
  51          $this->Name = $name;
  52          $this->Namespace = $namespace;
  53  
  54          // call the parents constructor
  55          $this->eZSOAPEnvelope();
  56      }
  57  
  58      /*!
  59        Decodes the SOAP response stream
  60      */
  61      function decodeStream( $request, $stream )
  62      {
  63          $stream = $this->stripHTTPHeader( $stream );
  64  
  65          $xml = new eZXML();
  66  
  67          $dom = $xml->domTree( $stream );
  68          $this->DOMDocument = $dom;
  69  
  70          if ( get_class( $dom ) == "ezdomdocument" )
  71          {
  72              // check for fault
  73              $response = $dom->elementsByNameNS( 'Fault', EZ_SOAP_ENV );
  74  
  75              if ( count( $response ) == 1 )
  76              {
  77                  $this->IsFault = 1;
  78                  $faultStringArray = $dom->elementsByName( "faultstring" );
  79                  $this->FaultString = $faultStringArray[0]->textContent();
  80  
  81                  $faultCodeArray = $dom->elementsByName( "faultcode" );
  82                  $this->FaultCode = $faultCodeArray[0]->textContent();
  83                  return;
  84              }
  85  
  86              // get the response
  87              $response = $dom->elementsByNameNS( $request->name() . "Response", $request->namespace() );
  88  
  89              $response = $response[0];
  90  
  91              if ( get_class( $response ) == "ezdomnode" )
  92              {
  93                  /* Cut from the SOAP spec:
  94                  The method response is viewed as a single struct containing an accessor
  95                  for the return value and each [out] or [in/out] parameter.
  96                  The first accessor is the return value followed by the parameters
  97                  in the same order as in the method signature.
  98  
  99                  Each parameter accessor has a name corresponding to the name
 100                  of the parameter and type corresponding to the type of the parameter.
 101                  The name of the return value accessor is not significant.
 102                  Likewise, the name of the struct is not significant.
 103                  However, a convention is to name it after the method name
 104                  with the string "Response" appended.
 105                  */
 106  
 107                  $responseAccessors = $response->children();
 108                  if ( count($responseAccessors) > 0 )
 109                  {
 110                      $returnObject = $responseAccessors[0];
 111                      $this->Value  = $this->decodeDataTypes( $returnObject );
 112                  }
 113              }
 114              else
 115              {
 116                  eZDebug::writeError( "Got error from server" );
 117              }
 118          }
 119          else
 120          {
 121              eZDebug::writeError( "Could not process XML in response" );
 122          }
 123      }
 124  
 125      /*!
 126        \static
 127        Decodes a DOM node and returns the PHP datatype instance of it.
 128      */
 129      function decodeDataTypes( $node, $type="" )
 130      {
 131          $returnValue = false;
 132  
 133          $attr = $node->attributeValueNS( "type", EZ_SOAP_SCHEMA_INSTANCE );
 134  
 135          if ( !$attr )
 136          {
 137              $attr = $node->attributeValueNS( "type", "http://www.w3.org/1999/XMLSchema-instance" );
 138          }
 139  
 140          $dataType = $type;
 141          $attrParts = explode( ":", $attr );
 142          if ( $attrParts[1] )
 143          {
 144              $dataType = $attrParts[1];
 145          }
 146  
 147  /*
 148          $typeNamespacePrefix = $this->DOMDocument->namespaceByAlias( $attrParts[0] );
 149  
 150          check that this is a namespace type definition
 151                  if ( ( $typeNamespacePrefix == EZ_SOAP_SCHEMA_DATA ) ||
 152                       ( $typeNamespacePrefix == EZ_SOAP_ENC )
 153                       )
 154  TODO: add encoding checks with schema validation.
 155  */
 156  
 157          switch ( $dataType )
 158          {
 159              case "string" :
 160              case "int" :
 161              case "float" :
 162              case 'double' :
 163              {
 164                  $returnValue = $node->textContent();
 165              } break;
 166  
 167              case "boolean" :
 168              {
 169                  if ( $node->textContent() == "true" )
 170                      $returnValue = true;
 171                  else
 172                      $returnValue = false;
 173              } break;
 174  
 175              case "base64" :
 176              {
 177                  $returnValue = base64_decode( $node->textContent() );
 178              } break;
 179  
 180              case "Array" :
 181              {
 182                  // Get array type
 183                  $arrayType = $node->attributeValueNS( "arrayType", EZ_SOAP_ENC );
 184  
 185                  $arrayTypeParts = explode( ":", $arrayType );
 186  
 187                  preg_match( "#(.*)\[(.*)\]#",  $arrayTypeParts[1], $matches );
 188  
 189                  $type = $matches[1];
 190                  $count = $matches[2];
 191  
 192                  $returnValue = array();
 193                  foreach( $node->children() as $child )
 194                  {
 195                      $returnValue[] = eZSOAPResponse::decodeDataTypes( $child, $type );
 196                  }
 197              }break;
 198  
 199              case "SOAPStruct" :
 200              {
 201                  $returnValue = array();
 202  
 203                  foreach( $node->children() as $child )
 204                  {
 205                      $returnValue[$child->name()] = eZSOAPResponse::decodeDataTypes( $child );
 206                  }
 207              }break;
 208  
 209              default:
 210              {
 211                  foreach ( $node->children() as $childNode )
 212                  {
 213                      // check data type for child
 214                      $attr = $childNode->attributeValueNS( "type", EZ_SOAP_SCHEMA_INSTANCE );
 215  
 216                      $dataType = false;
 217                      $attrParts = explode( ":", $attr );
 218                      $dataType = $attrParts[1];
 219  
 220  
 221                      $returnValue[$childNode->name()] = eZSOAPResponse::decodeDataTypes( $childNode );
 222                  }
 223  
 224              } break;
 225          }
 226  
 227          return $returnValue;
 228      }
 229  
 230      /*!
 231        Returns the XML payload for the response.
 232      */
 233      function payload( )
 234      {
 235          $doc = new eZDOMDocument();
 236          $doc->setName( "eZSOAP message" );
 237  
 238          $root = $doc->createElementNodeNS( EZ_SOAP_ENV, "Envelope" );
 239  
 240          $root->appendAttribute( $doc->createAttributeNamespaceDefNode( EZ_SOAP_XSI_PREFIX, EZ_SOAP_SCHEMA_INSTANCE ) );
 241          $root->appendAttribute( $doc->createAttributeNamespaceDefNode( EZ_SOAP_XSD_PREFIX, EZ_SOAP_SCHEMA_DATA ) );
 242          $root->appendAttribute( $doc->createAttributeNamespaceDefNode( EZ_SOAP_ENC_PREFIX, EZ_SOAP_ENC ) );
 243          $root->setPrefix( EZ_SOAP_ENV_PREFIX );
 244  
 245          // add the body
 246          $body = $doc->createElementNode( "Body" );
 247  
 248          $body->setPrefix( EZ_SOAP_ENV_PREFIX );
 249          $root->appendChild( $body );
 250  
 251          // Check if it's a fault
 252          if ( get_class( $this->Value ) == 'ezsoapfault' )
 253          {
 254              $fault = $doc->createElementNode( "Fault" );
 255              $fault->setPrefix( EZ_SOAP_ENV_PREFIX );
 256  
 257              $faultCodeNode = $doc->createElementNode( "faultcode" );
 258              $faultCodeNode->appendChild( eZDOMDocument::createTextNode( $this->Value->faultCode() ) );
 259  
 260              $fault->appendChild( $faultCodeNode );
 261  
 262              $faultStringNode = $doc->createElementNode( "faultstring" );
 263              $faultStringNode->appendChild( eZDOMDocument::createTextNode( $this->Value->faultString() ) );
 264  
 265              $fault->appendChild( $faultStringNode );
 266  
 267              $body->appendChild( $fault );
 268          }
 269          else
 270          {
 271              // add the request
 272              $responseName = $this->Name . "Response";
 273              $response = $doc->createElementNode( $responseName );
 274              $response->setPrefix( "resp" );
 275              $response->appendAttribute( $doc->createAttributeNamespaceDefNode( "resp", $this->Namespace ) );
 276  
 277              $return = $doc->createElementNode( "return" );
 278              $return->setPrefix( "resp" );
 279  
 280              $value = eZSOAPCodec::encodeValue( "return", $this->Value );
 281  
 282              $body->appendChild( $response );
 283  
 284              $response->appendChild( $value );
 285          }
 286  
 287          $doc->setRoot( $root );
 288  
 289          return $doc->toString();
 290      }
 291  
 292      /*!
 293        \static
 294        \private
 295        Strips the header information from the HTTP raw response.
 296      */
 297      function stripHTTPHeader( $data )
 298      {
 299          $missingxml = false;
 300          $start = strpos( $data, "<?xml" );
 301          if ( $start == 0 )
 302          {
 303              eZDebug::writeWarning( "missing <?xml ...> in HTTP response, attempting workaround",
 304                                     "eZSoapResponse::stripHTTPHeader" );
 305              $start = strpos( $data, "<E:Envelope" );
 306              $missingxml = true;
 307          }
 308          $data = substr( $data, $start, strlen( $data ) - $start );
 309  
 310          if ( $missingxml == true )
 311          {
 312              $data = '<?xml version="1.0"?>' . $data;
 313          }
 314  
 315          return $data;
 316      }
 317  
 318      /*!
 319        Returns the response value.
 320      */
 321      function value()
 322      {
 323          return $this->Value;
 324      }
 325  
 326      /*!
 327       Sets the value of the response.
 328      */
 329      function setValue( $value )
 330      {
 331          $this->Value = $value;
 332      }
 333  
 334      /*!
 335       Returns true if the response was a fault
 336      */
 337      function isFault()
 338      {
 339          return $this->IsFault;
 340      }
 341  
 342      /*!
 343       Returns the fault code
 344      */
 345      function faultCode()
 346      {
 347          return $this->FaultCode;
 348      }
 349  
 350      /*!
 351       Returns the fault string
 352      */
 353      function faultString()
 354      {
 355          return $this->FaultString;
 356      }
 357  
 358      /// Contains the response value
 359      var $Value = false;
 360      /// Contains the response type
 361      var $Type = false;
 362      /// Contains fault string
 363      var $FaultString = false;
 364      /// Contains the fault code
 365      var $FaultCode = false;
 366      /// Contains true if the response was an fault
 367      var $IsFault = false;
 368      /// Contains the name of the response, i.e. function call name
 369      var $Name;
 370      /// Contains the target namespace for the response
 371      var $Namespace;
 372  
 373      /// Contains the DOM document for the current SOAP response
 374      var $DOMDocument = false;
 375  }
 376  
 377  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7