<?php
defined('IN_HS') or exit('Access Denied');

$exts = array('php','php3','asp','aspx','cgi','jsp','pl','cfg');

$type = isset($type) ? $type : '';
$rename = isset($rename) ? trim($rename) : '';

if($dosubmit)
{
	if($type == 'overwrite')
	{
		if(!is_writable(HS_ROOT.'/'.$oldname)) showmessage($LANG['please_chmod']." $oldname ".$LANG['to_777_then_upload']);
		$rename = basename($oldname);
		$uploaddir = dirname($oldname);
	}
	elseif(isset($CHA['uploaddir']))
	{
		@extract($CHA);
		$uploaddir = $HS['uploaddir'].'/'.$channeldir.'/'.$uploaddir;
	}
	elseif(isset($MOD['uploaddir']))
	{
		@extract($MOD);
		$uploaddir = $uploaddir ? $HS['uploaddir'].'/'.$moduledir.'/'.$uploaddir : $HS['uploaddir'].'/'.$moduledir;
	}
	else 
	{
		@extract($HS);
	}

	if($rename)
	{
		$ext = fileext($rename);
		if(in_array($ext, $exts)) showmessage($LANG['illegal_extension']);
	}

	$uploaddir = $rename=='' ?  $uploaddir.'/'.date('Ym').'/' : $uploaddir.'/';
	$maxfilesize = isset($maxfilesize) ? $maxfilesize : 2048000 ;

	include_once HS_ROOT.'/include/upload.class.php';

	$fileArr = array('file'=>$_FILES['uploadfile']['tmp_name'],'name'=>$_FILES['uploadfile']['name'],'size'=>$_FILES['uploadfile']['size'],'type'=>$_FILES['uploadfile']['type'],'error'=>$_FILES['uploadfile']['error']);
	$uploadfiletype = isset($uploadfiletype) ? $uploadfiletype : $HS['uploadfiletype'];

	dir_create($uploaddir);

	$upload = new upload($fileArr, $rename, $uploaddir, $uploadfiletype, 1, $maxfilesize);
    if($upload->up())
	{
		if($type == 'thumb' || $type == 'both' || $type == 'water')
		{
			include HS_ROOT.'/include/watermark.class.php';
			$water_pos = isset($water_pos) ? $water_pos : $HS['water_pos'];
			$wm = new watermark($upload->saveto, 10, $water_pos);
		}
		$querystring ='';
		if($type == 'thumb')
		{
			if($HS['enablethumb'])
			{
				$width = $newwidth ? $newwidth : $HS['thumb_width'];
				$height = $newheight ? $newheight : $HS['thumb_height'];
				$wm->thumb($width,$height);
			}
			$querystring = '&width='.$width.'&height='.$height;
		}
		elseif($type == 'both')
		{
			if($HS['enablethumb'])
			{
				$thumbname='thumb_'.basename($upload->saveto);
				$width = $width ? $width : $HS['thumb_width'];
				$height = $height ? $height : $HS['thumb_height'];
				$wm->thumb($width,$height,$uploaddir.$thumbname);
			}
			$querystring = '&width='.$width.'&height='.$height;
		}
		elseif($type == 'water')
		{
			if($HS['water_type']==1)
			{
				$water_text = $water_text ? $water_text : $HS['water_text'];
                $water_fontcolor = $water_fontcolor ? $water_fontcolor : $HS['water_fontcolor'];
				$water_fontsize = $water_fontsize ? $water_fontsize : $HS['water_fontsize'];
				$wm->text($water_text,$upload->saveto,$water_fontcolor,$water_fontsize,$HS['water_font']);
			}
			elseif($HS['water_type']==2)
			{
				$water_image = $water_image ? $water_image : $HS['water_image'];
				$wm->image($water_image,$upload->saveto);
			}
		}
		if($type != 'overwrite' && isset($oldname) && $oldname && !preg_match("/^http:\/\//i",$oldname))
		{
			@unlink(HS_ROOT.'/'.$oldname);
		}
		showmessage($LANG['upload_success']."<script language='javascript'>window.opener.myform.".$uploadtext.".value='".$upload->saveto."';</script>","?".$PHP_QUERYSTRING.$querystring);
    }
	else
	{
		showmessage($upload->errmsg(), '?'.$PHP_QUERYSTRING.$querystring);
	}
}
else
{
	include admintpl('uppic', 'hs');
}
?>