xmlrpc_example.module

  1. examples
    1. 6 xmlrpc_example/xmlrpc_example.module
    2. 7 xmlrpc_example/xmlrpc_example.module
    3. 8 xmlrpc_example/xmlrpc_example.module
  2. drupal
    1. 4.7 developer/examples/xmlrpc_example.module
    2. 5 developer/examples/xmlrpc_example.module

Module file for xmlrpc_example module.

Functions & methods

NameDescription
xmlrpc_example_alter_formPresent a form to enable or disable the code implemented in hook_xmlrpc_alter.
xmlrpc_example_client_add_submitSubmit: query the XML-RPC endpoint for the method xmlrpc_example.add and report the result as a Drupal message.
xmlrpc_example_client_formPresent a form to get two arguments, and make a call to an XML-RPC server using these arguments as input, showing the result in a message.
xmlrpc_example_client_multicall_submitSubmit a multicall request: query the XML-RPC endpoint for the methods xmlrpc_example.add and xmlrpc_example.subtract and report the result as a Drupal message. Drupal's XML-RPC client builds the system.multicall request automatically when there…
xmlrpc_example_client_request_methods_submitSubmit: query the XML-RPC endpoint for the method system.listMethods and report the result as a Drupal message. The result is a list of the available methods in this XML-RPC server.
xmlrpc_example_client_subtract_submitSubmit: query the XML-RPC endpoint for the method xmlrpc_example.subtract and report the result as a Drupal message.
xmlrpc_example_infoA simple landing-page information function.
xmlrpc_example_menuImplements hook_menu().
xmlrpc_example_server_formPresent a form to configure the service options. In this case the maximum and minimum values for any of the operations (add or subtraction).
xmlrpc_example_xmlrpcImplements hook_xmlrpc().
xmlrpc_example_xmlrpc_alterImplements hook_xmlrpc_alter().
_xmlrpc_example_alter_addSum the two arguments without limit checking.
_xmlrpc_example_alter_subtractReturn the difference of the two arguments without limit checking.
_xmlrpc_example_server_addThis is the callback for the xmlrpc_example.add method.
_xmlrpc_example_server_subtractThis is the callback for the xmlrpc_example.subtract xmlrpc method.

File

