<?php
namespace Home\Controller;
use Think\Controller;
class AdminController extends Controller
{
	public function logout()
	{
		setcookie("xiha_adminpwd", "", -1, "/");
		@header("HTTP/1.1 301 Moved Permanently");
		@header("Location:/");
	}

	public function login()
	{
		if ($_POST["submit"]) {
			$pwd = md5(md5($_POST["pwd"]) . md5("815856515"));

			if ($pwd == c("adminpwd")) {
				$alert = "<script language='javascript'>alert('密码错误！');</script>";
				setcookie("xiha_adminpwd", $pwd, time() + (3600 * 24 * 2), "/");
				$alert = "<script language='javascript'>alert('登录成功！');window.location.href=\"" . u("index") . "\"</script>";
			}
			else {
				$alert = "<script language='javascript'>alert('密码错误！');</script>";
			}
		}

		$this->assign("alert", $alert);
		$this->display("admin/login");
	}

	public function kmlist()
	{
		$kid = (is_numeric($_GET["kid"]) ? $_GET["kid"] : "0");
		if (($_GET["do"] == "del") && $kid) {
			m("kms")->where("kid='$kid'")->delete();
		}

		if ($_POST["do"] == "add") {
			$num = (is_numeric($_POST["num"]) ? $_POST["num"] : "1");
			$ms = (is_numeric($_POST["ms"]) ? $_POST["ms"] : "1");

			if ($_GET["list"] == "peie") {
				$kind = 1;
			}
			else if ($_GET["list"] == "free") {
				$kind = 2;
			}
			else {
				$kind = 0;
			}

			$msg = "<ul class='list-group'>\r\n\t\t\t<li class='list-group-item active'>成功生成以下卡密</li>";

			for ($i = 0; $i < $num; $i++) {
				$data = array();
				$data["kind"] = $kind;
				$data["km"] = $this->getkm(12);
				$data["ms"] = $ms;
				$data["isuse"] = 0;
				$data["addtime"] = date("Y-m-d H:i:s");

				if (m("kms")->data($data)->add()) {
					$msg .= "<li class='list-group-item'>$data[km]</li>";
				}
			}

			$msg .= "</ul>";
			$this->assign("msg", $msg);
		}

		if (($_POST["do"] == "search") && ($s = i("post.key", "", "get_safe_str"))) {
			$where = "km like '%$s%'";

			if ($_GET["list"] == "peie") {
				$where .= " and kind=1";
			}

			if ($_GET["list"] == "free") {
				$where .= " and kind=2";
			}
			else {
				$where .= " and kind=0";
			}

			$rows = m("kms")->field("*")->where($where)->limit(10)->order("kid desc")->select();
			$p = 1;
			$pages = 1;
			$count = 1;
		}
		else {
			$p = (is_numeric($_GET["p"]) ? $_GET["p"] : "1");
			$start = 10 * ($p - 1);
			$next = $p + 1;
			$limit = "$start,10";

			if ($_GET["list"] == "peie") {
				$where = "kind=1";
			}
			else if ($_GET["list"] == "free") {
				$where = "kind=2";
			}
			else {
				$where = "kind=0";
			}

			$count = m("kms")->where($where)->count("kid");
			$pages = ceil($count / 10);
			$rows = m("kms")->field("*")->where($where)->limit($limit)->order("kid desc")->select();
		}

		$this->assign("page", $p);
		$this->assign("pages", $pages);
		$this->assign("count", $count);
		$this->assign("list", $rows);
		$this->display("admin/kmlist");
	}

	public function userset()
	{
		$uid = (is_numeric($_GET["uid"])) ? $_GET["uid"] : exit("非法操作！");
		if ($_POST["submit"] && $uid) {
			unset($_POST["submit"]);

			if ($_POST["resetpwd"]) {
				$_POST["pwd"] = "4d3ea8f0d1aa6fa07b6c0a5375645c48";
			}

			unset($_POST["resetpwd"]);
			m("users")->where("uid='$uid'")->save($_POST);
		}

		if (!$row = m("users")->field("*")->where("uid='$uid'")->find()) {
			exit("<script language='javascript'>alert('此用户不存在！');history.go(-1);</script>");
		}

		$this->assign("user", $row);
		$this->display("admin/userset");
	}

