You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.6 KiB

15 years ago
15 years ago
  1. <?php
  2. /**
  3. * @file RPC.php
  4. * This file is part of PROJECT.
  5. *
  6. * @brief Description
  7. *
  8. * @author Etenil <etenil@etenilsrealm.nl>
  9. *
  10. * @version 1.0
  11. * @date 20 February 2011
  12. *
  13. * Copyright (C)2011 Etenil
  14. *
  15. * All rights reserved.
  16. */
  17. class RPC
  18. {
  19. protected static $instance;
  20. protected static $funcalls;
  21. public static function call($funcname)
  22. {
  23. if(!is_array(self::$funcalls)) {
  24. self::$funcalls = array();
  25. }
  26. $args = func_get_args();
  27. array_shift($args);
  28. $args = array_map('trim', $args);
  29. if(self::filter($funcname, $args)) {
  30. $funcall = array(
  31. 'func' => $funcname,
  32. 'params' => $args,
  33. );
  34. self::$funcalls[] = $funcall;
  35. }
  36. }
  37. /**
  38. * Check if the event is not already called
  39. */
  40. private static function filter($funcname, $args)
  41. {
  42. foreach(self::$funcalls as $f) {
  43. if(isset($f['func']) &&
  44. isset($f['params']) &&
  45. $f['func'] == $funcname &&
  46. $f['params'][0] == $args[0])
  47. return false;
  48. }
  49. return true;
  50. }
  51. public static function cdata($text)
  52. {
  53. $args = func_get_args();
  54. return '<![CDATA['.
  55. $text.
  56. ']]>';
  57. }
  58. /**
  59. * Sends outgoing requests.
  60. */
  61. public static function commit()
  62. {
  63. // Cleaning rubbish.
  64. ob_clean();
  65. ob_start();
  66. // Just in case (warning)
  67. if(!is_array(self::$funcalls)) {
  68. self::$funcalls = array('ping');
  69. }
  70. header('Content-Type: application/json');
  71. printf('%s', json_encode(self::$funcalls));
  72. }
  73. /**
  74. * Handles incoming requests.
  75. */
  76. public function handle_json()
  77. {
  78. if(isset($_GET['do']) && $_GET['do'] == 'poll') {
  79. moxl\ping();
  80. } else {
  81. $json = file_get_contents('php://input');
  82. $request = json_decode($json);
  83. // Loading the widget.
  84. $widget_name = (string)$request->widget;
  85. // Preparing the parameters and calling the function.
  86. $params = (array)$request->params;
  87. $result = array();
  88. foreach($params as $p) {
  89. if(is_object($p) && $p->container)
  90. array_push($result, (array)$p->container);
  91. else
  92. array_push($result, $p);
  93. }
  94. $widgets = WidgetWrapper::getInstance(false);
  95. $widgets->run_widget($widget_name, (string)$request->func, $result);
  96. }
  97. }
  98. }
  99. ?>