<?php
	//last modified on 2015-04-17 

	/*
		单图上传
	*/
	function imagebutton($form='info',$field='thumb',$default='')
	{
		global $sitelang;
		if($default && is_image($default))
		{
			$imginfo=imageinfo(str_replace('//','/',SYSTEM_ROOT.$default));
			return '<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="'.$default.'" /><input accept="image/gif,image/jpeg,image/x-png" style="height:22px; border:#ccc 1px solid" onchange="$(\'#'.$field.'_img\').attr(\'src\',this.value);" type="file" id="'.$field.'_file" name="'.$field.'_file" /> <input type="button" onclick="$(\'#'.$field.'_file\').clone(true).insertAfter($(\'#'.$field.'_file\'));$(\'#'.$field.'_file\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" /><br /><img width="'.($imginfo['width']>240?240:$imginfo['width']).'" src="'.formaturl($default).'" id="'.$field.'_img" style="border:#EEEEEE 4px solid;margin:2px 0px" />';
		}
		else
		{
			return '<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="" /><input accept="image/gif,image/jpeg,image/x-png" style="height:22px; border:#ccc 1px solid" onchange="$(\'#'.$field.'_img\').attr(\'src\',this.value);" type="file" id="'.$field.'_file" name="'.$field.'_file" /> <input type="button" onclick="$(\'#'.$field.'_file\').clone(true).insertAfter($(\'#'.$field.'_file\'));$(\'#'.$field.'_file\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" />';
		}
	}

	function imageupload($key,$alowexts=array(),$originalname=0,$name='')
	{
		
		global $_userid,$uploadlang,$LA;

		if(isset($_FILES[$key]))
		{
				
			$alowexts=$alowexts && is_array($alowexts)?$alowexts:array('gif','jpg','jpeg','png','bmp');
			$_file_name=$_FILES[$key]['name'];
			$_file_type=preg_replace('#[^0-9a-z\./]#i', '', $_FILES[$key]['type']);
			$_file_tmp_name=str_replace("\\\\", "\\", $_FILES[$key]['tmp_name']);
			$_file_ext=strtolower(get_fileext($_file_name));
			$_file_size=preg_replace('#[^0-9]#','',$_FILES[$key]['size']);

			if(!isuploadedfile($_file_tmp_name))
			{
				return '';
			}

			if($_file_size > $LA['uploadsizelimit'])
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['maxsize_error'].'");self.history.back();</script>');
			}

			if (!is_array(@getimagesize($_file_tmp_name)))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}

			if(!in_array($_file_ext,$alowexts) || !in_array($_file_ext,explode('|',$LA['uploadtype'])))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}
		
			$_file_savename=$originalname?($name?$name.'.'.$_file_ext:basename($_file_name)):$_userid.'_'.time().'.'.$_file_ext;
			$_file_savepath=$originalname?'':date('Y-m-d').'/';
			@mkdir(SYSTEM_ROOT.'upload/image/'.$_file_savepath);
			if(@move_uploaded_file($_file_tmp_name, SYSTEM_ROOT.'upload/image/'.$_file_savepath.'/'.$_file_savename))
			{
				setwatermark(SYSTEM_ROOT.'upload/image/'.$_file_savepath.'/'.$_file_savename);
				return '/upload/image/'.$_file_savepath.$_file_savename;
			}
			else
			{
				return '';
			}
		}

		return '';
	}
	
	/*
		多图上传
	
	function imagesbutton($form='info',$field='thumb',$default='')
	{
		global $sitelang,$MEM,$_roleid;
		$btn='<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="'.$default.'" />';
		$a=array();
		if($default)
		{
			$ims=explode(',',$default);
			for($i=0;$i<sizeof($ims);$i++)
			{
				$im=explode('`',$ims[$i]);
				$ext=strtolower(get_fileext($im[0]));
				$iminfo=imageinfo(SYSTEM_ROOT.$im[0]);
				$a[$i]='<a href="#" class="screenshot" rel="'.formaturl($im[0]).'" width="'.intval(min(450,$iminfo['width']/2)).'" title="'.get_basename($im[1]).'" style="background:url('.LA_PATH.'statics/images/ext/'.$ext.'.gif) left center no-repeat;padding-left:24px">'.get_basename($im[1]).'</a>';
			}	
		}

		for($i=1;$i<=($_roleid?4:$MEM['gradeinfo']['images']);$i++)
		{
			$btn.='<p style="padding:2px 0px"><input accept="image/gif,image/jpeg,image/x-png" style="height:22px; border:#ccc 1px solid" onchange="$(\'#'.$field.'_img\').attr(\'src\',this.value);" type="file" id="'.$field.'_file_'.$i.'" name="'.$field.'_file[]" /> <input type="button" onclick="$(\'#'.$field.'_file_'.$i.'\').clone(true).insertAfter($(\'#'.$field.'_file_'.$i.'\'));$(\'#'.$field.'_file_'.$i.'\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" /></p>';
		}
		
		return implode(',',$a).'<br />'.$btn;
	}

	function imagesupload($key,$alowexts=array(),$originalname=0,$name='')
	{
		global $_userid,$uploadlang,$LA,$MEM,$_roleid;
		$ims=$_FILES[$key];
		$alowexts=$alowexts && is_array($alowexts)?$alowexts:array('gif','jpg','jpeg','png','bmp');
		$result=array();
		
		if(sizeof($ims['tmp_name'])>($_roleid?4:$MEM['gradeinfo']['images']))
		{
			exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['bad_upload'].'");self.history.back();</script>');
		}

		foreach($ims['tmp_name'] as $i => $im)
		{			
			$_file_name=$ims['name'][$i];
			$_file_type=preg_replace('#[^0-9a-z\./]#i', '', $ims['type'][$i]);
			$_file_tmp_name=str_replace("\\\\", "\\",$ims['tmp_name'][$i]);
			$_file_ext=strtolower(get_fileext($_file_name));
			$_file_size=preg_replace('#[^0-9]#','',$ims['size'][$i]);

			if(!isuploadedfile($_file_tmp_name))
			{
				continue;
			}

			if($_file_size > $LA['uploadsizelimit'])
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['maxsize_error'].'");self.history.back();</script>');
			}

			if (!is_array(@getimagesize($_file_tmp_name)))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}

			if(!in_array($_file_ext,$alowexts) || !in_array($_file_ext,explode('|',$LA['uploadtype'])))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}

			$_file_savename=$originalname?($name?$name.'.'.$_file_ext:basename($_file_name)):$_userid.'_'.time().mt_rand(10000,99999).'.'.$_file_ext;
			$_file_savepath=$originalname?'':date('Y-m-d').'/';
			@mkdir(SYSTEM_ROOT.'upload/image/'.$_file_savepath);

			if(@move_uploaded_file($_file_tmp_name, SYSTEM_ROOT.'upload/image/'.$_file_savepath.'/'.$_file_savename))
			{
				setwatermark(SYSTEM_ROOT.'upload/image/'.$_file_savepath.'/'.$_file_savename);
				$result[$i]='/upload/image/'.$_file_savepath.$_file_savename.'`'.str_replace(array('\'','`'),'',get_basename($_file_name));
			}
			else
			{
				continue;
			}
		}

		return implode(',',$result);
	}
	*/
	/*
		附件上传
	*/
	function attachmentbutton($form='info',$field='attach',$default='')
	{
		global $sitelang,$MEM,$LA,$_roleid;
		$btn='<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="'.$default.'" />';
		
		$a=array();
		if($default)
		{
			$atts=explode(',',$default);
			for($i=0;$i<sizeof($atts);$i++)
			{
				$att=explode('`',$atts[$i]);
				$ext=strtolower(get_fileext($att[0]));
				$a[$i]='<a href="'.formaturl($att[0]).'" target="_blank" style="background:url('.LA_PATH.'statics/images/ext/'.$ext.'.gif) left center no-repeat;padding-left:24px">'.get_basename($att[1]).'</a>';
			}	
		}

		for($i=1;$i<=($_roleid?4:$MEM['gradeinfo']['attaches']);$i++)
		{
			$btn.='<p style="padding:2px 0px"><input style="height:22px; border:#ccc 1px solid" type="file" id="'.$field.'_file_'.$i.'" name="'.$field.'_file[]" /> <input type="button" onclick="$(\'#'.$field.'_file_'.$i.'\').clone(true).insertAfter($(\'#'.$field.'_file_'.$i.'\'));$(\'#'.$field.'_file_'.$i.'\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" /></p>';
		}
		
		return implode(',',$a).'<br />'.$btn;
	}

	function attachmentupload($key,$alowexts=array(),$originalname=0,$name='')
	{
		global $_userid,$uploadlang,$LA,$MEM,$_roleid;
		$atts=$_FILES[$key];
		$alowexts=$alowexts && is_array($alowexts)?$alowexts:array('zip','rar','doc','pdf','xls','ppt','txt');
		$result=array();
		
		if(sizeof($atts['tmp_name'])>($_roleid?4:$MEM['gradeinfo']['attaches']))
		{
			exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['bad_upload'].'");self.history.back();</script>');
		}

		foreach($atts['tmp_name'] as $i => $att)
		{			
			$_file_name=$atts['name'][$i];
			$_file_type=preg_replace('#[^0-9a-z\./]#i', '', $atts['type'][$i]);
			$_file_tmp_name=str_replace("\\\\", "\\",$atts['tmp_name'][$i]);
			$_file_ext=strtolower(get_fileext($_file_name));
			$_file_size=preg_replace('#[^0-9]#','',$atts['size'][$i]);

			if(!isuploadedfile($_file_tmp_name))
			{
				continue;
			}

			if($_file_size > $LA['uploadsizelimit'])
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['maxsize_error'].'");self.history.back();</script>');
			}

			if(!in_array($_file_ext,$alowexts) || !in_array($_file_ext,explode('|',$LA['uploadtype'])))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}

			$_file_savename=$originalname?($name?$name.'.'.$_file_ext:basename($_file_name)):$_userid.'_'.time().mt_rand(10000,99999).'.'.$_file_ext;
			$_file_savepath=$originalname?'':date('Y-m-d').'/';
			@mkdir(SYSTEM_ROOT.'upload/file/'.$_file_savepath);

			if(@move_uploaded_file($_file_tmp_name, SYSTEM_ROOT.'upload/file/'.$_file_savepath.'/'.$_file_savename))
			{
				$result[$i]='/upload/file/'.$_file_savepath.$_file_savename.'`'.str_replace(array('\'','`'),'',get_basename($_file_name));
			}
			else
			{
				continue;
			}
		}

		return implode(',',$result);
	}

	/*
		视频上传
	*/
	function videobutton($form='info',$field='video',$default='')
	{
		global $sitelang;
		if($default && is_video($default))
		{
			$ext=strtolower(get_fileext($default));
			return '<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="'.$default.'" /><input style="height:22px; border:#ccc 1px solid" type="file" id="'.$field.'_file" name="'.$field.'_file" /> <input type="button" onclick="$(\'#'.$field.'_file\').clone(true).insertAfter($(\'#'.$field.'_file\'));$(\'#'.$field.'_file\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" /><br /><a  style="background:url('.LA_PATH.'statics/images/ext/'.$ext.'.gif) left center no-repeat;padding-left:24px" href="'.formaturl($default).'" target="_blank">'.get_basename($default).'</a>';
		}
		else
		{
			return '<input type="hidden" name="'.$form.'['.$field.']" id="'.$field.'_val" value="" /><input style="height:22px; border:#ccc 1px solid"  type="file" id="'.$field.'_file" name="'.$field.'_file" /> <input type="button" onclick="$(\'#'.$field.'_file\').clone(true).insertAfter($(\'#'.$field.'_file\'));$(\'#'.$field.'_file\').remove();" style="width:40px;line-height:16px;height:22px; border:#ccc 1px solid" value="'.$sitelang['button-cancel'].'" />';
		}
	}

	function videoupload($key,$alowexts=array(),$originalname=0,$name='')
	{
		
		global $_userid,$uploadlang,$LA;

		if(isset($_FILES[$key]))
		{
				
			$alowexts=$alowexts && is_array($alowexts)?$alowexts:array("flv" , "mp4" , "swf");
			$_file_name=$_FILES[$key]['name'];
			$_file_type=preg_replace('#[^0-9a-z\./]#i', '', $_FILES[$key]['type']);
			$_file_tmp_name=str_replace("\\\\", "\\", $_FILES[$key]['tmp_name']);
			$_file_ext=strtolower(get_fileext($_file_name));
			$_file_size=preg_replace('#[^0-9]#','',$_FILES[$key]['size']);

			if(!isuploadedfile($_file_tmp_name))
			{
				return '';
			}

			if($_file_size > $LA['uploadsizelimit'])
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['maxsize_error'].'");self.history.back();</script>');
			}

			if(!in_array($_file_ext,$alowexts) || !in_array($_file_ext,explode('|',$LA['uploadtype'])))
			{
				exit('<script language="javascript" type="text/javascript">alert("'.$uploadlang['alowexts_error'].'");self.history.back();</script>');
			}
		
			$_file_savename=$originalname?($name?$name.'.'.$_file_ext:basename($_file_name)):$_userid.'_'.time().'.'.$_file_ext;
			$_file_savepath=$originalname?'':date('Y-m-d').'/';
			@mkdir(SYSTEM_ROOT.'upload/video/'.$_file_savepath);
			if(@move_uploaded_file($_file_tmp_name, SYSTEM_ROOT.'upload/video/'.$_file_savepath.'/'.$_file_savename))
			{
				return '/upload/video/'.$_file_savepath.$_file_savename;
			}
			else
			{
				return '';
			}
		}

		return '';
	}

	/*
		上传检查
	*/
	function isuploadedfile($file)
	{
		return (is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\','\\',$file))); 
	}
?>
