<?php
class qrpc{
	var $host;
	var $passport_key;
	function qrpc($host, $passport){
		$this->host = $host;
		$this->passport_key = $passport;
	}
 
	function _load($act, $sql, $type = MYSQL_ASSOC){
		$auth = qbase::random(10);
		$param = array(
			'act'=>$act,
			'type'=>$type,
			'auth'=>$auth,
			'time'=>$_SERVER['REQUEST_TIME'],
		);
		$query = qbase::array2string($param);
		$hash = substr(md5($query . $this->passport_key . trim($sql)), 0, 20);
		$data = "query=" . urlencode($query) . "&hash=" . $hash . "&sql=" . urlencode($sql);
		$temp = qbase::load("http://" . $this->host . "/api/server.php", 0, $data);
		$return = @unserialize($temp);
		if(!is_array($return) || !isset($return['status'])) die('something wrong with the system');
		switch($return['status'] ){
			case 200:
				return qbase::sstripslashes($return['data']);
			break;

			case 400:
				die("bad request");
			break;

			case 403:
				die("Forbidden");
			break;

			case 408:
				return array();
			break;
		}
	}

	function queryAlone($QueryString){
		return $this->_load('queryAlone', $QueryString);
	}

	function queryFirst($QueryString, $type = MYSQL_ASSOC)
	{
		return $this->_load('queryFirst', $QueryString, $type);
	}

	function Query($QueryString, $type = false)
	{
		return $this->_load('Query', $QueryString, $type);
	}

	function fetchAll($QueryString, $type = MYSQL_ASSOC)
	{
		return $this->_load('fetchAll', $QueryString, $type);
	}

	function FreeResult($QueryId = -1)
	{
		return;
	}

	function NumRows($QueryId = -1)
	{
		return 0;
	}

	function version()
	{
		return 0;
	}

	function close($LinkId = -1)
	{
		return true;
	}

	function GetErr($msg)
	{
		echo $msg;
	}
}
?>