<?php
if(!defined('MOD'))  die("access die");
purviewCheck('site', 'plugin');
$action = $_GET['action'] ? $_GET['action'] : 'manage';
switch($action){
	case 'manage':
		$limit = 20;
		$page = max(intval($_GET['page']), 1);
		$offset = ($page - 1) * $limit ;
		$verify = intval($_GET['verify']);
		$where = $verify ? " where status = 0 " : "";
		$data = qdb::fetchAll("select * from ".TABLEPRE."site {$where} order by id desc limit {$offset}, {$limit}");
		$total = qdb::queryAlone("select count(*) as num from ".TABLEPRE."site  {$where} ");
	break;

	case 'add':
		if($_POST['myFormAc'] == 'update'){
			$param = $_POST['param'];
			$param['status'] = intval($param['status']);
			$param['ordering'] = intval($param['ordering']);
			if(!$param['name'] || !$param['url']) qbase::showmsg("出错了，请填写完整数据");
			$sql = qbase::createSql(TABLEPRE . "site", $param, 'insert');
			qdb::query($sql);
			cache::delete('friendlink');
			qbase::showmsg("友情链接添加成功", "default", "plugin.php?plugin=site&action=manage");
		}
	break;

	case 'modify':
		$id = intval($_GET['id']);
		$data = qdb::queryFirst("select * from ".TABLEPRE."site where id = '{$id}' ");
		if(empty($data)) qbase::showmsg("bad request");
		if($_POST['myFormAc'] == 'update'){
			$param = $_POST['param'];
			$param['status'] = intval($param['status']);
			$param['ordering'] = intval($param['ordering']);
			if(!$param['name'] || !$param['url']) qbase::showmsg("出错了，请填写完整数据");
			$sql = qbase::createSql(TABLEPRE . "site", $param, 'update') . " where id = '{$id}' ";
			qdb::query($sql);
			cache::delete('friendlink');
			qbase::showmsg("友情链接修改成功", "default", "plugin.php?plugin=site&action=manage");
		}
	break;

	case 'delete':
		checkAdminReferer();
		$id = intval($_GET['id']);
		qdb::query("delete from ".TABLEPRE."site  where id = '{$id}' ");
		cache::delete('friendlink');
		qbase::showmsg("删除成功", "default", $_SERVER['HTTP_REFERER']);
	break;

	case 'set':
		$file = R_P . "/config/friend_link.php";
		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['plugin_site_open'] = intval($_POST['plugin_site_open']);
			$param['plugin_site_logo'] = intval($_POST['plugin_site_logo']);
			$html = "<?php\n";
			foreach($param as $key=>$val){
				$html .= "\$_SET['{$key}'] = '{$val}';\n";
			}
			$html .= "?>";
			qbase::writetofile($file, $html);
			echo qbase::xmlprint("操作成功");
			exit();
		}else{
			if(is_file($file)) @include_once($file);
			else{
				$_SET['plugin_site_open'] = 0;
				$_SET['plugin_site_logo'] = 0;
			}
		}
	break;
}
include qbase::template($path . 'plugin/site');
?>