<?php

 require_once dirname(__FILE__)."/../../bin/configuration/configFactory.php";
 require_once dirname(__FILE__)."/../../bin/system/utility.php";
 require_once dirname(__FILE__)."/../pageBase.php";

 class upload extends pageBase
 {
 	public function load()
 	{
		 parent::load();
		 if(parent::isPostBack())
		 {	
			
	 		$config=configFactory::createConfiguration();
	 		$type=trim($config->getConfigValue("appsettings","allowImageType"));	//圖片類型
	 		$size=-1;	//文件大小上限
	 		$baseDir=trim($config->getConfigValue("appsettings","uploadDir"));		//上傳目錄
	 		$autoDir=trim($config->getConfigValue("appsettings","autoUploadDir"));	//是否創建新目錄
			
	 		$baseDir=utility::removeDirSlash($baseDir); 	 		
			$baseDir="../../".$baseDir;
			if(!is_dir($baseDir))
				mkdir($baseDir,0777);
			$saveDir=$baseDir."/";
			if($autoDir)
			{
				$saveDir=$baseDir."/".date("Ymd")."/";
				if(!is_dir($saveDir))
					mkdir($saveDir,0777);
			}
			$file=$_FILES["userfile"];
			$fileName=$file["name"];
			$fileExt="";//後綴
			$fileSize=$file["size"];
			if(strrpos($fileName,"."))
				$fileExt = substr($file["name"],strrpos($file["name"],".")+1);

			if($fileName=="")
			{
				$this->showMsg("請選擇要上傳的檔案!");
			}
			else if($fileExt == "" || strpos(strtolower($type),strtolower($fileExt)) === false)
			{
				$this->showMsg("請選擇合適的檔案類型!\\r\\n\\r\\n允許類型為:".$type);
			}
			else if($size == 0)
			{
				$this->showMsg("系統設置不能上傳檔案，如有疑問請聯繫管理員!");
			}
			else if($size > 0 && $file["size"] > $size * 1024)
			{
				$this->showMsg("檔案不能大於".$size."KB!");
			}
			else
			{
				//$fn=$saveDir.$file["name"];					
				$fn=$saveDir.date("YmdHis").floor(microtime()*1000);
				if(strrpos($file["name"],"."))
				{
					$fn .= substr($file["name"],strrpos($file["name"],"."));
				}
				if(!move_uploaded_file($file["tmp_name"],$fn))
				{
					$this->showMsg("檔案上傳失敗!");
				}
				else
				{
					echo "<script language=\"javascript\">";
					echo "if(window.opener != undefined){";
					echo "window.opener.returnValue = \"".str_replace("../../","/",$fn)."\";";
					echo "}else{";
					echo "window.returnValue = \"".str_replace("../../","/",$fn)."\";}";
					echo "window.close();</script>";
					exit;
				}
			}
		}
 	}
 	
 	private function showMsg($msg)
 	{
 		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
	 	echo "<script language=\"javascript\">window.alert(\"".$msg."\");history.back();</script>";
 	}
 }
  
 $page=new upload();
 $page->load();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="expires" content="0">
	<base target="_self" />
	<title>圖片檔案上傳</title>
	<link type="text/css" rel="stylesheet" href="../css/main.css" />
<style>
<!--
body {
  background: #cfcfcf;
  padding: 10px;
  text-align:center;
  height:20px;
}
#upload{
	margin:auto;
	margin-top:20px;
	margin-bottom:20px;
}
#upload div{
	padding-top:10px;
}
#upload div input{
	margin-right:30px;
}
-->
</style>
</head>

<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upimage2.php">
<div id="upload">
	<div><input type="file" id="userfile" name="userfile" /> </div>
	<div><input type="button" id="btnOK" name="btnOK" value="確 定" class="btn" onclick="btn_click(this);" /><input type="button" id="btnCancel" name="btnCancel" value="返 回" class="btn" onclick="btn_click(this);" /></div>
</div><!--#upload end-->
</form>
</body>
</html>
<script language="javascript">
<!--
	function btn_click(sender)
	{
		if(sender == document.getElementById("btnOK"))
		{
			if(document.getElementById("userfile").value == "")
			{
				window.alert("請選擇要上傳的檔案!");
				return;
			}
			else
			{
				document.getElementById("form1").submit();
			}
		}
		else if(sender == document.getElementById("btnCancel"))
		{
			window.returnValue = "";
			window.close();
		}
	}
//-->
</script>