<?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 = $config->getConfigValue("appsettings", "allowUploadType"); // 文件類型
				$size = intval($config->getConfigValue("appsettings", "allowImageSize")); // 文件大小上限
				$baseDir = utility::removeDirSlash($config->getConfigValue("appsettings", "uploadDir")); // 上傳目錄
				$autoDir = filter_var($config->getConfigValue("appsettings", "autoUploadDir"), FILTER_VALIDATE_BOOLEAN); // 是否創建新目錄
		
				$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 (empty($fileName)) {
					$this->showMsg("請選擇要上傳的檔案!");
				} elseif (empty($fileExt) || stripos(strtolower($type), strtolower($fileExt)) === false) {
					$this->showMsg("請選擇合適的檔案類型!\\r\\n\\r\\n允許類型為:" . $type);
				} elseif ($size === 0) {
					$this->showMsg("系統設置不能上傳檔案，如有疑問請聯繫管理員!");
				} elseif ($size > 0 && $fileSize > $size * 1024 * 1024) {
					$this->showMsg("檔案不能大於" . $size . "KB!");
				} else {
					$fn = $saveDir . date("YmdHis") . floor(microtime(true) * 1000);
					if (strrpos($fileName, ".")) $fn .= substr($fileName, strrpos($fileName, "."));
					if (!move_uploaded_file($file["tmp_name"], $fn)) {
						$this->showMsg("檔案上傳失敗!");
					} else {
						echo "<script language=\"javascript\">window.returnValue=\"" . str_replace("../../", "/", $fn) . "\";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 { padding: 10px; background: #cfcfcf; height: 20px; text-align:center; }
	#upload { margin: 20px auto; }
	#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="upfile.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>
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>