PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezsoap/classes/ezsoapserver.php

https://github.com/StephanBoganskyXrow/ezpublish
PHP | 251 lines | 129 code | 26 blank | 96 comment | 14 complexity | dd1764066b0ea935d04ad8e9f54acf9f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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-2011 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. $server = new eZSOAPServer( );
  37. $server->registerFunction( "addNumbers", array( "valueA" => "integer", "valueB" => "integer" ) );
  38. $server->registerObject( "Collection" );
  39. $server->processRequest();
  40. function addNumbers( $valueA, $valueB )
  41. {
  42. $return = $valueA + $valueB;
  43. settype( $return, "integer" );
  44. return $return;
  45. }
  46. class Collection
  47. {
  48. function Collection ()
  49. {
  50. }
  51. function subNumbers( $valueA, $valueB )
  52. {
  53. $return = $valueA - $valueB;
  54. settype( $return, "integer" );
  55. return $return;
  56. }
  57. }
  58. \endcode
  59. \sa eZSOAPClient eZSOAPRequest eZSOAPResponse
  60. */
  61. class eZSOAPServer
  62. {
  63. /*!
  64. Creates a new eZSOAPServer object.
  65. */
  66. function eZSOAPServer()
  67. {
  68. global $HTTP_RAW_POST_DATA;
  69. $this->RawPostData = $HTTP_RAW_POST_DATA;
  70. }
  71. function showResponse( $functionName, $namespaceURI, $value )
  72. {
  73. // Convert input data to XML
  74. $response = new eZSOAPResponse( $functionName, $namespaceURI );
  75. $response->setValue( $value );
  76. $payload = $response->payload();
  77. header( "SOAPServer: eZ soap" );
  78. header( "Content-Type: text/xml; charset=\"UTF-8\"" );
  79. Header( "Content-Length: " . strlen( $payload ) );
  80. if ( ob_get_length() )
  81. ob_end_clean();
  82. print( $payload );
  83. }
  84. /*!
  85. Registers all functions of an object on the server.
  86. Returns false if the object could not be registered.
  87. */
  88. function registerObject( $objectName, $includeFile = null )
  89. {
  90. if ( file_exists( $includeFile ) )
  91. include_once( $includeFile );
  92. if ( class_exists( $objectName ) )
  93. {
  94. $methods = get_class_methods( $objectName );
  95. foreach ( $methods as $method)
  96. {
  97. if ( strcasecmp ( $objectName, $method ) )
  98. $this->registerFunction( $objectName."::".$method );
  99. }
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. /*!
  108. Processes the SOAP request and prints out the
  109. propper response.
  110. */
  111. function processRequest()
  112. {
  113. global $HTTP_SERVER_VARS;
  114. if ( $_SERVER["REQUEST_METHOD"] != "POST" )
  115. {
  116. print( "Error: this web page does only understand POST methods" );
  117. exit();
  118. }
  119. $xmlData = $this->stripHTTPHeader( $this->RawPostData );
  120. $dom = new DOMDocument( '1.0', 'utf-8' );
  121. $dom->preserveWhiteSpace = false;
  122. $success = $dom->loadXML( $xmlData );
  123. // Check for non-parsing XML, to avoid call to non-object error.
  124. if ( !$success )
  125. {
  126. $this->showResponse( 'unknown_function_name', 'unknown_namespace_uri',
  127. new eZSOAPFault( 'Server Error',
  128. 'Bad XML' ) );
  129. return;
  130. }
  131. // add namespace fetching on body
  132. // get the SOAP body
  133. $body = $dom->getElementsByTagName( "Body" );
  134. $children = $body->item( 0 )->childNodes;
  135. if ( $children->length == 1 )
  136. {
  137. $requestNode = $children->item( 0 );
  138. // get target namespace for request
  139. $functionName = $requestNode->localName;
  140. $namespaceURI = $requestNode->namespaceURI;
  141. $params = array();
  142. // check parameters
  143. foreach ( $requestNode->childNodes as $parameterNode )
  144. {
  145. $params[] = eZSOAPResponse::decodeDataTypes( $parameterNode );
  146. }
  147. list( $objectName, $objectFunctionName ) = preg_split('/::/', $functionName, 2, PREG_SPLIT_NO_EMPTY);
  148. if ( !$objectFunctionName and in_array( $functionName, $this->FunctionList ) &&
  149. function_exists( $functionName ) )
  150. {
  151. $this->showResponse( $functionName, $namespaceURI,
  152. call_user_func_array( $functionName, $params ) );
  153. }
  154. else if ( $objectName and $objectFunctionName )
  155. {
  156. if ( !class_exists( $objectName ) )
  157. {
  158. $this->showResponse( $functionName, $namespaceURI,
  159. new eZSOAPFault( 'Server Error',
  160. 'Object not found' ) );
  161. }
  162. else
  163. {
  164. $object = new $objectName();
  165. if ( !method_exists( $object, $objectFunctionName ) )
  166. {
  167. $this->showResponse( $functionName, $namespaceURI,
  168. new eZSOAPFault( 'Server Error',
  169. 'Objectmethod not found' ) );
  170. }
  171. else
  172. {
  173. $this->showResponse( $functionName, $namespaceURI,
  174. call_user_func_array( array( $object, $objectFunctionName ), $params ) );
  175. }
  176. }
  177. }
  178. else
  179. {
  180. $this->showResponse( $functionName, $namespaceURI,
  181. new eZSOAPFault( 'Server Error',
  182. 'Method not found' ) );
  183. }
  184. }
  185. else
  186. {
  187. // error
  188. $this->showResponse( $functionName, $namespaceURI,
  189. new eZSOAPFault( 'Server Error',
  190. '"Body" element in the request '.
  191. 'has wrong number of children' ) );
  192. }
  193. }
  194. /*!
  195. Registers a new function on the server.
  196. Returns false if the function could not be registered.
  197. */
  198. function registerFunction( $name, $params=array() )
  199. {
  200. $this->FunctionList[] = $name;
  201. }
  202. /*!
  203. \static
  204. \private
  205. Strips the header information from the HTTP raw response.
  206. */
  207. function stripHTTPHeader( $data )
  208. {
  209. $start = strpos( $data, "<?xml version=\"1.0\"?>" );
  210. return substr( $data, $start, strlen( $data ) - $start );
  211. }
  212. /// Contains a list over registered functions
  213. public $FunctionList;
  214. /// Contains the RAW HTTP post data information
  215. public $RawPostData;
  216. }
  217. ?>