<%
include("language.html")
if _SESSION["logined"] ~= nil then
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add User Quickly</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
local domain = ""
if _GET["domain"] ~= nil then
	domain = _GET["domain"]
	local user = ""
%>
<style type="text/css">
a {text-decoration:none;}
a,a:visited {color:#000;background:inherit;}
img {border:0px;}
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
body,td,span {margin:0;padding:0;font:12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%else%>
body,td,span {margin:0;padding:0;font:11px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%end%>
input,select,textarea{margin:4px;}
input,textarea{background-color:expression((this.disabled && this.disabled==true)?"#D4D0C8":"")}
td{line-height:20px;}

button{
border:none;background:none;padding:0;
margin:0;width:auto;overflow:visible;text-align:center;
white-space:nowrap;height:24px;line-height:24px;
cursor:pointer;
color:#000;
margin-left:2px;
margin-top:2px;
}
button span, button em{
display:block;height:24px;line-height:22px;margin:0;
}	
button span{
font-size:8pt;
padding-left:12px;
background:url("images/button_left.gif") no-repeat 0 0;
}
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
button em{
font-style:normal;padding-right:14px;
background:url("images/button.gif") no-repeat 101% 0;
}
<%else%>
button em{
font-style:normal;padding-right:10px;
background:url("images/button.gif") no-repeat 100% 0;
}
<%end%>
button:hover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}
button span:hover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}
.buttonhover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}


.listhead
{
		border-top: 1px solid white; border-right: 1px solid #ACA899; border-left: 1px solid white; border-bottom: 2px solid #E1E2E3;
		font: 12px/22px arial; cursor: hand; text-align: center; background-color: #EFEFEF; overflow:hidden; height:18px;
}

.listhead2
{
		border-top: 1px solid white; border-right: 1px solid #ACA899; border-left: 1px solid white; border-bottom: 2px solid #FCC247;
		font: 12px/22px arial; cursor: hand; text-align: center; background-color: #FAF9F4; overflow:hidden; height:18px;
}

.listtr01
{
		background-color: #FFFFFF;
		text-align: left;
		padding: 3px;
}

.listtr02
{
		background-color: #3366CC;
		color: #FFF;
		text-align: left;
		padding: 3px;
}

.resizeblock{
	background-color:transparent;
	width:2px;
	float:right;
	cursor:e-resize;
}

</style>

