/framework/Rpc/lib/Horde/Rpc/Phpgw.php

https://github.com/ewandor/horde · PHP · 147 lines · 74 code · 11 blank · 62 comment · 11 complexity · 0caa679054a71f510abca88e4ca92623 MD5 · raw file

  1. <?php
  2. /**
  3. * The Horde_Rpc_Phpgw class provides an XMLRPC implementation of the
  4. * Horde RPC system compatible with phpgw. It is based on the
  5. * xmlrpc.php implementation by Jan Schneider.
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Michael Braun <mi.braun@onlinehome.de>
  11. * @category Horde
  12. * @package Rpc
  13. */
  14. class Horde_Rpc_Phpgw extends Horde_Rpc
  15. {
  16. /**
  17. * Resource handler for the XML-RPC server.
  18. *
  19. * @var resource
  20. */
  21. var $_server;
  22. /**
  23. * XMLRPC server constructor.
  24. */
  25. function __construct($request, $params = array())
  26. {
  27. parent::__construct($request, $params);
  28. $this->_server = xmlrpc_server_create();
  29. // Register only phpgw services.
  30. foreach ($GLOBALS['registry']->listMethods('phpgw') as $method) {
  31. $methods = explode('/', $method);
  32. array_shift($methods);
  33. $method = implode('.', $methods);
  34. xmlrpc_server_register_method($this->_server, $method, array('Horde_Rpc_Phpgw', '_dispatcher'));
  35. }
  36. }
  37. /**
  38. * Authorization is done by xmlrpc method system.login.
  39. */
  40. function authorize()
  41. {
  42. return true;
  43. }
  44. /**
  45. * Sends an RPC request to the server and returns the result.
  46. *
  47. * @param string The raw request string.
  48. *
  49. * @return string The XML encoded response from the server.
  50. */
  51. function getResponse($request)
  52. {
  53. $response = null;
  54. return xmlrpc_server_call_method($this->_server, $request, $response);
  55. }
  56. /**
  57. * Will be registered as the handler for all available methods
  58. * and will call the appropriate function through the registry.
  59. *
  60. * @access private
  61. *
  62. * @param string $method The name of the method called by the RPC request.
  63. * @param array $params The passed parameters.
  64. * @param mixed $data Unknown.
  65. *
  66. * @return mixed The result of the called registry method.
  67. */
  68. function _dispatcher($method, $params, $data)
  69. {
  70. global $registry;
  71. $method = str_replace('.', '/', 'phpgw.' . $method);
  72. if (!$registry->hasMethod($method)) {
  73. Horde::logMessage(sprintf(Horde_Rpc_Translation::t("Method \"%s\" is not defined"), $method), 'NOTICE');
  74. return sprintf(Horde_Rpc_Translation::t("Method \"%s\" is not defined"), $method);
  75. }
  76. // Try to resume a session
  77. if (isset($params[0]['kp3']) && $params[0]["kp3"] == session_name() && session_id() != $params[0]["sessionid"]) {
  78. Horde::logMessage("manually reload session ".$params[0]["sessionid"], 'NOTICE');
  79. session_regenerate_id();
  80. session_unset();
  81. session_id($params[0]["sessionid"]);
  82. }
  83. // Be authenticated or call system.login.
  84. $authenticated = $registry->isAuthenticated() || $method== "phpgw/system/login";
  85. if ($authenticated) {
  86. Horde::logMessage("rpc call $method allowed", 'NOTICE');
  87. return $registry->call($method, $params);
  88. } else {
  89. return PEAR::raiseError(Horde_Rpc_Translation::t("You did not authenticate."), 'horde.error');
  90. // return parent::authorize();
  91. // error 9 "access denied"
  92. }
  93. }
  94. /**
  95. * Builds an XMLRPC request and sends it to the XMLRPC server.
  96. *
  97. * This statically called method is actually the XMLRPC client.
  98. *
  99. * @param string|Horde_Url $url The path to the XMLRPC server on the
  100. * called host.
  101. * @param string $method The method to call.
  102. * @param Horde_Http_Client $client The transport client
  103. * @param array $params A hash containing any necessary
  104. * parameters for the method call.
  105. *
  106. * @return mixed The returned result from the method.
  107. * @throws Horde_Rpc_Exception
  108. */
  109. public static function request($url, $method, $client, $params = null)
  110. {
  111. $options['method'] = 'POST';
  112. $headers = array(
  113. 'User-Agent' => 'Horde RPC client',
  114. 'Content-Type', 'text/xml');
  115. try {
  116. $result = $client->post($url, xmlrpc_encode_request($method, $params), $headers);
  117. } catch (Horde_Http_Client_Exception $e) {
  118. throw new Horde_Rpc_Exception($result);
  119. }
  120. if ($result->code != 200) {
  121. throw new Horde_Rpc_Exception(Horde_Rpc_Translation::t("Request couldn't be answered. Returned errorcode: ") . $result->code);
  122. } elseif (strpos($result->getBody(), '<?xml') === false) {
  123. throw new Horde_Rpc_Exception(Horde_Rpc_Translation::t("No valid XML data returned:") . "\n" . $result->getBody());
  124. } else {
  125. $response = @xmlrpc_decode(substr($result->getBody(), strpos($result->getBody(), '<?xml')));
  126. if (is_array($response) && isset($response['faultString'])) {
  127. throw new Horde_Rpc_Exception($response['faultString']);
  128. } elseif (is_array($response) && isset($response[0]) &&
  129. is_array($response[0]) && isset($response[0]['faultString'])) {
  130. throw new Horde_Rpc_Exception($response[0]['faultString']);
  131. }
  132. return $response;
  133. }
  134. }
  135. }