<?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%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
AjaxHead();
$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_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", 3600 * 24);
	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", 3600 * 24);
	echo $returnValue;
}

function checkIsWrong($id,$from){
	$checkIsWrong = false;
	switch ($from) {
		case "youku": 
			$returnstr = getRemoteContent("http".":/"."/v".".yo"."uku"."."."com"."/"."pl"."ayer"."/"."get"."Pl"."ayLi"."st/Vi"."d"."eoI"."DS"."/".$id);
			if (strpos(" ".$returnstr,"error:") > 0 ||  strpos(" ".$returnstr,'"data":[]') > 0)  $checkIsWrong = true;
			break;
		case "iask":
			$returnstr = getRemoteContent("http".":/"."/v".".i"."a"."sk.c"."o"."m/v"."_pl"."ay."."php?"."vi"."d=".$id);
			if (strpos(" ".$returnstr,"<r"."es"."ul"."t>e"."r"."ror</"."re"."sul"."t>") > 0)  $checkIsWrong = true;
			break;
		case "tudou":
			$returnstr = getRemoteContent("h"."ttp:"."//v"."2.t"."u"."do"."u."."co"."m/v"."2/c"."dn?i"."d=".$id);
			if (strpos(" ".$returnstr,"<"."e er"."rn"."o='") > 0)  $checkIsWrong = true;
			break;
	}
	return $checkIsWrong;
}

function autoCheckWrong(){
	//if (isAutoCheck==0) then exit sub
	$vid=getForm("vid","get");$id=getForm("id","get");$from=getForm("from","get");$errinfo=getForm("err","get");
	if (empty($vid) || empty($id)) die("err");
	if (!is_numeric($vid)){ echoSaveStr("safe");exit();  }else{ $vid=intval($vid);}
	if (checkIsWrong($id,$from)){
	    $row = DB::fetch_first("select m_author,m_type,m_videoid,m_content from ".DB::table('info')." where m_author='auto' and m_type='reporterror' and m_videoid=".$vid);
		if (!is_array($row)) {
			DB::query("insert into ".DB::table('info')."(m_author,m_type,m_videoid,m_content) values ('auto','reporterror','$vid','$errinfo')");
	    }else{
			if (strpos(" ".$row['m_content'],$err)<1){
				DB::query("update ".DB::table('info')." set m_content='".$row['m_content'].chr(13).chr(10).$err."' where m_author='auto' and m_type='reporterror' and m_videoid=".$vid);
			}
	    }
    }
	die("ok");	
}

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