<!–[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/ie6.css" />
<![endif]–>

<script language="javascript" src="include/common.js"></script>
<script language="javascript" src="include/json.js"></script>
<script language="javascript">

var ajaxlock = false;
var xmlhttp,page,post;
xmlhttp = isIE ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();

function ajaxRequest(pagename,poststring,method)
{
	if(ajaxlock == false)
	{	
		delete xmlhttp;
		xmlhttp = isIE ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();

		ajaxlock = true;
		page = pagename;
		post = poststring;

		if(method == "GET")
			xmlhttp.open("GET",page+".html"+post);
		else
			xmlhttp.open("POST",page+".html");

		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange = ajaxResponse;
		if(method != "GET")
			xmlhttp.send(post);
		else
			xmlhttp.send("");
	}
}

function ajaxResponse()
{
	if (xmlhttp.readyState == 4)
	{
		ajaxlock = false;
		if (xmlhttp.status == 200)
		{
			try
			{
				switch(page)
				{
					case 'admin_adduser':		addUser_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}


var directories = new Array();
var ipmasks = new Array();
var filemasks = new Array();
var usergroups = new Array();
var schedules = new Array();
var subdir_perm = new Array();
var last_dir_id = null;

function addUserSubmit()
{
	var domain = $("domain").value;
	var username = $("username").value;
	var password = $("password").value;
	if(domain.length == 0)
	{
		alert("<%=LANG['str_no_domain']%>");
		return false;
	}
	if(username.length <= 0 || username.length > 64 || username.search(/[\\\/\<\>\:\?\"\'\|\[\]\{\}\*\&\=\%]/i) != -1 || username == " ")
	{
		alert("<%=LANG['str_invalid_username']%>");
		$("username").focus();
		return false;
	}
	//if(password.search(/[\\\/\<\>\:\?\"\'\|]/i) != -1)
	//{
	//	alert("<%=LANG['str_invalid_pass']%>");
	//	$("password").focus();
	//	return false;
	//}
	password = password.replace(/\\/g,'\\\\');
	password = password.replace(/\"/g,'\\"');

	if(password.length < 32)
	{
		var checkPassResult = checkPass(password);
		if(checkPassResult != '')
		{
			alert(checkPassResult);
			return false;
		}
	}

	var user = {
	username:trim($("username").value),
	password: password,
	max_download:"0",
	max_upload:"0",
	max_download_account:"0",
	max_upload_account:"0",
	max_connection:"0",
	connect_timeout:"<%=c_GetOptionInt(domain,DOPTION_COMMAND_TIMEOUT_INT)%>",
	idle_timeout:"<%=c_GetOptionInt(domain,DOPTION_TRANSFER_TIMEOUT_INT)%>",
	connect_per_ip:"0",
	pass_length:"0",
	show_hidden_file:0,
	change_pass:0,
	send_message:0,
	ratio_credit:"0",
	ratio_download:"1",
	ratio_upload:"1",
	ratio_count_method:0,
	enable_ratio:0,
	current_quota:"0",
	max_quota:"0",
	enable_quota:0,
	note_name:"",
	note_address:"",
	note_zip:"",
	note_phone:"",
	note_fax:"",
	note_email:"",
	note_memo:"",
	ipmasks:ipmasks,
	filemasks:filemasks,
	directories:directories,
	usergroups:usergroups,
	schedules:schedules,
	subdir_perm:subdir_perm,
	enable_schedule:0,
	limit_reset_type:"0",
	limit_enable_upload:0,
	cur_upload_size:"0",
	max_upload_size:"0",
	limit_enable_download:0,
	cur_download_size:"0",
	max_download_size:"0",
	enable_expire:0,
	expiretime:"",
	protocol_type:63,
	enable_password:($("enable_password").checked ? 1:0),
	enable_account:1,
	ssh_pubkey_path:"",
	enable_ssh_pubkey_auth:0,
	ssh_auth_method:0,
	enable_weblink:1,
	enable_uplink:1
	};

	var userString = encodeURIComponent(JSON.stringify(user));
	ajaxRequest("admin_adduser","domain="+domain+"&user="+userString+"&r=" + Math.random());
}

function addUser_result()
{
	var result = xmlhttp.responseText;
	if(result == "0" || result == 0)
	{
		alert("<%=LANG['str_existed_username']%>");
	}
	else if(result == "4" || result == 4)
	{
		alert("<%=LANG['str_existed_aduser']%>");
	}
	else if(result == "-1" || result == -1)
	{
		alert("<%=LANG['session_expire']%>");
		top.location = "admin_login.html";
	}
	else
	{
		top.closewindow(1);
	}
}


function AddDirectory()
{
	var homedir_check = 1;
	var f_dir = "";
	if(directories.length > 0)
		f_dir = urlEncode(JSON.stringify(directories[0]));

	var frametag = new Date().getTime();
	showMessagebox("<%=LANG['str_adddir']%>","<iframe src='admin_add_directory.html?homedir="+homedir_check+"&dir="+f_dir+"&seeds=" + Math.random() +"' width=450 height=320 border=0 frameborder=0 id='add"+frametag+"'></iframe>",AddDirectory_callback,450,320);
}

function AddDirectory_updateview()
{
	if(directories.length == 0)
		return;
	$("homedir").value = directories[0].dir;
}

function AddDirectory_callback(dirObject)
{
	if(typeof(dirObject) == "object")
	{
		if(dirObject.is_home_dir)
		{
			directories[0] = null;
			directories[0] = dirObject;
		}
		else
		{
			alert("<%=LANG['str_no_homedir']%>");
		}
		dirObject = null;
		AddDirectory_updateview();
	}
	$("hiddenInput").focus();
	$("hiddenInput").blur();
}

var charArray = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','~','!','@','#','$','%','^','&','*','_','-','+','=','(',')','{','}','[',']','<','>','?'];
function generateRandom(len) 
{
	len = <%=c_GetOptionInt(_GET["domain"],DOPTION_PASSWORD_LEN_INT)%>;
	if(len < 8)
	{
		len = 8;
	}
	var password_complexity = <%=c_GetOptionInt(_GET["domain"],DOPTION_INC_NONALPHANUMERIC_INT)%>;
    var result = "";

	for(var n=0; n<100; n++) 
	{
		for(var i=0; i<len; i++) 
		{
			var index = 0;
			if(password_complexity)
				index = Math.ceil(Math.random()*(charArray.length-1) );
			else
				index = Math.ceil(Math.random()*61 );
			result += charArray[index];
		}

		if(checkPass(result) == "")
			return result;
		else
			result = "";
	}
    return result;
}

function checkPass(s)
{   
	var len = <%=c_GetOptionInt(_GET["domain"],DOPTION_PASSWORD_LEN_INT)%>;
	var password_complexity1 = <%=c_GetOptionInt(_GET["domain"],DOPTION_INC_NUMBER_INT)%>;
	var password_complexity2 = <%=c_GetOptionInt(_GET["domain"],DOPTION_INC_LOWERCASE_INT)%>;
	var password_complexity3 = <%=c_GetOptionInt(_GET["domain"],DOPTION_INC_UPPERCASE_INT)%>;
	var password_complexity4 = <%=c_GetOptionInt(_GET["domain"],DOPTION_INC_NONALPHANUMERIC_INT)%>;


	if(s.length < len)
		return '<%=LANG["str_min_password"]%> >='+len;

	if(password_complexity1 && !s.match(/([0-9])+/))
		return '<%=LANG["str_password_complexity"]%> <%=LANG["str_password_complexity1"]%>';

	if(password_complexity2 && !s.match(/([a-z])+/))
		return '<%=LANG["str_password_complexity"]%> <%=LANG["str_password_complexity2"]%>';

	if(password_complexity3 && !s.match(/([A-Z])+/))
		return '<%=LANG["str_password_complexity"]%> <%=LANG["str_password_complexity3"]%>';

	if(password_complexity4 && !s.match(/[^a-zA-Z0-9]+/))
		return '<%=LANG["str_password_complexity"]%> <%=LANG["str_password_complexity4"]%>';
   

	return '';
 } 

</script>
</head>
<body style="background-color:#E6E6E6;">

<input id="domain" type="hidden" value="<%=_GET["domain"]%>">
<table width="100%" cellSpacing="0" cellPadding="0" border="0" align="center">
<tr style="background-color:#FFF;"><td colspan="2" style="padding:12px;"><img src="images/user48.png"> <%=LANG["str_quickadduser_tip"]%></td></tr>

<tr><td height="25" style="padding-left:6px; white-space:nowrap;"><%=LANG["str_username"]%>:&nbsp;&nbsp;</td><td><input id="username" type="text" style="width:195px;" maxlength="64" autocomplete="off"> <input id="anonymous" type="checkbox" onclick="checkAnonymous();"><%=LANG["str_anonymous"]%></td></tr>
<tr><td height="25" style="padding-left:6px; white-space:nowrap;"><%=LANG["str_password"]%>:</td><td><input id="password" type="text" style="width:150px;" autocomplete="off"> 
<button onclick="$('password').value = generateRandom(8);" title="<%=LANG["str_generate_pass"]%>"><span style="vertical-align:middle;"><em><img src="images/password.gif" align="absmiddle">&nbsp;</em></span></button>
<input id="enable_password" type="checkbox" onclick="checkPassword();" checked><%=LANG["str_enablepass"]%>
<input id="hiddenInput" type="text" style="width:0px;height:0px; border:0px;"></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td height="25" style="padding-left:6px;"><%=LANG["str_homedir"]%></td><td><input id="homedir" type="text" style="width:200px;" onclick='AddDirectory();' readonly> <button onclick='AddDirectory();'><span><em><%=LANG["button_choose"]%></em></span></button></td></tr>
</table>

<div style="margin-top:20px;width:100%;text-align:center;">
<button type='submit' onclick='addUserSubmit();'><span><em><%=LANG["button_submit"]%></em></span></button>&nbsp;
<button onclick='top.closewindow();'><span><em><%=LANG["button_cancel"]%></em></span></button>
</div>

<script type="text/javascript">

$("hiddenInput").focus();
$("hiddenInput").blur();
$("username").focus();

$("password").value = generateRandom(8);

function checkAnonymous()
{
	if($("anonymous").checked)
	{
		$("username").value = "anonymous";
		$("username").disabled = true;
		$("password").value = "";
		$("password").disabled = true;
		$("enable_password").checked = false;
	}
	else
	{
		$("username").value = "";
		$("username").disabled = false;
		$("password").disabled = false;
		$("enable_password").checked = true;
	}
	checkPassword();
}

function checkPassword()
{
	if($("enable_password").checked)
	{
		$("password").disabled = false;
	}
	else
	{
		$("password").disabled = true;
	}
}
checkPassword();

</script>
</body>
</html>

<% end %>

<% 
else
	print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
	print("<script>alert('"..LANG["session_expire"].."');top.location='admin_login.html';</script>")
end 
%>

<noscript><center><H2><%=LANG["error_nojavascript"]%></H2></center></noscript>