Runner.class.php5 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. foreach($p_args as $key => $value) {
  28. $p_args[$key] = utf8_decode($value);
  29. }
  30. $res = $this->callComp($p_class,$p_method,$p_args);
  31. if(isset($res) && $res !== false) {
  32. $res = $this->_formatRequestRes($res);
  33. echo "+ var res = " . trim($res) . "; res;";
  34. }
  35. return;
  36. }
  37. }
  38. final public function stop() {
  39. exit;
  40. }
  41. final public function callComp($p_class, $p_method, $p_args = array()) {
  42. if(file_exists(ROOT_DIR."apps/".$p_class."/".$p_class.".class.inc")) {
  43. try {
  44. $rMethod = new ReflectionMethod($p_class, $p_method);
  45. } catch(Exception $e) {
  46. Log::warning(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" couldn't be found.",$p_class,$p_method));
  47. return false;
  48. }
  49. if($rMethod->isPublic()) {
  50. $class = call_user_func_array(
  51. array(new ReflectionClass($p_class), 'newInstance'),
  52. array()
  53. );
  54. return $res = call_user_func_array(array($class, $p_method), $p_args);
  55. } else {
  56. Log::error(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" isn't public.",$p_class,$p_method));
  57. return false;
  58. }
  59. } else {
  60. Log::error(__FILE__,__LINE__,sprintf("Class \"%s\" isn't a component!",$p_class));
  61. }
  62. }
  63. final private function _formatRequestResEsc($val) {
  64. $val = str_replace("\\", "\\\\", $val);
  65. $val = str_replace("\r", "\\r", $val);
  66. //$val = str_replace("\r", "", $val);
  67. $val = str_replace("\n", "\\n", $val);
  68. //$val = str_replace("\n", "", $val);
  69. $val = str_replace("'", "\\'", $val);
  70. return str_replace('"', '\\"', $val);
  71. }
  72. final private function _formatRequestRes($p_result) {
  73. if (is_bool($p_result)) {
  74. return ($p_result) ? "Boolean(true)" : "Boolean(false)";
  75. }
  76. elseif (is_integer($p_result)) {
  77. return "parseInt($p_result)";
  78. }
  79. elseif (is_double($p_result)) {
  80. return "parseFloat($p_result)";
  81. }
  82. elseif (is_array($p_result) || is_object($p_result)) {
  83. //
  84. // XXX Arrays with non-numeric indices are not
  85. // permitted according to ECMAScript, yet everyone
  86. // uses them.. We'll use an object.
  87. //
  88. $s = "{ ";
  89. if (is_object($p_result)) {
  90. $p_result = get_object_vars($p_result);
  91. }
  92. foreach ($p_result as $k=>$v) {
  93. $esc_key = $this->_formatRequestResEsc($k);
  94. if (is_numeric($k))
  95. $s .= "$k: " . $this->_formatRequestRes($v) . ", ";
  96. else
  97. $s .= "\"$esc_key\": " . $this->_formatRequestRes($v) . ", ";
  98. }
  99. if (count($p_result))
  100. $s = substr($s, 0, -2);
  101. return $s . " }";
  102. }
  103. else {
  104. $esc_val = $this->_formatRequestResEsc($p_result);
  105. $s = "'$esc_val'";
  106. return $s;
  107. }
  108. }
  109. }
  110. ?>