<?php
$loginname=trim($_REQUEST["loginname"]);
$password=trim($_REQUEST["password"]);
$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
mysql_select_db(get_cfg_var("SIPDB"),$db);
$timestr=strftime("%Y-%m-%d %H:%M:%S");
if ($loginname!="" && $password!="") {
	$bHaveUserInfo=TRUE;
}
$n=0;
$userIP = $_SERVER["REMOTE_ADDR"];			//读取该用户当前使用电脑IP地址
$rs = mysql_query("select userid from login where ipaddr='".$userIP."'",$db);		//查找该IP是否已经登录
if (($row = mysql_fetch_array($rs))==FALSE) {			//还没有登录
	mysql_free_result($rs);
	if ($bHaveUserInfo) {
		$rs = mysql_query("select userid,password,type,onlines,suspendtime from users where loginname='".$loginname."' and checked='n'",$db);		//查找该用户ID记录
		if (($row = mysql_fetch_array($rs))==FALSE) {										//没有找到
			mysql_free_result($rs);
			$n=1;				//不存在该用户ID
		} else {
			$userid = $row["userid"];
			$usertype = $row["type"];
			$password1=$row["password"];
			$maxonlines=$row["onlines"];
			if (is_null($maxonlines)) {
				$maxonlines=1;
			}
			$suspendtime=$row["suspendtime"];
			if (is_null($suspendtime)) {
				$suspendtime="";
			}
			mysql_free_result($rs);
			$bPass=FALSE;
			if ($password!=$password1) {					//比较密码
				$n=2;			//密码不对
			} else {
				$bPass=TRUE;
			}
			if ($bPass) {
				$onlines=0;
				$rs = mysql_query("select count(ipaddr) as allcount from login where userid='".$userid."'",$db);		//查找该用户ID是否已经登录过
				if ($row = mysql_fetch_array($rs)) {
					$onlines=$row["allcount"];
					if (is_null($onlines)) {
						$onlines=0;
					}
				}
				mysql_free_result($rs);
				if ($onlines<$maxonlines) {	//还没有超过并发人数限制
					if ($suspendtime=="") {
						$remaintime=0;		//不限制上网时间
						$remaindata=0;		//不限制上网流量
						//向登录表中插入当前用户上网新记录
						mysql_query( "insert into login(ipaddr,userid,type,remaintime,remaindata,logintime,maskword) values('".$userIP."','".$userid."','".$usertype."',".strval($remaintime).",".strval($remaindata).",'".$timestr."','n')",$db );
						$tmprs = mysql_query("select ipaddr from contact where ipaddr='".$userIP."'",$db);
						if ( ($tmprow = mysql_fetch_array($tmprs))==FALSE ) {
							mysql_free_result($tmprs);
							mysql_query( "insert into contact(ipaddr,contacttime) values('".$userIP."',".strval(time()).")",$db );
						}
						else {
							mysql_free_result($tmprs);
							mysql_query( "update contact set contacttime=".strval(time())." where ipaddr='".$userIP."'",$db );
						}
						$_SESSION["userid"]=$userid;
						$n=3;				//登录成功
					}
					else {
						$n=4;				//被暂停
					}
				} else {
					$n=5;					//用户ID已经登录过
				}
			}
		}
	}
} else {
	$_SESSION["userid"]=$row["userid"];
	mysql_free_result($rs);
	$n=3;				//登录成功
}

mysql_close($db);

if ($n==3) {		//登录成功，转到酒店对外主页
	$_SESSION["StartTime"]=$timestr;
	ob_clean();
	header("Location: logout.php");
	exit;
}
?>


</html>

<html>

<head>
<title>User Login</title>

<script language="javascript">
document.oncontextmenu=mymenu;

function mymenu()
{
	window.event.returnValue=false;
}

function myload() 
{
<?php
	//登录未成功，显示原因
	if ($n==1) {
		echo("alert(".chr(34)."There don't exist this UserID!".chr(34).");");
	} elseif ($n==2) {
		echo("alert(".chr(34)."Password is incorrect!".chr(34).");");
	} elseif ($n==4) {
		echo("alert(".chr(34)."UserID is suspended!".chr(34).");");
	} elseif ($n==5) {
		echo("alert(".chr(34)."Online users of this UserID is over max users allowed!".chr(34).");");
	} else {
	}
?>
}
</script>

</head>

<body background="../bkg.bmp" onload="javascript:myload();">

<p align="center"><font size="5"><b>User Login</b></font></p>
<hr color="#999966">
<p></p>
<div align="center">
  <center>
  <form name="form1" method="POST" action="login.php">
  <table border="1" width="342" height="146" bordercolor="#008080" cellspacing="0" cellpadding="0">
    <tr>
      <td width="100" align="right" height="44">User ID</td>
      <td width="228" align="center" height="44">
          <input type="text" name="loginname" size="20">
        </td>
    </tr>
    <tr>
      <td width="100" align="right" height="44">Password</td>
      <td width="228" align="center" height="44">
          <input type="password" name="password" size="20">
        </td>
    </tr>
    <tr>
      <td colspan="2" width="334" align="center" height="52"><input type="submit" value="Login" name="B1"></td>
    </tr>
  </table>
  </form>
  </center>
</div>
<p></p>
<hr>
<p align="center">SIPMonitor System</p>

</body>

</html>
