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.

124 lines
2.9 KiB

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