<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include '../admin_loginstate.php';
include "../../include/conn.php";
include "../../include/function.php";
include "../admin_version.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(){
	include(_SYC_ROOT_.'include/class/clear.class.php');
	
	$path=_SYC_ROOT_.S_CacheFolder;
	$clear=new clear();
	$clear->deleteDir($path);
	unset($clear);
	die("ok");
}

function checkUpdate(){
	exit('<a href="http://www.yicicms.com/" target="_blank">暂时无法在线升级</a>');
	for($uu=0;$uu<3;$uu++){
		$UpdateContent=@file_get_contents("http://vip.syccms.com/php/update.xml");
		if($UpdateContent) break;
	}
	if(!IsNul($UpdateContent)){
		$UpdateContent=geturl("http://vip.syccms.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://www.yicicms.com" 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://'";
		}
	else{
		$sql="Select * from s_article where Lower(Left(S_Pic,7))='http://'";
		}
	$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(){
	$ver = file_get_contents('http://yiciserver.sinaapp.com/index.php?mod=version');
	$ver = json_decode($ver,true);
	if (isset($ver['status']) && $ver['status']==1) {
		return $ver['data'][0];
	}
}	
?>