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

	if (strlen($create)>0) {
		header("Location: adminEmailsEdit.php?create=1&selectdomain=".$selectdomain);
		printf('<a href="adminEmailsEdit.php?create=1&selectdomain="'.$selectdomain.'>Your browser does not support Header:Location tag. Please CLICK HERE to continue</a>');
		die;
	}
	if (strlen($cancel)>0) {
		header("Location: adminMenu.php");
		printf('<a href="adminMenu.php">Your browser does not support Header:Location tag. Please CLICK HERE to continue</a>');
		die;
	}
	include_once("MailAccount.class.php");
	include_once("checkLogin.inc.php");
	if (strlen($submitButton)>0) {//from adminMenu
		$letter="A";
		$page=1;
	}
	if (strlen($searchButton)>0) {
		$letter=null;
		$page=1;
	}
	if ((int)$page<1)
		$page=1;

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

	if ((strlen($sure)>0) && ($rights)) {
		$dbo=new MySql(false);
		$domain->_connectDB(&$dbo);
		$dbo->query("select * from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' group by ".$domain->getUserField());
		if ($dbo->numRows()) {
			$invert=-1;
			while ($mr=$dbo->fetchArray()) {
				$user=new MailAccount($mr[$domain->getUserField()],$domain->getId());
				$cbname="cb".md5($user->getUser());
				if ((int)$$cbname) {
					$user->kill();
				}
				$u=null;
			}
			$dbo->freeResult();
		}
		$dbo->close();
	}


	function setNavigate() {
		GLOBAL $found,$page,$limit,$navigate,$parameter,$selectdomain,$search;
		switch ((int)$navigate) {
			case 1:$page=(((int)$page-1)>0) ? (int)$page-1:1;break; //prev
			case 2:$page=1;break; //top
			case 3:$page=($found>PAGESIZE) ? $found/PAGESIZE:1;break; //end;
			case 4:$page= ($found>=($page*PAGESIZE)) ? ($page+1):(int)$page;break; //next
			default:$page=1;break;
		}
		$parameter="?selectdomain=".$selectdomain;
		$parameter.="&page=".$page;
		$parameter.="&search=".$search;
		if ($page==1)
			$limit=0;
		else
			$limit=($page-1)*PAGESIZE;
	}
	if ($limit==1) {
		$limit=0;
	}

	if ($rights) {
		$dbo=new MySql(false);
		$domain->_connectDB(&$dbo);
		if (strlen($erase)==0) {
			if (!empty($search)) {
				$dbo->query("select * from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' and ".$domain->getUserField()." group by ".$domain->getUserField()." like '%".$search."%'");
				$found=$dbo->numRows();
				setNavigate();
				$dbo->query("select ".$domain->getUserField()." from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' and ".$domain->getUserField()." like '%".$search."%' group by ".$domain->getUserField()." order by ".$domain->getUserField()." limit ".$limit.",".PAGESIZE);
			}
			elseif (Ord($letter)>0) {
				$dbo->query("select * from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' and left(".$domain->getUserField().",1)='".$letter."' group by ".$domain->getUserField());
				$found=$dbo->numRows();
				setNavigate();
				$dbo->query("select ".$domain->getUserField()." from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' and left(".$domain->getUserField().",1)='".$letter."' group by ".$domain->getUserField()." order by ".$domain->getUserField()." limit ".$limit.",".PAGESIZE);
			}
			else {
				$dbo->query("select * from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' group by ".$domain->getUserField());
				$found=$dbo->numRows();
				setNavigate();
				$dbo->query("select ".$domain->getUserField()." from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' group by ".$domain->getUserField()." order by ".$domain->getUserField()." limit ".$limit.",".PAGESIZE);
			}
			
			$tolimit=($limit+1)*PAGESIZE;
			if ($found<$tolimit)
				$tolimit=$found;
		}
		else
			setNavigate();
	}
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="center" valign="top"> 
<?php
	if ($rights) {
?>
      <form method="post" action="adminEmails.php<?php echo $parameter ?>" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0" align="top">
          <tr>
            <td>
              <table width="100%" border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0" align="top">
                <tr> 
                 <td bgcolor="#0099CC" align="left" width="100%"> 
                  <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>User 
                  account list on: <?php echo $domain->getDomain() ?></b> </font>
                 </td>
                 <td bgcolor="#0099CC" width="20%" align="right"> 
                  <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>
                  <a href="adminRelayIp.php?selectdomain=<?php echo $selectdomain ?>">
                  <img src="images/relayip.gif" border="0"></a></b> </font>
                 </td>
                </tr>
              </table>
            </td>
          </tr>
<?php
		if (strlen($erase)==0) {
?>
          <tr> 
            <td> 
              <table border="0" cellspacing="0" cellpadding="0">
                <tr> 
                  <td> 
                    <input type="text" name="search" size="40" maxlength="64" value="<?php echo $search ?>">
                  </td>
                  <td> 
                    <input type="submit" name="searchButton" value="Search">
                  </td>
                  <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                    Enter username or part of the username here, or to show all simple press search with an empty pattern, or select a letter</font></td>
                </tr>
              </table>
            </td>
          </tr>
          <tr> 
            <td> 
              <div align="center"> 
                <table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFCC99" width="100%">
                  <tr> 
<?php
		$i=48; //0
		while ($i<=90) {
			if (Ord($letter)==$i) {
?>
				<td width="2%" bgcolor="#FFFFCC"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b><?php echo Chr($i) ?></b></font></div>
				</td>
<?php
			}
			else  {
?>
				<td width="2%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b><a href="adminEmails.php<?php echo $parameter."&letter=".Chr($i) ?>"><?php echo Chr($i) ?></a></b></font></div>
				</td>
<?php
			}
			if (++$i==58)
				$i=65; //A
		}
?>
                  </tr>
                </table>
              </div>
            </td>
          </tr>
<?php
	} //!erase
?>
          <tr> 
            <td> 
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
	if (strlen($erase)==0) {
?>
                <tr bgcolor="#FFFFCC"> 
                  <td colspan="5"> 
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr> 
                        <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Found: 
                          <?php echo $found ?></font></td>
                        <td width="15%"> 
                          <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo ($limit+1)."-".$tolimit ?></font></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=1&letter=".$letter ?>">&lt; 
                            Previous</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=2&letter=".$letter ?>">Top</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=3&letter=".$letter ?>">End</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=4&letter=".$letter ?>">Next 
                            &gt;</a></font></b></div>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
                <tr bgcolor="#FFFFCC"> 
                  <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Username</b></font></td>
                  <td width="10%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Is 
                      Alias</font></b></div>
                  </td>
                  <td width="15%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Show 
                      alias tree</font></b></div>
                  </td>
                  <td width="10%"> 
                    <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Modify</b></font></div>
                  </td>
                  <td width="10%"> 
                    <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Delete</b></font></div>
                  </td>
                </tr>
<?php
		$invert=-1;
		if ($dbo->numRows()) {
			while ($mr=$dbo->fetchArray()) {
				$user=new MailAccount($mr[$domain->getUserField()],$domain->getId());
				if ($invert>0)
					$bg=' bgcolor="#F2F2E0"';
				else
					$bg="";
				if ($user->isAlias())
					$dot="&#149;";
				else
					$dot="&nbsp;";
?>
				<tr<?php echo $bg ?>> 
				<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmailsEdit.php?selectdomain=<?php echo $selectdomain."&user=".$user->getUser() ?>"><?php echo $user->getUser() ?></a></font></td>
				<td width="10%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="6"><?php echo $dot ?></font></div>
				</td>
				<td width="15%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><img src="images/simnext.gif" width="32" height="32" border="0" alt="(UNDER CONSTRUCTION!) Show links to <?php echo $user->getUser() ?>"></font></div>
				</td>
				<td width="10%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmailsEdit.php?selectdomain=<?php echo $selectdomain."&user=".$user->getUser() ?>"><img src="images/simnext.gif" width="32" height="32" border="0" alt="Modify <?php echo $user->getUser() ?>"></a></font></div>
				</td>
				<td width="10%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
				<input type="checkbox" name="cb<?php echo md5($user->getUser()) ?>" value="1">
				</font></div>
				</td>
				</tr>
<?php
				$invert*=-1;
			}
			$dbo->freeResult();
		}
		if (!$dbo->db) {
?>
			<tr<?php echo ' bgcolor="'.ERRORCOLOR.'"' ?>><td colspan="5"><font face="Verdana, Arial, Helvetica, sans-serif" size="3">Cant connect to remote mysql server</font></td></tr>
			<tr<?php echo ' bgcolor="'.ERRORCOLOR.'"' ?>><td colspan="5"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Host:<?php echo $dbo->ip ?></font></td></tr>
			<tr<?php echo ' bgcolor="'.ERRORCOLOR.'"' ?>><td colspan="5"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Port:<?php echo $dbo->port ?></font></td></tr>
			<tr<?php echo ' bgcolor="'.ERRORCOLOR.'"' ?>><td colspan="5"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Socket error:<?php echo $dbo->connecterror ?></font></td></tr>
			<tr<?php echo ' bgcolor="'.ERRORCOLOR.'"' ?>><td colspan="5"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql error:<?php echo $dbo->errmsg ?></font></td></tr>

<?php
		}
		$dbo->close();
?>
                <tr> 
                  <td colspan="5" bgcolor="#FFFFCC"> 
                    <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font> 
                      <font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                      <input type="submit" name="create" value="Create new account">
                      <input type="submit" name="cancel" value="Cancel">
                      <input type="submit" name="erase" value="Delete selected accounts">
                      <input type="hidden" name="selectdomain" value="<?php echo $selectdomain ?>">
                      <input type="hidden" name="tolimit" value="<?php echo $tolimit ?>">
                      <input type="hidden" name="letter" value="<?php echo $letter ?>">
                      </font></div>
                  </td>
                </tr>
                <tr> 
                  <td colspan="5" bgcolor="#FFFFCC"> 
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr> 
                        <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Found: 
                          <?php echo $found ?></font></td>
                        <td width="15%"> 
                          <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo ($limit+1)."-".$tolimit ?></font></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=1&letter=".$letter ?>">&lt; 
                            Previous</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=2&letter=".$letter ?>">Top</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=3&letter=".$letter ?>">End</a></font></b></div>
                        </td>
                        <td width="15%"> 
                          <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="adminEmails.php<?php echo $parameter."&navigate=4&letter=".$letter ?>">Next 
                            &gt;</a></font></b></div>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
<?php
		} //!erase
		else {
?>
                <tr bgcolor="<?php echo ERRORCOLOR ?>">
                  <td colspan="3"><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>Are you sure to delete selected user accounts?</b></font></div></td>
                </tr>
                <tr bgcolor="#FFFFCC"> 
                  <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Username</b></font></td>
                  <td width="10%"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Is 
                      Alias</font></b></div>
                  </td>
                  <td width="10%"> 
                    <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Delete</b></font></div>
                  </td>
                </tr>
<?php
			$dbo=new MySql(false);
			$domain->_connectDB(&$dbo);
			$dbo->query("select * from ".$domain->getMailTable()." where ".$domain->getDomainField()."='".$domain->getDomain()."' group by ".$domain->getUserField());
			if ($dbo->numRows()) {
				$invert=-1;
				while ($mr=$dbo->fetchArray()) {
					$user=new MailAccount($mr[$domain->getUserField()],$domain->getId());
					$cbname="cb".md5($user->getUser());
					if ((int)$$cbname)
						$chk=" checked";
					else
						continue;
					if ($invert>0)
						$bg=' bgcolor="#F2F2E0"';
					else
						$bg="";
					if ($user->isAlias())
						$dot="&#149;";
					else
						$dot="&nbsp;";
?>
				<tr<?php echo $bg ?>> 
				<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $user->getUser() ?></font></td>
				<td width="10%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="6"><?php echo $dot ?></font></div>
				</td>
				<td width="10%"> 
				<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
				<input type="checkbox" name="<?php echo $cbname ?>" value="1"<?php echo $chk ?>>
				</font></div>
				</td>
				</tr>
<?php
				$invert*=-1;
				}
				$dbo->freeResult();
			}
			$dbo->close()
?>
<tr> 
                  <td colspan="3" bgcolor="#FFFFCC"> 
                    <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font> 
                      <font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                      <input type="submit" name="notsure" value="Cancel">
                      <input type="submit" name="sure" value="Delete selected accounts">
                      <input type="hidden" name="selectdomain" value="<?php echo $selectdomain ?>">
					  <input type="hidden" name="tolimit" value="<?php echo $tolimit ?>">
					  <input type="hidden" name="letter" value="<?php echo $letter ?>">
                      </font></div>
                  </td>
                </tr>
<?php
		} //!ERASE ELSE END
?>
              </table>
            </td>
          </tr>
        </table>
      </form>
<?php
	} //if rights
	else
		require("norights.inc.php");
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>
