<?php
/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */
 
class Runner {
	public function __construct() {

	}
	
	final public function start() {
		session_cache_limiter("nocache");
		header ('Content-type: text/html; charset=UTF-8');
		header ("Content-Transfer-Encoding: 8bit");

		if(!empty($_REQUEST["rc"]) && !empty($_REQUEST["rm"])) {
			$p_class	= $_REQUEST["rc"];
			$p_method		= $_REQUEST["rm"];

			if(!empty($_REQUEST["ras"])) 
				$p_args = $_REQUEST["ras"];
			else
				$p_args = array();	
foreach($p_args as $key => $value) {
	$p_args[$key] = utf8_decode($value);
}
			$res = $this->callComp($p_class,$p_method,$p_args);
			if(isset($res) && $res !== false) {
				$res = $this->_formatRequestRes($res);
				echo "+ var res = " . trim($res) . "; res;";
			}
			return;	
		}
	}
	
	final public function stop() {
		exit;
	}

	final public function callComp($p_class, $p_method, $p_args = array()) {
		if(file_exists(ROOT_DIR."apps/".$p_class."/".$p_class.".class.inc")) {
			try {
				$rMethod = new ReflectionMethod($p_class, $p_method);
			} catch(Exception $e) {
				Log::warning(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" couldn't be found.",$p_class,$p_method));
				return false;
			}					
			if($rMethod->isPublic()) {
				$class = call_user_func_array(
						    array(new ReflectionClass($p_class), 'newInstance'),
							array()
						);			
				return $res = call_user_func_array(array($class, $p_method), $p_args);
			} else {
				Log::error(__FILE__,__LINE__,sprintf("Method \"%s->%s('".implode("','",$p_args)."')\" isn't public.",$p_class,$p_method));
				return false;	
			}
		} else {
			Log::error(__FILE__,__LINE__,sprintf("Class \"%s\" isn't a component!",$p_class));
		}
	}
	
	final private function _formatRequestResEsc($val) {
		
		$val = str_replace("\\", "\\\\", $val);		
		$val = str_replace("\r", "\\r", $val);
		//$val = str_replace("\r", "", $val);
		$val = str_replace("\n", "\\n", $val);
		//$val = str_replace("\n", "", $val);
		$val = str_replace("'", "\\'", $val);
		return str_replace('"', '\\"', $val);
	}	
	
	final private function _formatRequestRes($p_result) {
		if (is_bool($p_result)) {
			return ($p_result) ? "Boolean(true)" : "Boolean(false)";
		}
		elseif (is_integer($p_result)) {
			return "parseInt($p_result)";
		}
		elseif (is_double($p_result)) {
			return "parseFloat($p_result)";
		}
		elseif (is_array($p_result) || is_object($p_result)) {
			//
			// XXX Arrays with non-numeric indices are not
			// permitted according to ECMAScript, yet everyone
			// uses them.. We'll use an object.
			//
			$s = "{ ";
			if (is_object($p_result)) {
				$p_result = get_object_vars($p_result);
			}
			foreach ($p_result as $k=>$v) {
				$esc_key = $this->_formatRequestResEsc($k);
				if (is_numeric($k))
					$s .= "$k: " . $this->_formatRequestRes($v) . ", ";
				else
					$s .= "\"$esc_key\": " . $this->_formatRequestRes($v) . ", ";
			}
			if (count($p_result))
				$s = substr($s, 0, -2);
			return $s . " }";
		}
		else {
			$esc_val = $this->_formatRequestResEsc($p_result);
			$s = "'$esc_val'";
			return $s;
		}		
	}
}
?>