Location: PHPKode > projects > Replex > replex-0.9.0/lib/alias.php
<?php
/*************************************************************
$Id: alias.php,v 1.5 2001/05/19 02:43:13 jimir Exp $
 
 ***************************************************************/
  
class Alias {

  var $alias;
  var $dest;
  var $username;
  
  function retrieveAliasPerDomain($DBCONN,$domain) {
      $query = "SELECT * FROM virtual WHERE alias like '%@".$domain."' ORDER BY alias";
      $DBCONN->Query($query);
      unset($this->alias,$this->dest,$this->username);
      for($i=0;$i<$DBCONN->rows;$i++) {
       $DBCONN->GetRow($i);
       $this->alias[$i]=$DBCONN->data[alias];
       $this->dest[$i]=$DBCONN->data[dest];
       $this->username[$i]=$DBCONN->data[username];
      }
      $this->affected_rows=$DBCONN->rows; 

  }

  function retrieveAliasPerAccount($DBCONN,$account) {
      unset($this->alias,$this->dest,$this->username,$this->affected_rows);
      $query = "SELECT * FROM virtual WHERE dest='".$account."' OR dest='$account' OR dest like '".$account.",%' OR dest like '%,".$account."' OR dest like '%,".$account.",%' ORDER BY alias";
      $DBCONN->Query($query);
      for($i=0;$i<$DBCONN->rows;$i++) {
       $DBCONN->GetRow($i);
       $this->alias[$i]=$DBCONN->data[alias];
       $this->dest[$i]=$DBCONN->data[dest];
       $this->username[$i]=$DBCONN->data[username];
      }
      $this->affected_rows=$DBCONN->rows; 
  }
  
  function retrieveAlias($DBCONN,$alias,$mode=0) {
      $query = "SELECT * FROM virtual WHERE alias='$alias'";
      $DBCONN->QueryRow($query);
      $this->alias[0]=$DBCONN->data[alias];
      $this->dest[0]=$DBCONN->data[dest];
      $this->username[0]=$DBCONN->data[username];
      
      if($mode==1){
	//if $mode==1 then split up the comma-separated destination(s) 
	$this->dest=split(",",$this->dest[0]);
      }

  }
  
  function createAlias($DBCONN, $domain, $alias, $dest, $username) {
        if($alias=="*")
	$alias="";
 
        $query = "INSERT INTO virtual (alias,dest,username) VALUES ('".$alias."@".$domain."','$dest','$username')";
 
        if(!$DBCONN->Insert($query)) {
          //DB-ERROR creating the alias
          return 0;
        } 
        return 1;
 
 
  }

  function updateAlias($DBCONN, $domain, $alias, $oldalias, $dest) {
        if($alias=="*")
        $alias="";
 
	$dest=trim($dest);

	//first lets get the current destinations....check for redundancy
	$query1 = "SELECT dest FROM virtual WHERE alias='$oldalias'";
	$DBCONN->QueryRow($query1);
      	$olddest=$DBCONN->data[dest];
       	$olddest_array=split(",",$olddest);

	for($a=0;$a<count($olddest_array);$a++) {
		if($olddest_array[$a]==$dest)
		$already_there=1;
	}

	if(!$already_there && $dest!="none") {
		$newdest=$olddest.",".$dest;
	}
	else {
		$newdest=$olddest;
	}

        $query2 = "UPDATE virtual SET alias='".$alias."@".$domain."',dest='".$newdest."' WHERE alias='".$oldalias."'";
 
        if(!$DBCONN->Update($query2)) {
          //DB-ERROR updating the alias/destinations
          return 0;
        }
        return 1;
 
 
  }

  function checkDestDependencies($DBCONN,$alias,$dest) {
	//if a destination is an account, we check is this is its only alias, 
	//if so it can not be removed...it must be deleted from the account section first
	if(strrpos($dest,"@")===false) {
	}
	else {
		//we have an emailadress, no dependencies!
                return 1;
	}

	$query1="SELECT COUNT(*) AS C FROM virtual WHERE alias!='$alias' AND (dest='$dest' OR dest like '".$dest.",%' OR dest like '%,".$dest."' OR dest like '%,".$dest.",%')";
	$DBCONN->QueryRow($query1); 
	$count=$DBCONN->data[C];

	if($count>=1)
	return 1;
	else
	return 0;
  }


