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

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

	$saved=false;
	$invert=-1;

	include_once("checkLogin.inc.php");
	if (!$login->isRoot())
		$rights=false;
	else
		$rights=true;
	$admin=new Admin((int)$id);
	if (((int)$id) && (!isset($submitButton)) && ($rights)) {
		//load variables
		$tfusername=$admin->getName();
		$tfemail=$admin->getEmail();
		$cbroot=(int)$admin->isRoot();
		$dbo=new MySql();
		$dbo->query("select Id from ".MMCDOMAINTABLE."");
		if ($dbo->numRows()) {
			while ($mr=$dbo->fetchArray()) {
				$cbname="cb".$mr["Id"];
				if ($admin->haveRightToDomain($mr["Id"]))
					$$cbname="1";
				else
					$$cbname="";
			}
			$dbo->freeResult();
		}
		$dbo->close();
	}
	if ((isset($submitButton))  && ($rights)) {
		$error=0;
		if (strlen($tfusername)==0)
			$error|=1;
		//password
		if ((int)$id) {
			//modify
			if (strcmp($tfpassword,$tfpassword2)!=0) {
				$error|=2;
				$error|=4;
			}
		}
		else {
			if ((strcmp($tfpassword,$tfpassword2)!=0) || (strlen($tfpassword)==0)){
				$error|=2;
				$error|=4;
			}
		}
		if(!ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$",trim($tfemail)))
			$error|=8;

		if (!$error) {
			$adomains=null;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCDOMAINTABLE."");
			if ($dbo->numRows()) {
				$i=0;
				while ($mr=$dbo->fetchArray()) {
					$cbname="cb".$mr["Id"];
					if ((int)$$cbname)
						$adomains[$i++]["id"]=$mr["Id"];
				}
				$dbo->freeResult();
			}
			$dbo->close();
			if ((int)$id)
				if ((DEMOMODE) && ($admin->getId()==1)) {
					; //in demo mode you cant edit this admin (1)
				}
				else
					$admin->modify(trim($tfusername),trim($tfpassword),trim($tfemail),(int)$cbroot,$adomains);
			else
				$admin->create(trim($tfusername),trim($tfpassword),trim($tfemail),(int)$cbroot,$adomains);
			$saved=true;
		}
	}
	if ((int)$cbroot)
		$checkedroot=" checked";
	else
		$checkedroot="";

	if ((int)$id)
		$headline="Modify admin:".$admin->getName();
	else
		$headline="Create admin";
	if ((DEMOMODE) && ($admin->getId()==1))
		$headline="<i>(In demo mode you can't edit this admin!)</i> ".$headline;
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="left" valign="top"> 
<?php
	if (($rights) && (!$saved)) {
?>
      <form method="post" action="adminAdminsEdit.php?id=<?php echo $admin->getId() ?>" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
          <tr> 
            <td colspan="2" bgcolor="#0099CC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="2"><?php echo $headline ?></font></b></font></div>
            </td>
          </tr>
          <tr<?php echo TfColor($error,1) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Username:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfusername" size="40" maxlength="63" value="<?php echo $tfusername ?>">
              </font></td>
          </tr>
          <tr <?php echo TfColor($error,2) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Password (no change if empty):</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="tfpassword" size="40" maxlength="63" value="<?php echo $tfpassword ?>">
              </font></td>
          </tr>
          <tr <?php echo TfColor($error,4) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Retype password:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="tfpassword2" size="40" maxlength="63" value="<?php echo $tfpassword2 ?>">
              </font></td>
          </tr>
          <tr <?php echo TfColor($error,8) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">E-mail (you need this if you forget your password):</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfemail" size="40" maxlength="255" value="<?php echo $tfemail ?>">
              </font></td>
          </tr>
          <tr <?php echo TfColor($error,0) ?>> 
            <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Root 
              (only root can modify admins and add domains):</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="checkbox" name="cbroot" value="1"<?php echo $checkedroot ?>>
              </font></td>
          </tr>
          <tr> 
            <td colspan="2"> 
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr bgcolor="#FFFFCC"> 
                  <td colspan="2"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Full access 
                      to domains</font></b></div>
                  </td>
                </tr>
<?php
	$dbo=new MySql();
	$dbo->query("select Id from ".MMCDOMAINTABLE."");
	if ($dbo->numRows()) {
		$invert=-1;
		while ($mr=$dbo->fetchArray()) {
			$domain=new Domain($mr["Id"]);
			$cbname="cb".$mr["Id"];
			if ((int)$$cbname)
				$checked=" checked";
			else
				$checked="";
?>
                <tr <?php echo TfColor($error,0) ?>> 
                  <td width="50%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <?php echo $domain->getName() ?></font></td>
                  <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                    <input type="checkbox" name=" <?php echo $cbname ?>" value="1"<?php echo $checked ?>>
                    </font></td>
                </tr>
<?php
		}
		$dbo->freeResult();
	}
	$dbo->close();
?>
              </table>
            </td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC" align="center" valign="middle"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                <input type="submit" name="submitButton" value="Save">
                <input type="submit" name="cancel" value="Cancel">
                </font></div>
            </td>
          </tr>
        </table>
      </form>
<?php
	}
	elseif (!$rights)
		require("norights.inc.php");
	else  {
?>
		<div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><a href="adminAdmins.php">
		The Admin is successfully saved. Click here to continue...
		</a></b></font></div>
<?php
	}
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>