/lib/ezsoap/classes/ezsoapserver.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 231 lines · 129 code · 26 blank · 76 comment · 14 complexity · 2683f11ef07aad449a79c90ebdf880a4 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZSOAPServer class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package lib
  9. */
  10. /*!
  11. \class eZSOAPServer ezsoapserver.php
  12. \ingroup eZSOAP
  13. \brief The class eZSOAPServer handles SOAP server requensts
  14. Sample code for a SOAP server with one function, addNumbers.
  15. \code
  16. $server = new eZSOAPServer( );
  17. $server->registerFunction( "addNumbers", array( "valueA" => "integer", "valueB" => "integer" ) );
  18. $server->registerObject( "Collection" );
  19. $server->processRequest();
  20. function addNumbers( $valueA, $valueB )
  21. {
  22. $return = $valueA + $valueB;
  23. settype( $return, "integer" );
  24. return $return;
  25. }
  26. class Collection
  27. {
  28. function Collection ()
  29. {
  30. }
  31. function subNumbers( $valueA, $valueB )
  32. {
  33. $return = $valueA - $valueB;
  34. settype( $return, "integer" );
  35. return $return;
  36. }
  37. }
  38. \endcode
  39. \sa eZSOAPClient eZSOAPRequest eZSOAPResponse
  40. */
  41. class eZSOAPServer
  42. {
  43. /*!
  44. Creates a new eZSOAPServer object.
  45. */
  46. function eZSOAPServer()
  47. {
  48. global $HTTP_RAW_POST_DATA;
  49. $this->RawPostData = $HTTP_RAW_POST_DATA;
  50. }
  51. function showResponse( $functionName, $namespaceURI, $value )
  52. {
  53. // Convert input data to XML
  54. $response = new eZSOAPResponse( $functionName, $namespaceURI );
  55. $response->setValue( $value );
  56. $payload = $response->payload();
  57. header( "SOAPServer: eZ soap" );
  58. header( "Content-Type: text/xml; charset=\"UTF-8\"" );
  59. Header( "Content-Length: " . strlen( $payload ) );
  60. if ( ob_get_length() )
  61. ob_end_clean();
  62. print( $payload );
  63. }
  64. /*!
  65. Registers all functions of an object on the server.
  66. Returns false if the object could not be registered.
  67. */
  68. function registerObject( $objectName, $includeFile = null )
  69. {
  70. if ( file_exists( $includeFile ) )
  71. include_once( $includeFile );
  72. if ( class_exists( $objectName ) )
  73. {
  74. $methods = get_class_methods( $objectName );
  75. foreach ( $methods as $method)
  76. {
  77. if ( strcasecmp ( $objectName, $method ) )
  78. $this->registerFunction( $objectName."::".$method );
  79. }
  80. return true;
  81. }
  82. else
  83. {
  84. return false;
  85. }
  86. }
  87. /*!
  88. Processes the SOAP request and prints out the
  89. propper response.
  90. */
  91. function processRequest()
  92. {
  93. global $HTTP_SERVER_VARS;
  94. if ( $_SERVER["REQUEST_METHOD"] != "POST" )
  95. {
  96. print( "Error: this web page does only understand POST methods" );
  97. exit();
  98. }
  99. $xmlData = $this->stripHTTPHeader( $this->RawPostData );
  100. $dom = new DOMDocument( '1.0', 'utf-8' );
  101. $dom->preserveWhiteSpace = false;
  102. $success = $dom->loadXML( $xmlData );
  103. // Check for non-parsing XML, to avoid call to non-object error.
  104. if ( !$success )
  105. {
  106. $this->showResponse( 'unknown_function_name', 'unknown_namespace_uri',
  107. new eZSOAPFault( 'Server Error',
  108. 'Bad XML' ) );
  109. return;
  110. }
  111. // add namespace fetching on body
  112. // get the SOAP body
  113. $body = $dom->getElementsByTagName( "Body" );
  114. $children = $body->item( 0 )->childNodes;
  115. if ( $children->length == 1 )
  116. {
  117. $requestNode = $children->item( 0 );
  118. // get target namespace for request
  119. $functionName = $requestNode->localName;
  120. $namespaceURI = $requestNode->namespaceURI;
  121. $params = array();
  122. // check parameters
  123. foreach ( $requestNode->childNodes as $parameterNode )
  124. {
  125. $params[] = eZSOAPResponse::decodeDataTypes( $parameterNode );
  126. }
  127. list( $objectName, $objectFunctionName ) = preg_split('/::/', $functionName, 2, PREG_SPLIT_NO_EMPTY);
  128. if ( !$objectFunctionName and in_array( $functionName, $this->FunctionList ) &&
  129. function_exists( $functionName ) )
  130. {
  131. $this->showResponse( $functionName, $namespaceURI,
  132. call_user_func_array( $functionName, $params ) );
  133. }
  134. else if ( $objectName and $objectFunctionName )
  135. {
  136. if ( !class_exists( $objectName ) )
  137. {
  138. $this->showResponse( $functionName, $namespaceURI,
  139. new eZSOAPFault( 'Server Error',
  140. 'Object not found' ) );
  141. }
  142. else
  143. {
  144. $object = new $objectName();
  145. if ( !method_exists( $object, $objectFunctionName ) )
  146. {
  147. $this->showResponse( $functionName, $namespaceURI,
  148. new eZSOAPFault( 'Server Error',
  149. 'Objectmethod not found' ) );
  150. }
  151. else
  152. {
  153. $this->showResponse( $functionName, $namespaceURI,
  154. call_user_func_array( array( $object, $objectFunctionName ), $params ) );
  155. }
  156. }
  157. }
  158. else
  159. {
  160. $this->showResponse( $functionName, $namespaceURI,
  161. new eZSOAPFault( 'Server Error',
  162. 'Method not found' ) );
  163. }
  164. }
  165. else
  166. {
  167. // error
  168. $this->showResponse( $functionName, $namespaceURI,
  169. new eZSOAPFault( 'Server Error',
  170. '"Body" element in the request '.
  171. 'has wrong number of children' ) );
  172. }
  173. }
  174. /*!
  175. Registers a new function on the server.
  176. Returns false if the function could not be registered.
  177. */
  178. function registerFunction( $name, $params=array() )
  179. {
  180. $this->FunctionList[] = $name;
  181. }
  182. /*!
  183. \static
  184. \private
  185. Strips the header information from the HTTP raw response.
  186. */
  187. function stripHTTPHeader( $data )
  188. {
  189. $start = strpos( $data, "<?xml version=\"1.0\"?>" );
  190. return substr( $data, $start, strlen( $data ) - $start );
  191. }
  192. /// Contains a list over registered functions
  193. public $FunctionList;
  194. /// Contains the RAW HTTP post data information
  195. public $RawPostData;
  196. }
  197. ?>