<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' 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 "topic" : viewVideoTopic();break;
	case "select" : selectTopic();break;
	case "submittopic" : submitVideoTopic();break;
	case "getad" : getAdInfo();break;
	case "getselflabel" : getSelfLabel();break;
	case "checkuser" : checkUserExist();break;
	case "submitstate" : submitState();break;
	case "commend" : commendVideo();break;
	case "commendnews" : commendNews();break;
	case "gettopicdes" : getTopicDes();break;
	case "submittopicdes" : submitTopicDes();break;
	case "updatepic" : checkUpdatePic();break;
	case "cleartophit" : cleartophit();break;
	case "checkrepeat" : checkRepeat();break;
	case "restartcache" : restartCache();break;
}

function cleartophit() {
	$x = getForm("do", "get");
	if ($x == "dwm"){
		DB::query("UPDATE ".DB::table('data')." SET m_monthhit=m_weekhit+m_dayhit,m_weekhit=m_dayhit,m_dayhit=0");
		DB::query("UPDATE ".DB::table('news')." SET m_monthhit=m_weekhit+m_dayhit,m_weekhit=m_dayhit,m_dayhit=0");
	}else if ($x=="dw"){
		DB::query("UPDATE ".DB::table('data')." SET m_monthhit=m_monthhit+m_weekhit+m_dayhit,m_weekhit=m_dayhit,m_dayhit=0");
		DB::query("UPDATE ".DB::table('news')." SET m_monthhit=m_monthhit+m_weekhit+m_dayhit,m_weekhit=m_dayhit,m_dayhit=0");
    }else if ($x=="d"){
		DB::query("UPDATE ".DB::table('data')." SET m_weekhit=m_weekhit+m_dayhit,m_dayhit=0");
		DB::query("UPDATE ".DB::table('news')." SET m_weekhit=m_weekhit+m_dayhit,m_dayhit=0");
	}
	fwrite(fopen("imgs/last_clear_lock.js","wb"),"window.HitLastUpdateTime='".MyDate("Y",time())."/".MyDate("m",time())."/".MyDate("d",time())." ".MyDate('H:i:s', time())."';");
}

function restartCache() {
	memory('clear');
	echo "ok";
}

function checkRepeat() {
	$m_name = getForm("m_name","get");
	$num = DB::result_first("SELECT count(*) FROM ".DB::table('data')." WHERE m_name='$m_name'");
	echo $num == 0 ? 'ok' : 'err';
}

function submitTopicDes() {
	$id = getForm("f_m_id","post");
	$topicDes = encodeHtml(unescape(getForm("f_m_des","post")));
	if (!DB::update('topic', array('m_des' => $topicDes), array('m_id' => $id))) echo "err";else echo "ok";
}

function getTopicDes() {
	$id=getForm("id","get");
	$row = DB::fetch_first("SELECT m_des FROM ".DB::table('topic')." WHERE m_id='$id'");
	if (!$row) echo "err";else echo new_htmlspecialchars(decodeHtml($row['m_des']));
}

function commendVideo() {
	$id = getForm("id","get");
	$commendId = getForm("commendid","get");
	if (!DB::update('data', array('m_commend' => $commendId), array('m_id' => $id))) echo "err";else echo "submitok";
}

function commendNews() {
	$id = getForm("id","get");
	$commendId = getForm("commendid","get");
	if (!DB::update('news', array('m_commend' => $commendId), array('m_id' => $id))) echo "err";else echo "submitok";
}

function submitState() {
	$id = getForm("id","get");
	$vstate = getForm("state","get");
	if (!DB::update('data', array('m_state' => $vstate), array('m_id' => $id))) echo "err";else echo "submitok";
}

function getSelfLabel() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT m_content FROM ".DB::table('selflabel')." WHERE m_id='$id'");
	if (!$row) echo "err";else echo new_htmlspecialchars(decodeHtml($row['m_content']));
}

function checkUserExist() {
    $username = getForm("username","get");
	$id = getForm("id","get");
	if (empty($id)) {
		$num = DB::result_first("SELECT count(*) FROM `".DB::table('manager')."` WHERE m_username='$username'");
	} else {
		$num = DB::result_first("SELECT count(*) FROM `".DB::table('manager')."` WHERE m_id<>$id  AND m_username='$username'");
	}
	echo $num;
}

function checkUpdatePic() {
	$num = DB::result_first("SELECT count(*) FROM `".DB::table('data')."` WHERE m_pic LIKE 'http://%'");
	echo $num;
}

function getAdInfo() {
	$path = getForm("path","get");
	$adStr = new_htmlspecialchars(decodeHtml(loadFile($path)));
	echo $adStr;
}

function viewVideoTopic() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT m_name FROM ".DB::table('topic')." WHERE m_id='$id'");
	$topicName = $row['m_name'];
	if (computeStrLen($topicName) > 8) $topicName = getStrByLen($topicName,14)."..";
	echo $topicName;
}

function selectTopic() {
	$id = getForm("id","get");$topicId = getForm("topicid","get");
	$sss = makeTopicSelect("topicselect","...请选择专题...",$topicId);
	$sss = replaceStr($sss,"</select>","<option value=\"0\">取消专题</option></select>");
	echo $sss."<input type=\"button\" value=\"确定\" onclick='submitVideoTopic(".$id.")' /> <input type=\"button\" value=\"取消\" onclick='closeWin()' />";
}

function submitVideoTopic() {
	$id = getForm("id","get");
	$topic = getForm("topic","get");
	if (!DB::update('data', array('m_topic' => $topic), array('m_id' => $id))) echo "err";else echo "submitok";
}
?>
