<?php
/*
	File     : adminRelayIp.php
	Version  : 0.2.1 Beta
	Copyright: Attila Soki 2001
	e-mail   : atiware@gmx.net
	Date     : 2001-05-12
*/

	include_once("checkLogin.inc.php");
	include_once("Domain.class.php");

	if (strlen($cancel)>0) {
		header("Location: adminEmails.php?selectdomain=".$selectdomain."&letter=A");
		printf('<a href="adminEmails.php?selectdomain="'.$selectdomain.'&letter=A>Your browser does not support Header:Location tag. Please CLICK HERE to continue</a>');
		die;
	}

	if (strlen($searchButton)>0) {
		$searchButton=true;
	}
	if (strlen($erase)>0) {
		$erase=true;
	}
	if (strlen($sure)>0) {
		$sure=true;
	}
	if (strlen($notsure)>0) {
		$notsure=true;
	}

	$domain=new Domain((int)$selectdomain);
	if (($login->haveRightToDomain($domain->getId())) || ($login->isRoot()))
		$rights=true;
	else
		$rights=false;

	$total=0;
	$dbo=new MySql(false);
	$domain->_connectDB(&$dbo);
	$dbo->query("select count(*) as COUNT from relay_ip");
	if ($dbo->numRows()) {
		$mr=$dbo->fetchArray();
		$total=$mr["COUNT"];
		$dbo->freeResult();
	}
	$dbo->close();

	if (($sure) && ($rights)) {
		if ((!DEMOMODE) && ($domain->getId()!=1)) {
			$dbo=new MySql(false);
			$domain->_connectDB(&$dbo);
			$dbo->query("delete from relay_ip");
			$dbo->close();
		}
	}

	$error=0;
	if (($searchButton) && ($rights)) {
		$searchip=trim($searchip);


		if ((strlen($searchyear)>0) || (strlen($searchmonth)>0) || (strlen($searchday)>0)) {
			//some date
			if (!checkdate((int)$searchmonth,(int)$searchday,(int)$searchyear))
				$error|=1;
		}
		if ((strlen($searchhour)>0) || (strlen($searchmin)>0) || (strlen($searchsec)>0)) {
			if (((int)$searchhour>23) || ((int)$searchhour<0))
				$error|=2;
			if (((int)$searchmin>59) || ((int)$searchmin<0))
				$error|=4;
			if (((int)$searchsec>59) || ((int)$searchsec<0))
				$error|=8;
			if ((strlen($searchyear)==0) || (strlen($searchmonth)==0) || (strlen($searchday)==0))
				$error|=1;
		}
		if (!$error) {
			$havedate=false;
			$q="select * from relay_ip";
			if ((strlen($searchyear)>0) || (strlen($searchmonth)>0) || (strlen($searchday)>0)) {
				if ((strlen($searchhour)>0) || (strlen($searchmin)>0) || (strlen($searchsec)>0)) {
					$q.=" where ts>='".date("Y-m-d H:i:s",mktime((int)$searchhour,(int)$searchmin,(int)$searchsec,(int)$searchmonth,(int)$searchday,(int)$searchyear))."'";
					$q.=" and ";
				}
				else {
					//no time
					$q.=" where ts>='".date("Y-m-d H:i:s",mktime(0,0,0,(int)$searchmonth,(int)$searchday,(int)$searchyear))."'";
					$q.=" and ";
				}
				if (strlen($searchhour)>0)
					$tohour=(int)$searchhour;
				else
					$tohour=23;
				if (strlen($searchmin)>0)
					$tomin=(int)$searchmin;
				else
					$tomin=59;
				if (strlen($searchsec)>0)
					$tosec=(int)$searchsec;
				else
					$tosec=59;

				$q.="ts<='".date("Y-m-d H:i:s",mktime($tohour,$tomin,$tosec,(int)$searchmonth,(int)$searchday,(int)$searchyear))."'";
				$havedate=true;
			}
			if (strlen($searchip)>0) {
				if ($havedate)
					$q.=" and ";
				else
					$q.=" where ";
				$q.="ip like '".$searchip."'";
			}
			$dbo=new MySql(false);
			$domain->_connectDB(&$dbo);
			$dbo->query($q);
		}
	}
