<?php
/**
* FILE_NAME : attachment.php
*
* @copyright Copyright (c) 2012-2013 www.shuangyulin.com
* @author 双鱼林
* @package 附件管理
* @subpackage
* @version QQ287307421或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 = $attlist = '';
	
	$sql = "SELECT * FROM {$dbprefix}attachments";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
//		$row['uploadtime'] = formatdate($_CFG['format_type'],$row['uploadtime']);
		$row['size'] = formatbyte($row['size'],2);
		$attlist[] = $row;
	}
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("attlist", $attlist);
	
	$smarty->display("attachment.html");
}

/*------------------------------------------------------ */
//-- 上传附件
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->display("attachment_add.html");
}

/*------------------------------------------------------ */
//-- 上传附件成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	require_once( WEB_ROOT."admin/include/admin_upload_file.class.php");

	/*--  初始化变量，实例化上传类  --*/
	$file = $_FILES['file'];
	$upload_path='../attachments/upload';
	$allow_type=array('jpg','bmp','png','gif','jpeg','rar','zip','pdf');
	$max_size=2048000;
	
	$upload=new upFiles($file, $upload_path, $max_size, $allow_type);
	$picnum = $upload->upload();
	$pic = $upload->getSaveFileInfo();
	
	for ($i=0;$i<$picnum;$i++){
		$sql = "INSERT INTO {$dbprefix}attachments (id,filename,temname,attachtype,size,filepath,downloads) VALUE ('','".$pic[$i]['name']."','".$pic[$i]['savename']."','".$pic[$i]['type']."','".$pic[$i]['size']."','".$pic[$i]['path']."',0)";
		$query = $DB->query($sql);
	}
	
	if ($query) {
		showmsg("文件上传成功","attachment.php?act=show");
	} else {
		showmsg("文件上传失败");
	}
}

/*------------------------------------------------------ */
//-- 删除附件
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$id = $_REQUEST['id'];
	
	$sql = $row = $query = '';
	$sql = "SELECT * FROM {$dbprefix}attachments WHERE id = $id";
	$query = $DB->query($sql);
	$row = $DB->fetch_array($query);
	
	$filepath = WEB_ROOT.substr($row['filepath'],3);                      
	//打开附件目录
	$dir = opendir($filepath);
	
	$file = $filepath.'/'.$row['temname'];
	if (is_file($file)) {
		@unlink($file);
	}
	closedir($dir);
	  
	$sql = "DELETE FROM {$dbprefix}attachments WHERE id = $id";
	$query = $DB->query($sql);

	if ($query) {
		showmsg("文件删除成功","attachment.php?act=show");
	} else {
		showmsg("文件删除失败");
	}
}

/*------------------------------------------------------ */
//-- 删除全部附件
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'delall')
{
	$delid = isset($_REQUEST['delid']) ? $_REQUEST['delid'] : '';

	$list = array();
	$list = explode('/',$delid);
	
	for ($i=0;$i<count($list);$i++) {
		$sql = $row = $query = '';
		$sql = "SELECT * FROM {$dbprefix}attachments WHERE id = '".$list[$i]."'";
		$query = $DB->query($sql);
		$row = $DB->fetch_array($query);
		
		$filepath = WEB_ROOT.substr($row['filepath'],3);                         
		//打开附件目录
		$dir = opendir($filepath);
		
		$file = $filepath.'/'.$row['temname'];
		if (is_file($file)) {
			@unlink($file);
		}
		closedir($dir);
		  
		$sql = "DELETE FROM {$dbprefix}attachments WHERE id = '".$list[$i]."'";
		$query = $DB->query($sql);
	}
	
	showmsg("删除成功","attachment.php?act=show");
}
?>