<?php
require(dirname(__FILE__)."/"."global.php");

if($job=='apply'){
	if(!$lfjid){
		showerr('請先登錄');
	}
}

$query = $db->query("SELECT * FROM {$pre}friendlink_sort ORDER BY list DESC");
while($rs = $db->fetch_array($query)){
	$sortdb[]=$rs;
}

$SQL="WHERE yz=1 AND (endtime=0 OR endtime>$timestamp) ";
if($fid){
	$SQL.=" AND fid='$fid' ";
}

$colordb[$fid]='red;';
$rows=50;
if(1>$page){
	$page=1;
}
$min=($page-1)*$rows;

$showpage=getpage("{$pre}friendlink","$SQL","?","$rows");
$query = $db->query("SELECT * FROM {$pre}friendlink $SQL LIMIT $min,$rows");
while($rs = $db->fetch_array($query)){
	$rs[logo]=tempdir($rs[logo]);
	$_listdb[]=$rs;
}

$num=5-count($_listdb)%5;
for($i=0;$i<$num;$i++ ){
	$_listdb[]=array('display'=>'none');
}
$listdb=array_chunk($_listdb,5);


if($_POST)
{
	if(!$lfjid){
		showerr('請先登錄');
	}
	if(!get_cookie("yzImgNum")||get_cookie("yzImgNum")!=$yzimg)
	{
		showerr("驗證碼不符合");
	}
	else
	{
		set_cookie("yzImgNum","0");
	}
	
	if(!$postdb[name]){
		showerr("站點名稱不能為空");
	}
	if(!$postdb[fid])
	{
		showerr("請選擇一個分類");
	}
	if(!$postdb[url]){
		showerr("站點地址不能為空");
	}

	foreach( $_FILES AS $key=>$value ){

		if(is_array($value)){
			$postfile=$value['tmp_name'];
			$array[name]=$value['name'];
			$array[size]=$value['size'];
		} else{
			$postfile=$$key;
			$array[name]=${$key.'_name'};
			$array[size]=${$key.'_size'};
		}
		if($ftype[1]=='in'&&$array[name]){

			if(!eregi("(gif|jpg|png)$",$array[name])){
				showerr("LOGO,只能上傳GIF,JPG,PNG格式的文件,你不能上傳此文件:$array[name]");
			}
			$array[path]=$webdb[updir]."/friendlink";
	
			$array[updateTable]=1;	//統計用戶上傳的文件佔用空間大小
			$filename=upfile($postfile,$array);
			$postdb[logo]="friendlink/$filename";
		}

	}
	if($postdb[logo]&&!eregi("(gif|jpg|png)$",$postdb[logo])){
		showerr("LOGO,只能上傳GIF,JPG,PNG格式的文件,你不能上傳此文件:$array[name]");
	}
	
	if(!strstr($postdb[url],'htttp://')){
		$postdb[url]="htttp://".$postdb[url];
	}
	$postdb[name]=filtrate($postdb[name]);
	$postdb[url]=filtrate($postdb[url]);
	$postdb[descrip]=filtrate($postdb[descrip]);
	$postdb[logo]=filtrate($postdb[logo]);
}

if($action=='reg')
{
	if(!$lfjid){
		showerr('請先登錄');
	}
	$db->query("INSERT INTO `{$pre}friendlink` (`name` , `url` ,`fid` , `logo` , `descrip` , `list`,ifhide,yz,iswordlink,uid,username ) VALUES ('$postdb[name]','$postdb[url]','$postdb[fid]','$postdb[logo]','$postdb[descrip]','0','1','0','0','$lfjuid','$lfjid')");
	refreshto("?","你的申請資料已經提交succeed,請等待管理員審核後,才可以顯示出來",'10');
}
else
{
	$select_fid=select_fsort("postdb[fid]","");
	require(PHP168_PATH."inc/head.php");
	require(html("friendlink"));
	require(PHP168_PATH."inc/foot.php");
}




function select_fsort($name,$ckfid){
	global $db,$pre;
	$show="<select name='$name'><option value=''>請選擇</option>";
	$query = $db->query("SELECT * FROM {$pre}friendlink_sort ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$ckk=$ckfid==$rs[fid]?' selected ':'';
		$show.="<option value='$rs[fid]' $ckk>$rs[name]</option>";
	}
	$show.="</select>";
	return $show;
}


?>