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

	
	if (strlen($cancel)>0) {
		header("Location: adminEmails.php?selectdomain=".$selectdomain."&search=".$user."&searchButton=1");
		printf('<a href="adminEmails.php?selectdomain='.$selectdomain.'&search='.$user.'&searchButton=1">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");

	function chkError($flags=null,$mask=null) {
		$retval=false;
		if ($flags&$mask)
			$retval=true;
		return $retval;
	}

	//looped email ????!!!!
	function isOwnEmail($email=null) {
		GLOBAL $account;
		if (strcasecmp($email,$account->getEmail())==0)
			$retval=true;
		else
			$retval=false;
		return ($retval);
	}

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

	$error=0;
	$errorflags=0;
	$aliaserror=null;
	$aerrornote=null;
	$errornotecount=0;
	$saved=false;

	if (strlen($changeuser)>0) {
		$changeuser=true;
	}
	if (strlen($submitButton)>0) {
		$submitButton=true;
	}

	if ($rights) {
		if ($changeuser) {
			//chk username
			if (!$account->checkUserName($tfusername)) {
				$error=1;
				$errorflags|=1024; //username
				$aerrornote[$errornotecount++]="Modify username! Max 63 char, you can use 0-9 and a-z or @ _ . chars, or user already exists at this domain.";  
			}
			if (!$error) {
				$account->changeUsername($tfusername);
				$tfusername=$account->getUser();
				$saved=true;
			}
		}
		if ($submitButton) {
			$tfusername=trim($tfusername);
			$tfpassword1=trim($tfpassword1);
			$tfpassword2=trim($tfpassword2);
			$tfalias1=trim($tfalias1);
			$tfalias2=trim($tfalias2);
			$tfalias3=trim($tfalias3);
			$tfalias4=trim($tfalias4);


			if (!(int)$create) {
				if (strcmp($tfusername,$account->getUser())!=0) {
					$error=1;
					$errorflags|=1024;
					$aerrornote[$errornotecount++]="You can't modify username! Set username to ".$account->getUser()." or to modify username press [Change username] button!";
				}
				if ($account->isAlias()) {
					//is alias but if is alias checkbox is off (modify from alias to normal user)
					//require all alias to del, and password to set, no new alias
					if (!(int)$cbisalias) {
						for ($i=0;$i<$account->getAliasCount();$i++) {
							$cbname="cb".eregi_replace("[@,.]","_",$account->getAliasAt($i));
							$cberrorname="error".$cbname;
							if (!(int)$$cbname) {
								$error=1;
								$$cberrorname=true;
								$ae=1;
							}
						}
						if ($error)
							$aerrornote[$errornotecount++]="You must select all alias to delete!";
						if ((strlen($tfpassword1)==0) || (strcmp($tfpassword1,$tfpassword2)!=0)) {
							$errorflags|=1;
							$error=1;
							$aerrornote[$errornotecount++]="Password must be set. Both (password and retype) have to be the same!";
						}
						if ($error)
							$errorflags|=2; //checkbox
						if (strlen(trim($tfalias1))>0) {
							$error=1;
							$errorflags|=4;
						}
						if (strlen(trim($tfalias2))>0) {
							$error=1;
							$errorflags|=8;
						}
						if (strlen(trim($tfalias3))>0) {
							$error=1;
							$errorflags|=16;
						}
						if (strlen(trim($tfalias4))>0) {
							$error=1;
							$errorflags|=32;
						}
						if ($errorflags>=4)
							$aerrornote[$errornotecount++]="You can't add alias because you unselected the is_alias option!";
					}
					else {
						//alias and isalias cb on
						//alias can't have a password
						if ((strlen($tfpassword1)>0) || (strlen($tfpassword2)>0)) {
							$errorflags|=1;
							$error=1;
							$aerrornote[$errornotecount++]="Alias can't have a password! Please delete password fields!";
						}
						//or lamer and want to delete all alias and alias checkbox is on
						$werror=1;
						for ($i=0;$i<$account->getAliasCount();$i++) {
							$cbname="cb".eregi_replace("[@,.]","_",$account->getAliasAt($i));
							$cberrorname="error".$cbname;
							if (!(int)$$cbname) 
								$werror=0; //we have one or more off checkbox
							else
								$$cberrorname=true;
						}
						if ($werror) {
							if (strlen(trim($tfalias1))>0)
								$werror=0;
							if (strlen(trim($tfalias2))>0)
								$werror=0;
							if (strlen(trim($tfalias3))>0)
								$werror=0;
							if (strlen(trim($tfalias4))>0)
								$werror=0;
						}
						if ($werror) {
							$error=1;
							$errorflags|=2; //checkbox
							$aerrornote[$errornotecount++]="You can't delete all alias. Specify at least one new alias or switch off the Is_alias option!";
						}
						else {
							//ok, we dont have such error
							for ($i=0;$i<$account->getAliasCount();$i++) {
								$cbname="cb".eregi_replace("[@,.]","_",$account->getAliasAt($i));
								$cberrorname="error".$cbname;
								$$cberrorname=false;
							}
						}
						//new alias check
						if (strlen($tfalias1)>0) {
							if ((!$account->checkEmailFormat($tfalias1)) || (isOwnEmail($tfalias1))) {
								$error=1;
								$errorflags|=4;
							}
						}
						if (strlen($tfalias2)>0) {
							if ((!$account->checkEmailFormat($tfalias2)) || (isOwnEmail($tfalias2))) {
								$error=1;
								$errorflags|=8;
							}
						}
						if (strlen($tfalias3)>0) {
							if ((!$account->checkEmailFormat($tfalias3)) || (isOwnEmail($tfalias3))) {
								$error=1;
								$errorflags|=16;
							}
						}
						if (strlen($tfalias4)>0) {
							if ((!$account->checkEmailFormat($tfalias4)) || (isOwnEmail($tfalias4))) {
								$error=1;
								$errorflags|=32;
							}
						}
						if (($errorflags>=4) && (!($errorflags&1024)))
							$aerrornote[$errornotecount++]="Correct new alias fields! You can't add your own e-mail address as alias! Or the e-mail format is not correct.";
					}
				}
				else {
					//not alias
					if ((int)$cbisalias) {
						//but if isalias checkbox is on (modify normal user to alias)
						//must: empty passwords, one or more alias

						//alias cant have a password
						if ((strlen($tfpassword1)>0) || (strlen($tfpassword2)>0)) {
							$error=1;
							$errorflags|=1; //passwords
							$aerrornote[$errornotecount++]="Alias can't have a password! Please delete password fields or switch off the Is_alias option!";
						}
						$werror=1;
						if (strlen($tfalias1)>0) {
							$werror=0;
							if (!$account->checkEmailFormat($tfalias1)) {
								$werror=1;
								$error=1;
								$errorflags|=4; //alias1
							}
						}
						if (strlen($tfalias2)>0) {
							$werror=0;
							if (!$account->checkEmailFormat($tfalias2)) {
								$werror=1;
								$error=1;
								$errorflags|=8; //alias2
							}
						}
						if (strlen($tfalias3)>0) {
							$werror=0;
							if (!$account->checkEmailFormat($tfalias3)) {
								$werror=1;
								$error=1;
								$errorflags|=16; //alias3
							}
						}
						if (strlen($tfalias4)>0) {
							$werror=0;
							if (!$account->checkEmailFormat($tfalias4)) {
								$werror=1;
								$error=1;
								$errorflags|=32; //alias4
							}
						}
						if ($werror) {
							if ($errorflags>=4) {
								//we have one or more email format error
								$werror=0;
								$aerrornote[$errornotecount++]="E-mail format error on new alias fields! Please correct.";
							}
							else  {
								$errorflags|=4; //show an error at alias1
								$aerrornote[$errornotecount++]="You must specify at least one new alias!";
							}
							$error=1;
						}
					}
					else {
						//not alias and alias checkbox is off
						//must check passwords, or empty passwords
						//all alias field must be empty
						if ((strlen($tfpassword1)>0) && (strcmp($tfpassword1,$tfpassword2)!=0)) {
							$error=1;
							$errorflags|=1; //passwords
							$aerrornote[$errornotecount++]="Password fields must be same or empty!";
						}

						if (strlen($tfalias1)>0) {
							$error=1;
							$errorflag|=4;
						}
						if (strlen($tfalias2)>0) {
							$error=1;
							$errorflag|=8;
						}
						if (strlen($tfalias3)>0) {
							$error=1;
							$errorflag|=16;
						}
						if (strlen($tfalias4)>0) {
							$error=1;
							$errorflag|=32;
						}
						if (($errorflags>=4) && (!($errorflags&1024)))
							$aerrornote[$errornotecount++]="You can't specify new alias or select the Is_alias option!";
					}
				}
				if (!$error) {
					//save
					$aalias=null;
					if (strlen($tfalias1)>0)
						$aalias[0]=$tfalias1;
					if (strlen($tfalias2)>0)
						$aalias[1]=$tfalias2;
					if (strlen($tfalias3)>0)
						$aalias[2]=$tfalias3;
					if (strlen($tfalias4)>0)
						$aalias[3]=$tfalias4;


					if (($account->isAlias()) && (!(int)$cbisalias)) {
						//alias > normal
						$account->convertToNormal($tfpassword1);
						$saved=true;
					}
					elseif (($account->isAlias()) && ((int)$cbisalias)) {
						//alias but no change
						if (sizeof($aalias)>0) {
							$account->addAlias($aalias);
						//del selected alias
						}
						for ($i=0;$i<$account->getAliasCount();$i++) {
							$cbname="cb".eregi_replace("[@,.]","_",$account->getAliasAt($i));
							if ((int)$$cbname)
								$account->killAlias($account->getAliasAt($i));
						}
						$saved=true;
					}
					elseif ((!$account->isAlias()) && ((int)$cbisalias)) {
						//normal > alias
						$account->convertToAlias($aalias);
						$saved=true;
					}
					else {
						//not alias but no change
						if (strlen($tfpassword1)>0) {
							$account->setPassword($tfpassword1);
							$saved=true;
						}
					}
				}
			}
			else {
				//create
				if ((int)$cbisalias) {
					//want an alias
					//username,no pwd,one or more alias
					if (!$account->checkUserName($tfusername)) {
						$error=1;
						$errorflags|=1024; //username
						$aerrornote[$errornotecount++]="Username error! User already exists or bad username format. Max 63 char, use a-z and 0-9 or @ _ . chars.";
					}

					//no pwd
					if ((strlen($tfpassword1)>0) || (strlen($tfpassword2)>0)) {
						$error=1;
						$errorflags|=1;
						$aerrornote[$errornotecount++]="Alias can't have a password. Please delete password fields or switch off the Is_alias option.";
					}

					//one or more alias
					$werror=1;
					if (strlen($tfalias1)>0) {
						$werror=0;
						if (!$account->checkEmailFormat($tfalias1)) {
							$werror=1;
							$error=1;
							$errorflags|=4; //alias1
						}
					}
					if (strlen($tfalias2)>0) {
						$werror=0;
						if (!$account->checkEmailFormat($tfalias2)) {
							$werror=1;
							$error=1;
							$errorflags|=8; //alias2
						}
					}
					if (strlen($tfalias3)>0) {
						$werror=0;
						if (!$account->checkEmailFormat($tfalias3)) {
							$werror=1;
							$error=1;
							$errorflags|=16; //alias3
						}
					}
					if (strlen($tfalias4)>0) {
						$werror=0;
						if (!$account->checkEmailFormat($tfalias4)) {
							$werror=1;
							$error=1;
							$errorflags|=32; //alias4
						}
					}
					if ($werror) {
						if ($errorflags>=4) {
							//we have one or more email format error
							$werror=0;
							$aerrornote[$errornotecount++]="E-mail format error on new alias fields! Please correct.";
						}
						else {
							$errorflags|=4; //show an error at alias1
							$aerrornote[$errornotecount++]="You must specify at least one new alias!";
						}
						$error=1;
					}
				}
				else {
					//want a normal user (not alias)
					//username,pwd, no alias
					if (!$account->checkUserName($tfusername)) {
						$error=1;
						$errorflags|=1024; //username
						$aerrornote[$errornotecount++]="Username error! User already exists or bad username format. Max 63 char, use a-z and 0-9 or @ _ . chars.";
					}

					//pwd
					if ((strlen($tfpassword1)==0) || (strcmp($tfpassword1,$tfpassword2)!=0)) {
						$error=1;
						$errorflags|=1;
						$aerrornote[$errornotecount++]="Passwords must be the same and not empty!";
					}

					//alias
					if (strlen($tfalias1)>0) {
						$error=1;
						$errorflag|=4;
					}
					if (strlen($tfalias2)>0) {
						$error=1;
						$errorflag|=8;
					}
					if (strlen($tfalias3)>0) {
						$error=1;
						$errorflag|=16;
					}
					if (strlen($tfalias4)>0) {
						$error=1;
						$errorflag|=32;
					}
					if (($errorflags>=4) && (!($errorflags&1024)))
						$aerrornote[$errornotecount++]="You can't specify alias here, or select the Is_alias option.";
				}
				if (!$error) {
					//create, no err-> save
					$aalias=null;
					if (strlen($tfalias1)>0)
						$aalias[0]=$tfalias1;
					if (strlen($tfalias2)>0)
						$aalias[1]=$tfalias2;
					if (strlen($tfalias3)>0)
						$aalias[2]=$tfalias3;
					if (strlen($tfalias4)>0)
						$aalias[3]=$tfalias4;

					$account=new MailAccount($tfusername,$domain->getId());
					$account->create((int)$cbisalias,$tfpassword1,$aalias);
					$create=0; //created, now modify editmode

					//init
					$cbisalias=$account->isAlias();
					$tfusername=$account->getUser();
					$tfalias1="";
					$tfalias2="";
					$tfalias3="";
					$tfalias4="";
					$tfpassword1="";
					$tfpassword2="";

					$saved=true;

				}
			}
		}
		elseif (!$changeuser) {
			//not submit, not changeuser
			$cbisalias=$account->isAlias();
			$tfusername=$account->getUser();
		}
	}
	if (!(int)$create)
		$headline="Modify user:";
	else
		$headline="Create new user account";
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="center" valign="top"> 
<?php
	if ($rights) {
?>
      <form method="post" action="adminEmailsEdit.php?selectdomain=<?php echo $selectdomain."&user=".$account->getUser()."&create=".(int)$create ?>" name="form1" ENCTYPE="multipart/form-data">
        
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
            <td colspan="2" bgcolor="<?php echo ((int)$create) ? "#FFCC99":"#0099CC" ?>" width="100%"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><?php echo $headline.$account->getEmail() ?></b></font></div>
            </td>
          </tr>
          <tr bgcolor="#FFFFCC"> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Domain</font></td>
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $account->getDomain() ?></font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,1024)) ? 'bgcolor="'.ERRORCOLOR.'"':'bgcolor="#F2F2E0"' ?>> 
          <td width="30%"><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 ?>">
<?php
	if (!(int)$create) {
?>
            <input type="submit" name="changeuser" value="Change username">
<?php
	}
?>
            </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,1)) ? 'bgcolor="'.ERRORCOLOR.'"':"" ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Password 
            <?php echo ((int)$create) ? "":"(no change if empty)" ?>:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="tfpassword1" size="40" maxlength="32" value="<?php echo $tfpassword1 ?>">
              </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,1)) ? 'bgcolor="'.ERRORCOLOR.'"':'bgcolor="#F2F2E0"' ?>> 
          <td width="30%"><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="32" value="<?php echo $tfpassword2 ?>">
              </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,2)) ? 'bgcolor="'.ERRORCOLOR.'"':'' ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Is 
            alias:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="checkbox" name="cbisalias" value="1"<?php echo ($cbisalias) ? " checked":"" ?>>
              </font></td>
          </tr>
          <tr> 
            <td colspan="2"> 
