<?php
!defined('A')&&die($jsp="<meta http-equiv='refresh' content=0;url='/'>");
/*
	$set[15]  //限定上传图片尺寸（单位MB）
	$set[16]  //限定上传动画尺寸（单位MB）
	$set[17]  //限定上传影音文件尺寸（单位MB）
	$set[18]  //限定上传其它文件尺寸（单位MB）
*/
$ji=5;
if(!$i||$i['rank']<$ji)
	die('<script>alert("'.$ji.' 级以上用户才有上传权限！\n您现在权限是 '.$i['rank'].' 级");parent.closeSafe();</script>');
elseif($i['rank']<10){
	$set[15] = 200;
	$set[16] = 200;
	$set[17] = 200;
	$set[18] = 10;
}elseif($i['rank']<20){
	$set[15] = 500;
	$set[16] = 500;
	$set[17] = 500;
	$set[18] = 100;
}else{
	$set[15] = 2000;
	$set[16] = 2000;
	$set[17] = 2000;
	$set[18] = 2000;
}

@ini_set('default_charset','utf-8');
date_default_timezone_set(SQ); 

$tupian = 'gif|jpg|jpeg|gif|png';
$donghua = 'swf';
$yingyin = 'mp3|mp4';  //'mp3|mp4|wav|wma|wmv|mid|midi|avi|mpg|mpeg|asf|asx|mov|rm|rmvb|ram|ra';  //设置允许上传的影音文件类型，用|分开写
$xiazai  = 'txt|rar|zip|doc|xls|chm|hlp|php|asp|sql';  //设置允许上传的其它文件类型，用|分开写

$inis = ini_get_all();
$uploadmax = $inis['upload_max_filesize'];
if (!is_array($_FILES['uploadfile']) || !$_FILES['uploadfile']['size']) {
	die('<script>alert("出现错误！请暂时停止上传。\n原因分析：\n1、上传数组参数为空 \n2、文件超过系统最大上传限量'.$uploadmax['global_value'].'（'.$uploadmax['local_value'].'）");parent.closeSafe();</script>');
}

if ($_FILES['uploadfile']['size'] > $set[$_REQUEST['in_id']]*1024*1024) {
	die('<script>alert("对不起，上传的文件不能大于'.$set[$_REQUEST['in_id']].'MB"); parent.closeSafe();</script>');
}
if ($_REQUEST['in_id']<15 || $_REQUEST['in_id']>18) {
	die('<script>alert("出现错误！请暂时停止上传。文件类型参数缺失");parent.closeSafe();</script>');
}
if ($set[$_REQUEST['in_id']] == 0) {
  die('<script> alert("系统设定为禁止上传"); parent.closeSafe();</script>');
}
$suffix=substr(strrchr($_FILES['uploadfile']['name'],"."),1);
switch ($_REQUEST['in_id']) {
	case 15:$type = "images"; $reg = $tupian;break;
//	case 16:$type = "swf"; $reg = 'swf';break;
	case 17:$type = $suffix;$reg = $yingyin;break;
	default:$type = $suffix;$reg = $xiazai;break;
}
if (!preg_match('/\.('.$reg.')$/i', $_FILES['uploadfile']['name'], $matches)){
	die('<script>alert("提示！请选择一个有效的文件：允许的格式有（'.$reg.'）");parent.closeSafe();</script>');
}
//上传临时路径
$upload_dir='upload'.D.$type.D.date("Ym");
if(!is_dir($dir=C.D.$upload_dir))FI($dir);

if($fp=@fopen($_FILES['uploadfile']['tmp_name'],'rb')) {
	$img_contents=@fread($fp,$_FILES['uploadfile']['size']);
	@fclose($fp);
}else{
	$img_contents=@file_get_contents($_FILES['uploadfile']['tmp_name']);
}
if($suffix=='php' or $suffix=='asp'){
	if($i['rank']<21)die('<script>alert("提示！没有权限！");parent.closeSafe();</script>');
}
if($suffix!='rar'&&$suffix!='zip'&&preg_match('/<\?php|base64_decode|base64_encode/i',$img_contents,$m_err)){
	die('<script> alert("提示！禁止提交。该文件含有禁止的代码'.str_replace('?','\?', $m_err[0]).'。"); parent.closeSafe();</script>');
}
$upload_file=date('YmdHis').K.$suffix;

if(@move_uploaded_file($_FILES['uploadfile']['tmp_name'],C.D.$upload_dir.D.$upload_file)){
	die('<script type="text/javascript">
  try{
    window.parent.insertFile("'.ML.str_replace('%2F',D,urlencode($upload_dir.D.$upload_file)).'","'.$_REQUEST['in_id'].'");
    parent.closeSafe();
	alert("上传成功！");
  }catch(err){
    alert(err);
  }
  </script>');
}else{
  die('<script>alert("出现错误！请暂时停止上传");parent.closeSafe();</script>');
}