<?php
error_reporting(E_ALL ^ E_NOTICE);
define('IN_DISCUZ', true);
header("Content-Type:text/html;charset=gbk");
require_once '../../../config/config_global.php';
require_once '../../../config/config_ucenter.php';
require_once '../../function/function_core.php';
require_once '../../class/class_core.php';
$discuz = & discuz_core::instance();
$discuz->init();
loadcache('plugin');
require_once 'qqrobot.fun.php';
global $_G;

if($_POST)
{
	$key = trim($_POST["key"]);
}
else
{
	$key = trim($_GET["key"]);
}
if($key!=$_G['cache']['plugin']['qqrobot']['key'])
	exit();

if($_POST)
{
	$ExternalId = trim($_POST["ExternalId"]);
	$Sender = trim($_POST["Sender"]);
	$Message = trim($_POST["Message"]);
	if($ExternalId == -1)//加好友
	{
		echo($_G['setting']['bbname'].' 欢迎您的到来，请到'.$_G['setting']['discuzurl'].'/home.php?mod=spacecp&ac=plugin&id=qqrobot:qqrobot 绑定QQ号码，以便有新的回帖通知您的QQ。');
	}
	else if($ExternalId == -6)//震动
	{
		$uid = DB::result_first('select uid from '.DB::table('qqrobot_qq')." where qq=$Sender");
		if($uid)
		{
			$time = TIMESTAMP;
			$sig = md5("$uid#$time#$key");
			echo("请点击下面的链接登陆：\n".$_G['setting']['discuzurl']."/autologin.php?uid=$uid&time=$time&sig=$sig\n本链接有效期30秒");exit();
		}
		echo('[face123.gif]操作失败，您没有绑定QQ或者找不到您的帐号');exit();
	}
	else if($ExternalId >= 0 || $ExternalId<-100)//qq私聊、群聊、讨论组
	{
		if(preg_match('/^([0-9]+)z([0-9a-f]{32})$/', $Message, $result))
		{
			$iuh_user = DB::query("select * from ".DB::table('common_member')." where uid=".$result[1]);
			if($result[2] == md5(md5($iuh_user['salt'])))
			{
				$exists = DB::query("select * from ".DB::table('qqrobot_qq')." where uid=".$result[1]);
				if(!DB::num_rows($exists))
				{
					DB::query("INSERT INTO ".DB::table('qqrobot_qq')." (uid, qq) VALUES (".$result[1].",'$Sender')");
					echo("[face124.gif]绑定成功");
					DB::query("update ".DB::table('common_member_profile')." set qq='$Sender' where uid=".$result[1]);
				}
				else
				{
					echo("[face123.gif]已经绑定成功，无需重复绑定");
				}
			}
		}
		else if($Message == "登陆" || $Message == "login")
		{
			$uid = DB::result_first('select uid from '.DB::table('qqrobot_qq')." where qq=$Sender");
			if($uid)
			{
				$time = TIMESTAMP;
				$sig = md5("$uid#$time#$key");
				echo("请点击下面的链接登陆：\n".$_G['setting']['discuzurl']."/autologin.php?uid=$uid&time=$time&sig=$sig\n本链接有效期30秒");exit();
			}
			echo('[face123.gif]操作失败，您没有绑定QQ或者找不到您的帐号');exit();
		}
		else if($Sender==$Message)
		{
			DB::query("delete from ".DB::table("qqrobot_qq")." where qq=".$Sender);
			echo("[face124.gif]解除绑定成功");
		}
		else if(preg_match('/^心情 (.*?)$/', $Message, $result))
		{
			$uid = DB::result_first('select uid from '.DB::table('qqrobot_qq')." where qq=$Sender");
			if($uid)
			{
				$username = DB::result_first("select username from ".DB::table('common_member')." where uid=$uid");
				if($username)
				{
							$setarr = array(
			'uid' => $uid,
			'username' => $username,
			'dateline' => TIMESTAMP,
			'message' => iconv('utf-8','gb2312',$result[1]),
			'ip' => '',
			'status' => 0,
		);
					DB::insert("home_doing", $setarr);
					echo('[face124.gif]心情发送成功！');exit();
				}
			}
			echo('[face123.gif]操作失败，您没有绑定QQ或者找不到您的帐号');exit();
		}
		else if(preg_match('/^问：(.*?)答：(.*?)$/', $Message, $result))
		{
			DB::insert("qqrobot_qa", array('q' => iconv('utf-8','gb2312',$result[1]), 'a' => iconv('utf-8','gb2312',$result[2])));
			echo("我学会了：$result[1]您现在问我：$result[1]试试看");exit();
		}
		else if(preg_match('/^url (.*?)$/', $Message, $result))
		{
			require_once libfile('function/filesock');
			$html = _dfsockopen("http://api.liqwei.com/shorter/?link=$result[1]");
			echo(strip_tags($html));
		}
		else if(preg_match('/^gp (.*?)$/', $Message, $result))
		{
			require_once libfile('function/filesock');
			$html = _dfsockopen("http://api.liqwei.com/stock/?code=sh:$result[1]");
			$html = iconv('gb2312','utf-8',$html);
			echo(strip_tags($html));
		}
		else if(preg_match('/^密码 (.*?)$/', $Message, $result))
		{
			$uid = DB::result_first('select uid from '.DB::table('qqrobot_qq')." where qq=$Sender");
			if($uid)
			{
				$salt = DB::result_first("select salt from ".DB::table('ucenter_members')." where uid=$uid");
				if($salt)
				{
					DB::query("update ".DB::table('ucenter_members')." set password='".md5(md5($result[1]).$salt)."' where uid=$uid");
					echo('密码修改成功，请牢记新密码！');exit();
				}
			}
			echo('密码修改失败，您没有绑定QQ或者找不到您的帐号');exit();
		}
		else if($Message=='邀请码')
		{
			$uid = DB::result_first('select uid from '.DB::table('qqrobot_qq')." where qq=$Sender");
			if($uid)
			{
				$code = strtolower(random(6));
				$dateline = TIMESTAMP;
				$endtime = $dateline + 1*60*60;
				DB::query("INSERT INTO ".DB::table('common_invite')." (uid, code, dateline, endtime) VALUES ($uid,'$code',$dateline,$endtime)");
				$id = DB::result_first('select id from '.DB::table('common_invite')." where dateline=$dateline order by id desc limit 1");
				if($id)
				{
					echo('您的邀请连接是：'.$_G['setting']['discuzurl']."/home.php?mod=invite&id=$id&c=$code ，有效期1个小时，请通过qq、邮件等方式发送给您的好友\n\n[face79.gif][face79.gif][face79.gif]");exit();
				}
			}
			echo('[face123.gif]您还没有绑定QQ，不能给您发放邀请码。请到'.$_G['setting']['discuzurl'].'/home.php?mod=spacecp&ac=plugin&id=qqrobot:qqrobot 完成绑定后再申请邀请码。');exit();
		}
		else if(preg_match('/^ip (.*?)$/', $Message, $result))
		{
			$p = array('ip'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'GetIP', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^id (.*?)$/', $Message, $result))
		{
			$p = array('number'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'GetUserID', $p);
			echo(strip_tags($result));
		}
		else if($Message=='today')
		{
			$p = array();
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'History', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^m (.*?)$/', $Message, $result))
		{
			$p = array('mobile'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Mobile', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^tq (.*?)$/', $Message, $result))
		{
			$p = array('city'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Weather', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^jt (.*?)$/', $Message, $result))
		{
			$p = array('str'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'StrConvSimplified', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^ft (.*?)$/', $Message, $result))
		{
			$p = array('str'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'StrConvTraditional', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^lt (.*?)$/', $Message, $result))
		{
			echo(strip_tags("http://wpa.qq.com/msgrd?v=3&uin=$result[1]&site=qq&menu=yes"));
		}
		else if(preg_match('/^md5 (.*?)$/', $Message, $result))
		{
			echo(md5($result[1]));
		}
		else if(preg_match('/^qh (.*?)$/', $Message, $result))
		{
			$p = array('str'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Zicode', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^yb (.*?)$/', $Message, $result))
		{
			$p = array('str'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Zicode', $p);
			echo(strip_tags($result));
		}
		else if($Message=='joke')
		{
			$p = array();
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'joke', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^alexa (.*?)$/', $Message, $result))
		{
			$p = array('host'=>$result[1]);
			$html = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'alexa', $p);
			echo('"'.$result[1].'"alexa排名：'.strip_tags($html));
		}
		else if(preg_match('/^wb (.*?)$/', $Message, $result))
		{
			$p = array('str'=>$result[1]);
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'wubi', $p);
			echo(strip_tags($result));
		}
		else if(preg_match('/^((工行)|(icbc)|(工行报价)|(建行)|(ccb)|(建行报价)|(中行)|(boc)|(中行报价))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"工行报价"));
			echo(strip_tags($result."\n本机器人由小灰提供，请联系QQ:4111852"));
		}
		else if(preg_match('/^((国际)|(现货)|(国际报价)|(国际黄金))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"现货黄金"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((白银延期)|(agtd))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"Ag(T+D)"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((白银)|(现货白银))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"现货白银"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((黄金延期)|(autd))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"Au(T+D)"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^(原油)$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"美原油连"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^(美指)$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"美元指数"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((汇率)|(人民币))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"人民币"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((道指)|(道琼指数))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"道琼指数"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((纳指)|(纳斯达克))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"纳斯达克"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((标普)|(标普500))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"标普500"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((上证综指)|(上证指数))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"上证指数"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^(深证成指)$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"深证成指"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((恒生)|(恒生指数))$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"恒生指数"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^(渤海原油)$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"原油"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^(螺纹钢)$/', $Message, $result))
		{
			$result = HttpSoap('web.svnhost.cn', 80, '/webservice.asmx', 'http://tempuri.org/', 'Baojia', array('id'=>"螺纹钢西部"));
			echo(strip_tags($result));
		}
		else if(preg_match('/^((aae)|(anjiekuaidi)|(anxindakuaixi)|(baifudongfang)|(biaojikuaidi)|(bht)|(cces)|(Coe)|(changyuwuliu)|(datianwuliu)|(debangwuliu)|(dpex)|(dhl)|(dsukuaidi)|(fedex)|(feikangda)|(fenghuangkuaidi)|(ganzhongnengda)|(guangdongyouzhengwuliu)|(huitongkuaidi)|(hengluwuliu)|(huaxialongwuliu)|(jiayiwuliu)|(jinguangsudikuaijian)|(jixianda)|(jiajiwuliu)|(jiayunmeiwuliu)|(kuaijiesudi)|(lianhaowuliu)|(longbanwuliu)|(minghangkuaidi)|(peisihuoyunkuaidi)|(quanchenkuaidi)|(quanjitong)|(quanritongkuaidi)|(quanyikuaidi)|(shenghuiwuliu)|(suer)|(shengfengwuliu)|(tiandihuayu)|(tiantian)|(tnt)|(ups)|(wanjiawuliu)|(wenjiesudi)|(wuyuansudi)|(wanxiangwuliu)|(xinbangwuliu)|(xinfengwuliu)|(xingchengjibian)|(xinhongyukuaidi)|(yafengsudi)|(yibangwuliu)|(youshuwuliu)|(yuanchengwuliu)|(yuantong)|(yuanweifeng)|(yuanzhijiecheng)|(yuefengwuliu)|(yunda)|(yuananda)|(Yuntongkuaidi)|(zhaijisong)|(zhongtiewuliu)|(zhongtong)|(zhongyouwuliu)) (.*?)$/', $Message, $result))
		{
			//echo("http://api.kuaidi100.com/api?id=42b8d11f02faa24d&com=$result[1]&nu=".$result[count($result)-1]."&show=3&muti=1&order=desc");
			require_once libfile('function/filesock');
			$html = _dfsockopen("http://api.kuaidi100.com/api?id=42b8d11f02faa24d&com=$result[1]&nu=".$result[count($result)-1]."&show=3&muti=1&order=asc");
			echo(strip_tags($html));
		}
		else if($Message=="kuaidi")
			echo("使用方法，快递公司代码+空格+单号，例如：yuantong 1407157585
快递公司代码  公司名称
aae AAE全球专递
anjiekuaidi 安捷快递
anxindakuaixi 安信达快递
baifudongfang 百福东方
biaojikuaidi 彪记快递
bht BHT
cces 希伊艾斯快递
Coe 中国东方（COE）
changyuwuliu 长宇物流
datianwuliu 大田物流
debangwuliu 德邦物流
dpex DPEX
dhl DHL
dsukuaidi D速快递
fedex fedex
feikangda 飞康达物流
fenghuangkuaidi 凤凰快递
ganzhongnengda 港中能达物流
guangdongyouzhengwuliu 广东邮政物流
huitongkuaidi 汇通快运
hengluwuliu 恒路物流
huaxialongwuliu 华夏龙物流
jiayiwuliu 佳怡物流
jinguangsudikuaijian 京广速递
jixianda 急先达
jiajiwuliu 佳吉物流
jiayunmeiwuliu 加运美
kuaijiesudi 快捷速递
lianhaowuliu 联昊通物流
longbanwuliu 龙邦物流
minghangkuaidi 民航快递
peisihuoyunkuaidi 配思货运
quanchenkuaidi 全晨快递
quanjitong 全际通物流
quanritongkuaidi 全日通快递
quanyikuaidi 全一快递
shenghuiwuliu 盛辉物流
suer 速尔物流
shengfengwuliu 盛丰物流
tiandihuayu 天地华宇
tiantian 天天快递
tnt TNT
ups UPS
wanjiawuliu 万家物流
wenjiesudi 文捷航空速递
wuyuansudi 伍圆速递
wanxiangwuliu 万象物流
xinbangwuliu 新邦物流
xinfengwuliu 信丰物流
xingchengjibian 星晨急便
xinhongyukuaidi 鑫飞鸿物流快递
yafengsudi 亚风速递
yibangwuliu 一邦速递
youshuwuliu 优速物流
yuanchengwuliu 远成物流
yuantong 圆通速递
yuanweifeng 源伟丰快递
yuanzhijiecheng 元智捷诚快递
yuefengwuliu 越丰物流
yunda 韵达快运
yuananda 源安达
Yuntongkuaidi 运通快递
zhaijisong 宅急送
zhongtiewuliu 中铁快运
zhongtong 中通速递
zhongyouwuliu 中邮物流
");


		else
		{
			$tmp = stripsearchkey(iconv('utf-8','gb2312',$Message));
			$query = DB::query("SELECT q,a from ".DB::table('qqrobot_qa')." where q like '%$tmp%' limit 1");
			if(DB::num_rows($query) > 0)
			{
				$result = '';
				while($qa = DB::fetch($query))
					$result .= iconv('gb2312','utf-8',$qa['a']);
				echo($result);
			}
			else
			{
				require('xiaoi.php');   
				$i = new XiaoI();
				$result = $i->chat($Message);
				echo(str_replace("\\r", "\r", $result));
			}
		}

	}
}
else
{
	$send = DB::fetch_first("SELECT * FROM ".DB::table('qqrobot_send').' ORDER BY id limit 0,1');
	if ($send)
	{
		echo($send['content']);
		DB::query("delete from ".DB::table('qqrobot_send')." where id=".$send['id']);
	}
	/*
	*/
}
?>