<?php
ob_start();
error_reporting(7);
@set_magic_quotes_runtime(0);
define('R_P', dirname(dirname(__FILE__)));
define('C_P', R_P . '/cache');
define('T_P', R_P . '/templates/');
include(R_P . '/config/options.php');
include(R_P . '/config/config.php');
header("Content-Type: text/html; charset={$_SET['def_charset']}");
define('W_P', $_SET['weburl'].$_SET['webpath']);
define('DEBUG', intval($_SET['debug']) ? true : false);
define('REWRITE', $_SET['rewrite'] ? true : false);
define('AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
define('JSONP', (isset($_GET['jsonp']) && intval($_GET['jsonp'])) ? true : false);
define('CACHE_VERSION', intval($_SET['version']) ? true : false);
define('TABLEPRE', $_SET['tablepre']);
define('QMECMS_CONNECT', 'mysql');
$channel = $usergroup = $_MODULE = array();
$magic_quote = get_magic_quotes_gpc();
require_once(R_P . '/include/class.db.php');
require_once(R_P . '/include/inc.functions.php');
require_once(R_P . '/include/class.cache_'. $_SET['cache'] .'.php');
require_once(R_P . '/cache/config/common.php');
$data = array('status'=>200, 'data'=>array());
parse_str($_POST['query'], $query);
$hash = trim($_POST['hash']);
$sql = stripslashes(trim(urldecode($_POST['sql'])));
$query_verify = qbase::array2string($query);
$hash_verify = substr(md5($query_verify  . $_SET['passport_key'] . $sql), 0, 20);
if($hash_verify == $hash) {
	if($_SERVER['REQUEST_TIME'] - $query['time'] > 8) {
		$data['status'] = 408; // request time out
	}else{
		switch(strtolower($query['act'])){
			case 'query':
				$data['data'] = qdb::query($sql, $query['type']);
			break;
			case 'queryalone':
				$data['data'] = qdb::queryAlone($sql);
			break;
			case 'queryfirst':
				$data['data'] = qdb::queryFirst($sql);
			break;
			case 'fetchall':
				$data['data'] = qdb::fetchAll($sql);
			break;
			default:
				$data['status'] = 400; // Bad Request
			break;
		}
	}
}else{
	$data['status'] = 403; // Forbidden
}
_output($data);
function _output($data){
	echo serialize($data);
}
?>