<?php
include_once('config.php');
include_once('saet.ex.class.php');

$m = new SaeMysql();
$time = time();

if ($_SERVER['REQUEST_METHOD'] == "POST") {
	$ExternalId = trim($_POST['ExternalId']);
	$Sender = trim($_POST['Sender']);
	$Message = trim($_POST['Message']);
	
	if ($ExternalId == 0 && (empty($Sender) || empty($Message))) exit("抱歉，暂不支持处理此类消息");
	$ExternalId = ($ExternalId == 0) ? msgRoute($Message) : $ExternalId;
	
	switch ($ExternalId) {
		case 0:
			if ($userLine = $m->getLine("SELECT * FROM robot_user WHERE qq = '$Sender'")) {
				$oauth_token = $userLine['oauth_token'];
				$oauth_token_secret = $userLine['oauth_token_secret'];
				$state = $userLine['state'];
				$uCode = $userLine['uCode'];
				if ($state) {
					$c = new SaeTClient(WB_AKEY, WB_SKEY, $oauth_token, $oauth_token_secret);
					$backMsg = $c->update($Message);
					if (empty($backMsg['error_code']) && isset($backMsg['id'])) {
						$Message = "您的微博发送成功 id:".$backMsg['id']." 查看此微博\n"."http://api.t.sina.com.cn/".$backMsg['user']['id']."/statuses/".$backMsg['id'];
					} else {
						$Message = "微博发送失败，请尝试重新授权\n".$backMsg['error_code']." ".$backMsg['error'];
					}
				} else {
					$Message = "您还没有绑定微博，请绑定\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
				}
			} else {
				$uCode = uCode();
				$m->runSql("INSERT INTO robot_user (qq, robot, state, uCode, utime, atime) VALUES ('$Sender', '$robot', '0', '$uCode', '$time', '$time')");
				$Message = "您还没有绑定微博，请绑定\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
			}
			break;
		case -1:
			$uCode = uCode();
			if (!$userLine = $m->getLine("SELECT * FROM robot_user WHERE qq = '$Sender'")) {
				$m->runSql("INSERT INTO robot_user (qq, robot, state, uCode, utime, atime) VALUES ('$Sender', '$robot', '0', '$uCode', '$time', '$time')");
				$Message = $Sender." 欢迎您! 您还没有绑定微博，请绑定\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
			} else {
				$Message = $Sender." 欢迎您! 您需要重新绑定微博\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
			}
			break;
		case -3:
			if ($userLine = $m->getLine("SELECT * FROM robot_user WHERE qq = '$Sender'")) {
				$oauth_token = $userLine['oauth_token'];
				$oauth_token_secret = $userLine['oauth_token_secret'];
				$state = $userLine['state'];
				$uCode = $userLine['uCode'];
				if ($state) {
					$c = new SaeTClient(WB_AKEY, WB_SKEY, $oauth_token, $oauth_token_secret);
					$backMsg = $c->update($Message);
					if (empty($backMsg['error_code']) && isset($backMsg['id'])) {
						$Message = "您的签名同步成功 id:".$backMsg['id']." 查看此微博\n"."http://api.t.sina.com.cn/".$backMsg['user']['id']."/statuses/".$backMsg['id'];
					} else {
						$Message = "您的签名同步失败，请尝试重新授权\n".$backMsg['error_code']." ".$backMsg['error'];
					}
				} else {
					$Message = "您还没有绑定微博，请绑定\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
				}
			} else {
				$uCode = uCode();
				$m->runSql("INSERT INTO robot_user (qq, robot, state, uCode, utime, atime) VALUES ('$Sender', '$robot', '0', '$uCode', '$time', '$time')");
				$Message = "您还没有绑定微博，请绑定\n".$siteUrl."oauth.php?qq=".$Sender."&uc=".$uCode;
			}
			break;
		case -6:
			$Message = "Hi，我是QQ机器人，查看帮助\n".$siteUrl;
			break;
		case -101:
			$Message = "Hi，我是QQ机器人，查看帮助\n".$siteUrl;
			break;
		case -102:
			$Message = "QQ机器人目前还不支持图片处理哦！";
			break;
		default:
			if ($ExternalId > 10000) exit;
	}
	echo "$Message";
}

if ($_SERVER['REQUEST_METHOD'] == "GET") {
	//echo("0\n12345678\nmessage");
}

function msgRoute($message) {
	$message = str_replace("", "(本消息由您的好友通过手机QQ发送，体验手机QQ请登录： http://mobile.qq.com/c )", $message);
	$message = str_replace("", "(通过iPhone QQ发送，详情请访问： http://apple.qq.com/ )", $message);

	$help = array("?","??","???","/?","/??","/???","h","help","/h","/help","？","？？","？？？","/？","/？？","/？？？","bz","/bz","/帮助",);

	if (in_array($message, $help)) 		return -101;
	if ($message == "[image]") 			return -102;
	return 0;
}

function uCode ($strlen = 10) {
	$str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	srand((double)microtime()*1000000);
	for($i = 0; $i < $strlen; $i++) {
		$uCode .= substr($str,(rand() % (strlen($str))), 1);
	}
	return $uCode;
}
?>