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

  var $username;
  var $password;
  var $domain_name;
  
  function retrieveAccounts($DBCONN,$type=NULL,$search=NULL,$sort=NULL,$perpage=NULL,$page=NULL) {
    if(empty($sort)) {
      $sortby="domain_name";	
    }
    else {
      if($sort==1)
      $sortby="ra.username";
      else 
      $sortby="ra.domain_name";
            	
    }
    if(empty($type)) {
      $type="showall";	
    }
    
    if(empty($perpage)) {
      $perpage_number=30;
    }
    else {
      switch($perpage) {
        case 1:
        $perpage_number=30;
        break;
        case 2:
        $perpage_number=50;
        break;
        case 3:
        $perpage_number=100;
        break;
        case 4:
        $perpage_number=5;
        break;
      }
    }
    
    if(empty($page)) {
      $page=1;	
    }
    
  
     if($type=="search") {
       if($search["COLUMN"]==1)
      	$column="ra.username";
      	else 
      	$column="ra.domain_name";
      	
      	$content=$search["CONTENT"];
      	$this->perpage=$perpage_number;
        //fetch all accounts that match
        $query = "SELECT ra.username,ra.password,ra.domain_name FROM accountuser ra WHERE ".$column." like '%".$content."%' ORDER BY ".$sortby;
      }
      else if($type=="search_id") {
       $SEARCH = new Search;
       $SEARCH->retrieveSearchSql($DBCONN,$search);
       $query =	trim($SEARCH->getSearchSql());
       $this->perpage = $perpage_number = $SEARCH->getPerpage();
      }
      else if($type=="onedomain") {
        $column="ra.domain_name";
 
        $content=$search["CONTENT"];
        $this->perpage=$perpage_number;
        //fetch all accounts that match
        $query = "SELECT ra.username,ra.password,ra.domain_name FROM accountuser ra WHERE ".$column."= '".$content."' ORDER BY ".$sortby;
      } 
      else {
       //fetch all accounts
       $this->perpage=$perpage_number;
       $query = "SELECT ra.username,ra.password,ra.domain_name FROM accountuser ra ORDER BY ".$sortby;
      }
    

    //calculate the limit
    $limit_end=$perpage_number;   
    $limit_start=($page*$limit_end)-$perpage_number;
    $limit="LIMIT ".$limit_start.",".$limit_end;
    
    $DBCONN->Query($query." ".$limit);
    for($i=0;$i<$DBCONN->rows;$i++){   // for each row of results
      $DBCONN->GetRow($i);       // get the data for row $i
      $this->username[$i]=$DBCONN->data[username];
      $this->password[$i]=$DBCONN->data[password];
      $this->domain_name[$i]=$DBCONN->data[domain_name];
      
    }

    $DBCONN->Query($query); //doing the query againg just for getting total number of rows....hmmmm not good
    $this->affected_rows=$DBCONN->rows;
    $temp_total=$this->affected_rows/$perpage_number;
    if($temp_total/1==0)
    $this->total_pages=$temp_total;
    else
    $this->total_pages=((int)$temp_total)+1;
    
    $this->search_sql=$query;
  }
 

  function retrieveAccount($DBCONN,$account) {
      $query = "SELECT ra.username,ra.password,ra.prefix,ra.domain_name,rv.alias FROM accountuser ra, virtual rv WHERE ra.username='".$account."' AND (rv.dest=ra.username OR rv.dest like '".$account.",%' OR rv.dest like '%,".$account."' OR rv.dest like '%,".$account.",%') ORDER BY rv.alias";
      $DBCONN->Query($query);
      for($i=0;$i<$DBCONN->rows;$i++) {
        $DBCONN->GetRow($i); 
       $this->username[$i]=$DBCONN->data[username];
       $this->password[$i]=$DBCONN->data[password];
       $this->prefix[$i]=$DBCONN->data[prefix];
       $this->domain_name[$i]=$DBCONN->data[domain_name];
       $this->alias[$i]=$DBCONN->data[alias];
      }
      return 1;
  }
  
  function retrieveDomainInfo($DBCONN,$domain) {
      $query = "SELECT ra.username,ra.password,ra.prefix,ra.domain_name FROM accountuser ra WHERE ra.domain_name='".$domain."' ORDER BY username";
      if(!$DBCONN->Query($query)) {
       return 0;
      }
      else {
       for($i=0;$i<$DBCONN->rows;$i++) {
      	$DBCONN->GetRow($i);
      	$this->username[$i]=$DBCONN->data[username];
      	$this->password[$i]=$DBCONN->data[password];
       	$this->prefix[$i]=$DBCONN->data[prefix];
       	$this->domain_name[$i]=$DBCONN->data[domain_name];
       }
       $this->affected_rows=$DBCONN->rows;
       return 1;
      }
  }
  
  function createAccount($DBCONN, $IMAP, $username, $password, $domain, $prefix, $alias) {
  	//create account info in:
  	//accountuser, virtual and on cyrus create a mailbox user.$username
        $IMAP->createmb("user.".$username);
        
        $query1 = "INSERT INTO accountuser (username,password,prefix,domain_name) VALUES ('$username','$password','$prefix','$domain')";  	
        $query2 = "INSERT INTO virtual (alias,dest,username) VALUES ('".$alias."@".$domain."','$username','$username')";
        
        if(!$DBCONN->Insert($query1)) {
          //DB-ERROR accountuser	
          return 0;
        }
        
        if(!$DBCONN->Insert($query2)) {
          //DB-ERROR virtual
          return 0;	
        }
        
        return 1;
  
  
  }
  
  function deleteAccount($DBCONN,$IMAP,$ALIAS,$account) {
    $query1 = "DELETE FROM accountuser WHERE username='$account'";
    $query2 = "DELETE FROM virtual WHERE dest='$account'";


    $IMAP->deletemb("user.".$account);
    
    if(!$DBCONN->Delete($query2)) {
      return 0;
    }
    
    //we need to do some serious cleaining.....
    $ALIAS->retrieveAliasPerAccount($DBCONN,$account);
    for($a=0;$a<count($ALIAS->alias);$a++) {
      $ALIAS->deleteDestination($DBCONN,$ALIAS->getAlias($a),$account); 
    }
    
    if(!$DBCONN->Delete($query1)) {
      return 0;
    }

    return 1;
  }
  
  function updateAccount($DBCONN,$account,$password,$alias,$domain) {
    
    $query1 = "UPDATE accountuser SET password='$password' WHERE username='$account'";
    if(!$DBCONN->Update($query1)) {
      return 0;
    }
    
    //first lets remove all existing
    $query2 = "DELETE FROM virtual WHERE dest='$account'";  
    $DBCONN->Delete($query2);

    //then lets put in the new
    $b=0;
    for($a=0;$a<count($alias);$a++) {
      if(!empty($alias[$a])) {
        if($alias[$a]=="*")
        $alias[$a]="";
        $aliastmp=trim($alias[$a])."@".$domain;
        $query3 = "INSERT INTO virtual (alias,dest,username) VALUES ('$aliastmp','$account','$account')"; 
        $DBCONN->Insert($query3);  
      }
    }

        
    return 1;
  
  }
  
  function isAliasUsed($DBCONN,$alias,$domain) {
	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))
  	return 1;
  	else
  	return 0;
  }
  
  function strRemoveDomain($str) {
    //string is e.g. hide@address.com and ends up being jimi
    $remove=strrchr($str,'@');
    $str=str_replace($remove,'',$str);
    return $str;
  }
  
  # GET Functions
  function getUserName($i=0) {
    return $this->username[$i];
  }  
  function getPassword($i=0) {
    return $this->password[$i];	
  }
  
  function getPrefix($i=0) {
    return $this->prefix[$i];	
  }

  function getAlias($i=0) {
    return $this->alias[$i];	
  }
  
  function getDomainName($i=0) {
    return $this->domain_name[$i];	
  }
  
  function getAffectedRows() {
    return $this->affected_rows;	
  }
  
  
  
} //class end
?>
Return current item: Replex