xmlrpc_example/xmlrpc_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for xmlrpc_example module.
  5. */
  6. /**
  7. * @defgroup xmlrpc_example Example: XML-RPC
  8. * @ingroup examples
  9. * @{
  10. * Demonstration of XML-RPC in Drupal 7.
  11. *
  12. * This is an example of how to implement and XML-RPC server by registering
  13. * callbacks to specific methods and how to make xmlrpc calls using the built-in
  14. * xmlrpc() factory provided by Drupal.
  15. *
  16. * For experimentation you may be interested in the
  17. * @link http://drupal.org/project/xmlrpctester XML-RPC Tester module @endlink.
  18. *
  19. * Note that the @link http://drupal.org/project/services Services module @endlink
  20. * is another common way to do XML-RPC at this time.
  21. *
  22. * @see hook_xmlrpc()
  23. * @see xmlrpc()
  24. * @see xmlrpc_errno()
  25. * @see xmlrpc_error_msg()
  26. */
  27. // This is the common part of the module, implementing all the code required
  28. // for the client and the server part (most of this code is UI related). The
  29. // menu definition is the only part shared in this implementation.
  30. /**
  31. * Implements hook_menu().
  32. *
  33. * Registers all the demonstration forms.
  34. */
  35. function xmlrpc_example_menu() {
  36. $items['examples/xmlrpc'] = array(
  37. 'title' => 'XML-RPC Example',
  38. 'description' => 'Information about the XML-RPC example',
  39. 'page callback' => 'xmlrpc_example_info',
  40. 'access callback' => TRUE,
  41. );
  42. // This is the server configuration form menu entry. This form can be used to
  43. // configure the settings of the exposed services. An XML-RPC server does not
  44. // require a configuration form, and has been included here as an example.
  45. $items['examples/xmlrpc/server'] = array(
  46. 'title' => 'XML-RPC Server configuration',
  47. 'description' => 'Server configuration form',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('xmlrpc_example_server_form'),
  50. 'access callback' => TRUE,
  51. 'weight' => 0,
  52. );
  53. // This is the client form menu entry. This form is used to allow user
  54. // interaction with the services, but again, user interface is not required
  55. // to create an XML-RPC client with Drupal.
  56. $items['examples/xmlrpc/client'] = array(
  57. 'title' => 'XML-RPC Client form',
  58. 'description' => 'Demonstrates client side XML-RPC calls with Drupal',
  59. 'page callback' => 'drupal_get_form',
  60. 'page arguments' => array('xmlrpc_example_client_form'),
  61. 'access callback' => TRUE,
  62. 'weight' => 1,
  63. );
  64. // This part is completely optional. It allows the modification of services
  65. // defined by this or other modules. This configuration form is used to
  66. // enable the hook_xmlrpc_alter API and alter current existing services
  67. $items['examples/xmlrpc/alter'] = array(
  68. 'title' => 'XML-RPC Alterations',
  69. 'description' => 'Demonstrates how to alter defined XML-RPC services',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('xmlrpc_example_alter_form'),
  72. 'access callback' => TRUE,
  73. 'weight' => 2,
  74. );
  75. return $items;
  76. }
  77. /**
  78. * A simple landing-page information function.
  79. */
  80. function xmlrpc_example_info() {
  81. $server = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  82. $options = array(
  83. 'system.listMethods' => array(),
  84. );
  85. // Make the xmlrpc request and process the results.
  86. $supported_methods = xmlrpc($server, $options);
  87. if ($supported_methods === FALSE) {
  88. drupal_set_message(t('Error return from xmlrpc(): Error: @errno, Message: @message', array('@errno' => xmlrpc_errno(), '@message' => xmlrpc_error_msg())));
  89. }
  90. return array(
  91. 'basic' => array('#markup' => t('This XML-RPC example presents code that shows <ul><li><a href="!server">XML-RPC server code</a></li><li><a href="!client">XML-RPC client code</a></li><li>and <a href="!alter">an example hook_xmlrpc_alter() call</a></li></ul>', array('!server' => url('examples/xmlrpc/server'), '!client' => url('examples/xmlrpc/client'), '!alter' => url('examples/xmlrpc/alter')))),
  92. 'method_array' => array('#markup' => theme('item_list', array('title' => t('These methods are supported by !server', array('!server' => $server)), 'items' => $supported_methods))),
  93. );
  94. }
  95. // This is the server part of the module, implementing a simple and little
  96. // server with just two simple services. The server is divided in two
  97. // different parts: the XML-RPC implementation (required) and a webform
  98. // interface (optional) to configure some settings in the server side.
  99. //
  100. // The XMLRPC server will define two different services:
  101. //
  102. // - subtract: perform the subtraction of two numbers. The minimum and maximum
  103. // values returned by the server can be configured in the server configuration
  104. // form.
  105. // - add: perform the addition of two numbers. The minimum and maximum values
  106. // returned by the server can be configured in the server configuration form.
  107. //
  108. // If the result value for the operation is over the maximum limit, a custom
  109. // error number 10001 is returned. This is an arbitrary number and could be any
  110. // number.
  111. //
  112. // If the result value for the operation is below the minimum limit, a custom
  113. // error number 10002 is returned. Again, this value is arbitrary and could be
  114. // any other number. Client applications must know the meaning of the error
  115. // numbers returned by the server.
  116. // The following code is the XML-RPC implementation of the server part. The first
  117. // step is to define the methods. This methods should be associated to callbacks
  118. // that will be defined later.
  119. /**
  120. * Implements hook_xmlrpc().
  121. *
  122. * Provides Drupal with an array to map XML-RPC callbacks to existing functions.
  123. * These functions may be defined in other modules. The example implementation
  124. * defines specific functions for the example services.
  125. *
  126. * Note: Drupal's built-in XML-RPC server already includes several methods by
  127. * default:
  128. *
  129. * Service dicovery methods:
  130. * - system.listMethods: return a list of the methods the server has, by name.
  131. * - system.methodSignature: return a description of the argument format a
  132. * - system.methodHelp: returns a text description of a particular method.
  133. * particular method expects.
  134. *
  135. * Other:
  136. * - system.multicall: perform several method calls in a single xmlrpc request.
  137. * - system.getCapabilities: determine if a given capability is supported.
  138. *
  139. * The methods defined by hook_xmlrpc() will be added to those provided by
  140. * default by Drupal's XML-RPC server.
  141. *
  142. * @see hook_xmlrpc()
  143. */
  144. function xmlrpc_example_xmlrpc() {
  145. $methods[] = array(
  146. // First argument is the method name.
  147. 'xmlrpc_example.add',
  148. // Callback to execute when this method is requested.
  149. '_xmlrpc_example_server_add',
  150. // An array defines the types of output and input values for this method.
  151. array(
  152. // The first value is the return type, an integer in this case.
  153. 'int',
  154. // First operand is an integer.
  155. 'int',
  156. // Second operatnd is an integer.
  157. 'int',
  158. ),
  159. // Include a little description that is shown when XML-RPC server is
  160. // requested for the implemented methods list.
  161. t('Returns the sum of the two arguments.') // Method description
  162. );
  163. // The subtract method is similar to the addition, only the method name,
  164. // callback and description are different.
  165. $methods[] = array(
  166. 'xmlrpc_example.subtract',
  167. '_xmlrpc_example_server_subtract',
  168. array('int', 'int', 'int'),
  169. t('Return difference of the two arguments.')
  170. );
  171. return $methods;
  172. }
  173. // The following code for the server is optional if the callbacks already exist.
  174. // A server may implement methods associated to callbacks like node_load(),
  175. // variable_get() or any other existing function (php functions as well).
  176. //
  177. // If the callbacks associated to the methods don't exist they must be
  178. // created. This implementation requires two specific callbacks:
  179. // - _xmlrpc_example_server_add()
  180. // - _xmlrpc_example_server_subtract()
  181. //
  182. /**
  183. * This is the callback for the xmlrpc_example.add method.
  184. *
  185. * Sum the two arguments and return value or an error if the result is out of the
  186. * configured limits.
  187. *
  188. * @param $num1
  189. * The first number to be summed.
  190. * @param $num2
  191. * The second number to be summed.
  192. * @return
  193. * The sum of the arguments, or error if it is not in server defined bounds.
  194. *
  195. * @see xmlrpc_error()
  196. */
  197. function _xmlrpc_example_server_add($num1, $num2) {
  198. $sum = $num1 + $num2;
  199. // If result is not within maximum and minimum limits, return corresponding error
  200. $max = variable_get('xmlrpc_example_server_max', 10);
  201. $min = variable_get('xmlrpc_example_server_min', 0);
  202. if ($sum > $max) {
  203. return xmlrpc_error(10001, t('Result is over the upper limit (@max) defined by the server.', array('@max' => $max)));
  204. }
  205. if ($sum < $min) {
  206. return xmlrpc_error(10002, t('Result is below the lower limit defined by the server (@min).', array('@min' => $min)));
  207. }
  208. // Otherwise return the result.
  209. return $sum;
  210. }
  211. /**
  212. * This is the callback for the xmlrpc_example.subtract xmlrpc method.
  213. *
  214. * Return the difference of the two arguments, or an error if the result is out of the
  215. * configured limits..
  216. *
  217. *
  218. * @param numeric $num1
  219. * @param numeric $num2
  220. * @return
  221. * The difference of the two arguments, or error if it is not in server defined bounds.
  222. *
  223. * @see xmlrpc_error()
  224. */
  225. function _xmlrpc_example_server_subtract($num1, $num2) {
  226. $diference = $num1 - $num2;
  227. $max = variable_get('xmlrpc_example_server_max', 10);
  228. $min = variable_get('xmlrpc_example_server_min', 0);
  229. // If result is not within maximum and minimum limits, return corresponding error
  230. if ($diference > $max) {
  231. return xmlrpc_error(10001, t('Result is above the upper limit (@max) defined by the server.', array('@max' => $max)));
  232. }
  233. if ($diference < $min) {
  234. return xmlrpc_error(10002, t('Result is below the lower limit (@min) defined by the server.', array('@min' => $min)));
  235. }
  236. // Otherwise return the result.
  237. return $diference;
  238. }
  239. // User interface for the XML-RPC Server part.
  240. // A server does not require an interface at all. In this implementation we
  241. // use a server configuration form to set the limits available for the addition
  242. // and subtraction operations.
  243. /**
  244. * Present a form to configure the service options. In this case the maximum
  245. * and minimum values for any of the operations (add or subtraction).
  246. */
  247. function xmlrpc_example_server_form() {
  248. $form = array();
  249. $form['explanation'] = array(
  250. '#markup' => '<div>' . t('This is the XML-RPC server configuration page.<br />Here you may define the maximum and minimum values for the addition or subtraction exposed services.<br />') . '</div>',
  251. );
  252. $form['xmlrpc_example_server_min'] = array(
  253. '#type' => 'textfield',
  254. '#title' => t('Enter the minimum value returned by the subtraction or addition methods'),
  255. '#description' => t('If the result of the operation is lower than this value, a custom XML-RPC error will be returned: 10002.'),
  256. '#default_value' => variable_get('xmlrpc_example_server_min', 0),
  257. '#size' => 5,
  258. '#required' => TRUE,
  259. );
  260. $form['xmlrpc_example_server_max'] = array(
  261. '#type' => 'textfield',
  262. '#title' => t('Enter the maximum value returned by sub or add methods'),
  263. '#description' => t('if the result of the operation is bigger than this value, a custom XML-RPC error will be returned: 10001.'),
  264. '#default_value' => variable_get('xmlrpc_example_server_max', 10),
  265. '#size' => 5,
  266. '#required' => TRUE,
  267. );
  268. $form['info'] = array(
  269. '#type' => 'markup',
  270. '#markup' => '<div>' . t('Use the <a href="!link">XML-RPC Client example form</a> to experiment', array('!link' => url('examples/xmlrpc/client'))),
  271. );
  272. if (variable_get('xmlrpc_example_alter_enabled', FALSE)) {
  273. $form['overridden'] = array(
  274. '#type' => 'markup',
  275. '#markup' => '<div><strong>' . t('Just a note of warning: The <a href="!link">alter form</a> has been used to disable the limits, so you may want to turn that off if you do not want it.', array('!link' => url('examples/xmlrpc/alter'))) . '</strong></div>',
  276. );
  277. }
  278. return system_settings_form($form);
  279. }
  280. // The server part of the module ends here.
  281. // This is the client part of the module. If defines a form with two input
  282. // fields to call xmlrpc_example.add or xmlrpc_example.subtract methods on this
  283. // host. Please note that having a user interface to query an XML-RPC service is
  284. // not required. A method can be requested to a server using the xmlrpc()
  285. // function directly. We have included an user interface to make the testing
  286. // easier.
  287. // The client user interface part of the module starts here.
  288. /**
  289. * Present a form to get two arguments, and make a call to an XML-RPC server
  290. * using these arguments as input, showing the result in a message.
  291. */
  292. function xmlrpc_example_client_form() {
  293. $form = array();
  294. $form['explanation'] = array(
  295. '#markup' => '<div>' . t('This example demonstrates how to make XML-RPC calls with Drupal. <br />The "Request methods" button makes a request to the server and asks for the available list of methods, as a service discovery request. <br/>The "Add integers" and "Subtract integers" use the xmlrpc() function to act as a client, calling the XML-RPC server defined in this same example for some defined methods.<br />An XML-RPC error will result if the result in the addition or subtraction requested is out of bounds defined by the server. These error numbers are defined by the server. <br />The "Add and Subtract" button performs a multicall operation on the XML-RPC server: several requests in a single XML-RPC call.<br />') . '</div>',
  296. );
  297. // We are going to call add and subtract methods, and they work with integer values.
  298. $form['num1'] = array(
  299. '#type' => 'textfield',
  300. '#title' => t('Enter an integer'),
  301. '#default_value' => 2,
  302. '#size' => 5,
  303. '#required' => TRUE,
  304. );
  305. $form['num2'] = array(
  306. '#type' => 'textfield',
  307. '#title' => t('Enter a second integer'),
  308. '#default_value' => 2,
  309. '#size' => 5,
  310. '#required' => TRUE,
  311. );
  312. // Include several buttons, each of them calling a different method.
  313. // This button submits a XML-RPC call to the system.listMethods method.
  314. $form['information'] = array(
  315. '#type' => 'submit',
  316. '#value' => t('Request methods'),
  317. '#submit' => array('xmlrpc_example_client_request_methods_submit'),
  318. );
  319. // This button submits a XML-RPC call to the xmlrpc_example.add method.
  320. $form['add'] = array(
  321. '#type' => 'submit',
  322. '#value' => t('Add the integers'),
  323. '#submit' => array('xmlrpc_example_client_add_submit'),
  324. );
  325. // This button submits a XML-RPC call to the xmlrpc_example.subtract method.
  326. $form['subtract'] = array(
  327. '#type' => 'submit',
  328. '#value' => t('Subtract the integers'),
  329. '#submit' => array('xmlrpc_example_client_subtract_submit'),
  330. );
  331. // This button submits a XML-RPC call to the system.multicall method.
  332. $form['add_subtract'] = array(
  333. '#type' => 'submit',
  334. '#value' => t('Add and Subtract'),
  335. '#submit' => array('xmlrpc_example_client_multicall_submit'),
  336. );
  337. if (variable_get('xmlrpc_example_alter_enabled', FALSE)) {
  338. $form['overridden'] = array(
  339. '#type' => 'markup',
  340. '#markup' => '<div><strong>' . t('Just a note of warning: The <a href="!link">alter form</a> has been used to disable the limits, so you may want to turn that off if you do not want it.', array('!link' => url('examples/xmlrpc/alter'))) . '</strong></div>',
  341. );
  342. }
  343. return $form;
  344. }
  345. /**
  346. * Submit: query the XML-RPC endpoint for the method system.listMethods
  347. * and report the result as a Drupal message. The result is a list of the
  348. * available methods in this XML-RPC server.
  349. *
  350. * Important note: Not all XML-RPC servers implement this method. Drupal's
  351. * built-in XML-RPC server implements this method by default.
  352. *
  353. * @param $form
  354. * @param $form_state
  355. *
  356. * @see xmlrpc()
  357. * @see xmlrpc_errno()
  358. * @see xmlrpc_error_msg()
  359. */
  360. function xmlrpc_example_client_request_methods_submit($form, &$form_state) {
  361. // First define the endpoint of the XML-RPC service, in this case this is our
  362. // own server.
  363. $server = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  364. // Then we should define the method to call. xmlrpc() requires that all the
  365. // information related to the called method be passed as an array in the form
  366. // of 'method_name' => arguments_array
  367. $options = array(
  368. 'system.listMethods' => array(),
  369. );
  370. // Make the xmlrpc request and process the results.
  371. $result = xmlrpc($server, $options);
  372. if ($result === FALSE) {
  373. drupal_set_message(
  374. t('Error return from xmlrpc(): Error: @errno, Message: @message',
  375. array('@errno' => xmlrpc_errno(), '@message' => xmlrpc_error_msg())),
  376. 'error'
  377. );
  378. }
  379. else {
  380. drupal_set_message(
  381. t('The XML-RPC server returned this response: <pre>@response</pre>',
  382. array('@response' => print_r($result, TRUE)))
  383. );
  384. }
  385. }
  386. /**
  387. * Submit: query the XML-RPC endpoint for the method xmlrpc_example.add
  388. * and report the result as a Drupal message.
  389. *
  390. * @param $form
  391. * @param $form_state
  392. *
  393. * @see xmlrpc()
  394. * @see xmlrpc_errno()
  395. * @see xmlrpc_error_msg()
  396. */
  397. function xmlrpc_example_client_add_submit($form, &$form_state) {
  398. // First define the endpoint of the XML-RPC service, in this case is our
  399. // own server.
  400. $server = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  401. // Then we should define the method to call. xmlrpc() requires that all the
  402. // information related to the called method is passed as an array in the form
  403. // of 'method_name' => arguments_array
  404. $options = array(
  405. 'xmlrpc_example.add' => array(
  406. (int) $form_state['values']['num1'],
  407. (int) $form_state['values']['num2'],
  408. ),
  409. );
  410. // Make the xmlrpc request and process the results.
  411. $result = xmlrpc($server, $options);
  412. if ($result === FALSE) {
  413. drupal_set_message(
  414. t('Error return from xmlrpc(): Error: @errno, Message: @message',
  415. array('@errno' => xmlrpc_errno(), '@message' => xmlrpc_error_msg())),
  416. 'error'
  417. );
  418. }
  419. else {
  420. drupal_set_message(
  421. t('The XML-RPC server returned this response: @response',
  422. array('@response' => print_r($result, TRUE)))
  423. );
  424. }
  425. }
  426. /**
  427. * Submit: query the XML-RPC endpoint for the method xmlrpc_example.subtract
  428. * and report the result as a Drupal message.
  429. *
  430. * @param $form
  431. * @param $form_state
  432. *
  433. * @see xmlrpc()
  434. * @see xmlrpc_errno()
  435. * @see xmlrpc_error_msg()
  436. * @see xmlrpc_example_client_add_submit()
  437. */
  438. function xmlrpc_example_client_subtract_submit($form, &$form_state) {
  439. $server = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  440. $options = array(
  441. 'xmlrpc_example.subtract' => array(
  442. (int) $form_state['values']['num1'],
  443. (int) $form_state['values']['num2'],
  444. ),
  445. );
  446. // Make the xmlrpc request and process the results.
  447. $result = xmlrpc($server, $options);
  448. if ($result === FALSE) {
  449. drupal_set_message(
  450. t('Error return from xmlrpc(): Error: @errno, Message: @message',
  451. array('@errno' => xmlrpc_errno(), '@message' => xmlrpc_error_msg())),
  452. 'error'
  453. );
  454. }
  455. else {
  456. drupal_set_message(
  457. t('The XML-RPC server returned this response: @response',
  458. array('@response' => print_r($result, TRUE)))
  459. );
  460. }
  461. }
  462. /**
  463. * Submit a multicall request: query the XML-RPC endpoint for the methods
  464. * xmlrpc_example.add and xmlrpc_example.subtract and report the result as a
  465. * Drupal message. Drupal's XML-RPC client builds the system.multicall request
  466. * automatically when there is more than one method to call.
  467. *
  468. * @param $form
  469. * @param $form_state
  470. *
  471. * @see xmlrpc()
  472. * @see xmlrpc_errno()
  473. * @see xmlrpc_error_msg()
  474. * @see xmlrpc_example_client_multicall_submit()
  475. */
  476. function xmlrpc_example_client_multicall_submit($form, &$form_state) {
  477. $server = url($GLOBALS['base_url'] . '/xmlrpc.php', array('external' => TRUE));
  478. /*
  479. * Drupal's built-in xmlrpc server supports the system.multicall method.
  480. *
  481. * To make a multicall request, the main invoked method should be the
  482. * function 'system.multicall', and the arguments to make this call must be
  483. * defined as an array of single method calls, being the array keys the
  484. * service methods to be called, and the array elements the method arguments.
  485. *
  486. * See the code below this comment as example.
  487. */
  488. // Build an array of several calls, Drupal's xmlrpc built-in support will
  489. // construct the correct system.multicall request for the server.
  490. $options = array(
  491. 'xmlrpc_example.add' => array(
  492. (int) $form_state['values']['num1'],
  493. (int) $form_state['values']['num2'],
  494. ),
  495. 'xmlrpc_example.subtract' => array(
  496. (int) $form_state['values']['num1'],
  497. (int) $form_state['values']['num2'],
  498. ),
  499. );
  500. // Make the xmlrpc request and process the results.
  501. $result = xmlrpc($server, $options);
  502. if ($result === FALSE) {
  503. drupal_set_message(
  504. t('Error return from xmlrpc(): Error: @errno, Message: @message',
  505. array('@errno' => xmlrpc_errno(), '@message' => xmlrpc_error_msg()))
  506. );
  507. }
  508. else {
  509. drupal_set_message(
  510. t('The XML-RPC server returned this response: <pre>@response</pre>',
  511. array('@response' => print_r($result, TRUE)))
  512. );
  513. }
  514. }
  515. // The client part of the module ends here.
  516. // The alteration part of the module starts here. hook_xmlrpc_alter() is
  517. // useful when you want to extend, limit or alter methods defined by other
  518. // modules. This part is not required to have an XML-RPC server or client
  519. // working, but is useful to understand what can we do using current xmlrpc
  520. // API provided by drupal.
  521. //
  522. // This code can be defined in other module to alter the methods exposed by
  523. // this xmlrpc demonstration server, or can be used to alter methods defined
  524. // by other modules implementing hook_xmlrpc()
  525. //
  526. // As with the rest of the example module, an user interface is not required to
  527. // make use of this hook. A configuration form is included to enable/disable
  528. // this functionality, but this part is optional if you want to implement
  529. // hook_xmlrpc_alter()
  530. // This is the XML-RPC code for the alteration part. It will check if an option
  531. // to enable the functionality is enabled and then alter it. We alter the
  532. // 'xmlrpc_example.add' and 'xmlrpc_example.subtract' methods, changing the
  533. // associated callback with custom functions. The modified methods (with
  534. // new callbacks associated) will perform the addition or subtraction of the
  535. // integer inputs, but will never check for limits nor return errors.
  536. /**
  537. * Implements hook_xmlrpc_alter().
  538. *
  539. * Check to see if xmlrpc_example.add and xmlrpc_example.subtract methods are
  540. * defined and replace their callbacks with custom code.
  541. *
  542. * @see hook_xmlrpc_alter()
  543. */
  544. function xmlrpc_example_xmlrpc_alter(&$methods) {
  545. // Only perform alterations if instructed to do so.
  546. if (!variable_get('xmlrpc_example_alter_enabled', 0)) {
  547. return;
  548. }
  549. // Loop all defined methods (other modules may have include additional methods)
  550. foreach ($methods as $index => $method) {
  551. // First element in the method array is the method name.
  552. if ($method[0] == 'xmlrpc_example.add') {
  553. // Replace current callback with custom callback (second argument of the array)
  554. $methods[$index][1] = '_xmlrpc_example_alter_add';
  555. }
  556. // Do the same for the substraction method.
  557. if ($method[0] == 'xmlrpc_example.subtract') {
  558. $methods[$index][1] = '_xmlrpc_example_alter_subtract';
  559. }
  560. }
  561. }
  562. // Now we define the custom callbacks replacing the original defined by the
  563. // altered methods: xmlrpc_example.add and _xmlrpc_example.subtract . These
  564. // new callbacks will not check if the result of the operation is within the
  565. // limits defined by the server and will always return the value of the operation.
  566. /**
  567. * Sum the two arguments without limit checking.
  568. *
  569. * This is the replacement callback for the xmlrpc_example.add xmlrpc method.
  570. *
  571. * @param $num1
  572. * @param $num2
  573. * @return
  574. * The sum of the arguments
  575. */
  576. function _xmlrpc_example_alter_add($num1, $num2) {
  577. return $num1 + $num2;
  578. }
  579. /**
  580. * Return the difference of the two arguments without limit checking.
  581. *
  582. * This is the replacement callback for the xmlrpc_example.subtract xmlrpc method.
  583. *
  584. * @param numeric $num1
  585. * @param numeric $num2
  586. * @return
  587. * The difference of the two arguments
  588. */
  589. function _xmlrpc_example_alter_subtract($num1, $num2) {
  590. return $num1 - $num2;
  591. }
  592. // Our implementation of hook_xmlrpc_alter will work only if a system variable
  593. // is set to true, and we need a configuration form to enable/disable this
  594. // 'feature'. This is the user interface to enable or disable the hook_xmlrpc_alter
  595. // operations.
  596. /**
  597. * Present a form to enable or disable the code implemented in hook_xmlrpc_alter.
  598. */
  599. function xmlrpc_example_alter_form() {
  600. $form = array();
  601. $form['explanation'] = array(
  602. '#markup' => '<div>' . t('This is a configuration form to enable the alteration of XML-RPC methods using hook_xmlrpc_alter.<br />hook_xmlrpc_alter() can be used to alter the current defined methods by other modules. In this case as demonstration, we will overide current add and subtraction methods with others not being limited. Remember that this hook is optional and is not required to create XMLRPC services.<br />') . '</div>',
  603. );
  604. $form['xmlrpc_example_alter_enabled'] = array(
  605. '#type' => 'checkbox',
  606. '#title' => t('Overide current xmlrpc_example.add and xmlrpc_example.subtraction methods'),
  607. '#description' => t('If this checkbox is enabled, the default methods will be replaced with custom methods that ignore the XML-RPC server maximum and minimum restrictions.'),
  608. '#default_value' => variable_get('xmlrpc_example_alter_enabled', 0),
  609. );
  610. $form['info'] = array(
  611. '#markup' => '<div>' . t('Use the <a href="!link">client submission form</a> to see the results of checking this checkbox', array('!link' => url('examples/xmlrpc/client'))) . '</div>',
  612. );
  613. return system_settings_form($form);
  614. }
  615. /**
  616. * @} End of "defgroup xmlrpc_example".
  617. */
Login or register to post comments