<?php
	if ($account->isAlias()) {
?>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                  <td colspan="2" bgcolor="#FFFFCC"> 
                    <div align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Aliases</font></b></div>
                  </td>
                </tr>
                <tr bgcolor="#FFCC99"> 
                <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Alias</b></font></td>
                <td> 
                  <div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Delete 
                      alias </b></font></div>
                  </td>
                </tr>
<?php
		$invert=-1;
		for ($i=0;$i<$account->getAliasCount();$i++) {
			if ($invert>0)
				$bg=' bgcolor="#F2F2E0"';
			else
				$bg="";
			$cbname="cb".eregi_replace("[@,.]","_",$account->getAliasAt($i));
			$cberrorname="error".$cbname;
			if ($$cberrorname)
				$bg=' bgcolor="'.ERRORCOLOR.'"';
?>
                <tr<?php echo $bg ?>> 
                <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $account->getAliasAt($i) ?></font></td>
                <td> 
                  <div align="left"> 
                      <input type="checkbox" name="<?php echo $cbname ?>" value="1"<?php echo ((int)$$cbname) ? " checked":"" ?>>
                    </div>
                  </td>
                </tr>
<?php
			$invert*=-1;
		}
?>
              </table>
<?php
	}
?>
            </td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFCC99"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Create 
                new alias</b></font></div>
            </td>
          </tr>
          <tr <?php echo (chkError($errorflags,4)) ? 'bgcolor="'.ERRORCOLOR.'"':'' ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">New 
            alias:</font></td>
          <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
            <input type="text" name="tfalias1" size="40" maxlength="255" value="<?php echo $tfalias1 ?>">
              </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,8)) ? 'bgcolor="'.ERRORCOLOR.'"':'bgcolor="#F2F2E0"' ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">New 
            alias:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfalias2" size="40" maxlength="255" value="<?php echo $tfalias2 ?>">
              </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,16)) ? 'bgcolor="'.ERRORCOLOR.'"':'' ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">New 
            alias:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfalias3" size="40" maxlength="255" value="<?php echo $tfalias3 ?>">
              </font></td>
          </tr>
          <tr <?php echo (chkError($errorflags,32)) ? 'bgcolor="'.ERRORCOLOR.'"':'bgcolor="#F2F2E0"' ?>> 
          <td width="30%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">New 
            alias:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="tfalias4" size="40" maxlength="255" value="<?php echo $tfalias4 ?>">
              </font></td>
          </tr>
<?php
	if ($saved) {
?>
          <tr bgcolor="#D0F876"> 
          <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><div align="center"><b>
             [&nbsp;Saved&nbsp;]</b></div></font></td>
          </tr>
<?php
	}
	if ($errornotecount) {
?>
          <tr <?php echo 'bgcolor="'.ERRORCOLOR.'"' ?>> 
          <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>You have 
            one or more errors</b></font></td>
          </tr>
<?php
		$invert=-1;
		for ($i=0;$i<$errornotecount;$i++) {
			if ($invert>0)
				$bg=' bgcolor="#F2F2E0"';
			else
				$bg="";
?>
          <tr<?php echo $bg ?>> 
          <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $aerrornote[$i] ?>
          </font></td>
          </tr>
<?php
			$invert*=-1;
		}
	}
?>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC"> 
              <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="Back to list">
                </font></div>
            </td>
          </tr>
        </table>
      </form>
<?php
	}
	else
		require("norights.inc.php");
?>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>