<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller
{
	public function sz()
	{
		$user = $this->islogin();
		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if (!$qid || !$row = m("qqs")->field("*")->where("qid='$qid' and uid='{$user['uid']}'")->find()) {
			exit("<script language='javascript'>alert('QQ不存在');history.go(-1);</script>");
		}

		$this->assign("cell", $_GET["cell"]);
		$list = m("qqs")->field("*")->where("sidzt=0")->order("qid desc")->select();
		$this->assign("qqrow", $row);
		$this->assign("liked", 0);
		$this->assign("arr", $list);
		$this->display($this->mb . "/sz");
	}

	public function changmb()
	{
		if ($_GET["mb"] == "web") {
			setcookie("klmz_mb", "web", time() + (3600 * 24 * 2), "/");
		}
		else if ($_GET["mb"] == "wap") {
			setcookie("klmz_mb", "wap", time() + (3600 * 24 * 2), "/");
		}
		else if ($_GET["mb"] == "html5") {
			setcookie("klmz_mb", "html5", time() + (3600 * 24 * 2), "/");
		}

		@header("Location:/");
	}

	public function dx()
	{
		$user = $this->islogin();
		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if (!$qid || !$row = m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			exit("<script language='javascript'>alert('QQ不存在');history.go(-1);</script>");
		}

		if (!get_isvip($user["vip"], $user["vipend"])) {
			$this->get_exit("对不起，此功能仅VIP能使用");
		}

		if ($_GET["do"] == "clear") {
			unset($_SESSION["o" . $row[qq]]);
			exit("<script language='javascript'>alert('检测数据已清楚！');window.location.href=\"" . u("dx") . "?qid=" . $qid . "\";</script>");
		}

		$url = "http://m.qzone.com/friend/mfriend_list?res_uin=$row[qq]&res_type=normal&format=json&count_per_page=10&page_index=0&page_type=0&mayknowuin=&qqmailstat=&sid={$row['sid']}";
		$json = file_get_contents($url);
		$arr = json_decode($json, true);

		if ($arr["code"] == -3000) {
			exit("<script language='javascript'>alert('SID已过期');history.go(-1);</script>");
		}

		$this->assign("qqrow", $row);
		$this->assign("hydx", 0);
		$this->assign("arr", $arr["data"]["list"]);
		$this->display("wap/dx");
	}

	public function userinfo()
	{
		$user = $this->islogin();

		if ($_POST["submit"]) {
			if ($pwd = $_POST["pwd"]) {
				if (strlen($pwd) < 5) {
					exit("<script language='javascript'>alert('新密码太简单！');history.go(-1);</script>");
				}

				$pwd = md5(md5($pwd) . md5("815856515"));
				$data["pwd"] = $pwd;
			}

			$phone = i("post.phone", "", "get_safe_str");

			if (m("users")->field("uid")->where("phone='$phone' and uid != '{$user["uid"]}'")->find()) {
				exit("<script language='javascript'>alert('此手机号已绑定其他用户！');history.go(-1);</script>");
			}

			$data["phone"] = $phone;
			m("users")->where("uid='{$user["uid"]}'")->save($data);
			$msg = "修改成功，<a href='" . u("user") . "'>返回服务中心</a>";
		}

		$this->assign("msg", $msg);
		$this->display($this->mb . "/userinfo");
	}

	public function logout()
	{
		if ($user = $this->islogin()) {
			$newsid = md5(uniqid() . rand(1, 1000));
			m("users")->where("uid='" . $user[uid] . "'")->setField("sid", $newsid);
		}

		setcookie("xiha_sid", "", -1, "/");
		@header("Location:/");
	}

	public function add()
	{
		$user = $this->islogin();
		$this->display($this->mb . "/add");
	}

	public function shop()
	{
		$user = $this->islogin();

		if ($do = $_POST["do"]) {
			$km = i("post.km", "", "get_safe_str");

			if ($do == "peie") {
				if ($km == "freeiwyd") {
					if ($user["peie"]) {
						exit("<script language='javascript'>alert('你已经拥有配额了');history.go(-1);</script>");
					}
					else {
						m("users")->where("uid='{$user["uid"]}'")->setInc("peie", 1);
						echo "<script language='javascript'>alert('成功增加1个配额');</script>";
					}
				}
				else if (!$row = m("kms")->field("*")->where("km='$km' and kind=1")->find()) {
					exit("<script language='javascript'>alert('配额卡卡密不存在');history.go(-1);</script>");
				}
				else if ($row[isuse]) {
					exit("<script language='javascript'>alert('此增加配额卡密已经被使用');history.go(-1);</script>");
				}
				else {
					$data["isuse"] = 1;
					$data["uid"] = $user[uid];
					$data["usetime"] = date("Y-m-d H:i:s");
					m("kms")->where("kid='{$row["kid"]}'")->save($data);
					m("users")->where("uid='{$user["uid"]}'")->setInc("peie", $row["ms"]);
					echo "<script language='javascript'>alert('成功增加{$row["ms"]}个配额');</script>";
				}
			}
			else if (!$row = m("kms")->field("*")->where("km='$km' and (kind=0 or kind=2)")->find()) {
				exit("<script language='javascript'>alert('VIP卡卡密不存在');history.go(-1);</script>");
			}
			else if ($row[isuse]) {
				exit("<script language='javascript'>alert('此VIP卡卡密已经被使用');history.go(-1);</script>");
			}
			else {
				if (get_isvip($user["vip"], $user["vipend"]) && ($row["kind"] == 2)) {
					exit("<script language='javascript'>alert('对不起，你已经是会员，不能再使用试用卡！');history.go(-1);</script>");
				}
				else {
					$data["isuse"] = 1;
					$data["uid"] = $user[uid];
					$data["usetime"] = date("Y-m-d H:i:s");
					m("kms")->where("kid='{$row["kid"]}'")->save($data);

					if ($row["kind"] == 2) {
						$udata["vip"] = 1;
						$udata["vipstart"] = date("Y-m-d");
						$udata["vipend"] = date("Y-m-d", strtotime("+ {$row["ms"]} days"));
						m("users")->where("uid='{$user["uid"]}'")->save($udata);
						echo "<script language='javascript'>alert('成功开通$row[ms]天VIP，你的VIP到期日期:{$udata['vipend']}');</script>";
					}
					else if (get_isvip($user["vip"], $user["vipend"])) {
						$vipend = date("Y-m-d", strtotime("+ {$row["ms"]} months", strtotime($user[vipend])));
						m("users")->where("uid='{$user["uid"]}'")->setField("vipend", $vipend);
						$msg = "成功续费" . $row[ms] . "个月VIP，你的VIP到期日期:" . $vipend;
						echo "<script language='javascript'>alert('成功续费$row[ms]个月VIP，你的VIP到期日期:$vipend');</script>";
					}
					else {
						$udata["vip"] = 1;
						$udata["vipstart"] = date("Y-m-d");
						$udata["vipend"] = date("Y-m-d", strtotime("+ {$row["ms"]} months"));
						m("users")->where("uid='{$user["uid"]}'")->save($udata);
						echo "<script language='javascript'>alert('成功开通$row[ms]个月VIP，你的VIP到期日期:{$udata["vipend"]}');</script>";
					}
				}
			}
		}

		$this->display($this->mb . "/shop");
	}

	public function setlook()
	{
		$user = $this->islogin();
		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if (!$qid || !$row = m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			exit("<script language='javascript'>alert('QQ不存在');history.go(-1);</script>");
		}

		$this->assign("do", "look");
		$this->assign("msg", $msg);
		$this->assign("qqrow", $row);
		$this->display($this->mb . "/setlook");
	}

	public function get_exit($msg, $do = 0)
	{
		if ($_POST["ajax"]) {
			exit($msg);
		}
		else if ($do) {
			echo "<script language='javascript'>alert('$msg');</script>";
		}
		else {
			exit("<script language='javascript'>alert('$msg');history.go(-1);</script>");
		}
	}

	public function qqset()
	{
		$user = $this->islogin();
		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if (!$qid || !$row = m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			$this->get_exit("QQ不存在");
		}

		if ($do = $_POST["do"]) {
			$is = (is_numeric($_POST["is"]) ? $_POST["is"] : "0");
			$net = (is_numeric($_POST["net"]) ? $_POST["net"] : "0");

			if ($do == "sid") {
				$sid = i("post.sid", "", "get_safe_str");
				$skey = i("post.skey", "", "get_safe_str");

				if (!preg_match("/^.{24}$/", $sid)) {
					$this->get_exit("SID格式不正确");
					exit("<script language='javascript'>alert('SID格式不正确');history.go(-1);</script>");
				}
				else if (!preg_match("/^\@[0-9a-zA-Z]{9}$/", $skey)) {
					$this->get_exit("SKEY格式不正确");
				}
				else {
					$data["sid"] = $sid;
					$data["skey"] = $skey;
					$data["sidzt"] = 0;
					$data["skeyzt"] = 0;

					if ($row["iszan"]) {
						$data["iszan"] = 2;
					}

					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("SID/SKEY更新成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
			}
			else if ($do == "3gqq") {
				$data["is3gqq"] = $is;

				if ($is) {
					$data["3gqqnet"] = $net;
				}

				if ($is && !$net) {
					$this->get_exit("请选择个合适的服务器");
				}
				else {
					if ((c("free3gqq") < $net) && !get_isvip($user["vip"], $user["vipend"])) {
						$this->get_exit("对不起，此服务器仅VIP能使用");
					}
					else {
						if ($net && ($row["3gqqnet"] != $net) && (c("netnum") <= get_net_count("3gqq", $net))) {
							$this->get_exit("$net号服务器已满");
						}
						else {
							if (!$is) {
								$data["3gqqnet"] = 0;
							}

							m("qqs")->where("qid='$qid'")->save($data);
							$this->get_exit("挂扣配置修改成功", 1);
							$row = m("qqs")->field("*")->where("qid='$qid'")->find();
						}
					}
				}
			}
			else if ($do == "reply") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$is = (is_numeric($_POST["is"]) ? $_POST["is"] : "0");
					$con = i("post.content", "", "get_safe_str");
					$data["isreply"] = $is;

					if ($is) {
						$data["replycon"] = $con;
						$rate = (is_numeric($_POST["rate"]) ? $_POST["rate"] : "0");
						$data["replyrate"] = $rate;
						$data["replynet"] = $net;
					}

					if ($is && !$net) {
						$this->get_exit("请选择个合适的服务器");
					}
					else {
						if ($net && ($row["replynet"] != $net) && (c("netnum") <= get_net_count("reply", $net))) {
							$this->get_exit("$net号服务器已满");
						}
						else {
							if (!$is) {
								$data["replynet"] = 0;
							}

							m("qqs")->where("qid='$qid'")->save($data);
							$this->get_exit("秒评配置修改成功", 1);
							$row = m("qqs")->field("*")->where("qid='$qid'")->find();
						}
					}
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "zf") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$is = (is_numeric($_POST["is"]) ? $_POST["is"] : "0");
					$con = i("post.content", "", "get_safe_str");
					$data["iszf"] = $is;

					if ($is) {
						$data["zfcon"] = $con;
						$data["zfok"] = i("post.zfok", "", "get_safe_str");
						$rate = (is_numeric($_POST["rate"]) ? $_POST["rate"] : "0");
						$data["zfrate"] = $rate;
						$data["zfnet"] = $net;
					}

					if ($is && !$net) {
						$this->get_exit("请选择个合适的服务器");
					}
					else {
						if ($net && ($row["zfnet"] != $net) && (c("netnum") <= get_net_count("zf", $net))) {
							$this->get_exit("$net号服务器已满");
						}
						else {
							if (!$is) {
								$data["zfnet"] = 0;
							}

							m("qqs")->where("qid='$qid'")->save($data);
							$this->get_exit("转发说说配置修改成功", 1);
							$row = m("qqs")->field("*")->where("qid='$qid'")->find();
						}
					}
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "zan") {
				$data["iszan"] = $is;

				if ($is) {
					$rate = (is_numeric($_POST["rate"]) ? $_POST["rate"] : "0");
					$data["zanrate"] = $rate;
					$data["zannet"] = $net;
				}

				if ((0 < $is) && ($rate < 4) && !get_isvip($user["vip"], $user["vipend"])) {
					$this->get_exit("对不起，仅VIP能选择频率低于4分钟！");
				}
				else {
					if ($is && !$net) {
						$this->get_exit("请选择个合适的服务器");
					}
					else {
						if ((c("freezan") < $net) && !get_isvip($user["vip"], $user["vipend"])) {
							$this->get_exit("对不起，此服务器仅VIP能使用！");
						}
						else {
							if ($net && ($row["zannet"] != $net) && (c("netnum") <= get_net_count("zan", $net))) {
								$this->get_exit("$net号服务器已满");
							}
							else {
								if (!$is) {
									$data["zannet"] = 0;
								}

								m("qqs")->where("qid='$qid'")->save($data);
								$this->get_exit("秒赞配置修改成功", 1);
								$row = m("qqs")->field("*")->where("qid='$qid'")->find();
							}
						}
					}
				}
			}
			else if ($do == "shuo") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$type = (is_numeric($_POST["type"]) ? $_POST["type"] : "0");
					$con = i("post.content", "", "get_safe_str");
					$url = i("post.shuopic", "", "get_safe_str");
					$phone = i("post.shuophone", "", "get_safe_str");
					$data["isshuo"] = $is;

					if ($is) {
						$data["shuoshuo"] = $con;
						$data["shuopic"] = $url;
						$data["shuophone"] = $phone;
						$rate = (is_numeric($_POST["rate"]) ? $_POST["rate"] : "1");
						$data["shuorate"] = $rate;
						$data["shuonet"] = $net;
					}

					if (($type == 1) && !$url) {
						$this->get_exit("图文说说，图片地址不能为空！");
					}
					else {
						if ($is && !$net) {
							$this->get_exit("请选择个合适的服务器");
						}
						else {
							if ($net && ($row["shuonet"] != $net) && (c("netnum") <= get_net_count("shuo", $net))) {
								$this->get_exit("$net号服务器已满");
							}
							else {
								if (!$is) {
									$data["shuonet"] = 0;
								}

								m("qqs")->where("qid='$qid'")->save($data);
								$this->get_exit("自动说说配置修改成功", 1);
								$row = m("qqs")->field("*")->where("qid='$qid'")->find();
							}
						}
					}
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "del") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["isdel"] = $is;

					if ($is) {
						$data["delnet"] = $net;
					}

					if ($is && !$net) {
						$this->get_exit("请选择个合适的服务器");
					}
					else {
						if ($net && ($row["delnet"] != $net) && (c("netnum") <= get_net_count("del", $net))) {
							$this->get_exit("$net号服务器已满");
						}
						else {
							if (!$is) {
								$data["delnet"] = 0;
							}

							m("qqs")->where("qid='$qid'")->save($data);
							$this->get_exit("自动删除说说配置修改成功", 1);
							$row = m("qqs")->field("*")->where("qid='$qid'")->find();
						}
					}
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "qd") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$con = i("post.content", "", "get_safe_str");
					$data["isqd"] = $is;

					if ($is) {
						$data["qdcon"] = $con;
						$data["qdnet"] = $net;
					}

					if ($is && !$net) {
						$this->get_exit("请选择个合适的服务器");
					}
					else {
						if ($net && ($row["qdnet"] != $net) && (c("netnum") <= get_net_count("qd", $net))) {
							$this->get_exit("$net号服务器已满");
						}
						else {
							if (!$is) {
								$data["qdnet"] = 0;
							}

							m("qqs")->where("qid='$qid'")->save($data);
							$this->get_exit("自动签到配置修改成功", 1);
							$row = m("qqs")->field("*")->where("qid='$qid'")->find();
						}
					}
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "upload") {
				if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png")) {
					if (0 < $_FILES["file"]["error"]) {
						exit("<script language='javascript'>alert('图片上传失败。" . $_FILES["file"]["error"] . "');history.go(-1);</script>");
					}
					else {
						$image = file_get_contents($_FILES["file"]["tmp_name"]);
						$this->uin = $row["qq"];
						$this->sid = $row["sid"];

						if ($uploadimg = $this->uploadimg($image)) {
							echo "<script language='javascript'>alert('图片上传成功，请点击保存！');</script>";
						}
						else {
							exit("<script language='javascript'>alert('上传QQ空间失败！" . $qzone->error[0] . "');history.go(-1);</script>");
						}
					}
				}
				else {
					exit("<script language='javascript'>alert('图片格式不正确！');history.go(-1);</script>");
				}
			}
			else if ($do == "ly") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["isly"] = $is;
					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("互刷留言配置修改成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "zyzan") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["iszyzan"] = $is;
					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("互刷主页赞配置修改成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "ht") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["isht"] = $is;
					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("花藤服务配置修改成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "vipqd") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["isvipqd"] = $is;
					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("会员签到配置修改成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
			else if ($do == "ts") {
				if (get_isvip($user["vip"], $user["vipend"])) {
					$data["ists"] = $is;
					m("qqs")->where("qid='$qid'")->save($data);
					$this->get_exit("图书签到配置修改成功", 1);
					$row = m("qqs")->field("*")->where("qid='$qid'")->find();
				}
				else {
					$this->get_exit("对不起，此功能仅VIP能使用");
				}
			}
		}

		if ($uploadimg) {
			$row["shuopic"] = $uploadimg;
		}

		if ($_GET["sid"]) {
			$row["sid"] = $_GET["sid"];
		}

		if ($_GET["skey"]) {
			$row["skey"] = $_GET["skey"];
		}

		$row = $this->getzt($row);
		$this->assign("qqrow", $row);
		$this->display($this->mb . "/qqset");
	}

	public function uploadimg($image, $image_size = array())
	{
		$url = "http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2";
		$post = "picture=" . urlencode(base64_encode($image)) . "&base64=1&hd_height=" . $image_size[1] . "&hd_width=" . $image_size[0] . "&hd_quality=90&output_type=json&preupload=1&charset=utf-8&output_charset=utf-8&logintype=sid&Exif_CameraMaker=&Exif_CameraModel=&Exif_Time=&uin=" . $this->uin . "&sid=" . $this->sid;
		$data = preg_replace("/\s/", "", $this->get_curl($url, $post));
		preg_match("/_Callback\((.*)\);/", $data, $arr);
		$data = json_decode($arr[1], true);
		if ($data && array_key_exists("filemd5", $data)) {
			$this->msg[] = "图片上传成功！";
			$post = "output_type=json&preupload=2&md5=" . $data["filemd5"] . "&filelen=" . $data["filelen"] . "&batchid=" . time() . rand(100000, 999999) . "&currnum=0&uploadNum=1&uploadtime=" . time() . "&uploadtype=1&upload_hd=0&albumtype=7&big_style=1&op_src=15003&charset=utf-8&output_charset=utf-8&uin=" . $this->uin . "&sid=" . $this->sid . "&logintype=sid&refer=shuoshuo";
			$img = preg_replace("/\s/", "", $this->get_curl($url, $post));
			preg_match("/_Callback\(\[(.*)\]\);/", $img, $arr);
			$data = json_decode($arr[1], true);
			if ($data && array_key_exists("picinfo", $data)) {
				if ($data[picinfo][albumid] != "") {
					$this->msg[] = "图片信息获取成功！";
					return "{$data['picinfo']['albumid']},{$data['picinfo']['lloc']},{$data['picinfo']['sloc']},{$data['picinfo']['type']},{$data['picinfo']['height']},{$data['picinfo']['width']},,,";
				}
				else {
					$this->msg[] = "图片信息获取失败！";
					return NULL;
				}
			}
			else {
				$this->error[] = "图片信息获取失败！";
				return NULL;
			}
		}
		else {
			$this->error[] = "图片上传失败！原因：" . $data["msg"];
			return NULL;
		}
	}

	public function reg()
	{
		$user = i("post.user", "", "get_safe_str");
		$pwd = i("post.pwd", "", "get_safe_str");
		$mail = i("post.mail", "", "get_safe_str");
		if ($_POST["code"] && (strtolower($_SESSION["xiha_code"]) != strtolower($_POST["code"]))) {
			exit("<script language='javascript'>alert('验证码错误');history.go(-1);</script>");
		}

		if (!preg_match("/^[a-z0-9][a-z0-9\.\_\-]+@[a-z0-9]+\.[a-z]{2,4}$/i", $mail)) {
			exit("<script language='javascript'>alert('邮箱地址不正确');history.go(-1);</script>");
		}
		else if (m("users")->field("uid")->where("mail='$mail'")->find()) {
			exit("<script language='javascript'>alert('此邮箱已经注册过');history.go(-1);</script>");
		}
		else if (m("users")->field("uid")->where("user='$user'")->find()) {
			exit("<script language='javascript'>alert('用户名已存在！');history.go(-1);</script>");
		}
		else if (strlen($pwd) < 5) {
			exit("<script language='javascript'>alert('密码太简单！');history.go(-1);</script>");
		}
		else {
			$_SESSION["xiha_code"] = "";
			$userip = get_client_ip(0, true);
			$now = date("Y-m-d H:i:s");
			$data[user] = $user;
			$data[pwd] = md5(md5($pwd) . md5("815856515"));
			$data["sid"] = md5(uniqid() . rand(1, 1000));
			$data[active] = c("regactive");
			$data[peie] = c("regpeie");
			$data[mail] = $mail;
			$data[city] = get_ip_city($userip);
			$data[regip] = $userip;
			$data[lastip] = $userip;
			$data[regtime] = $now;
			$data[lasttime] = $now;

			if (m("users")->data($data)->add()) {
				$row = m("users")->field("*")->where("user='$user' and pwd='{$data["pwd"]}'")->find();
				$code = md5(uniqid() . rand(1, 1000));
				$data1[uid] = $row[uid];
				$data1[mail] = $row[mail];
				$data1[code] = $code;
				$data1[endtime] = date("Y-m-d H:i:s", strtotime("+2 day"));
				$data1[addtime] = $now;

				if (!c("regactive")) {
					m("mailcode")->data($data1)->add();
					$this->send_active_mail($row[mail], $code, $row[user]);
					exit("<script language='javascript'>alert('注册成功，激活邮件已经发送！请到邮箱查看激活连接！如果没有收到邮件，你可以用刚注册的账号登录一次，将会再次发送激活哦邮件！');history.go(-1);</script>");
				}
				else {
					exit("<script language='javascript'>alert('注册成功，返回登陆');window.location.href('{:U('login')}');</script>");
				}
			}
			else {
				exit("<script language='javascript'>alert('保存数据库失败！');history.go(-1);</script>");
			}
		}
	}

	public function login()
	{
		if ($_POST["do"] == "login") {
			$user = i("post.user", "", "get_safe_str");
			$pwd = i("post.pwd", "", "get_safe_str");
			$pwd = md5(md5($pwd) . md5("815856515"));

			if (is_numeric($user)) {
				$where = "(user='$user' or phone='$user') and pwd='$pwd'";
			}
			else {
				$where = "(user='$user' or mail='$user') and pwd='$pwd'";
			}

			if (!$row = m("users")->field("*")->where($where)->find()) {
				exit("<script language='javascript'>alert('用户名/手机号/邮箱与密码不匹配');history.go(-1);</script>");
			}
			else if (!$row[login]) {
				exit("<script language='javascript'>alert('你已经被限制登陆，请联系管理员！');history.go(-1);</script>");
			}
			else if (!$row[active]) {
				if ($coderow = m("mailcode")->field("code")->where("uid='{$row["uid"]}'")->find()) {
					$code = $coderow["code"];
				}
				else {
					$code = md5(uniqid() . rand(1, 1000));
					$data[uid] = $row[uid];
					$data[mail] = $row[mail];
					$data[code] = $code;
					$data[endtime] = date("Y-m-d H:i:s", strtotime("+2 day"));
					$data[addtime] = date("Y-m-d H:i:s");
					m("mailcode")->data($data)->add();
				}

				$this->send_active_mail($row[mail], $code, $row[user]);
				exit("<script language='javascript'>alert('账号没激活，激活邮件已发送到$row[mail]，请尽快激活！');history.go(-1);</script>");
			}
			else {
				$newsid = md5(uniqid() . rand(1, 1000));
				$data["sid"] = $newsid;
				$data["lastip"] = get_client_ip(0, true);
				$data["lasttime"] = date("Y-m-d H:i:s");
				m("users")->where("uid='{$row["uid"]}'")->save($data);

				if ($check) {
					setcookie("xiha_sid", $newsid, time() + (3600 * 24 * 14), "/");
				}
				else {
					setcookie("xiha_sid", $newsid, time() + (3600 * 24 * 2), "/");
				}

				exit("<script language='javascript'>alert('登陆成功!');window.location.href='/';</script>");
			}
		}
		else if ($_POST["do"] == "reg") {
			$user = i("post.user", "", "get_safe_str");
			$pwd = i("post.pwd", "", "get_safe_str");
			$mail = i("post.mail", "", "get_safe_str");
			if (!$_POST["code"] || (strtolower($_SESSION["xiha_code"]) != strtolower($_POST["code"]))) {
				exit("<script language='javascript'>alert('验证码错误');history.go(-1);</script>");
			}

			if (!preg_match("/^[a-z0-9][a-z0-9\.\_\-]+@[a-z0-9]+\.[a-z]{2,4}$/i", $mail)) {
				exit("<script language='javascript'>alert('邮箱地址不正确');history.go(-1);</script>");
			}
			else if (m("users")->field("uid")->where("mail='$mail'")->find()) {
				exit("<script language='javascript'>alert('此邮箱已经注册过');history.go(-1);</script>");
			}
			else if (m("users")->field("uid")->where("user='$user'")->find()) {
				exit("<script language='javascript'>alert('用户名已存在！');history.go(-1);</script>");
			}
			else if (strlen($pwd) < 5) {
				exit("<script language='javascript'>alert('密码太简单！');history.go(-1);</script>");
			}
			else {
				$_SESSION["xiha_code"] = "";
				$userip = get_client_ip(0, true);
				$now = date("Y-m-d H:i:s");
				$data[user] = $user;
				$data[pwd] = md5(md5($pwd) . md5("815856515"));
				$data["sid"] = md5(uniqid() . rand(1, 1000));
				$data[active] = c("regactive");
				$data[peie] = c("regpeie");
				$data[mail] = $mail;
				$data[city] = get_ip_city($userip);
				$data[regip] = $userip;
				$data[lastip] = $userip;
				$data[regtime] = $now;
				$data[lasttime] = $now;

				if (m("users")->data($data)->add()) {
					$row = m("users")->field("*")->where("user='$user' and pwd='{$data["pwd"]}'")->find();
					$code = md5(uniqid() . rand(1, 1000));
					$data1[uid] = $row[uid];
					$data1[mail] = $row[mail];
					$data1[code] = $code;
					$data1[endtime] = date("Y-m-d H:i:s", strtotime("+2 day"));
					$data1[addtime] = $now;

					if (!c("regactive")) {
						m("mailcode")->data($data1)->add();
						$this->send_active_mail($row[mail], $code, $row[user]);
						exit("<script language='javascript'>alert('注册成功，激活邮件已经发送！请到邮箱查看激活连接！如果没有收到邮件，你可以用刚注册的账号登录一次，将会再次发送激活哦邮件！');history.go(-1);</script>");
					}
					else {
						exit("<script language='javascript'>alert('注册成功，返回登陆');history.go(-1);</script>");
					}
				}
				else {
					exit("<script language='javascript'>alert('保存数据库失败！');history.go(-1);</script>");
				}
			}
		}

		$this->display("web/login");
	}

	public function newpwd()
	{
		$now = date("Y-m-d H:i:s");
		$code = i("get.code", "", "get_safe_str");

		if (!$row = m("mailcode")->field("*")->where("code='$code' and kind=1 and endtime>'$now'")->find()) {
			@header("HTTP/1.1 301 Moved Permanently");
			@header("Location:/");
		}
		else if (!$user = m("users")->field("uid")->where("uid='{$row["uid"]}'")->find()) {
			@header("HTTP/1.1 301 Moved Permanently");
			@header("Location:/");
		}

		if ($_POST["do"] == "setpwd") {
			$pwd = i("post.pwd", "", "get_safe_str");
			$repwd = i("post.repwd", "", "get_safe_str");

			if (strlen($pwd) < 5) {
				exit("<script language='javascript'>alert('密码太简单！');history.go(-1);</script>");
			}
			else if ($pwd != $repwd) {
				exit("<script language='javascript'>alert('两次密码不一致！');history.go(-1);</script>");
			}

			$pwd = md5(md5($pwd) . md5("815856515"));
			m("users")->where("uid='{$user["uid"]}'")->setField("pwd", $pwd);
			exit("<script language='javascript'>alert('密码设置成功,马上登录！');window.location.href='" . u("login") . "';</script>");
		}

		$this->assign("code", $code);
		$this->display("web/newpwd");
	}

	public function findpwd()
	{
		if ($_POST["do"] == "findpwd") {
			$mail = i("post.mail", "", "get_safe_str");
			if ($_POST["code"] && (strtolower($_SESSION["xiha_code"]) != strtolower($_POST["code"]))) {
				exit("<script language='javascript'>alert('验证码错误');history.go(-1);</script>");
			}

			if (!preg_match("/^[a-z0-9][a-z0-9\.\_\-]+@[a-z0-9]+\.[a-z]{2,4}$/i", $mail)) {
				exit("<script language='javascript'>alert('邮箱地址不正确');history.go(-1);</script>");
			}
			else if (!$user = m("users")->field("*")->where("mail='$mail'")->find()) {
				exit("<script language='javascript'>alert('此邮箱不存在');history.go(-1);</script>");
			}
			else {
				$_SESSION["xiha_code"] = "";
				$code = md5(uniqid() . rand(1, 1000));
				$data[kind] = 1;
				$data[uid] = $user[uid];
				$data[mail] = $user[mail];
				$data[code] = $code;
				$data[endtime] = date("Y-m-d H:i:s", strtotime("+2 day"));
				$data[addtime] = date("Y-m-d H:i:s");
				m("mailcode")->data($data)->add();
				$this->send_active_mail($user[mail], $code, $user[user], "findpwd");
				exit("<script language='javascript'>alert('重置密码链接已经发送至$user[mail]！请到邮箱查看连接，重设密码！');history.go(-1);</script>");
			}
		}

		$this->display("web/findpwd");
	}

	public function user()
	{
		$user = $this->islogin();
		if ($_POST["submit"] && ($_GET["do"] == "phone")) {
			if (!preg_match("/^(1(([35][0-9])|(47)|[8][0126789]))\d{8}$/i", $_POST["phone"])) {
				echo "<script language='javascript'>alert('手机号码格式不正确！');</script>";
			}
			else {
				$phone = i("post.phone", "", "get_safe_str");

				if (m("users")->field("uid")->where("phone='$phone' and uid != '{$user["uid"]}'")->find()) {
					echo "<script language='javascript'>alert('此手机号已绑定其他用户！');</script>";
				}
				else {
					m("users")->where("uid='{$user["uid"]}'")->setField("phone", $phone);
					echo "<script language='javascript'>alert('手机号码修改成功！');</script>";
					$user = $this->islogin();
				}
			}
		}

		if ($_POST["submit"] && ($_GET["do"] == "pwd")) {
			$pwd = md5(md5($_POST["pwd"]) . md5("815856515"));

			if ($user["pwd"] != $pwd) {
				echo "<script language='javascript'>alert('原密码不正确！');</script>";
			}
			else {
				$phone = $_POST["phone"];

				if (strlen($_POST["npwd"]) < 5) {
					echo "<script language='javascript'>alert('新密码长度不能小于5！');</script>";
				}
				else {
					$npwd = md5(md5($_POST["npwd"]) . md5("815856515"));
					m("users")->where("uid='{$user["uid"]}'")->setField("pwd", $npwd);
					echo "<script language='javascript'>alert('密码修改成功！');</script>";
				}
			}
		}

		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if ($qid && m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			if ($_GET["do"] == "del") {
				m("qqs")->where("qid='$qid'")->delete();
			}
			else if ($_GET["do"] == "zanoff") {
				m("qqs")->where("qid='$qid'")->setField("iszan", 0);
			}
			else if ($_GET["do"] == "zanon") {
				m("qqs")->where("qid='$qid'")->setField("iszan", 1);
			}
		}

		$user["qqcount"] = m("qqs")->where("uid='{$user["uid"]}'")->count("qid");
		$rows = m("qqs")->field("*")->where("uid='{$user["uid"]}'")->order("qid desc")->select();
		$this->assign("list", $rows);
		$this->display($this->mb . "/user");
	}

	public function getzt($row)
	{
		if ($row) {
			if ($row["sidzt"] || $row["skeyzt"]) {
				$row["isgx"] = "<font color=red>需要更新</font>";
			}
			else {
				$row["isgx"] = "<font color=green>正常</font>";
			}

			$row["sidzt"] = ($row[sidzt] ? "<font color=red>失效</font>" : "<font color=green>正常</font>");
			$row["skeyzt"] = ($row[skeyzt] ? "<font color=red>失效</font>" : "<font color=green>正常</font>");
			$row["zanzt"] = getzt($row[iszan]);
			$row["shuozt"] = getzt($row[isshuo]);
			$row["replyzt"] = getzt($row[isreply]);
			$row["delzt"] = getzt($row[isdel]);
			$row["qdzt"] = getzt($row[isqd]);
			$row["3gqqzt"] = getzt($row[is3gqq]);
			$row["lyzt"] = getzt($row[isly]);
			$row["zyzanzt"] = getzt($row[iszyzan]);
			$row["zfzt"] = getzt($row[iszf]);
			$row["htzt"] = getzt($row[isht]);
			$row["vipqdzt"] = getzt($row[isvipqd]);
			$row["tszt"] = getzt($row[ists]);
		}

		return $row;
	}

	public function qq()
	{
		$user = $this->islogin();
		$qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0");
		if (!$qid || !$row = m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			exit("<script language='javascript'>alert('QQ不存在');history.go(-1);</script>");
		}

		if ($qid && m("qqs")->field("*")->where("qid='$qid' and uid='{$user["uid"]}'")->find()) {
			if ($_GET["do"] == "del") {
				m("qqs")->where("qid='$qid'")->delete();
				@header("Location:" . u("user"));
				exit();
			}
		}

		$row = $this->getzt($row);
		$this->assign("qqrow", $row);
		$this->display($this->mb . "/qq");
	}

	public function index()
	{
		$this->assign("user", $this->get_user_info());
		$rows = m("qqs")->field("*")->order("qid desc")->limit(24)->select();
		$this->assign("list", $rows);
		$this->display($this->mb . "/index");
	}

	private function islogin()
	{
		if (!$this->loginuser = $this->get_user_info()) {
			@header("HTTP/1.1 301 Moved Permanently");
			@header("Location:/");
		}
		else {
			$this->assign("user", $this->loginuser);
			return $this->loginuser;
		}
	}

	public function active()
	{
		if ($_GET["do"] == "gethtml") {
			$this->display("active");
			exit();
		}

		$now = date("Y-m-d H:i:s");
		$code = i("get.code", "", "get_safe_str");

		if (!$row = m("mailcode")->field("*")->where("code='$code' and endtime>'$now'")->find()) {
			@header("HTTP/1.1 301 Moved Permanently");
			@header("Location:/");
		}
		else if (!$user = m("users")->field("*")->where("uid='{$row["uid"]}'")->find()) {
			@header("HTTP/1.1 301 Moved Permanently");
			@header("Location:/");
		}

		m("users")->where("uid='{$user["uid"]}'")->setField("active", 1);
		setcookie("xiha_sid", $user["sid"],time() + (3600 * 24 * 14), "/");
		exit("<script language='javascript'>alert('激活成功，进入用户中心看看！');window.location.href='" . u("user") . "';</script>");
	}

	private function send_active_mail($mail, $code = "", $user = "", $do = "active")
	{
		$sendto = $mail;
		$title = c("web_name");
		if ($do == "findpwd") {
			$content="$user,您好。帮助你找回在&nbsp;" . c("web_name") . "&nbsp;的密码!<br>请点击下面的连接完成设置新密码！<br><a href='http://" . $_SERVER["SERVER_NAME"] . u("newpwd") . "?code=$code'>http://" . $_SERVER["SERVER_NAME"] . u("newpwd") . "?code=$code</a><br>(如果链接不能点击，请复制并粘贴到浏览器的地址栏，然后按回车键，该链接48小时内有效。)";
		}
		else {
			$content="$user,您好。欢迎你注册&nbsp;" . c("web_name") . "!<br>点击下面的连接完成激活！<br><a href='http://" . $_SERVER["SERVER_NAME"] . u("active") . "?code=$code'>http://" . $_SERVER["SERVER_NAME"] . u("active") . "?code=$code</a><br>(如果链接不能点击，请复制并粘贴到浏览器的地址栏，然后按回车键，该链接48小时内有效。)";
		}
		$this->send_mail($sendto, $title, $content);
	}

private function send_mail($to, $sub, $msg) {
	$From = C('mail_email');
	$Host = C('mail_host');
	$Port = C('mail_port');
	$SMTPAuth = 1;
	$Username = C('mail_email');
	$Password = C('mail_pwd');
	$Nickname = C('web_name');
	$SSL = false;
	$mail = new SMTP($Host , $Port , $SMTPAuth , $Username , $Password , $SSL);
	$mail->att = array();
	if($mail->send($to , $From , $sub , $msg, $Nickname)) {
		return true;
	} else {
		return $mail->log;
	}
}

	public function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

		if ($post) {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}

		if ($header) {
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}

		if ($cookie) {
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}

		if ($referer) {
			curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
		}

		if ($ua) {
			curl_setopt($ch, CURLOPT_USERAGENT, $ua);
		}
		else {
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
		}

		if ($nobaody) {
			curl_setopt($ch, CURLOPT_NOBODY, 1);
		}

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	}
}


?>
