<?php
/**
* FILE_NAME : friendlink.php
*
* @copyright Copyright (c) www.shuangyulin.com
* @author 双鱼林设计
* @package 友情链接管理
* @subpackage
* @version QQ:287307421或254540457
*/

define('IN_WEB', TRUE);
	
require_once( str_replace('\\', '/', dirname(__FILE__)). '/include/global.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])) {
    $_REQUEST['act'] = 'show';
} else {
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 友情链接列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
	$sql = $query = $friendlist = '';
	
	$sql = "SELECT * FROM {$dbprefix}friendlinks ORDER BY displayorder";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
		$friendlist[] = $row;
	}
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("friendlist", $friendlist);
	
	$smarty->display("friendlink.html");
}

/*------------------------------------------------------ */
//-- 增加友情链接
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->display("friendlink_add.html");
}

/*------------------------------------------------------ */
//-- 增加友情链接成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	$friend = $_REQUEST['friend'];
	$friend['filepath'] = '';
	$sql = $query = '';
	
	if ($_FILES['logopic']['name'] != ''){
		require_once( WEB_ROOT."admin/include/admin_upload.class.php");
		require_once( WEB_ROOT."admin/include/admin_miniature.class.php");
		
		$file = $_FILES['logopic'];
		$upload_path = '../attachments/frindlink';
		$allow_type=array('jpg','bmp','png','gif','jpeg');
		$max_size=2048000;
		
		$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
		$upload->upload();
		$pic = $upload->getSaveFileInfo();

		/*--  缩略图  --*/
		$m=new Miniature();
		$str_file = WEB_ROOT.substr($pic['file'],2);
		$m->ReadImage($str_file);
		$m->ResizeImage(88,31);//智能缩小
		$m->ImageType=4;//保存成jpg格式
		$logo=$pic['basename'];
		$m->SaveImage($pic['thumb_dir'],$logo);
		
		/*--  删除原图  --*/
		$dir = WEB_ROOT.substr($pic['path'],3).'/';                         
		//打开 images 目录
		$dirhandle = opendir($dir);
		
		/*--  列出 images 目录中的文件  --*/
		while (($file = readdir($dirhandle)) !== false) {
		  $file = $dir.$pic['savename'];
		  @unlink($file);
		}
		 closedir($dirhandle);
		
		$friend['logo'] = $logo.'.jpg';
		$friend['filepath'] = $pic['thumb_dir'];
		
		$m->DestroyImage();//销毁图片
	}
	
	/*--  插入信息  --*/
	$sql = "INSERT INTO {$dbprefix}friendlinks (id,name,url,logo,filepath,displayorder)
			VALUE ('','".$friend['name']."','".$friend['url']."','".$friend['logo']."','".$friend['filepath']."',
			'".$friend['displayorder']."')";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("友情链接添加成功！","friendlink.php?act=show");
	} else {
		showmsg("友情链接添加失败！","friendlink.php?act=show");
	}
}

/*------------------------------------------------------ */
//-- 编辑友情链接信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$id = $_REQUEST['id'];
	
	$sql = $query = $row = '';
	
	/*--  查询信息  --*/
	$sql = "SELECT * FROM {$dbprefix}friendlinks WHERE id = $id";
	$query = $DB->query($sql);
	$list = $DB->fetch_array($query);
	
	$list['logo'] = $web_url.substr($list['filepath'],3).$list['logo'];
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("link_id",$list['id']);
	$smarty->assign("friendname",$list['name']);
	$smarty->assign("friendurl",$list['url']);
	$smarty->assign("friendlogo",$list['logo']);
	$smarty->assign("friendorder",$list['displayorder']);
	
	$smarty->display("friendlink_edit.html");
}

/*------------------------------------------------------ */
//-- 编辑友情链接成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'editok')
{
	$friend = $_REQUEST['friend'];
	$sql = $query = '';
	
	/*--  查询出原始数据  --*/
	$sql = "SELECT * FROM {$dbprefix}friendlinks WHERE id = '".$friend['id']."'";
	$query = $DB->query($sql);
	$oldfriend = $DB->fetch_array($query);
	
	if ($oldfriend['logo']) {
		/*--  取图片名  --*/
		$ext = strrchr($oldfriend['logo'],'.');
		$basename = basename($oldfriend['logo'],$ext);
		
		if ($_FILES['logopic']['name'] != ''){
			require_once( WEB_ROOT."admin/include/admin_upload.class.php");
			require_once( WEB_ROOT."admin/include/admin_miniature.class.php");
			/*--  实例化上传类  --*/
			$file = $_FILES['logopic'];
			$upload_path = '../attachments/frindlink';
			$allow_type=array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();

			/*--  缩略图  --*/
			$m=new Miniature();
			$str_file = WEB_ROOT.substr($pic['file'],2);
			$m->ReadImage($str_file);
			$m->ResizeImage(88,31);//智能缩小
			$m->ImageType=4;//保存成jpg格式
			$m->SaveImage($oldfriend['filepath'],$basename);
			
			/*--  删除原图  --*/
			$dir = WEB_ROOT.substr($pic['path'],3).'/';                         
			//打开 images 目录
			$dirhandle = opendir($dir);
			
			/*--  列出 images 目录中的文件  --*/
			while (($file = readdir($dirhandle)) !== false) {
			  $file = $dir.$pic['savename'];
			  @unlink($file);
			}
			 closedir($dirhandle);
			 
			$m->DestroyImage();//销毁图片
		}
	} else {
		$basename = '';
		
		if ($_FILES['logopic']['name'] != ''){
			require_once( WEB_ROOT."admin/include/admin_upload.class.php");
			require_once( WEB_ROOT."admin/include/admin_miniature.class.php");

			$file = $_FILES['logopic'];
			$upload_path = '../attachments/frindlink';
			$allow_type=array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();

			/*--  缩略图  --*/
			$m=new Miniature();
			$str_file = WEB_ROOT.substr($pic['file'],2);
			$m->ReadImage($str_file);
			$m->ResizeImage(88,31);//智能缩小
			$m->ImageType=4;//保存成jpg格式
			$m->SaveImage($pic['thumb_dir'],$pic['basename']);
			 
			$m->DestroyImage();//销毁图片
			
			/*--  修改信息  --*/
			$friend['name'] = $pic['basename'].'.jpg';
			$sql = "UPDATE {$dbprefix}friendlinks SET logo = '".$friend['name']."', filepath = '".$pic['thumb_dir']."' WHERE id = '".$friend['id']."'";
			$query = $DB->query($sql);
		}
	}
	
	/*--  修改信息  --*/
	$sql = "UPDATE {$dbprefix}friendlinks SET name = '".$friend['name']."', url = '".$friend['url']."', displayorder = '".$friend['displayorder']."' WHERE id = '".$friend['id']."'";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("友情链接修改成功！","friendlink.php?act=show");
	} else {
		showmsg("友情链接修改失败！","friendlink.php?act=show");
	}
}

/*------------------------------------------------------ */
//-- 删除友情链接
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$id = $_REQUEST['id'];
	$sql = $query = '';
	
	/*--  删除信息  --*/
	$sql = "DELETE FROM {$dbprefix}friendlinks WHERE id = $id";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("友情链接删除成功！","friendlink.php?act=show");
	} else {
		showmsg("友情链接删除失败！","friendlink.php?act=show");
	}
}
?>