  function deleteDestination($DBCONN,$alias,$dest) {
	//never use this function without first doing a 'checkDestDependencies'    
	$query1 = "SELECT dest FROM virtual WHERE alias='$alias'";
        $DBCONN->QueryRow($query1);
        $olddest=$DBCONN->data[dest];
        $olddest_array=split(",",$olddest);
	
	$count=count($olddest_array); 
        for($a=0;$a<count($olddest_array);$a++) {
		//delete the $dest
                if($olddest_array[$a]!=$dest) {
			//just making sure that the destinations are comma-separated
			if(($a+1)<$count) {
				$newdest=$newdest.$olddest_array[$a].",";
			}
			else {
				$newdest=$newdest.$olddest_array[$a];
			}
		}
	
        }
	//if the last character is a comma....we just remove that
	if(strrchr($newdest,",")==",") {
	  $newdest=substr($newdest,0,strlen($newdest)-1);
        }

	$query2 = "UPDATE virtual SET dest='$newdest' WHERE alias='$alias'";
        if(!$DBCONN->Update($query2)) {
          //DB-ERROR updating the destinations
          return 0;
        }
        return 1;

  }

  function checkAliasDependencies($DBCONN,$alias) {
	//check if an account has this alias as its only alias
        $query1 = "SELECT dest FROM virtual WHERE alias='$alias'";
        $DBCONN->QueryRow($query1);
        $dest=$DBCONN->data[dest];
        $dest_array=split(",",$dest);
 
        $b=0;
        for($a=0;$a<count($dest_array);$a++) {
                if(strrpos($dest_array[$a],"@")===false) {
                        //we found an account - now we count the occurences in the virtual-table, and
                        //it should be more than 1
                        $newdest_array[$b]=$dest_array[$a];
                        $b++;
                }
        }
        for($b=0;$b<count($newdest_array);$b++) {
                $d=$newdest_array[$b];
                $query2="SELECT COUNT(*) AS C FROM virtual WHERE dest='$d' OR dest like '".$d.",%' OR dest like '%,".$d."' OR dest like '%,".$d.",%'";
                $DBCONN->QueryRow($query2);
                $c=$DBCONN->data[C];
                if($c==1)
                return 0;
        }
	return 1;

  }

   function deleteAlias($DBCONN,$alias) {
	//never use this function without first doing a 'checkAliasDependencies'
	$query1="DELETE FROM virtual WHERE alias='$alias'";
	if($DBCONN->Delete($query1))
	return 1;
	else
	return 0;

   }

   function isAliasUsed($DBCONN,$alias,$domain,$oldalias=0) {
	$alias=trim($alias);
	if($alias=="*")
	$alias=""; //if alias is blank in database it should match the "*"!!!! 

	$query = "SELECT alias FROM virtual WHERE alias='".$alias."@".$domain."'";
        if($DBCONN->Exists($query)) {
		$alias_is_used=1;
	}

	if($oldalias) {
		//tjeck if old and new alias are the same....then OK
		if($oldalias==$alias."@".$domain) {
			$alias_is_used=0;
		}
	}
	//echo "TEST".$oldalias."=".$alias."@".$domain."TEST";
	//exit;
	if($alias_is_used)
	return 1;
        else
        return 0;
  }


  function strRemoveAlias($str) {
    //string is e.g. hide@address.com and ends up being replex.org
    $remove=strrchr($str,'@');
    $str=substr($remove,1);
    return $str;
  } 
   
  # GET Functions
  function getAlias($i=0) {
    return $this->alias[$i];
  }  
  function getDest($i=0) {
    return $this->dest[$i];	
  }
  
  function getUsername($i=0) {
    return $this->username[$i];	
  }
  
  function getAffectedRows() {
    return $this->affected_rows;	
  }
  
  
  
} //class end
?>
Return current item: Replex