<?php
header("Content-type: text/html;charset=gb2312");
include '../../comm/conn.php';
include '../../comm/config.php';
include "../admin_version.php";
include '../../comm/checkpostandget.php';
include "../../include/function.php";
$action=SafeRequest("action","get");
switch($action){
		case 'topic':
			TopicSelect();
			break;
		case 'topicsave':
			TopicSave();
			break;
		case 'editreco':
			EditReco();
			break;
		case 'chgclasslist':
			ChgClassList();
			break;
		case 'savecls':
			SaveCls();
			break;
		case 'getpicnum':
			GetPicNum();
			break;
		case 'remark':
			Remark();
			break;
		case 'getver':
			getVer();
			break;
		case 'checkupdate':
			checkUpdate();
			break;
		case 'cache':
			cacheUpdate();
			break;
		case 'spanjs':
			spanjs();
			break;
		case 'getrenum':
			getRenum();
			break;
	}

function getRenum(){
	global $db;
	$tabel=SafeRequest("tabel","get");
	$numresult=$db->query("select S_ID from `$tabel`");
	$numrows=$db->num_rows($numresult);
	mysql_free_result($numresult); 
	echo $numrows;
	}

function spanjs(){
	global $db;
	$id=SafeRequest("id","get");
	if(!file_exists("../../js/adjs")){
		mkdir("../../js/adjs",0777);
		}
	$sql="select S_TheAdrJs,S_ID,S_IsUse from s_adsense where S_ID=$id";
	$row=$db->getrow($sql);
	if($row){
		if($row['S_IsUse']==0){
			fwrite(fopen("../../js/adjs/".$row['S_ID'].".js","wb"),$row['S_TheAdrJs']);
			}
		else{
			fwrite(fopen("../../js/adjs/".$row['S_ID'].".js","wb"),"document.write(\"广告未启用!\");");
			}	
		 
		}
	die("ok");	
	}

function cacheUpdate(){
	$d=_SYC_ROOT_.S_CacheFolder;
	if (is_dir($d)) {
	  	$dh=opendir($d);//打开目录
 		 while (false !== ( $file = readdir ($dh))) {
   			if($file!="." && $file!="..") { 
      			$fullpath=$d."/".$file;
      			if(!is_dir($fullpath)) {
         		 	unlink($fullpath);//删除目录中的所有文件
     				 } 
				}
   		 	}
   		 closedir($dh);
		}
	die("ok");
	}

function checkUpdate(){
	for($uu=0;$uu<3;$uu++){
		$UpdateContent=@file_get_contents("http://soft.yesfeng.com/php/update.xml");
		if($UpdateContent) break;
	}
	if(!IsNul($UpdateContent)){
		$UpdateContent=geturl("http://soft.yesfeng.com/php/update.xml");
		}
	if(!file_exists("../backup/log.txt")){
		fwrite(fopen("../backup/log.txt","wb"),"");
	}
	$Local=@file_get_contents("../backup/log.txt");
	$FileInfoArr=explode(chr(13),$UpdateContent);
	$infos="<font color=green>当前没有新的文件需要更新!</font>";
	for($i=0;$i<count($FileInfoArr);$i++){
		$FileArr=explode('|',$FileInfoArr[$i]);		
		$FileArr[0]=trim($FileArr[0]);
		$FileArr[0]=ReplaceStr($FileArr[0],".xml",".php");
		$FileArr[2]=trim($FileArr[2]);
		$FileInfo=$FileArr[0].'|'.$FileArr[2];
		if(stristr(strtolower($Local),strtolower($FileInfo))==false){
			$infos="".$FileArr[0]."&nbsp;文件未更新!&nbsp;&nbsp;&nbsp;<a href=admin_update.php>点击更新文件</a>";
			die($infos);
			}	
		}
	die($infos);	
}

function getVer(){
	global $S_Version;
		$version=getXmlVersion();
		if(strtolower(trim($version))!=strtolower(trim($S_Version))){
			$version=$version."&nbsp;<a href=http://soft.yesfeng.com/download target=_blank>[点击升级]</a>";
			}	
		echo $version;
	}

function Remark(){
		global $db;
		$S_ID=SafeRequest("id","get");
		$Remark=SafeRequest("tid","get");
		if(SafeRequest("tid","get")==""){
			$Remark="完结";
			$IsEnd=0;
			}
		else{
			$Remark="连载至".$Remark."集";
			$IsEnd=1;
			}	
		$sql="update s_video set  S_Remark='".$Remark."',S_IsEnd=".$IsEnd."  where S_ID=".$S_ID."";
		if($db->query($sql)){
			echo "ok";
			}
	}
	
function GetPicNum(){
	global $db;
	$SID=SafeRequest("sid","get");
	if($SID==1){
		$sql="Select * from s_video where Lower(Left(S_Pic,7))='http://'  order by S_ID desc";
		}
	else{
		$sql="Select * from s_article where Lower(Left(S_Pic,7))='http://'  order by S_ID desc";
		}
	$num=$db->num_rows($db->query($sql));	
	echo $num;
	}

