PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezsoap/classes/ezsoapserver.php

https://github.com/aurelienRT1/ezpublish
PHP | 252 lines | 129 code | 26 blank | 97 comment | 14 complexity | d2e6e703674625a6dc20c5fd53fb22e7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //
  3. // Definition of eZSOAPServer class
  4. //
  5. // Created on: <14-May-2002 10:45:38 bf>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*!
  31. \class eZSOAPServer ezsoapserver.php
  32. \ingroup eZSOAP
  33. \brief The class eZSOAPServer handles SOAP server requensts
  34. Sample code for a SOAP server with one function, addNumbers.
  35. \code
  36. //include_once( "lib/ezsoap/classes/ezsoapserver.php" );
  37. $server = new eZSOAPServer( );
  38. $server->registerFunction( "addNumbers", array( "valueA" => "integer", "valueB" => "integer" ) );
  39. $server->registerObject( "Collection" );
  40. $server->processRequest();
  41. function addNumbers( $valueA, $valueB )
  42. {
  43. $return = $valueA + $valueB;
  44. settype( $return, "integer" );
  45. return $return;
  46. }
  47. class Collection
  48. {
  49. function Collection ()
  50. {
  51. }
  52. function subNumbers( $valueA, $valueB )
  53. {
  54. $return = $valueA - $valueB;
  55. settype( $return, "integer" );
  56. return $return;
  57. }
  58. }
  59. \endcode
  60. \sa eZSOAPClient eZSOAPRequest eZSOAPResponse
  61. */
  62. class eZSOAPServer
  63. {
  64. /*!
  65. Creates a new eZSOAPServer object.
  66. */
  67. function eZSOAPServer()
  68. {
  69. global $HTTP_RAW_POST_DATA;
  70. $this->RawPostData = $HTTP_RAW_POST_DATA;
  71. }
  72. function showResponse( $functionName, $namespaceURI, $value )
  73. {
  74. // Convert input data to XML
  75. $response = new eZSOAPResponse( $functionName, $namespaceURI );
  76. $response->setValue( $value );
  77. $payload = $response->payload();
  78. header( "SOAPServer: eZ soap" );
  79. header( "Content-Type: text/xml; charset=\"UTF-8\"" );
  80. Header( "Content-Length: " . strlen( $payload ) );
  81. if ( ob_get_length() )
  82. ob_end_clean();
  83. print( $payload );
  84. }
  85. /*!
  86. Registers all functions of an object on the server.
  87. Returns false if the object could not be registered.
  88. */
  89. function registerObject( $objectName, $includeFile = null )
  90. {
  91. if ( file_exists( $includeFile ) )
  92. include_once( $includeFile );
  93. if ( class_exists( $objectName ) )
  94. {
  95. $methods = get_class_methods( $objectName );
  96. foreach ( $methods as $method)
  97. {
  98. if ( strcasecmp ( $objectName, $method ) )
  99. $this->registerFunction( $objectName."::".$method );
  100. }
  101. return true;
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. /*!
  109. Processes the SOAP request and prints out the
  110. propper response.
  111. */
  112. function processRequest()
  113. {
  114. global $HTTP_SERVER_VARS;
  115. if ( $_SERVER["REQUEST_METHOD"] != "POST" )
  116. {
  117. print( "Error: this web page does only understand POST methods" );
  118. exit();
  119. }
  120. $xmlData = $this->stripHTTPHeader( $this->RawPostData );
  121. $dom = new DOMDocument( '1.0', 'utf-8' );
  122. $dom->preserveWhiteSpace = false;
  123. $success = $dom->loadXML( $xmlData );
  124. // Check for non-parsing XML, to avoid call to non-object error.
  125. if ( !$success )
  126. {
  127. $this->showResponse( 'unknown_function_name', 'unknown_namespace_uri',
  128. new eZSOAPFault( 'Server Error',
  129. 'Bad XML' ) );
  130. return;
  131. }
  132. // add namespace fetching on body
  133. // get the SOAP body
  134. $body = $dom->getElementsByTagName( "Body" );
  135. $children = $body->item( 0 )->childNodes;
  136. if ( $children->length == 1 )
  137. {
  138. $requestNode = $children->item( 0 );
  139. // get target namespace for request
  140. $functionName = $requestNode->localName;
  141. $namespaceURI = $requestNode->namespaceURI;
  142. $params = array();
  143. // check parameters
  144. foreach ( $requestNode->childNodes as $parameterNode )
  145. {
  146. $params[] = eZSOAPResponse::decodeDataTypes( $parameterNode );
  147. }
  148. list( $objectName, $objectFunctionName ) = preg_split('/::/', $functionName, 2, PREG_SPLIT_NO_EMPTY);
  149. if ( !$objectFunctionName and in_array( $functionName, $this->FunctionList ) &&
  150. function_exists( $functionName ) )
  151. {
  152. $this->showResponse( $functionName, $namespaceURI,
  153. call_user_func_array( $functionName, $params ) );
  154. }
  155. else if ( $objectName and $objectFunctionName )
  156. {
  157. if ( !class_exists( $objectName ) )
  158. {
  159. $this->showResponse( $functionName, $namespaceURI,
  160. new eZSOAPFault( 'Server Error',
  161. 'Object not found' ) );
  162. }
  163. else
  164. {
  165. $object = new $objectName();
  166. if ( !method_exists( $object, $objectFunctionName ) )
  167. {
  168. $this->showResponse( $functionName, $namespaceURI,
  169. new eZSOAPFault( 'Server Error',
  170. 'Objectmethod not found' ) );
  171. }
  172. else
  173. {
  174. $this->showResponse( $functionName, $namespaceURI,
  175. call_user_func_array( array( $object, $objectFunctionName ), $params ) );
  176. }
  177. }
  178. }
  179. else
  180. {
  181. $this->showResponse( $functionName, $namespaceURI,
  182. new eZSOAPFault( 'Server Error',
  183. 'Method not found' ) );
  184. }
  185. }
  186. else
  187. {
  188. // error
  189. $this->showResponse( $functionName, $namespaceURI,
  190. new eZSOAPFault( 'Server Error',
  191. '"Body" element in the request '.
  192. 'has wrong number of children' ) );
  193. }
  194. }
  195. /*!
  196. Registers a new function on the server.
  197. Returns false if the function could not be registered.
  198. */
  199. function registerFunction( $name, $params=array() )
  200. {
  201. $this->FunctionList[] = $name;
  202. }
  203. /*!
  204. \static
  205. \private
  206. Strips the header information from the HTTP raw response.
  207. */
  208. function stripHTTPHeader( $data )
  209. {
  210. $start = strpos( $data, "<?xml version=\"1.0\"?>" );
  211. return substr( $data, $start, strlen( $data ) - $start );
  212. }
  213. /// Contains a list over registered functions
  214. public $FunctionList;
  215. /// Contains the RAW HTTP post data information
  216. public $RawPostData;
  217. }
  218. ?>