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

	if (strlen($cancel)>0) {
		header("Location: adminDomains.php");
		printf('<a href="adminDomains.php">Your browser does not support Header:Location tag. Please CLICK HERE to continue</a>');
		die;
	}
	define("INVERTCOLOR","#F2F2E0");
	
	include_once("Domain.class.php");
	
	function TfColor($error,$control) {
		global $invert,$invert2;
		if ($error&$control)
			$retval='bgcolor="'.ERRORCOLOR.'"';
		elseif ($invert>0)
			$retval='bgcolor="'.INVERTCOLOR.'"';
		else
			$retval="";
		if ($invert2>0)
			$invert*=-1;
		$invert2*=-1;
		return $retval;
	}

	$saved=false;
	$invert=-1;
	$invert2=-1;
	include_once("checkLogin.inc.php");
	if ((!$login->haveRightToDomain((int)$id)) && (!$login->isRoot()))
		$rights=false;
	else
		$rights=true;
	$domain=new Domain((int)$id);
	if (($rights) && (!isset($submitButton)) && ((int)$id)) {
		$tfdomain=$domain->getName();
		$tfmip=$domain->getMySqlServerIp();
		if ((DEMOMODE) && ($domain->getId()==1))
			$tfmport="(you can't see this in demo mode!)";
		else
			$tfmport=$domain->getMySqlServerPort();
		$tfmdb=$domain->getMySqlDb();
		if ((DEMOMODE) && ($domain->getId()==1))
			$tfmuser="(you can't see this in demo mode!)";
		else
			$tfmuser=$domain->getMySqlUser(); //in demo mode you cant see the mysql user of domain id 1
//		$tfmpwd=$domain->getMySqlPassword(); //dont send. in source can see any
		$tfmailtable=$domain->getMailTable();
		$tfuserfield=$domain->getUserField();
		$tfdomainfield=$domain->getDomainField();
		$tfaliasfield=$domain->getAliasField();
		$tfisaliasfield=$domain->getIsAliasField();
		$tfpasswordfield=$domain->getPasswordField();
		$cbpwdcrypt=$domain->hasCryptedPasswords();
	}
	elseif (($rights) && (isset($submitButton))) {
		$error=0;
		if (strlen($tfdomain)==0)
			$error|=1;
		if (strlen($tfmip)==0)
			$error|=2;
		if (strlen($tfmport)==0)
			$error|=4;
		if (strlen($tfmdb)==0)
			$error|=8;
		if (strlen($tfmuser)==0)
			$error|=16;
		if (!(int)$id) {
			//only if create
			if (strlen($tfmpwd)==0)
				$error|=32;
		}
		if (strlen($tfmailtable)==0)
			$error|=64;
		if (strlen($tfuserfield)==0)
			$error|=128;
		if (strlen($tfdomainfield)==0)
			$error|=256;
		if (strlen($tfaliasfield)==0)
			$error|=512;
		if (strlen($tfisaliasfield)==0)
			$error|=1024;
		if (strlen($tfpasswordfield)==0)
			$error|=2048;
		if (!$error) {
			if ((int)$id) {
				//modify
				if ((DEMOMODE) && ($domain->getId()==1)) //in demo mode you cant modify id 1
					;
				else
					$domain->modify(trim($tfdomain),trim($tfmip),trim($tfmport),trim($tfmdb),trim($tfmuser),trim($tfmpwd),trim($tfmailtable),trim($tfuserfield),trim($tfdomainfield),trim($tfaliasfield),trim($tfisaliasfield),trim($tfpasswordfield),(int)$cbpwdcrypt);
				$saved=true;
			}
			else {
				$newid=$domain->create(trim($tfdomain),trim($tfmip),trim($tfmport),trim($tfmdb),trim($tfmuser),trim($tfmpwd),trim($tfmailtable),trim($tfuserfield),trim($tfdomainfield),trim($tfaliasfield),trim($tfisaliasfield),trim($tfpasswordfield),(int)$cbpwdcrypt);
				if ((int)$cbcreatetable) {
					$domain->createDb();
				}
				$saved=true;
			}
		}
	}
	elseif (!(int)$id) {
		//new (empty site)
		//defaults
		$tfmip="localhost";
		$tfmport="3306";
		$tfuserfield="user";
		$tfdomainfield="domain";
		$tfaliasfield="alias";
		$tfisaliasfield="is_alias";
		$tfpasswordfield="password";
	}
	if ((int)$id)
		$headline="Modify domain:".$domain->getName();
	else
		$headline="Create domain";
	if ((DEMOMODE) && ($domain->getId()==1))
		$headline="<i>(You can't modify this domain in demo mode!)</i> ".$headline;
	require("head.inc.php");