?>
<?php
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="left" valign="top"> 
<?php
	if ($rights) {
?>
      <form method="post" action="adminRelayIp.php" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td bgcolor="#0099CC" colspan="2"> 
              <div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Relay_ip 
                table on: <?php echo $domain->getDomain() ?></b></font></div>
            </td>
          </tr>
<?php
	if ($erase) {
		if ((DEMOMODE) && ($domain->getId()==1)) {
?>
			<tr bgcolor="<?php echo ERRORCOLOR ?>"> 
			<td colspan="2">
			<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><i>(In DEMO MODE you can't empty this relay_ip table!)</i></b>
			</font></div>
			</td>
<?php
	}
?>
			<tr bgcolor="<?php echo ERRORCOLOR ?>"> 
			<td colspan="2">
			<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>Empty relay_ip?</b>
			</font></div>
			</td>
			<tr> 
			<td colspan="2"> 
			<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
			<input type="hidden" name="selectdomain" value="<?php echo $selectdomain ?>">
			<input type="submit" name="notsure" value="Cancel">
			<input type="submit" name="sure" value="Empty relay_ip">
			</font></div>
			</td>
			<td>
			</tr>
<?php
	}
	else {
?>
          <tr> 
            <td colspan="2"> 
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr> 
                  <td bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#FFFF99" ?>" width="7%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Year 
                      </font></b></div>
                  </td>
                  <td bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#FFFF99" ?>" width="5%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Month</font></b></div>
                  </td>
                  <td bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#FFFF99" ?>" width="2%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Day</font></b></div>
                  </td>
                  <td bgcolor="#FFFF99" width="3%">&nbsp;</td>
                  <td bgcolor="<?php echo ($error&2) ? ERRORCOLOR:"#FFFF99" ?>" width="5%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Hour</font></b></div>
                  </td>
                  <td bgcolor="<?php echo ($error&4) ? ERRORCOLOR:"#FFFF99" ?>" width="5%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Min</font></b></div>
                  </td>
                  <td bgcolor="<?php echo ($error&8) ? ERRORCOLOR:"#FFFF99" ?>" width="2%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Sec</font></b></div>
                  </td>
                  <td bgcolor="#FFFF99" width="3%">&nbsp;</td>
                  <td bgcolor="#FFFF99" width="15%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Ip</font></b></div>
                  </td>
                  <td bgcolor="#FFFF99">&nbsp;</td>
                  <td bgcolor="#FFFF99" width="20%"> 
                    <div align="right"></div>
                  </td>
                </tr>
                <tr align="center"> 
                  <td width="7%" bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="right"> 
                      <input type="text" name="searchyear" size="6" maxlength="4" value="<?php echo $searchyear ?>">
                    </div>
                  </td>
                  <td width="5%" bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="center"> 
                      <input type="text" name="searchmonth" size="4" maxlength="2" value="<?php echo $searchmonth ?>">
                    </div>
                  </td>
                  <td width="2%" bgcolor="<?php echo ($error&1) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="left"> 
                      <input type="text" name="searchday" size="4" maxlength="2" value="<?php echo $searchday ?>">
                    </div>
                  </td>
                  <td width="3%">&nbsp;</td>
                  <td width="5%" bgcolor="<?php echo ($error&2) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="right"> 
                      <input type="text" name="searchhour" size="4" maxlength="2" value="<?php echo $searchhour ?>">
                    </div>
                  </td>
                  <td width="5%" bgcolor="<?php echo ($error&4) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="center"> 
                      <input type="text" name="searchmin" size="4" maxlength="2" value="<?php echo $searchmin ?>">
                    </div>
                  </td>
                  <td width="2%" bgcolor="<?php echo ($error&8) ? ERRORCOLOR:"#F2F2E0" ?>"> 
                    <div align="left"> 
                      <input type="text" name="searchsec" size="4" maxlength="2" value="<?php echo $searchsec ?>">
                    </div>
                  </td>
                  <td width="3%">&nbsp;</td>
                  <td width="15%"> 
                    <div align="center"> 
                      <input type="text" name="searchip" size="17" maxlength="15" value="<?php echo $searchip ?>">
                    </div>
                  </td>
                  <td> 
                    <div align="left"> 
                      <input type="hidden" name="selectdomain" value="<?php echo $selectdomain ?>">
                      <input type="submit" name="searchButton" value="Search">
                      <input type="submit" name="cancel" value="Cancel">
                    </div>
                  </td>
                  <td width="20%"> 
                    <div align="right"> 
                      <input type="submit" name="erase" value="Empty relay_ip table">
                    </div>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr> 
            <td width="20%" bgcolor="#FFFF99"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Found: <?php echo $dbo->numRows() ?>
            </font></td>
            <td bgcolor="#F2F2E0"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Total record count: <?php echo $total ?></font></td>
          </tr>
          <tr bgcolor="#FFFF99"> 
            <td width="20%"> 
              <div align="left"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Ip</font></b></div>
            </td>
            <td> 
              <div align="left"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Timestamp</font></b></div>
            </td>
          </tr>
<?php
	if ($dbo->numRows()) {
		$invert=-1;
		while ($mr=$dbo->fetchArray()) {
			if ($invert>0)
				$bg=' bgcolor="#F2F2E0"';
			else
				$bg="";
?>
          <tr<?php echo $bg ?>> 
            <td width="20%"> 
              <div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $mr["ip"] ?></font></div>
            </td>
            <td> 
              <div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $mr["ts"] ?>
               </font></div>
            </td>
          </tr>
<?php
			$invert*=-1;
		}
		$dbo->freeResult();
	}
	$dbo->close();
} //if !erase else end
?>
        </table>
      </form>
<?php
	} //if rights
	else
		require("norights.inc.php");
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>