<?php
if (!defined('R_P')) exit('Access Denied');
$action = $_GET['action']?$_GET['action']:'show';
$votetime = 24 * 7; //防止重复投票的小时数，默认为一周
if ($action == 'save') {
	$voteid = intval($_POST['voteid']);
	$url = "index.php?ac=vote&action=show&voteid={$voteid}";
	$my_vote = qdb::QueryFirst("select addtime,totime,passed,checkip from ".TABLEPRE."votesubject where voteid='{$voteid}'");
	if (empty($my_vote)) qbase::showmsg("出错了，没有找到该投票", "default", W_P);
	if ($my_vote['passed'] == 0) qbase::showmsg("出错了，该投票已经关闭了", "default", $url);
	if ($_SERVER['REQUEST_TIME'] < $my_vote['addtime']) qbase::showmsg('投票还没有开始，谢谢关注', 'default', $url);
	if (($_SERVER['REQUEST_TIME'] > $my_vote['totime']) && ($my_vote['totime'] > 0)) qbase::showmsg('抱歉，投票已经结束，谢谢关注', 'default', $url);
	$ip = qbase::get_ip();
	if($my_vote['checkip']){
		$dateline = qdb::QueryAlone("select dateline from ".TABLEPRE."voteip where ip='" . $ip . "' and voteid='{$voteid}' limit 1");
		if (!$dateline) {
			$iif = 0;
			$no_vote = 0;
		} else {
			if (($_SERVER['REQUEST_TIME'] - $dateline) < $votetime * 3600) {
				$iif = 1;
				$no_vote = 1;
			} else {
				$iif = 0;
				$no_vote = 1;
			} 
		} 
	}else{
		$cc = isset($_COOKIE['_v']) ? explode('-', trim($_COOKIE['_v'])) : array();
		if(in_array($voteid, $cc)){
			$iif = 1;
			$no_vote = 1;
		}else{
			$iif = 0;
			$no_vote = 0;
			$cc[] = $voteid;
			qbase::phpsetcookie('_v', implode('-', $cc), $votetime * 3600);
		}
	}
	if (!$iif) {
		$temp = qdb::fetchAll("select optionid from ".TABLEPRE."voteoption where voteid='{$voteid}' ");
		$optionids = array();
		foreach($temp as $val) {
			$optionids[] = $val['optionid'];
		} 
		$option = $_POST['option'];
		$options = '';
		if (is_array($option)) {
			$temp = array();
			foreach($option as $v) {
				$v = intval($v);
				if (in_array($v, $optionids)) $temp[] = $v;
			} 
			$options = implode(",", $temp);
		} else {
			if (in_array(intval($option), $optionids)) $options = intval($option);
		} 
		if (!$options) qbase::showmsg('需要提交投票选项', 'default', $url);
		qdb::Query("update ".TABLEPRE."voteoption set number=number+1 WHERE voteid = '{$voteid}' and optionid IN ($options)");
		if($my_vote['checkip']){
			$temptime = $_SERVER['REQUEST_TIME'];
			include_once(R_P . "/include/class.browserinfo.php");
			$browse = new BrowserInfo($_SERVER["HTTP_USER_AGENT"]);
			$os = addslashes($browse -> OS . $browse -> OS_Version);
			$ie = addslashes($browse -> Browser . $browse -> Browser_Version);
			if ($no_vote == 0) {
				qdb::Query("insert into ".TABLEPRE."voteip (voteid,ip,dateline,ie,os) values('{$voteid}','" . $ip . "','$temptime','" . $ie . "','" . $os . "')");
			} else {
				$sql = "update  ".TABLEPRE."voteip set dateline='$temptime',ie='" . $ie . "',os='" . $os . "'  where ip='" . $ip . "' and voteid='{$voteid}'";
				qdb::Query($sql);
			} 
		}
		qbase::showmsg('谢谢您参与投票', 'default', $url);
	} else {
		qbase::showmsg('您已经投过票了，不要重复', 'default', $url);
	} 
} elseif ($action == 'show') {
	$voteid = intval($_GET['voteid']);
	$num = qdb::QueryAlone("SELECT SUM(number) FROM  ".TABLEPRE."voteoption where voteid='$voteid'");
	$subject = qdb::QueryFirst("select * from ".TABLEPRE."votesubject where voteid='$voteid'");
	if (empty($subject)) qbase::showmsg("出错了，没有找到该投票");
	if ($subject['passed'] == 0) qbase::showmsg("出错了，该投票已经关闭了");
	$subject['status'] = $_SERVER['REQUEST_TIME'] < $subject['totime'] ? true : false;
	$subject['addtime'] = qbase::formatTime('Y-m-d H点', $subject['addtime']);
	if ($subject['totime'] > 0) {
		$subject['totime'] = qbase::formatTime('Y-m-d H点', $subject['totime']);
	} else {
		$subject['totime'] = "没有时间限制";
	} 
	$options = qdb::fetchAll("select * from ".TABLEPRE."voteoption where voteid=$voteid");
	foreach($options as &$option) {
		$option['percent1'] = ($option['number'] / $num) * 360;
		$option['percent2'] = 360 - $option['percent1'];
		$option['percent3'] = intval(($option['number'] / $num) * 100);
	} 
	unset($option);
	$title = strip_tags($subject['subject']);
	$keyword = $description = $title ;
} else {
	qbase::showmsg('出了一点无关紧要的问题', 'default', 'index.php?ac=vote&action=show');
} 
include qbase::template($_SET['skin'] . '/vote');

?>