?>
  <tr>
   <td colspan="2" align="left" valign="top"> 
<?php
	if ($rights) {
		if (!$saved) {
?>
      <form method="post" action="adminDomainsEdit.php?id=<?php echo $id ?>" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td colspan="2" bgcolor="#0099CC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><?php echo $headline ?> 
              </b></font></div>
            </td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,1) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Domain 
              name:</font></td>
            <td width="72%" <?php echo TfColor($error,1) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfdomain" size="40" maxlength="127" value="<?php echo $tfdomain ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,2) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql 
              server (ip or domain)</font></td>
            <td width="72%" <?php echo TfColor($error,2) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfmip" size="40" maxlength="127" value="<?php echo $tfmip ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,4) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql 
              server port (default:3306):</font></td>
            <td width="72%" <?php echo TfColor($error,4) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfmport" size="10" maxlength="5" value="<?php echo $tfmport ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,8) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql 
              database name:</font></td>
            <td width="72%" <?php echo TfColor($error,8) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfmdb" size="40" maxlength="64" value="<?php echo $tfmdb ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,16) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql 
              username:</font></td>
            <td width="72%" <?php echo TfColor($error,16) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfmuser" size="40" maxlength="63" value="<?php echo $tfmuser ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,32) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql 
              password <?php echo ((int)$id) ? "(no change if empty)":"" ?>:</font></td>
            <td width="72%" <?php echo TfColor($error,32) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="tfmpwd" size="40" maxlength="63" value="<?php echo $tfmpwd ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,64) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Mail 
              table name:</font></td>
            <td width="72%" <?php echo TfColor($error,64) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfmailtable" size="40" maxlength="63" value="<?php echo $tfmailtable ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,128) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name 
              of field user:</font></td>
            <td width="72%" <?php echo TfColor($error,128) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfuserfield" size="40" maxlength="63" value="<?php echo $tfuserfield ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,256) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name 
              of field domain:</font></td>
            <td width="72%" <?php echo TfColor($error,256) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfdomainfield" size="40" maxlength="63" value="<?php echo $tfdomainfield ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,512) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name 
              of field alias:</font></td>
            <td width="72%" <?php echo TfColor($error,512) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfaliasfield" size="40" maxlength="63" value="<?php echo $tfaliasfield ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,1024) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name 
              of field is_alias:</font></td>
            <td width="72%" <?php echo TfColor($error,1024) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfisaliasfield" size="40" maxlength="63" value="<?php echo $tfisaliasfield ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,2048) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name 
              of field password:</font></td>
            <td width="72%" <?php echo TfColor($error,2048) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfpasswordfield" size="40" maxlength="63" value="<?php echo $tfpasswordfield ?>">
              </font></td>
          </tr>
          <tr> 
            <td width="50%" <?php echo TfColor($error,0) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Use encrypted 
              mail passwords:</font></td>
            <td width="72%" <?php echo TfColor($error,0) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="checkbox" name="cbpwdcrypt" value="1"<?php echo (int)$cbpwdcrypt ? " checked":"" ?>>
              </font></td>
          </tr>
<?php
	if (!(int)$id) {
		//create
?>
          <tr> 
            <td width="50%" <?php echo TfColor($error,0) ?>><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Create 
              db and tables on mysql server (mail,relay_ip):</font></td>
            <td width="72%" <?php echo TfColor($error,0) ?>> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="checkbox" name="cbcreatetable" value="1"<?php echo (int)$cbcreatetable ? " checked":"" ?>>
              </font></td>
          </tr>
<?php
	}
?>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC" align="center" valign="middle"> 
              <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                <input type="submit" name="submitButton" value="Save">
                <input type="submit" name="cancel" value="Cancel">
                </font></b></div>
            </td>
          </tr>
        </table>
      </form>
<?php
		}
		else {
			//saved
?>
		<div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><a href="adminDomains.php">
		The Domain is successfully saved. Click here to continue...
		</a></b></font></div>
<?php
		}
	}
	else {
		require("norights.inc.php");
	}
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>