<?php
/***********************************************************************************/
/* MassCRM客户关系管理系统							   */
/* ========================      						   */
/* 欢迎使用 MassCRM ,在您开始使用之前,请务必完整阅读本软件附带的全部说明档	   */
/***********************************************************************************/
/* Welcome to http://www.massboxes.com & http://masscrm.massboxes.com		   */
/***********************************************************************************/
	function checkRole($table_name,$module_num,$bu_auto_id,$role_name){
	 	global $smart;
	 	global $boxesDB;
	 			
	 	$boxesDB->setTableName("boxes_user");
		$sql="where bu_auto_id='".$bu_auto_id."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
		
		$bu_role=$boxes_check[0][bu_role];
		
		$boxesDB->setTableName("boxes_role");
		$sql="where br_name='".$bu_role."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$br_value=$boxes_check[0][br_value];
	 	
	 	$boxesDB->setTableName("boxes_function");
		$sql="where (bf_module_num='$module_num' and bf_ename='".$role_name."')";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$bf_auto_id=$boxes_check[0][bf_auto_id];
		
		if(!$bu_role or !$br_value or ($bf_auto_id and substr($br_value,$bf_auto_id-1,1)!=1)){
			$error="请求失败：你没有模块“".$boxes_check[0][bf_module]."”“".$boxes_check[0][bf_name]."”的权限！";
			echo"<script>alert(\"$error\");self.close();</script>";
			exit;
		}
	}
	
	include "../account/".$_REQUEST[account].".boxes.inc.php";
	require_once "../includes/config.inc.php";
	require_once('../adodb/adodb.inc.php');
	require_once "../includes/BiaoCommon.class.php";
	$conn = &ADONewConnection('mysql');
	$conn->debug=false;
	$conn->PConnect($MYSQL_HOSTNAME, $MYSQL_USERNAME, $MYSQL_PASSWORD, $DATABASE);
	$boxesDB=new BiaoCommon($conn); 
	
	$table_name=$_REQUEST[table_name];
	$module_name=$_REQUEST[module_name];
	$module_num=$_REQUEST[module_num];
	$bu_auto_id=$_REQUEST[bu_auto_id];
	
	checkRole($table_name,$module_num,$bu_auto_id,"DOWNLOAD");
	
	$real_file_path = $_REQUEST[file_name];
	if(is_file($real_file_path)){
		@set_time_limit(600);
		$filename = basename($real_file_path);
		$filesize = filesize($real_file_path);

		header("Content-Type: application/force-download; name=\"$filename\"");
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: $filesize");
		header("Content-Disposition: attachment; filename=\"$filename\"");
		header("Expires: 0");
//		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		readfile("$real_file_path");

		//print("<script>window.close();</script>");
		exit();
	}
	else 
		print("<script>window.close();</script>");
?>