<?php
if(!defined('R_P')) exit('Access Denied');
@include_once(R_P . "/config/friend_link.php");
if(!intval($_SET['plugin_site_open'])) base::showmsg("Friend-link is closed");
$_SET['plugin_site_logo'] = intval($_SET['plugin_site_logo']);
$url = REWRITE ? "index-ac-friend_link.html" : "index.php?ac=friend_link";
$op = $_GET['op'] ? trim($_GET['op']) : 'view';
switch($op){
	case 'view':
		$data = qdb::fetchAll("select * from ".TABLEPRE."site order by ordering desc, id desc");
	break;

	case 'add':
		if($_POST['myFormAc'] == 'update'){
			check_sesion_code();			
			$param = get_friends_post();
			if(!$param['name'] || !$param['url'] || !$param['pwd'] || ($_SET['plugin_site_logo'] && !trim($param['logo']))) qbase::showmsg("出错了，请填写完整数据");
			$param['pwd'] = md5(strtolower($param['pwd']));
			$sql = qbase::createSql(TABLEPRE . "site", $param, 'insert');
			qdb::query($sql);
			qbase::showmsg("友情链接添加成功", "default", $url);
		}else{
			$data = array();
		}
	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'){
			check_sesion_code();
			$param = get_friends_post();
			if(md5(strtolower($param['pwd'])) != $data['pwd'] || !$data['pwd']) qbase::showmsg("密码错误，请重新输入", $_SET['skin'], "index.php?ac=friend_link&op=modify&id={$id}");
			if(intval($_POST['delete']) > 0){
				qdb::query("delete from ". TABLEPRE . "site where id = '{$id}' ");
				qdb::query("删除成功", $_SET['skin'], $url);
			}else{
				if(!$param['name'] || !$param['url'] || ($_SET['plugin_site_logo'] && !trim($param['logo']))) qbase::showmsg("出错了，请填写完整数据");
				unset($param['pwd'], $param['ordering']);
				$sql = qbase::createSql(TABLEPRE . "site", $param, 'update') . " where id = '{$id}' ";
				qdb::query($sql);
				qdb::query("您的修改需要经过管理员重新审核，请等待", $_SET['skin'], $url);
			}
		}
	break;

	case 'url':
		$id = intval($_GET['id']);
		$url = qdb::queryAlone("select url from ". TABLEPRE . "site where id = '{$id}' ");
		if(empty($url)) qbase::showmsg("bad request");	
		qdb::query("update ". TABLEPRE . "site set views = views + 1 where id = '{$id}' ");
		header("location:" . $url);
		exit();
	break;

	default:
		qbase::showmsg("bad request");
	break;
}
include qbase::template($_SET['skin'] . '/friend_link');

function check_sesion_code(){
	global $_SET, $url;
	ini_get('session.auto_start') || @session_start();
	$code = trim($_SESSION['code']);
	$verify_code = trim($_POST['verify_code']);
	if(!$code || $code != $verify_code) qbase::showmsg("出错了，验证码错误");
}

function get_friends_post(){
	$field = array('name','url','description','logo','pwd');
	$param = $_POST['param'];
	foreach($param as $key=>$val){
		if(!in_array($key, $field)) qbase::showmsg("bad request");
	}
	$param['status'] = 0;
	$param['ordering'] = 0;
	$param['pwd'] = trim($param['pwd']);
	$param['description'] = qbase::substrs(trim(htmlspecialchars(strip_tags($param['description']))), 0, 250);
	$param['logo'] = trim(htmlspecialchars(strip_tags($param['logo'])));
	return $param;
}
?>