#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n "";
RAMDISK="/usr/ramdisk"
if [ -f ${RAMDISK}/PG.conf ]; then
        . ${RAMDISK}/PG.conf
        PGPATH="${RAMDISK}"
else
        . /etc/PG.conf
fi
FLOWEYE=${PGPATH}/bin/floweye
LDAPAUTH=${PGPATH}/bin/ldapauth
myself="/cgi-bin/`basename $0`"

echo -n "
<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">
<head><title>޸</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">
<META HTTP-EQUIV=\"Cache-Control\" content=\"no-cache\">
<META name=\"viewport\" content=\"width=device-width,user-scalable=no\"/> 
<style type=\"text/css\">
body {background:#ffffff;font-family:arial;}
.login {position:absolute;width:350px;height:200px;top:40%;left:45%;margin-top:-100px;margin-left:-150px;}
</style></head><body>
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function TrimAll(str)
{
        var s=\"\";
        for (i = 0; i < str.length; i++) {
                if (str.charAt(i) != ' ') {
                        s = s + str.charAt(i);
                }
        }
        return s;
}
function beforeCommit(frm)
{
	var usrname = document.getElementById(\"username\");
	var curpwd  = document.getElementById(\"curpwd\");
	var passwd1 = document.getElementById(\"passwd1\");
	var passwd2 = document.getElementById(\"passwd2\");
	usrname.value = TrimAll(username.value);
	if (usrname.value == \"\") {
		alert(\"û!\");
		usrname.select();
		return false;
	}
	if (curpwd.value == \"\") {
		alert(\"뵱ǰ!\");
		curpwd.select();
		return false;
	}
	passwd1.value = TrimAll(passwd1.value);
	if (passwd1.value == \"\") {
		alert(\"!\");
		passwd1.select();
		return false;
	}
	passwd2.value = TrimAll(passwd2.value);
	if (passwd2.value != passwd1.value) {
		alert(\"벻ͬ!\");
		passwd1.select();
		return false;
	}
	return true;
}
function onGotoLogin()
{
        window.location.href = \"/cgi-bin/login?clientip=${CGI_clientip}\";
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
  mkdir -p ${RAMDISK}/tmp
  ${FLOWEYE} webauth stat > ${RAMDISK}/tmp/webauth.stat
  . ${RAMDISK}/tmp/webauth.stat
  [ "${webauth_script}" = "NULL" ] && webauth_script=""
  if [ "${webauth_type}" = "1" ]; then  
      if [ "${webauth_adserver}" = "0.0.0.0" -o "${webauth_adport}" = "0" -o "${webauth_adpath}" = "" ]; then 
        echo -n "<script language=javascript> alert(\"޸ʧܣ½򲻴ڣϵԱ\"); </script>"
    	else 
        echo "<br><br><br><br><br><center><h2>Ժύ޸ġ</h2></center>"        
        webauth_adpath=`echo "${webauth_adpath}" | sed "s/{loginDN}/${CGI_username}/g"`
        errmsg=`${LDAPAUTH} -h ${webauth_adserver} -p ${webauth_adport} -D ${webauth_adpath} -w ${CGI_curpwd} -s ${CGI_passwd1} 2>/dev/null`
        if [ "$?" = "0" ]; then
            echo -n "<script language=javascript> alert(\"޸ĳɹ!\"); </script><META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=/cgi-bin/login\" />"
        else
            echo -n "<script language=javascript> alert(\"޸ʧ!\"); </script>"
        fi
    	fi
  else
    	errmsg=`${FLOWEYE} usrinfo set name=${CGI_username} passwd="${CGI_curpwd}:${CGI_passwd1}"`
    	if [ "$?" != "0" ]; then
                    echo "<script language=javascript>"
                    echo "alert(\"ʧ:${errmsg}!\");"
                    echo "</script>"
    	else
                    echo "<script language=javascript>"
                    echo "alert(\"ɹ!\");"
                    echo "</script><META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=/cgi-bin/login\" />"
    	fi
  fi
fi

echo -n "
<body>
<div class=\"login\" id=\"login\">
<form action=\"${myself}\" method=\"POST\" onsubmit=\"return beforeCommit(this)\" enctype=\"multipart/form-data\">
<table>
	<tr><td colspan=\"2\" align=\"center\"><img src=\"/login_logo.gif\" /></td></tr>
	<tr>
		<td style=\"width:90px;font-size:14px;\" align=\"center\"><b>û</b></td>
		<td><input type=\"text\" value=\"${CGI_username}\" id=\"username\" name=\"username\" style=\"width:170px;\" /></td>
	</tr>
	<tr><td align=\"center\" style=\"font-size:14px;\"><b>ǰ</b></td>
	<td><input type=\"password\" value=\"${CGI_curpwd}\" id=\"curpwd\" name=\"curpwd\" style=\"width:170px\" /></td></tr>
	<tr><td align=\"center\" style=\"font-size:14px;\"><b></b></td>
	<td><input type=\"password\" value=\"${CGI_passwd1}\" id=\"passwd1\" name=\"passwd1\" style=\"width:170px\" /></td></tr>
	<tr><td align=\"center\" style=\"font-size:14px;\"><b></b></td>
	<td><input type=\"password\" value=\"${CGI_passwd2}\" id=\"passwd2\" name=\"passwd2\" style=\"width:170px\" /></td></tr>
	<tr><td colspan=\"2\"></td></tr>
	<tr><td></td>
	<td><input type=\"submit\" value=\"ύ\" style=\"width:70px;height:22px;\"/>&nbsp;&nbsp;
	<input type=button value=\"ص¼...\" style=\"width:90px;height:22px;\" onclick=\"onGotoLogin()\" /></td></tr>
</table>
</form
</div>
</body>
</html>
";