PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/xampp/php/PEAR/XML/RPC2/Client.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 297 lines | 87 code | 29 blank | 181 comment | 12 complexity | 25e168ae4488051df53545b07527301f MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
  4. /**
  5. * +-----------------------------------------------------------------------------+
  6. * | Copyright (c) 2004-2006 Sergio Goncalves Carvalho |
  7. * +-----------------------------------------------------------------------------+
  8. * | This file is part of XML_RPC2. |
  9. * | |
  10. * | XML_RPC2 is free software; you can redistribute it and/or modify |
  11. * | it under the terms of the GNU Lesser General Public License as published by |
  12. * | the Free Software Foundation; either version 2.1 of the License, or |
  13. * | (at your option) any later version. |
  14. * | |
  15. * | XML_RPC2 is distributed in the hope that it will be useful, |
  16. * | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. * | GNU Lesser General Public License for more details. |
  19. * | |
  20. * | You should have received a copy of the GNU Lesser General Public License |
  21. * | along with XML_RPC2; if not, write to the Free Software |
  22. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
  23. * | 02111-1307 USA |
  24. * +-----------------------------------------------------------------------------+
  25. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com> |
  26. * +-----------------------------------------------------------------------------+
  27. *
  28. * @category XML
  29. * @package XML_RPC2
  30. * @author Sergio Carvalho <sergio.carvalho@portugalmail.com>
  31. * @copyright 2004-2006 Sergio Carvalho
  32. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  33. * @version CVS: $Id: Client.php,v 1.14 2008/09/10 18:50:31 sergiosgc Exp $
  34. * @link http://pear.php.net/package/XML_RPC2
  35. */
  36. // }}}
  37. // dependencies {{{
  38. require_once 'XML/RPC2/Exception.php';
  39. require_once 'XML/RPC2/Backend.php';
  40. // }}}
  41. /**
  42. * XML_RPC client class. Use this class to access remote methods.
  43. *
  44. * To use this class, construct it providing the server URL and method prefix.
  45. * Then, call remote methods on the new instance as if they were local.
  46. *
  47. * Example:
  48. * <code>
  49. * require_once 'XML_RPC2/Client.php';
  50. *
  51. * $client = XML_RPC2_Client('http://xmlrpc.example.com/1.0/', 'example.');
  52. * $result = $client->hello('Sergio');
  53. * print($result);
  54. * </code>
  55. *
  56. * The above example will call the example.hello method on the xmlrpc.example.com
  57. * server, under the /1.0/ URI.
  58. *
  59. * @category XML
  60. * @package XML_RPC2
  61. * @author Sergio Carvalho <sergio.carvalho@portugalmail.com>
  62. * @copyright 2004-2006 Sergio Carvalho
  63. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  64. * @link http://pear.php.net/package/XML_RPC2
  65. */
  66. abstract class XML_RPC2_Client
  67. {
  68. const VERSION = '1.0.4';
  69. // {{{ properties
  70. /**
  71. * uri Field (holds the uri for the XML_RPC server)
  72. *
  73. * @var string
  74. */
  75. protected $uri = null;
  76. /**
  77. * proxy Field (holds the proxy server data)
  78. *
  79. * @var string
  80. */
  81. protected $proxy = null;
  82. /**
  83. * Holds the prefix to prepend to method names
  84. *
  85. * @var string
  86. */
  87. protected $prefix = null;
  88. /**
  89. * Holds the debug flag
  90. *
  91. * @var boolean
  92. */
  93. protected $debug = false;
  94. /**
  95. * Hold the encoding of the client request
  96. *
  97. * @var string
  98. */
  99. protected $encoding = 'iso-8859-1';
  100. /**
  101. * Hold the SSL verify flag
  102. *
  103. * @var boolean
  104. */
  105. protected $sslverify = true;
  106. // }}}
  107. // {{{ remoteCall___()
  108. /**
  109. * ugly hack flag to avoid http://bugs.php.net/bug.php?id=21949
  110. *
  111. * see XML_RPC2_Backend_Xmlrpcext_Value::createFromNative() from more infos
  112. */
  113. protected $uglyStructHack = true;
  114. /**
  115. * remoteCall executes the XML-RPC call, and returns the result
  116. *
  117. * NB : The '___' at the end of the method name is to avoid collisions with
  118. * XMLRPC __call()
  119. *
  120. * @param string Method name
  121. * @param array Parameters
  122. */
  123. public abstract function remoteCall___($methodName, $parameters);
  124. // }}}
  125. // {{{ constructor
  126. /**
  127. * Construct a new XML_RPC2_Client.
  128. *
  129. * To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/).
  130. * Optionally, some options may be set as an associative array. Accepted keys are :
  131. * 'prefix', 'proxy', 'debug' => see correspondant property to get more informations
  132. *
  133. * @param string URI for the XML-RPC server
  134. * @param array (optional) Associative array of options
  135. */
  136. protected function __construct($uri, $options = array())
  137. {
  138. if (!$uriParse = parse_url($uri)) {
  139. throw new XML_RPC2_InvalidUriException(sprintf('Client URI \'%s\' is not valid', $uri));
  140. }
  141. $this->uri = $uri;
  142. if (isset($options['prefix'])) {
  143. if (!($this->testMethodName___($options['prefix']))) {
  144. throw new XML_RPC2_InvalidPrefixException(sprintf('Prefix \'%s\' is not valid', $options['prefix']));
  145. }
  146. $this->prefix = $options['prefix'];
  147. }
  148. if (isset($options['proxy'])) {
  149. if (!$proxyParse = parse_url($options['proxy'])) {
  150. throw new XML_RPC2_InvalidProxyException(sprintf('Proxy URI \'%s\' is not valid', $options['proxy']));
  151. }
  152. $this->proxy = $options['proxy'];
  153. }
  154. if (isset($options['debug'])) {
  155. if (!(is_bool($options['debug']))) {
  156. throw new XML_RPC2_InvalidDebugException(sprintf('Debug \'%s\' is not valid', $options['debug']));
  157. }
  158. $this->debug = $options['debug'];
  159. }
  160. if (isset($options['encoding'])) {
  161. // TODO : control & exception
  162. $this->encoding = $options['encoding'];
  163. }
  164. if (isset($options['uglyStructHack'])) {
  165. $this->uglyStructHack = $options['uglyStructHack'];
  166. }
  167. if (isset($options['sslverify'])) {
  168. if (!(is_bool($options['sslverify']))) {
  169. throw new XML_RPC2_InvalidSslverifyException(sprintf('SSL verify \'%s\' is not valid', $options['sslverify']));
  170. }
  171. $this->sslverify = $options['sslverify'];
  172. }
  173. }
  174. // }}}
  175. // {{{ create()
  176. /**
  177. * Factory method to select, create and return a XML_RPC2_Client backend
  178. *
  179. * To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/).
  180. *
  181. * Optionally, some options may be set.
  182. *
  183. * @param string URI for the XML-RPC server
  184. * @param array (optional) associative array of options (see constructor)
  185. */
  186. public static function create($uri, $options = array())
  187. {
  188. if (isset($options['backend'])) {
  189. XML_RPC2_Backend::setBackend($options['backend']);
  190. }
  191. $backend = XML_RPC2_Backend::getClientClassname();
  192. return new $backend($uri, $options);
  193. }
  194. // }}}
  195. // {{{ __call()
  196. /**
  197. * __call Catchall. This method catches remote method calls and provides for remote forwarding.
  198. *
  199. * If the parameters are native types, this method will use XML_RPC_Value::createFromNative to
  200. * convert it into an XML-RPC type. Whenever a parameter is already an instance of XML_RPC_Value
  201. * it will be used as provided. It follows that, in situations when XML_RPC_Value::createFromNative
  202. * proves inacurate -- as when encoding DateTime values -- you should present an instance of
  203. * XML_RPC_Value in lieu of the native parameter.
  204. *
  205. * @param string Method name
  206. * @param array Parameters
  207. * @return mixed The call result, already decoded into native types
  208. */
  209. public function __call($methodName, $parameters)
  210. {
  211. $args = array($methodName, $parameters);
  212. return @call_user_func_array(array($this, 'remoteCall___'), $args);
  213. }
  214. // }}}
  215. // {{{ displayDebugInformations___()
  216. /**
  217. * Display debug informations
  218. *
  219. * NB : The '___' at the end of the method name is to avoid collisions with
  220. * XMLRPC __call()
  221. *
  222. * @param string $request XML client request
  223. * @param string $body XML server response
  224. */
  225. protected function displayDebugInformations___($request, $body)
  226. {
  227. print '<pre>';
  228. print "***** Request *****\n";
  229. print htmlspecialchars($request);
  230. print "***** End Of request *****\n\n";
  231. print "***** Server response *****\n";
  232. print htmlspecialchars($body);
  233. print "\n***** End of server response *****\n\n";
  234. }
  235. // }}}
  236. // {{{ displayDebugInformations2___()
  237. /**
  238. * Display debug informations (part 2)
  239. *
  240. * NB : The '___' at the end of the method name is to avoid collisions with
  241. * XMLRPC __call()
  242. *
  243. * @param mixed $result decoded server response
  244. */
  245. protected function displayDebugInformations2___($result)
  246. {
  247. print "***** Decoded result *****\n";
  248. print_r($result);
  249. print "\n***** End of decoded result *****";
  250. print '</pre>';
  251. }
  252. // }}}
  253. // {{{ testMethodName___()
  254. /**
  255. * Return true is the given method name is ok with XML/RPC spec.
  256. *
  257. * NB : The '___' at the end of the method name is to avoid collisions with
  258. * XMLRPC __call()
  259. *
  260. * @param string $methodName method name
  261. * @return boolean true if ok
  262. */
  263. protected function testMethodName___($methodName)
  264. {
  265. return (preg_match('~^[a-zA-Z0-9_.:/]*$~', $methodName));
  266. }
  267. }
  268. ?>