	public function dllist()
	{
		$uid = (is_numeric($_REQUEST["uid"]) ? $_REQUEST["uid"] : "0");
		$rmb = (is_numeric($_REQUEST["rmb"]) ? $_REQUEST["rmb"] : "0");
		if ($uid && !$row = m("users")->field("uid")->where("uid='$uid'")->find()) {
			echo "<script language='javascript'>alert('用户不存在！');</script>";
			$uid = 0;
		}

		if (($_GET["do"] == "del") && $uid) {
			m("users")->where("uid='$uid'")->setField("daili", "0");
		}

		if ($_POST["submit"] && $uid) {
			if ($_POST["do"] == "add") {
				m("users")->where("uid='$uid'")->setField("daili", "1");
			}
			else {
				if (($_POST["do"] == "cz") && $uid && $rmb) {
					m("users")->where("uid='$uid'")->setInc("rmb", $rmb);
				}
				else {
					if (($_POST["do"] == "kc") && $uid && $rmb) {
						m("users")->where("uid='$uid'")->setDec("rmb", $rmb);
					}
				}
			}
		}

		$p = (is_numeric($_GET["p"]) ? $_GET["p"] : "1");
		$start = 10 * ($p - 1);
		$next = $p + 1;
		$limit = "$start,10";
		$count = m("users")->where("daili>0")->count("uid");
		$pages = ceil($count / 10);
		$rows = m("users")->field("*")->where("daili>0")->limit($limit)->order("uid desc")->select();
		$this->assign("page", $p);
		$this->assign("pages", $pages);
		$this->assign("count", $count);
		$this->assign("list", $rows);
		$this->display("admin/dllist");
	}

	public function userlist()
	{
		$uid = (is_numeric($_GET["uid"]) ? $_GET["uid"] : "0");
		if (($_GET["do"] == "del") && $uid) {
			m("qqs")->where("uid='$uid'")->delete();
			m("users")->where("uid='$uid'")->delete();
		}
		else {
			if (($_GET["do"] == "active") && $uid) {
				m("users")->where("uid='$uid'")->setField("active", "1");
			}
			else {
				if (($_GET["do"] == "jie") && $uid) {
					m("users")->where("uid='$uid'")->setField("login", "1");
				}
				else {
					if (($_GET["do"] == "hei") && $uid) {
						m("users")->where("uid='$uid'")->setField("login", "0");
					}
				}
			}
		}

		if (($_POST["do"] == "search") && ($s = i("post.key", "", "get_safe_str"))) {
			$type = (is_numeric($_POST["type"]) ? $_POST["type"] : "0");

			if ($type == 1) {
				$where = "mail like '%$s%'";
			}
			else if ($type == 2) {
				$where = "uid='$s'";
			}
			else {
				$where = "user like '%$s%'";
			}

			if ($_GET["list"] == "vip") {
				$where .= " and vip>0 and vipend > '" . date("Y-m-d") . "'";
			}

			$rows = m("users")->field("*")->where($where)->limit(10)->order("uid desc")->select();
			$p = 1;
			$pages = 1;
			$count = 1;
		}
		else {
			$p = (is_numeric($_GET["p"]) ? $_GET["p"] : "1");
			$start = 10 * ($p - 1);
			$next = $p + 1;
			$limit = "$start,10";

			if ($_GET["list"] == "vip") {
				$where .= "vip>0 and vipend > '" . date("Y-m-d") . "'";
			}

			$count = m("users")->where($where)->count("uid");
			$pages = ceil($count / 10);
			$rows = m("users")->field("*")->where($where)->limit($limit)->order("uid desc")->select();
		}

		$this->assign("page", $p);
		$this->assign("pages", $pages);
		$this->assign("count", $count);
		$this->assign("list", $rows);
		$this->display("admin/userlist");
	}

	public function qqlist()
	{
		if (($_GET["do"] == "del") && ($qid = (is_numeric($_GET["qid"]) ? $_GET["qid"] : "0"))) {
			m("qqs")->where("qid='$qid'")->delete();
		}

		if (($_POST["do"] == "search") && ($uin = (is_numeric($_POST["uin"]) ? $_POST["uin"] : "0"))) {
			$rows = m("qqs")->field("*")->where("qq like '%$uin%'")->limit(10)->order("qid desc")->select();
			$p = 1;
			$pages = 1;
			$count = 1;
			$sple = 0;
		}
		else {
			$p = (is_numeric($_GET["p"]) ? $_GET["p"] : "1");
			$start = 10 * ($p - 1);
			$next = $p + 1;
			$limit = "$start,10";
			$count = m("qqs")->where($where)->count("qid");
			$pages = ceil($count / 10);
			$rows = m("qqs")->field("*")->where($where)->limit($limit)->order("qid desc")->select();
		}

		$this->assign("page", $p);
		$this->assign("pages", $pages);
		$this->assign("count", $count);
		$this->assign("list", $rows);
		$this->display("admin/qqlist");
	}

	public function set()
	{
		if ($_POST["submit"]) {
			unset($_POST["submit"]);

			if (!$_POST["adminpwd"]) {
				unset($_POST["adminpwd"]);
			}
			else {
				$_POST["adminpwd"] = md5(md5($_POST["adminpwd"]) . md5("815856515"));
			}

			foreach ($_POST as $k => $value ) {
				m("webconfigs")->execute("insert into " . c("DB_PREFIX") . "webconfigs set vkey='$k',value='$value' on duplicate key update value='$value'");
			}
		}

		if ($rows = m("webconfigs")->field("*")->select()) {
			foreach ($rows as $row ) {
				$config[$row["vkey"]] = $row["value"];
			}
		}

		$this->assign("config", $config);
		$this->display("admin/set");
	}

