PageRenderTime 62ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/horde-3.3.13/lib/Horde/RPC/PhpSoap.php

#
PHP | 213 lines | 87 code | 21 blank | 105 comment | 10 complexity | 0099e3f564a921619bcc5db7a8b247da MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /**
  3. * The Horde_RPC_PhpSoap class provides a PHP 5 Soap implementation
  4. * of the Horde RPC system.
  5. *
  6. * $Horde: framework/RPC/RPC/PhpSoap.php,v 1.1.2.6 2009/11/06 14:35:37 jan Exp $
  7. *
  8. * Copyright 2003-2009 The Horde Project (http://www.horde.org/)
  9. *
  10. * See the enclosed file COPYING for license information (LGPL). If you
  11. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12. *
  13. * @author Chuck Hagenbuch <chuck@horde.org>
  14. * @since Horde 3.2
  15. * @package Horde_RPC
  16. */
  17. class Horde_RPC_PhpSoap extends Horde_RPC {
  18. /**
  19. * Resource handler for the RPC server.
  20. *
  21. * @var object
  22. */
  23. var $_server;
  24. /**
  25. * List of types to emit in the WSDL.
  26. *
  27. * @var array
  28. */
  29. var $_allowedTypes = array();
  30. /**
  31. * List of method names to allow.
  32. *
  33. * @var array
  34. */
  35. var $_allowedMethods = array();
  36. /**
  37. * Name of the SOAP service to use in the WSDL.
  38. *
  39. * @var string
  40. */
  41. var $_serviceName = null;
  42. /**
  43. * SOAP server constructor
  44. *
  45. * @access private
  46. */
  47. public function __construct($params = array())
  48. {
  49. NLS::setCharset('UTF-8');
  50. parent::Horde_RPC($params);
  51. if (!empty($params['allowedTypes'])) {
  52. $this->_allowedTypes = $params['allowedTypes'];
  53. }
  54. if (!empty($params['allowedMethods'])) {
  55. $this->_allowedMethods = $params['allowedMethods'];
  56. }
  57. if (!empty($params['serviceName'])) {
  58. $this->_serviceName = $params['serviceName'];
  59. }
  60. $this->_server = new SoapServer(Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/rpc.php?wsdl', true, -1));
  61. $this->_server->addFunction(SOAP_FUNCTIONS_ALL);
  62. $this->_server->setClass('Horde_RPC_PhpSoap_Caller', $params);
  63. }
  64. /**
  65. * Takes an RPC request and returns the result.
  66. *
  67. * @param string The raw request string.
  68. *
  69. * @return string The XML encoded response from the server.
  70. */
  71. function getResponse($request)
  72. {
  73. /* We can't use Util::bufferOutput() here for some reason. */
  74. $beginTime = time();
  75. ob_start();
  76. $this->_server->handle($request);
  77. Horde::logMessage(
  78. sprintf('SOAP call: %s(%s) by %s serviced in %d seconds, sent %d bytes in response',
  79. $GLOBALS['__horde_rpc_PhpSoap']['lastMethodCalled'],
  80. implode(', ', array_map(create_function('$a', 'return is_array($a) ? "Array" : $a;'),
  81. $GLOBALS['__horde_rpc_PhpSoap']['lastMethodParams'])),
  82. Auth::getAuth(),
  83. time() - $beginTime,
  84. ob_get_length()),
  85. __FILE__, __LINE__, PEAR_LOG_INFO
  86. );
  87. return ob_get_clean();
  88. }
  89. /**
  90. * Builds a SOAP request and sends it to the SOAP server.
  91. *
  92. * This statically called method is actually the SOAP client.
  93. *
  94. * @param string $url The path to the SOAP server on the called host.
  95. * @param string $method The method to call.
  96. * @param array $params A hash containing any necessary parameters for
  97. * the method call.
  98. * @param $options Optional associative array of parameters which can be:
  99. * user - Basic Auth username
  100. * pass - Basic Auth password
  101. * proxy_host - Proxy server host
  102. * proxy_port - Proxy server port
  103. * proxy_user - Proxy auth username
  104. * proxy_pass - Proxy auth password
  105. * timeout - Connection timeout in seconds.
  106. * allowRedirects - Whether to follow redirects or not
  107. * maxRedirects - Max number of redirects to follow
  108. * namespace
  109. * soapaction
  110. * from - SMTP, from address
  111. * transfer-encoding - SMTP, sets the
  112. * Content-Transfer-Encoding header
  113. * subject - SMTP, subject header
  114. * headers - SMTP, array-hash of extra smtp
  115. * headers
  116. *
  117. * @return mixed The returned result from the method or a PEAR
  118. * error object on failure.
  119. */
  120. public function request($url, $method, $params = null, $options = array())
  121. {
  122. if (!isset($options['timeout'])) {
  123. $options['timeout'] = 5;
  124. }
  125. if (!isset($options['allowRedirects'])) {
  126. $options['allowRedirects'] = true;
  127. $options['maxRedirects'] = 3;
  128. }
  129. if (isset($options['user'])) {
  130. $options['login'] = $options['user'];
  131. unset($options['user']);
  132. }
  133. if (isset($options['pass'])) {
  134. $options['password'] = $options['pass'];
  135. unset($options['pass']);
  136. }
  137. $options['location'] = $url;
  138. $options['uri'] = $options['namespace'];
  139. $soap = new SoapClient(null, $options);
  140. return $soap->__soapCall($method, $params);
  141. }
  142. }
  143. class Horde_RPC_PhpSoap_Caller {
  144. /**
  145. * List of method names to allow.
  146. *
  147. * @var array
  148. */
  149. protected $_allowedMethods = array();
  150. /**
  151. */
  152. public function __construct($params = array())
  153. {
  154. if (!empty($params['allowedMethods'])) {
  155. $this->_allowedMethods = $params['allowedMethods'];
  156. }
  157. }
  158. /**
  159. * Will be registered as the handler for all methods called in the
  160. * SOAP server and will call the appropriate function through the registry.
  161. *
  162. * @todo PEAR SOAP operates on a copy of this object at some unknown
  163. * point and therefore doesn't have access to instance
  164. * variables if they're set here. Instead, globals are used
  165. * to track the method name and args for the logging code.
  166. * Once this is PHP 5-only, the globals can go in favor of
  167. * instance variables.
  168. *
  169. * @access private
  170. *
  171. * @param string $method The name of the method called by the RPC request.
  172. * @param array $params The passed parameters.
  173. * @param mixed $data Unknown.
  174. *
  175. * @return mixed The result of the called registry method.
  176. */
  177. public function __call($method, $params)
  178. {
  179. $method = str_replace('.', '/', $method);
  180. if (!empty($this->_params['allowedMethods']) &&
  181. !in_array($method, $this->_params['allowedMethods'])) {
  182. return sprintf(_("Method \"%s\" is not defined"), $method);
  183. }
  184. $GLOBALS['__horde_rpc_PhpSoap']['lastMethodCalled'] = $method;
  185. $GLOBALS['__horde_rpc_PhpSoap']['lastMethodParams'] =
  186. !empty($params) ? $params : array();
  187. if (!$GLOBALS['registry']->hasMethod($method)) {
  188. return sprintf(_("Method \"%s\" is not defined"), $method);
  189. }
  190. return $GLOBALS['registry']->call($method, $params);
  191. }
  192. }