<%
if _SESSION["username"] == nil then
%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache, must-revalidate" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="include/common.js"></script>
<body>
<%
include("language.html")

local username = _GET["username"] or _POST["username"] or ""
local password = _GET["password"] or _POST["password"] or ""
local remember = _GET["remember"] or _POST["remember"] or ""
local redir = _GET["redir"] or _POST["redir"] or ""

username = string.gsub(username,"%%20"," ")
password = string.gsub(password,"%%20"," ")


local result = c_CheckUser(username,password)
if result ~= OK_CHECK_CONNECTION then
	c_AddWebLog("User '"..username.."' login failed! (IP:".._REMOTE_IP..")","0",DOMAIN_LOG_WEB_RESPOND)
	print("<script>alert('"..LOGINERROR_STR[tonumber(result)].."');location='login.html';</script>")
else
	if _COOKIE["UID"] ~= nil then
		_SESSION_ID = _COOKIE["UID"]
		local retval = SessionModule.load(_SESSION_ID)
		if retval == false then
			_SESSION_ID = SessionModule.new()
			if _UseSSL == true then
				_SETCOOKIE = _SETCOOKIE.."Set-Cookie: UID=".._SESSION_ID.."; HttpOnly; secure\r\n"
			else
				_SETCOOKIE = _SETCOOKIE.."Set-Cookie: UID=".._SESSION_ID.."; HttpOnly\r\n"
			end
			rawset(_COOKIE,"UID",_SESSION_ID)
		end
	else
		_SESSION_ID = SessionModule.new()
		if _UseSSL == true then
			_SETCOOKIE = _SETCOOKIE.."Set-Cookie: UID=".._SESSION_ID.."; HttpOnly; secure\r\n"
		else
			_SETCOOKIE = _SETCOOKIE.."Set-Cookie: UID=".._SESSION_ID.."; HttpOnly\r\n"
		end
		rawset(_COOKIE,"UID",_SESSION_ID)
	end

	_USERNAME = username
	_NOWPATH = "/"
	rawset(_SESSION,"username",username)
	rawset(_SESSION,"currentpath","/")
	rawset(_SESSION,"ipaddress",_REMOTE_IP)
	SessionModule.save(_SESSION_ID)

	c_AddConnection(username,"/",_REMOTE_IP,_SESSION_ID,"LOGIN OK")
	c_AddWebLog("User '"..username.."' logged in ok! (IP:".._REMOTE_IP..")",_SESSION_ID,DOMAIN_LOG_WEB_RESPOND)

	local strBrowser = "Web Browser"
	local strUserAgent = string.match(strHead,"User%-Agent:%s?(%s[^\r\n]*)")
	if strUserAgent ~= nil then
		strUserAgent = string.lower(strUserAgent)
		if string.find(strUserAgent, "windows phone") then
			strBrowser = "Windows Phone"
		elseif string.find(strUserAgent, "android") then
			strBrowser = "Android"
		elseif string.find(strUserAgent, "iphone") then
			strBrowser = "iPhone"
		elseif string.find(strUserAgent, "ipad") then
			strBrowser = "iPad"
		elseif string.find(strUserAgent, "ipod") then
			strBrowser = "iPod"
		elseif string.find(strUserAgent, "msie") then
			strBrowser = "MSIE"
		elseif string.find(strUserAgent, "opera") then
			strBrowser = "Opera"
		elseif string.find(strUserAgent, "firefox") then
			strBrowser = "Firefox"
		elseif string.find(strUserAgent, "chrome") then
			strBrowser = "Chrome"
		elseif string.find(strUserAgent, "netscape") then
			strBrowser = "Netscape"
		elseif string.find(strUserAgent, "safari") then
			strBrowser = "Safari"
		end
	else
		strBrowser = ""
	end

	c_SetClientVersion(_SESSION_ID, strBrowser)

	c_DoWebEvent(WEB_USER_LOGIN_EVENT,_SESSION_ID)

	if remember ~= "" then
		setcookie("client_login_name",username,900000000)
	end


	if strBrowser ~= "" then
%>
<script language="javascript">

	var welcomeMessage = "<%=specialhtml_encode(c_GetWelcomeMessage())%>";
	welcomeMessage = welcomeMessage.replace(/&lt;BR&gt;/ig,"<br>");

	if(welcomeMessage != "")
	{
		showMessagebox("<%=LANG['welcome']%>","<table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0' style='padding:10px;'><tr><td height=260 align='left'><div style='height:260px; overflow:auto; clear:none;'><div style='float: left;'><img src='images/info.gif' align='absmiddle'>&nbsp;&nbsp;</div>"+welcomeMessage+"</div></td></tr><tr align='center'><td><button id='btn_submit' type='submit' onclick='login();' tabindex='1'><span><em><%=LANG['create_submit']%></em></span></button>&nbsp;&nbsp;&nbsp;<button id='btn_cancel' onclick='logout();' tabindex='2'><span><em><%=LANG['create_cancel']%></em></span></button></td></tr></table>", login, 420, 320);

		if(isIE)
		{
			$("btn_submit").onclick = function(){login();};
			$("btn_cancel").onclick = function(){logout();};
		}
	}
	else
	{
		login();
	}

	function login()
	{
		<% if redir ~= "" then %>
		gotoUrl("main.html?redir=<%=specialhtml_encode(redir)%>");
		<% else %>
		gotoUrl("main.html");
		<% end %>
	}

	function logout()
	{
		gotoUrl("logout.html");
	}

	function gotoUrl(url)
	{
		var isBrowserIE = navigator.userAgent.indexOf('MSIE')==-1? 0:1;
		if (isBrowserIE) 
		{
			try
			{
				var referLink = document.createElement("a");
				referLink.href = url;
				document.body.appendChild(referLink);
				referLink.click();
			}
			catch(e)
			{
				window.location = url;
			}
		} 
		else
		{
			window.location = url;
		}
	}

</script>
<%
	else
		print("<script>location='main.html';</script>")
	end

end
%>
</body>
</html>

<% 
else
	print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
	print("<meta http-equiv='Developer' content='www.wftpserver.com'>")
	print("<script>top.location='main.html';</script>")
end 
%>
