Runner.class.php5 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * DON'T REMOVE THE FOLLOWING LICENSE
  4. * INFORMATION!
  5. * ----------------------------------
  6. * Copyright by
  7. * Dennis Ritz
  8. * Author: Dennis Ritz
  9. * dennis.ritz@gmx.net
  10. * 2007-2008
  11. * ----------------------------------
  12. */
  13. class Runner {
  14. public function __construct() {
  15. }
  16. final public function start() {
  17. //session_cache_limiter("nocache");
  18. header ('Content-type: text/html; charset=UTF-8');
  19. header ("Content-Transfer-Encoding: 8bit");
  20. if(!empty($_REQUEST["rc"]) && !empty($_REQUEST["rm"])) {
  21. $p_class = $_REQUEST["rc"];
  22. $p_method = $_REQUEST["rm"];
  23. if(!empty($_REQUEST["ras"]))
  24. $p_args = $_REQUEST["ras"];
  25. else
  26. $p_args = array();
  27. $res = $this->callComp($p_class,$p_method,$p_args);
  28. if(isset($res) && $res !== false) {
  29. $res = $this->_formatRequestRes($res);
  30. echo "+ var res = " . trim($res) . "; res;";
  31. }
  32. return;
  33. }
  34. }
  35. final public function stop() {
  36. exit;
  37. }
  38. final public function callComp($p_class, $p_method, $p_args = array()) {
  39. if(file_exists(ROOT_DIR."apps/".$p_class."/".$p_class.".class.inc")) {
  40. try {
  41. $rMethod = new ReflectionMethod($p_class, $p_method);
  42. } catch(Exception $e) {
  43. Log::warning(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" couldn't be found.",$p_class,$p_method));
  44. return false;
  45. }
  46. if($rMethod->isPublic()) {
  47. $class = call_user_func_array(
  48. array(new ReflectionClass($p_class), 'newInstance'),
  49. array()
  50. );
  51. return $res = call_user_func_array(array($class, $p_method), $p_args);
  52. } else {
  53. Log::error(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" isn't public.",$p_class,$p_method));
  54. return false;
  55. }
  56. } else {
  57. Log::error(__FILE__,__LINE__,sprintf("Class \"%s\" isn't a component!",$p_class));
  58. }
  59. }
  60. private function _formatRequestResEsc($val) {
  61. $val = str_replace("\\", "\\\\", $val);
  62. $val = str_replace("\r", "\\r", $val);
  63. //$val = str_replace("\r", "", $val);
  64. $val = str_replace("\n", "\\n", $val);
  65. //$val = str_replace("\n", "", $val);
  66. $val = str_replace("'", "\\'", $val);
  67. return str_replace('"', '\\"', $val);
  68. }
  69. private function _formatRequestRes($p_result) {
  70. if (is_bool($p_result)) {
  71. return ($p_result) ? "Boolean(true)" : "Boolean(false)";
  72. }
  73. elseif (is_integer($p_result)) {
  74. return "parseInt($p_result)";
  75. }
  76. elseif (is_double($p_result)) {
  77. return "parseFloat($p_result)";
  78. }
  79. elseif (is_array($p_result) || is_object($p_result)) {
  80. //
  81. // XXX Arrays with non-numeric indices are not
  82. // permitted according to ECMAScript, yet everyone
  83. // uses them.. We'll use an object.
  84. //
  85. $s = "{ ";
  86. if (is_object($p_result)) {
  87. $p_result = get_object_vars($p_result);
  88. }
  89. foreach ($p_result as $k=>$v) {
  90. $esc_key = $this->_formatRequestResEsc($k);
  91. if (is_numeric($k))
  92. $s .= "$k: " . $this->_formatRequestRes($v) . ", ";
  93. else
  94. $s .= "\"$esc_key\": " . $this->_formatRequestRes($v) . ", ";
  95. }
  96. if (count($p_result))
  97. $s = substr($s, 0, -2);
  98. return $s . " }";
  99. }
  100. else {
  101. $esc_val = $this->_formatRequestResEsc($p_result);
  102. $s = "'$esc_val'";
  103. return $s;
  104. }
  105. }
  106. }
  107. ?>