eZ Publish  [4.0]
ezsoapserver.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSOAPServer class
00004 //
00005 // Created on: <14-May-2002 10:45:38 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZSOAPServer ezsoapserver.php
00033   \ingroup eZSOAP
00034   \brief The class eZSOAPServer handles SOAP server requensts
00035 
00036   Sample code for a SOAP server with one function, addNumbers.
00037   \code
00038 //include_once( "lib/ezsoap/classes/ezsoapserver.php" );
00039 
00040 $server = new eZSOAPServer( );
00041 $server->registerFunction( "addNumbers", array( "valueA" => "integer", "valueB" => "integer" ) );
00042 $server->registerObject( "Collection" );
00043 $server->processRequest();
00044 
00045 function addNumbers( $valueA, $valueB )
00046 {
00047     $return = $valueA + $valueB;
00048     settype( $return, "integer" );
00049     return $return;
00050 }
00051 
00052 class Collection
00053 {
00054     function Collection ()
00055     {
00056 
00057     }
00058     function subNumbers( $valueA, $valueB )
00059     {
00060         $return = $valueA - $valueB;
00061         settype( $return, "integer" );
00062         return $return;
00063     }
00064 }
00065   \endcode
00066   \sa eZSOAPClient eZSOAPRequest eZSOAPResponse
00067 
00068 */
00069 
00070 //include_once( "lib/ezsoap/classes/ezsoaprequest.php" );
00071 //include_once( "lib/ezsoap/classes/ezsoapfault.php" );
00072 //include_once( "lib/ezsoap/classes/ezsoapresponse.php" );
00073 
00074 class eZSOAPServer
00075 {
00076     /*!
00077       Creates a new eZSOAPServer object.
00078     */
00079     function eZSOAPServer()
00080     {
00081         global $HTTP_RAW_POST_DATA;
00082         $this->RawPostData = $HTTP_RAW_POST_DATA;
00083     }
00084 
00085 
00086     function showResponse( $functionName, $namespaceURI, $value )
00087     {
00088         // Convert input data to XML
00089         $response = new eZSOAPResponse( $functionName, $namespaceURI );
00090         $response->setValue( $value );
00091 
00092         $payload = $response->payload();
00093 
00094         header( "SOAPServer: eZ soap" );
00095         header( "Content-Type: text/xml; charset=\"UTF-8\"" );
00096         Header( "Content-Length: " . strlen( $payload ) );
00097 
00098         if ( ob_get_length() )
00099             ob_end_clean();
00100 
00101         print( $payload );
00102     }
00103 
00104     /*!
00105       Registers all functions of an object on the server.
00106 
00107       Returns false if the object could not be registered.
00108     */
00109     function registerObject( $objectName, $includeFile = null )
00110     {
00111         if ( file_exists( $includeFile ) )
00112             include_once( $includeFile );
00113 
00114         if ( class_exists( $objectName ) )
00115         {
00116             $methods = get_class_methods( $objectName );
00117             foreach ( $methods as $method)
00118             {
00119                 if ( strcasecmp ( $objectName, $method ) )
00120                     $this->registerFunction( $objectName."::".$method );
00121             }
00122             return true;
00123         }
00124         else
00125         {
00126             return false;
00127         }
00128     }
00129 
00130     /*!
00131       Processes the SOAP request and prints out the
00132       propper response.
00133     */
00134     function processRequest()
00135     {
00136         global $HTTP_SERVER_VARS;
00137 
00138         if ( $_SERVER["REQUEST_METHOD"] != "POST" )
00139         {
00140             print( "Error: this web page does only understand POST methods" );
00141             exit();
00142         }
00143 
00144         $xmlData = $this->stripHTTPHeader( $this->RawPostData );
00145 
00146         $dom = new DOMDocument( '1.0', 'utf-8' );
00147         $dom->preserveWhiteSpace = false;
00148         $success = $dom->loadXML( $xmlData );
00149 
00150         // Check for non-parsing XML, to avoid call to non-object error.
00151         if ( !$success )
00152         {
00153             $this->showResponse( 'unknown_function_name', 'unknown_namespace_uri',
00154                                  new eZSOAPFault( 'Server Error',
00155                                                   'Bad XML' ) );
00156             return;
00157         }
00158 
00159         // add namespace fetching on body
00160         // get the SOAP body
00161         $body = $dom->getElementsByTagName( "Body" );
00162 
00163         $children = $body->item( 0 )->childNodes;
00164 
00165         if ( $children->length == 1 )
00166         {
00167             $requestNode = $children->item( 0 );
00168             // get target namespace for request
00169             $functionName = $requestNode->localName;
00170             $namespaceURI = $requestNode->namespaceURI;
00171 
00172             $params = array();
00173             // check parameters
00174             foreach ( $requestNode->childNodes as $parameterNode )
00175             {
00176                 $params[] = eZSOAPResponse::decodeDataTypes( $parameterNode );
00177             }
00178 
00179             list( $objectName, $objectFunctionName ) = preg_split('/::/', $functionName, 2, PREG_SPLIT_NO_EMPTY);
00180             if ( !$objectFunctionName and in_array( $functionName, $this->FunctionList ) &&
00181                  function_exists( $functionName ) )
00182             {
00183                 $this->showResponse( $functionName, $namespaceURI,
00184                                      call_user_func_array( $functionName, $params ) );
00185             }
00186             else if ( $objectName and $objectFunctionName )
00187             {
00188                 if ( !class_exists( $objectName ) )
00189                 {
00190                     $this->showResponse( $functionName, $namespaceURI,
00191                                          new eZSOAPFault( 'Server Error',
00192                                                           'Object not found' ) );
00193                 }
00194                 else
00195                 {
00196                     $object = new $objectName();
00197                     if ( !method_exists( $object, $objectFunctionName ) )
00198                     {
00199                         $this->showResponse( $functionName, $namespaceURI,
00200                                              new eZSOAPFault( 'Server Error',
00201                                                               'Objectmethod not found' ) );
00202                     }
00203                     else
00204                     {
00205                         $this->showResponse( $functionName, $namespaceURI,
00206                                              call_user_func_array( array( $object, $objectFunctionName ), $params ) );
00207                     }
00208                 }
00209             }
00210             else
00211             {
00212                 $this->showResponse( $functionName, $namespaceURI,
00213                                      new eZSOAPFault( 'Server Error',
00214                                                       'Method not found' ) );
00215             }
00216         }
00217         else
00218         {
00219             // error
00220             $this->showResponse( $functionName, $namespaceURI,
00221                                  new eZSOAPFault( 'Server Error',
00222                                                   '"Body" element in the request '.
00223                                                   'has wrong number of children' ) );
00224 
00225         }
00226     }
00227 
00228     /*!
00229       Registers a new function on the server.
00230 
00231       Returns false if the function could not be registered.
00232     */
00233     function registerFunction( $name, $params=array() )
00234     {
00235         $this->FunctionList[] = $name;
00236     }
00237 
00238 
00239     /*!
00240       \static
00241       \private
00242       Strips the header information from the HTTP raw response.
00243     */
00244     function stripHTTPHeader( $data )
00245     {
00246         $start = strpos( $data, "<?xml version=\"1.0\"?>" );
00247         return substr( $data, $start, strlen( $data ) - $start );
00248     }
00249 
00250     /// Contains a list over registered functions
00251     public $FunctionList;
00252     /// Contains the RAW HTTP post data information
00253     public $RawPostData;
00254 }
00255 
00256 ?>