<?php
require_once(dirname(__FILE__)."/common.php");
defined('IN_DJCMS') or exit('Access Denied');
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
$action = getForm("action", "get");
switch ($action)
{
	case "digg" : case "score" : case "tread" : scoreVideo($action);break;
	case "videoscore" : case "newsscore" : getscore($action);break;
	case "diggnews" : case "treadnews" : case "scorenews" : scoreNews($action);break;
	case "diggnum" : scoreNum();break;
	case "reporterr" : reportErr();break;
	case "autocheck" : autoCheckWrong();break;
	case "hit" : updateHit();break;
	case "hitnews" : updateNewsHit();break;
	case "typeoption" : viewTypeoption();break;
}

function getscore($ac){
	$id = getForm("id","get");
	if (empty($id)) die("err");
	if ($ac == "newsscore"){
		$row = DB::fetch_first("SELECT m_digg,m_tread,m_score FROM ".DB::table('news')." WHERE m_id=$id");
	}else{
		$row = DB::fetch_first("SELECT m_digg,m_tread,m_count as m_score FROM ".DB::table('data')." WHERE m_id=$id");
	}
	if (is_array($row)) {
		die("[".$row['m_digg'].",".$row['m_tread'].",".$row['m_score']."]");
    }
	die("err");
}

function updateNewsHit()
{
	$id = getForm("id","get");
	if (empty($id)) die("err");
	if (!is_numeric($id)){ echoSaveStr("safe");exit(); }else{ $id=intval($id);}
	DB::query("update ".DB::table('news')." set m_hit=m_hit+1 where m_id=$id");
	$row = DB::fetch_first("select `m_hit` from ".DB::table('news')." where `m_id`=$id");
	if(is_array($row))echo $row['m_hit'];else return "err";
}

function updateHit()
{
	$id = getForm("id","get");
	if (empty($id)) die("err");
	if (!is_numeric($id)){ echoSaveStr("safe");exit(); }else{ $id=intval($id);}
	DB::query("update ".DB::table('data')." set m_hit=m_hit+1 where m_id=$id");
	$row = DB::fetch_first("select `m_hit` from ".DB::table('data')." where `m_id`=$id");
	if(is_array($row)) echo $row['m_hit'];else return "err";
}

function reportErr() {
	$backurl = getRefer();$id = getSubStrByFromAndEnd($backurl,"?id=","","start");$m_author=getForm("m_errauthor","post");$m_content=getForm("m_errcontent","post");
	if (empty($id)){ alertMsg("参数错误","");exit();}
	if (empty($m_author)){ alertMsg("请填写报错者","");exit();}
	if (empty($m_content)){ alertMsg("请填写报错内容","");exit();}
	$infoarr = array(
	'm_author' => $m_author,
	'm_type' => 'reporterror',
	'm_videoid' => $id,
	'm_content' => $m_content,
	'm_ip' => getIp(),
	'm_addtime' => time(),
	);
	if (DB::insert('info', $infoarr)) {
        echo "报错成功,多谢支持!";
	} else {
	    echo "发生错误";
	}
	echo "<script>setTimeout('window.close()',2000)</script>";
}

function scoreVideo($operType)
{
	$id = getForm("id","get");$score = getForm("score","get");
	if (getcookie("djcms_score$id") == "ok") die("havescore");
	if (empty($id)) die("err");
	if (!is_numeric($id)){ echoSaveStr("safe");exit(); }else{ $id=intval($id);}
	if ($operType == "score") {
		if (!is_numeric($score)){ echoSaveStr("safe");exit(); }else{ $score=intval($score);}
		DB::query("update ".DB::table('data')." set `m_digg`=`m_digg`+1".ifthen($score>0,",`m_score`=`m_score`+".$score,"")." where `m_id`=".$id);
		$returnValue="";
	}else if ($operType == "digg") {
		$row = DB::fetch_first("select `m_digg` from ".DB::table('data')." where `m_id`=$id");
		DB::query("update ".DB::table('data')." set `m_digg`=`m_digg`+1 where `m_id`=$id");
		if (is_array($row)) $digg=$row['m_digg'];else $digg=0; 
		$returnValue=$digg+1;
	} else if ($operType == "tread") {
		$row = DB::fetch_first("select `m_tread` from ".DB::table('data')." where `m_id`=$id");
		DB::query("update ".DB::table('data')." set `m_tread`=`m_tread`+1 where `m_id`=$id");
		if (is_array($row)) $tread=$row['m_tread'];else $tread=0;
		$returnValue=$tread+1;
	} else {
		die("err");
	}
	dsetcookie("djcms_score$id", "ok", 86400);
	echo $returnValue;
}

function scoreNews($operType)
{
	$id = getForm("id","get");$score = getForm("score","get");
	if (getcookie("djcms_scorenews$id") == "ok") die("havescore");
	if (empty($id)) die("err");
	if (!is_numeric($id)){ echoSaveStr("safe");exit(); }else{ $id=intval($id);}
	if ($operType == "scorenews") {
		if (!is_numeric($score)){ echoSaveStr("safe");exit(); }else{ $score=intval($score);}
		DB::query("update ".DB::table('news')." set `m_digg`=`m_digg`+1".ifthen($score>0,",`m_score`=`m_score`+".$score,"")." where `m_id`=".$id);
		$returnValue="";
	}else if ($operType == "diggnews") {
		$row = DB::fetch_first("select `m_digg` from ".DB::table('news')." where `m_id`=$id");
		DB::query("update ".DB::table('news')." set `m_digg`=`m_digg`+1,`m_score`=`m_score`+1 where `m_id`=$id");
		if (is_array($row)) $digg=$row['m_digg'];else $digg=0; 
		$returnValue=$digg+1;
	} else if ($operType == "treadnews") {
		$row = DB::fetch_first("select `m_tread` from ".DB::table('news')." where `m_id`=$id");
		DB::query("update ".DB::table('news')." set `m_tread`=`m_tread`+1 where `m_id`=$id");
		if (is_array($row)) $tread=$row['m_tread'];else $tread=0;
		$returnValue=$tread+1;
	} else {
		die("err");
	}
	dsetcookie("djcms_scorenews$id", "ok", 86400);
	echo $returnValue;
}

function viewTypeoption()
{
	echo "document.write(".chr(34)."";
	makeTypeOption(0,"&nbsp;|&nbsp;&nbsp;");
	echo "".chr(34).");";
}
?>
