<?php

//接口密码，请修改，并与远程控制端密码一致
$pid = "123456";

/*
 * @version        $Id: linkapi.php 182 2013-09-15 00:32:06Z qinjinpeng $
 */

include dirname(__FILE__)."/include/common.inc.php";

//权限检查
if($pid != $sid){
	echo_json(0, -1);
}

$ac = empty($ac)?"":$ac;

switch($ac){
	
	case "list":
		if(empty($kw)){
			$query = $dsql->Execute("me", "select f.id,f.sortrank,f.url,f.webname,f.dtime,f.ischeck,t.typename from #@__flink f LEFT JOIN #@__flinktype t ON f.typeid=t.id order by id desc");
		}else{
			$kw = array_utf82gb($kw);
			$query = $dsql->Execute("me", "select f.id,f.sortrank,f.url,f.webname,f.dtime,f.ischeck,t.typename from #@__flink f LEFT JOIN #@__flinktype t ON f.typeid=t.id where f.webname like '%$kw%' OR f.url like '%$kw%' order by id desc");
		}
		$rs = array();
		while($row = $dsql->GetArray('me')){
			$rs[] = $row;
		}
		echo_json(1, $rs);
	break;
	
	case "add":
		if(empty($dopost)){
			$query = $dsql->Execute("me", "SELECT * FROM #@__flinktype");
			$flinktype = array();
			while($row = $dsql->GetArray('me')){
				$flinktype[] = $row;
			}
			echo_json(1, 	$flinktype );
		}else{
			$url = array_utf82gb($url);
			$webname = array_utf82gb($webname);
			$msg = array_utf82gb($msg);
			$email = array_utf82gb($email);
			$row = $dsql->GetOne("SELECT * FROM #@__flink where url='$url'");
			if(empty($row)){
				$time = time();
				$dsql->ExecuteNoneQuery("INSERT INTO #@__flink (`url`,`webname`,`sortrank`,`msg`,`email`,`ischeck`,`typeid`,`dtime`) values ('$url', '$webname', '$sortrank', '$msg' ,'$email' ,'$ischeck', '$typeid', '$time')");
				makehtml();
				$status = 1;
			}else{
				$status = -1;
			}
			echo_json( 1, array("id"=>$i, "status"=>$status) );
		}
	break;
	
	case "edit":
		if(!empty($dopost)){
			$url = array_utf82gb($url);
			$webname = array_utf82gb($webname);
			$msg = array_utf82gb($msg);
			$email = array_utf82gb($email);
			$dsql->ExecuteNoneQuery("UPDATE #@__flink SET `sortrank`=$sortrank,url='$url',webname='$webname',msg='$msg',`email`='$email',typeid='$typeid',ischeck='$ischeck'  where id='$id'");
			makehtml();
			echo_json( 1 );
		}else{
			$query = $dsql->Execute("me", "SELECT * FROM #@__flinktype");
			$flinktype = array();
			while($row = $dsql->GetArray('me')){
				$flinktype[] = $row;
			}
			$flink = $dsql->GetOne("SELECT * FROM #@__flink WHERE id='$id'");
			echo_json(1, 	array( 'flink'=>$flink, 'flinktype'=>$flinktype ) );
		}
	break;
	
	case "del":
		$dsql->ExecuteNoneQuery("DELETE FROM `#@__flink` WHERE id IN ($id)");
		makehtml();
		echo_json( 1 );
	break;
	
}


//编码自动转换，支持数组
function array_gb2utf8($arr){
	global $cfg_soft_lang;
	if($cfg_soft_lang!='utf-8'){
		if(is_array($arr)){
			foreach($arr as $k => $v) {
				$arr[$k] = array_gb2utf8( $v );
			}
		}else{
			$arr = gb2utf8( $arr );
		}
	}
	return $arr;
}

function array_utf82gb($arr){
	global $cfg_soft_lang;
	if($cfg_soft_lang=='gb2312'){
		if(is_array($arr)){
			foreach($arr as $k => $v) {
				$arr[$k] = array_utf82gb( $v );
			}
		}else{
			$arr = utf82gb( $arr );
		}
	}
	return $arr;
}

//json输出
function echo_json($status, $data=''){
	global $jsoncallback;
	echo "$jsoncallback(".json_encode( array_gb2utf8( array('status'=>$status, 'data'=>$data) ) ).")";
	exit;
}

function makehtml(){
		global $dsql,$cfg_basedir,$cfg_templets_dir;
		require_once DEDEINC."/arc.partview.class.php";
		$GLOBALS['_arclistEnv'] = 'index';
		$row = $dsql->GetOne("Select * From `#@__homepageset`");
		$row['templet'] = MfTemplet($row['templet']);
		$pv = new PartView();
		$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
		$pv->SaveToHtml(dirname(__FILE__).'/index.html');
}
