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.

109 lines
2.4 KiB

  1. <?php
  2. /**
  3. * @file RPC.php
  4. * This file is part of Movim
  5. *
  6. * @brief Handle incoming requests and call the widgets
  7. *
  8. * @author Jaussoin Timothée
  9. *
  10. * @version 1.0
  11. * @date 28 october 2014
  12. *
  13. * All rights reserved.
  14. */
  15. class RPC
  16. {
  17. protected static $instance;
  18. protected static $funcalls;
  19. public static function call($funcname)
  20. {
  21. if(!is_array(self::$funcalls)) {
  22. self::$funcalls = array();
  23. }
  24. $args = func_get_args();
  25. array_shift($args);
  26. /*$args = array_map(
  27. function($string) {
  28. return preg_replace("/[\t\r\n]/", '', trim($string));
  29. },
  30. $args);*/
  31. if(self::filter($funcname, $args)) {
  32. $funcall = array(
  33. 'func' => $funcname,
  34. 'params' => $args,
  35. );
  36. self::$funcalls[] = $funcall;
  37. } elseif(isset($args[0])) {
  38. //\system\Logs\Logger::log('RPC cleaning system : '.$funcname.', '.$args[0].' cleared');
  39. }
  40. }
  41. /**
  42. * Check if the event is not already called
  43. */
  44. private static function filter($funcname, $args)
  45. {
  46. foreach(self::$funcalls as $f) {
  47. if(isset($f['func']) &&
  48. isset($f['params']) &&
  49. $f['func'] == $funcname &&
  50. $f['params'] === $args)
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Sends outgoing requests.
  57. */
  58. public static function commit()
  59. {
  60. return self::$funcalls;
  61. }
  62. public static function clear()
  63. {
  64. self::$funcalls = array();
  65. }
  66. /**
  67. * Handles incoming requests.
  68. */
  69. public function handle_json($request)
  70. {
  71. // Loading the widget.
  72. if(isset($request->widget)) {
  73. $widget_name = (string)$request->widget;
  74. } else {
  75. return;
  76. }
  77. $result = array();
  78. // Preparing the parameters and calling the function.
  79. if(isset($request->params)) {
  80. $params = (array)$request->params;
  81. foreach($params as $p) {
  82. if(is_object($p) && isset($p->container))
  83. array_push($result, (array)$p->container);
  84. else
  85. array_push($result, $p);
  86. }
  87. }
  88. $widgets = WidgetWrapper::getInstance();
  89. $widgets->runWidget($widget_name, (string)$request->func, $result);
  90. }
  91. }
  92. ?>