	public function cron()
	{
		$this->display("admin/cron");
	}

	public function setdali()
	{
		if ($_POST["submit"]) {
			unset($_POST["submit"]);

			foreach ($_POST as $k => $value ) {
				m("dailiconfigs")->execute("insert into " . c("DB_PREFIX") . "dailiconfigs set vkey='$k',value='$value' on duplicate key update value='$value'");
			}
		}

		if ($rows = m("dailiconfigs")->field("*")->select()) {
			foreach ($rows as $row ) {
				$config[$row["vkey"]] = $row["value"];
			}
		}

		$this->assign("config", $config);
		$this->display("admin/setdali");
	}

	public function setnet()
	{
		if ($_POST["submit"]) {
			unset($_POST["submit"]);

			foreach ($_POST as $k => $value ) {
				m("webconfigs")->execute("insert into " . c("DB_PREFIX") . "webconfigs set vkey='$k',value='$value' on duplicate key update value='$value'");
			}
		}

		if ($rows = m("webconfigs")->field("*")->select()) {
			foreach ($rows as $row ) {
				$config[$row["vkey"]] = $row["value"];
			}
		}

		$this->assign("config", $config);
		$this->display("admin/setnet");
	}

	public function setgg()
	{
		if ($_POST["submit"]) {
			unset($_POST["submit"]);

			foreach ($_POST as $k => $value ) {
				m("webconfigs")->execute("insert into " . c("DB_PREFIX") . "webconfigs set vkey='$k',value='$value' on duplicate key update value='$value'");
			}
		}

		if ($rows = m("webconfigs")->field("*")->select()) {
			foreach ($rows as $row ) {
				$config[$row["vkey"]] = $row["value"];
			}
		}

		$this->assign("config", $config);
		$this->display("admin/setgg");
	}

	public function setmail()
	{
		if ($_POST["submit"]) {
			unset($_POST["submit"]);

			foreach ($_POST as $k => $value ) {
				m("webconfigs")->execute("insert into " . c("DB_PREFIX") . "webconfigs set vkey='$k',value='$value' on duplicate key update value='$value'");
			}
		}

		if ($rows = m("webconfigs")->field("*")->select()) {
			foreach ($rows as $row ) {
				$config[$row["vkey"]] = $row["value"];
			}
		}

		$this->assign("config", $config);
		$this->display("admin/setmail");
	}

	public function index()
	{
		if ($_GET["do"] == "delvip") {
			if ($rows = m("users")->field("uid")->where("vip>0 and vipend < '" . date("Y-m-d") . "'")->order("uid desc")->select()) {
				foreach ($rows as $row ) {
					$uid = $row["uid"];
					m("users")->where("uid='$uid'")->setField("vip", "0");
					m("qqs")->where("uid='$uid'")->delete();
				}

				echo "<script language='javascript'>alert('已成功清理VIP到期用户');</script>";
			}
			else {
				echo "<script language='javascript'>alert('暂时没有待清理的用户');</script>";
			}
		}
		else if ($_GET["do"] == "delqq") {
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set zannet=0,replynet=0,shuonet=0,delnet=0,qdnet=0,3gqqnet=0,zfnet=0 where sidzt=1");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set zannet=0 where zannet>0 and iszan=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set replynet=0 where replynet>0 and isreply=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set shuonet=0 where shuonet>0 and isshuo=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set delnet=0 where delnet>0 and isdel=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set qdnet=0 where qdnet>0 and isqd=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set 3gqqnet=0 where 3gqqnet>0 and is3gqq=0");
			m("webconfigs")->execute("update " . c("DB_PREFIX") . "qqs set zfnet=0 where zfnet>0 and iszf=0");
			echo "<script language='javascript'>alert('清理SID过期QQ成功！');</script>";
		}

		$this->display("admin/index");
	}

	private function getkm($len = 12)
	{
		$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		$strlen = strlen($str);
		$randstr = "";

		for ($i = 0; $i < $len; $i++) {
			$randstr .= $str[mt_rand(0, $strlen - 1)];
		}

		return $randstr;
	}

	public function __construct()
	{
		parent::__construct();

		if (ACTION_NAME != "login") {
			if (isset($_COOKIE["xiha_adminpwd"]) && c("adminpwd") && ($_COOKIE["xiha_adminpwd"] == c("adminpwd"))) {
				$this->assign("islogin", 1);
			}
			else {
				@header("Location:" . u("login"));
				exit();
			}
		}
	}
}


?>
