<?php
ob_start();
include("CheckSession.php");
?>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

require_once("class/FileConfig.php");
require_once("class/FileOperation.php");

$db = new DBOperation();
$fo = new FileOperation();

$id = $_GET["id"];
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
if($_SESSION['rightLevel'] == "0")
{
	$querySql = "SELECT * FROM FileData where CId = '".$db->mysql_real_escape_string($id)."'";
}
elseif($_SESSION['rightLevel'] == "1")
{
	$querySql = "SELECT * FROM FileData,AdminUser,GroupRights where AFlag = '1' and CId = '".$db->mysql_real_escape_string($id)."' and AGroupId = GId And AGroupId = ".$_SESSION['groupId'];
}
elseif($_SESSION['rightLevel'] == "2")
{
	$querySql = "SELECT * FROM FileData where CId = '".$db->mysql_real_escape_string($id)."' and CCreateAcc = '".$_SESSION['userId']."'";
}
$db->query($querySql);
$row = $db->fetch_row();
$db->close_db();
if($row != null)
{
	$CMemo = $row[1];
	$CNewFileName = $row[2];
	$COrgFileName = $row[3];
}
else
{
	echo '<script type="text/javascript">';
  echo 'alert("無權限下載檔案!!!!");';
  echo 'window.close();';
  echo '</script>';
  exit();
}

if(str_indexof($_SESSION['rights'],"C1Q") >= 0)
{ 
  //$fileNameData = $fo->get_basename(rtrim($CNewFileName, '/'));
  $downloadUrl = $_FILECONFIG['UsedDir'].$CNewFileName;
  header("Content-type: text/html; charset=utf-8");
  header('Content-type:application/force-download');
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header("Content-Length: " .(string)(filesize($downloadUrl)) );
  header("Content-Disposition: attachment; filename=".iconv("utf-8", "big5", $COrgFileName));
  ob_clean();
  flush();
  readfile(iconv("utf-8", "big5", $downloadUrl));
}
else
{
	echo '<script type="text/javascript">';
  echo 'alert("無權限下載檔案!!!!");';
  echo 'window.close();';
  echo '</script>';
  exit();
}
?>