function ChgClassList(){
	global $db;
	$S_ID=SafeRequest("id","get");
	$S_ClassID=SafeRequest("cid","get");
	$S_SysID=SafeRequest("sid","get");
	if($S_SysID==1){
		$sql="select S_Name,S_ID from s_class where S_SystemID=1";
		}
	elseif($S_SysID==2){
		$sql="select S_Name,S_ID from s_class where S_SystemID=2";
		}
		
	
	$str="<select onchange=\"savechgclass(this.value,".$S_ID.")\">\r\n";
	$result=$db->query($sql);
	if($result){
		while($row=$db->fetch_array($result)){
			if($S_ClassID==$row['S_ID']){
				$isselect="selected";
				}
			else{
				$isselect="";
				}	
		$str.="<option value=\"".$row['S_ID']."\"  ".$isselect.">".$row['S_Name']."</option>\r\n";
			}
		}
	$str.="</select>\r\n";
	echo $str;
	}
function SaveCls(){
	global $db;
	$S_ID=SafeRequest("id","get");
	$S_ClassID=SafeRequest("cid","get");
	$S_SycID=SafeRequest("sid","get");
	if($S_SycID==1){
		$sql="update s_video set S_ClassID=".$S_ClassID." where S_ID=".$S_ID."";
		}
	elseif($S_SycID==2){
		$sql="update s_article set S_ClassID=".$S_ClassID." where S_ID=".$S_ID."";
		}
	
	if($db->query($sql)){
		echo "<font color=green>修改成功</font>";
		}
	else{
		echo "<font color=red>修改出错</font>";
		}	
	}	

function TopicSave(){
		global $db;
		$S_ID=SafeRequest("id","get");
		$TopicID=SafeRequest("tid","get");
		$Types=SafeRequest("types","get");
		if(!IsNum($TopicID)){$TopicID==0;}
		if(!IsNum($Types)){$Types==1;}
		if($Types==1){
			$sql="update s_video set S_Topic=".$TopicID." where S_ID=".$S_ID."";
			if($db->query($sql)){
				echo "ok";
				}
			}
		else{
			$sql="update s_article set  S_Topic=".$TopicID."  where S_ID=".$S_ID."";
			if($db->query($sql)){
				echo "ok";
				}
			}	
		
	}

function TopicSelect(){
		global $db;
		$S_ID=SafeRequest("s_id","get");
		$TopicID=SafeRequest("tid","get");
		$Types=SafeRequest("types","get");
		if($Types==1){
			echo "<select name='TopicID' id='videotopicid'><option value='0'>默认无专题</option>";			
			$sql="select * from s_topic where S_SystemID=1";
			$result=$db->query($sql);
			if($result){
					while ($row=$db->fetch_array($result)){
						if($row['S_ID']==$TopicID){
							echo "<option value='".$row['S_ID']."' selected='selected'>".$row['S_Name']."</option>";
							}
						else{
							echo "<option value='".$row['S_ID']."'>".$row['S_Name']."</option>";
							}	
					}
				}
			echo "</select>";	
		}
		else{
			echo "<select name='TopicID' id='videotopicid'><option value='0'>默认无专题</option>";	
			$sql="select * from s_topic where S_SystemID=2";
			$result=$db->query($sql);
			if($result){
					while ($row=$db->fetch_array($result)){
						if($row['S_ID']==$TopicID){
							echo "<option value='".$row['S_ID']."' selected='selected'>".$row['S_Name']."</option>";
							}
						else{
							echo "<option value='".$row['S_ID']."'>".$row['S_Name']."</option>";
							}	
					}
				}
			echo "</select>";	
		}

		echo "&nbsp;<input type='button' value='确定'  onclick='ctopic(".$S_ID.")'  class='rb1' /> <input type='button' value='取消' id='topicclose'  class='rb1' onclick='closet()'/>";
	

	}

function EditReco(){
		global $db;
		$S_ID=SafeRequest("id","get");
		$Level=SafeRequest("level","get");
		$S_Type=SafeRequest("types","get");
		if(!IsNum($S_Type)){$S_Type==0;}
		if($S_Type==0){
			$table="s_video";
			}
		elseif($S_Type==2){
			$table="s_topic";
			}
		else{
			$table="s_article";
			}	
		$sql="update ".$table." set S_Reco=".$Level." where S_ID=".$S_ID."";
		if($db->query($sql)){
			echo "ok";
			}
			
		

	}
	
function getXmlVersion(){
	$doc = new DOMDocument();
	$cc=$doc->load('http://soft.yesfeng.com/version.xml');
	$ver = $doc->getElementsByTagName("syccms");	
	foreach( $ver as $v )
	{
		$ver = $v->getElementsByTagName( "version" );
		if(function_exists("mb_convert_encoding")){
			$version =  mb_convert_encoding($ver->item(0)->nodeValue,"GBK","UTF-8");  
			}
		else{
			$version = iconv( "UTF-8",  "GBK//IGNORE" , $ver->item(0)->nodeValue);
			}
	}
	return $version;
}	
?>