<?php
header('Content-Type: text/html; charset=UTF-8');
require(dirname(__FILE__) .'/check.php');


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


if ( OT::GetStr2( OT::ParaArr('mudi') ) == 'upFileDeal' ){


	// 打开用户表，并检测用户是否登录
	$MB->Open('','login',2);
	$MB->Close();


	$upPathMode		= OT::PostStr('upPath');
	$saveTo			= OT::PostStr('saveTo');

	switch ($upPathMode){
		case 'product':
			$upPath = ProductFileAdminDir;
			break;

		case 'images':
			$upPath = ImagesFileAdminDir;
			break;

		case 'download':
			$upPath = DownloadFileAdminDir;
			break;

		case 'users':
			$upPath = UsersFileAdminDir;
			break;

		case 'adminFile':
			$upPath = FileAdminDir;

		default:
			$upPathMode = 'info';
			$upPath = InfoImgAdminDir;
			break;

	}

	$sysImagesArr = Cache::PhpFile('sysImages');

	$isDir	= $sysImagesArr['SI_isDir'];

	$saveDirPath = Info::DateTimeDir($sysImagesArr['SI_isDir']);
	if (strlen($saveDirPath) > 0){
		if (! File::CreateDir(OT_ROOT . str_replace('../','',$upPath) . $saveDirPath)){
			$saveDirPath = '';
		}
	}

	$upFileArr = File::AddUploadPara($_FILES['upFile']);
	// die($upFileArr['ext']);
	if (! File::IsUploadType($upFileArr['ext'],'common')){
		die('
		<script language="javascript">
		alert("上传失败（格式：'. $upFileArr['ext'] .'）！请选择允许上传的文件格式");window.close();
		</script>
		');
	}

	$fileRelPath = $saveDirPath . $upFileArr['newName'];
	$fileAbsPath = OT_ROOT . str_replace('../','',$upPath) . $fileRelPath;
	if (! File::Upload($upFileArr['tmp_name'], $fileAbsPath)){
		die('
		<script language="javascript" type="text/javascript">
		alert("上传失败，请重新上传.（请检查目标目录是否可写，或服务器最大上传限制。'. (get_cfg_var('upload_max_filesize') ? get_cfg_var('upload_max_filesize') : '不允许上传附件') .'）");window.close();
		</script>
		');
	}

	$fileMode		= OT::Post('fileMode');
	$fileFormName	= OT::Post('fileFormName');
	$fileDir		= OT::Post('fileDir');

	$imgUrl = GetUrl::Dir(true,1) . str_replace('../','',$upPath) . $fileRelPath;

	$record=array();
	$record['UF_time']		= TimeDate::Get();
	$record['UF_oss']		= $saveTo;
	$record['UF_type']		= $upPathMode;
	$record['UF_oldName']	= $upFileArr['name'];
	$record['UF_name']		= $fileRelPath;
	$record['UF_fileName']	= $upFileArr['newName'];
	$record['UF_ext']		= $upFileArr['ext'];
	$record['UF_size']		= filesize($fileAbsPath);

	if (strpos('|gif|jpg|jpeg|png|bmp|','|'. $upFileArr['ext'] .'|') !== false){
		$imgPath = $upPath . $fileRelPath;
		/* if (Is::ImgMuma($imgPath)){
			File::Del($imgPath);
			JS::AlertCloseEnd('该图片含有可疑代码，疑似木马图片，禁止上传。\n\n请用图片处理软件重新保存下或更换张图片.');
		} */
	}

	$DB->InsertParam('upFile',$record);

	if ($saveTo != 'web'){
		$ossArr = AreaApp::OssDeal($saveTo, $fileRelPath, $upPath . $fileRelPath);
		if ($ossArr['res']){
			$imgUrl = $ossArr['path'];
			$fileRelPath = $ossArr['path'];
		}else{
			die('
			<script language="javascript" type="text/javascript">
			alert("上传失败，请检查【插件参数设置】-[云存储] - '. $ossArr['name'] .' 设置是否正确\n失败可能原因：'. Str::MoreReplace($ossArr['note'],'js') .'");window.close();
			</script>
			');
		}
	}

	echo('
	<script language="javascript" type="text/javascript">
	');
	if ($fileMode=='editor'){
		echo('
		try { opener.$id("upImgStr").value +="'. $fileRelPath .'|"; }catch (e) {}
		opener.InsertStrToEditor(\''. $fileFormName .'\',\''. ExtHtmlStr($upFileArr['name'], $imgUrl, $upFileArr['ext']) .'\');
		');
	}elseif ($fileMode=='input'){
		echo('
		opener.document.getElementById("'. $fileFormName .'").value="'. $fileRelPath .'";
		');
	}else{
		echo('
		opener.'. $fileFormName .'.value="'. $imgUrl .'";
		');
	}
	echo('
	window.close();
	</script>
	');

	$DB->Close();
	die();
}


function ExtHtmlStr($title, $fileUrl, $fileExt){
	$extDir = GetUrl::Dir(true,1) .'upFiles/infoImg/ext/';
	switch ($fileExt){
		case 'bmp': case 'jpg': case 'jpeg': case 'gif': case 'png':
			$retStr = '<img src="'. $fileUrl .'" border="0">';
			break;

		case 'swf': case 'doc': case 'xls': case 'ppt': case 'pdf': case 'txt': case 'docx': case 'xlsx': case 'pptx': case 'rar': case 'zip': case 'iso': case 'js': case 'mdb': case 'psd': case 'xml': case 'mp3':
			$retStr = '<a href="'. $fileUrl .'" target="_blank"><img src="'. $extDir . $fileExt .'.gif" border="0" alt="'. $fileExt .'类型" title="'. $fileExt .'类型">'. $title .'</a>';
			break;

		case 'avi': case 'mpeg': case 'mpg': case 'ra': case 'rm': case 'rmvb': case 'mov': case 'qt': case 'asf': case 'wmv': case 'mp4': case 'wma': case 'wav': case 'mod': case 'cd': case 'md': case 'aac': case 'mid': case 'ogg': case 'm4a':
			$retStr = '<a href="'. $fileUrl .'" target="_blank"><img src="'. $extDir .'mov.gif" border="0" alt="视频音频类型" title="视频音频类型">'. $title .'</a>';
			break;

		default:
			$retStr = '<a href="'. $fileUrl .'" target="_blank"><img src="'. $extDir .'file.gif" border="0" alt="'. $fileExt .'类型" title="'. $fileExt .'类型">'. $title .'</a>';
			break;

	}
	
	return $retStr;
}

?>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><!-- big5  gb2312 gbk -->
	<title>上传文件</title>
	<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
	<meta name="robots" content="none">
	<link rel="stylesheet" type="text/css" href="template/share.css" />
</head>

<body style="margin:8px;">

<script language='javascript' type='text/javascript'>
// 检测表单
function CheckForm(){
	if (document.getElementById("upFile").value == ""){
		alert("请选择文件");return false;
	}

	initAd();
	return true;
}

</script>

<style type="text/css">
#sponsorAdDiv {position:absolute; height:1; width:1; top:100px; left:100px;}
</style>
<script language='javascript' type='text/javascript'>
adTime=8;
chanceAd=1;
var ns=(document.layers);
var ie=(document.all);
var w3=(document.getElementById && !ie);
adCount=0;

function initAd(){
	if(!ns && !ie && !w3) return;
	if(ie)	adDiv=eval('document.all.sponsorAdDiv.style');
	else if(ns)	adDiv=eval('document.layers["sponsorAdDiv"]');
	else if(w3)	adDiv=eval('document.getElementById("sponsorAdDiv").style');
	randAd=Math.ceil(Math.random()*chanceAd);
	if (ie||w3)
		adDiv.visibility="visible";
	else
		adDiv.visibility ="show";
	if(randAd==1) showAd();
}

function showAd(){
	if(adCount<adTime*10){
		adCount+=1;
		if (ie){
			documentWidth  =document.body.offsetWidth/2+document.body.scrollLeft-20;
			documentHeight =document.body.offsetHeight/2+document.body.scrollTop-20;
		}else if (ns){
			documentWidth=window.innerWidth/2+window.pageXOffset-20;
			documentHeight=window.innerHeight/2+window.pageYOffset-20;
		}else if (w3){
			documentWidth=self.innerWidth/2+window.pageXOffset-20;
			documentHeight=self.innerHeight/2+window.pageYOffset-20;
		}
		adDiv.left=documentWidth-150;adDiv.top =documentHeight-25;
		setTimeout("showAd()",100);
	}
}
function closeAd(){
	if (ie||w3)
		adDiv.display="none";
	else
		adDiv.visibility ="hide";
}
</script>

<?php
$URL = OT::GetParam(array('upMode'));
?>
<div id="sponsorAdDiv" style="visibility:hidden">
<table style="width:250px; height:70px;" border='0' cellspacing='1' class="border1_1" bgcolor="#ACD6FF"><tr><td align='center'>
	<table style="width:100%; height:100%;"  border='0' cellspacing='0' summary=''>
	<tr><td align="center" class="font2_2">
		正在上传文件，请稍候......
	</td></tr>
	</table>
</td></tr></table>
</div>


<form id='dealForm' name='dealForm' method='post' action="?mudi=upFileDeal" enctype="multipart/form-data" onsubmit="return CheckForm()" style="width:800px;margin:0 auto;">
<input type="hidden" name="upPath" value="<?php echo(urlencode(OT::GetStr('upPath'))); ?>" />
<input type="hidden" name="fileMode" value="<?php echo(urlencode(OT::GetStr('fileMode'))); ?>" />
<input type="hidden" name="fileFormName" value="<?php echo(urlencode(OT::GetStr('fileFormName'))); ?>" />

<div class='tabMenu'>
<ul>
	<?php echo(AdmArea::UploadNavBox($URL, 'upFile')); ?>
</ul>
</div>


<table align='center' cellpadding="0" cellspacing="0" border="0" summary=''>
<tr>
	<td>
		<fieldset><legend class='font2_2' style="font-size:14px;"><b>上传文件</b></legend>
		<table style="width:750px; height:110px;" border="0" align="center" summary=''>
		<tr> 
			<td class="font1_2">本地文件：</td>
			<td colspan="3" align="left">
				<input type="file" size="28" style="width:380px;" id="upFile" name="upFile" />
				<span id="fileAlert" style="color:red;"></span>
			</td>
		</tr>
		<tr> 
			<td class="font1_2">&ensp;&ensp;保存至：</td>
			<td colspan="3" align="left" class="font1_2">
				<?php echo(AdmArea::UploadSaveBox($sysAdminArr['SA_upFileOss'])); ?>
			</td>
		</tr>
		<tr>
			<td colspan="4" align="center">
				<div style="float:right;padding:8px 2px 3px 0;">
					<span title="php.ini 里可调整 post_max_size 参数">表单限制：<?php echo((get_cfg_var('post_max_size') ? ''. get_cfg_var('post_max_size') : '')); ?></span>，<span title="php.ini 里可调整 upload_max_filesize 参数">上传限制：<?php echo((get_cfg_var('upload_max_filesize') ? ''. get_cfg_var('upload_max_filesize') : '')); ?></span>
				</div>
				<input type="image" src="<?php echo(AdmTpl::ImgSrc('button_upload')); ?>" />
			</td>
		</tr>
		</table>
		</fieldset>
	</td>
</tr>
</table>
</form>

<script language='javascript' type='text/javascript'>
var fileForm = document.getElementById("upFile");
fileForm.onchange = function () {
	var fileSize = fileForm.files[0].size;
	var fileName = fileForm.files[0].name;
	if (fileSize > 1024*1024*20){
		document.getElementById("fileAlert").innerHTML = "<div style='padding:5px 1px;font-weight:bold;'>（文件大小："+ getFileSize(fileSize) +"，超过20M，可能会上传超时，如上传超时，建议使用【后台大文件上传】插件来上传）</div>";
	}
}

// 计算文件大小函数(保留两位小数), Size为字节大小
function getFileSize(size) {
	if (! size){ return "--"; }

	var num = 1024;
	if (size < num){
		return size + "字节";
	}else if (size < Math.pow(num, 2)){
		return (size / num).toFixed(2) + "KB"; //kb
	}else if (size < Math.pow(num, 3)){
		return (size / Math.pow(num, 2)).toFixed(2) + "MB"; //M
	}else if (size < Math.pow(num, 4)){
		return (size / Math.pow(num, 3)).toFixed(2) + "GB"; //G
	}else{
		return (size / Math.pow(num, 4)).toFixed(2) + "TB"; //T
	}
}
</script>

</body>
</html>