Location: PHPKode > projects > XMail PHP Admin Interface (XPAI) > xpai/includes/xmail.class.php
<?php
/*

Copyright (C) 2001 SOGware by Paul Heijman.
Released under the GPL and PHP licenses as stated in the the README file which
should have been included with this document.

Modified by Andreas Klein, hide@address.com, 020316

*/

//XMail.class Version 0.1
//Requires PHP 4.0 or later 
//Requires XMail 0.76 or later 

class XMail {
	
//////////////
//// General functions

// Executes command
function execute($cmd, $ret_type, $server_ip, $server_port, $vars="")
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $result = "";
  $result_code = true;
  $fp = fsockopen($server_ip, $server_port, $errno, $errstr, 5);
  if (!$fp) {
  	die($errstr);
  }
  
  $server_intro = fgets($fp, 1024); //read server info
	if (substr($server_intro, 0 ,6) != "+00000")
	{
  	$result_code = false;
	} else { 
	
  	$md5pwd = "#".md5(substr($server_intro, strpos($server_intro, '<'), strpos($server_intro, '>')-strpos($server_intro, '<')+1).$_SESSION['xpai_pwd']);  
  
    fputs($fp, "\"".$_SESSION['xpai_user']."\"\t\"".$md5pwd."\"\n"); //send login info
    $ret = fgets($fp, 2048); //read login info
    if (substr($ret, 0 ,6) != "+00000") //not logged in 
  	{ 
    	$result_code = false;
    } else {
      // add " to cmd-string
      $cmd = "\"".str_replace ("\t", "\"\t\"", $cmd)."\"";
      $cmd = str_replace ("\"\"", "\"", $cmd);
    
      fputs($fp, "$cmd\n"); //send command
      $ret = fgets($fp, 2048); //read ret info
      $_SESSION['LastXMAILResult'] = $ret;
      if ( ($ret[0] == '-') || (! $ret)) //error exec command || xmail didnt anser
      {
        $result_code = false;
      } else {      
        switch ($ret_type) 
        {
        	case 0: //no more data expected from server
        		$result_code = true;
        		break;
        	case 1: //read data from server
        		$ret = array();
        		do { //read command output
      /*
                          $linein = fgets($fp, 2048);
                          do
                          {
                            $lineinrep = str_replace("\t\t", "\t", $linein);
                          } while (strlen($lineinrep) != strlen($linein));
                          $tmp = explode("\t", $linein);
      */
                          $tmp = fgetcsv($fp, 2048, "\t");
        			if (count($tmp) == 1) 
							{
        				array_push($ret, $tmp[0]); //use first (only) element
        			} else {
        				array_push($ret, $tmp); //use all elements
        			}
        		} while ((substr($tmp[0], 0, 1) != '.'));
        		array_pop($ret); // remove trailing dot
            fputs($fp, "\"quit\"\n"); //send logout info
            fclose($fp);
        		return($ret);
        		break;
        	case 2: //send vars to server
        		// add " to var-string
      			if ($vars != "")
      			{
      			  $vars = trim($vars);
          		$vars = "\"".str_replace ("\t", "\"\t\"", $vars)."\"";
          		$vars = str_replace ("\"\"", "\"", $vars);
      				$vars .= "\n";
      			}
        		fputs($fp, $vars.".\n"); //send command
         		$ret = fgets($fp, 2048); //read ret info
            $_SESSION['LastXMAILResult'] = $ret;
      			if ($ret[0] == '-') //error exec command 
        			$result_code = FALSE;
        		break;
        	case 3: //send vars to server
        		fputs($fp, $vars.".\n"); //send command
         		$ret = fgets($fp, 2048); //read ret info
            $_SESSION['LastXMAILResult'] = $ret;
      			if ($ret[0] == '-') //error exec command 
        			$result_code = FALSE;
        		break;
        }
  		}
  	}
	}
  fputs($fp, "\"quit\"\n"); //send logout info
  fclose($fp);
	return $result_code;
} //end function
	
// Quits connection	
function quit() 
{
 	return ('No function');
} //end function

// Keeps connection	
function noop() 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "noop";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Checks password	
function check_pwd($pwd, $user, $domain="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($user == "")
	  return false;
  if ($pwd == "")
	  return false;
	if ($domain != "")
	{
		if ($this->userauth($domain, $user, $pwd))
		  return true;
	} else {
    $enc_pwd = "";
    for ($lenctr=0; $lenctr<strlen($pwd); $lenctr++)
    {
      $enc_pwd .= sprintf("%02x", ((ord($pwd[$lenctr]) ^ 101) & 255));
    }
		$suvars = $this->cfgfileget("ctrlaccounts.tab", $_SESSION['ip'], $_SESSION['port']);
		if (is_array($suvars))
		{
  		if (is_array($suvars[0]))
  		{
    		foreach ($suvars as $suvar)
    		{
    		  if (!strcasecmp($suvar[0], $user))
    			{
    			  if (!strcmp($suvar[1], $enc_pwd))
      			{
      			  return true;
      			}
    				break;
    			}  
    		}
			} else {
  		  if (!strcasecmp($suvars[0], $user))
  			{
  			  if (!strcmp($suvars[1], $enc_pwd))
    			{
    			  return true;
    			}
  			}  
  		}
		}
	}
	return false;
} //end function


/////////////////////
//// Domain functions

// List handled domains
function domainlist() 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "domainlist";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Add a domain
function domainadd($l_domain) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "domainadd\t$l_domain";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Delete a domain
function domaindel($l_domain) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "domaindel\t$l_domain";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// List handled domain aliases
function aliasdomainlist($l_domain = "") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_domain != "")
		$cmd = "aliasdomainlist\t$l_domain";
	else 
	  $cmd = "aliasdomainlist";
	
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Add a domain alias
function aliasdomainadd($l_domain, $l_aliasdomain) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "aliasdomainadd\t$l_domain\t$l_aliasdomain";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Delete a domain
function aliasdomaindel($l_domain) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "aliasdomaindel\t$l_domain";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Get custom domain file
function custdomget($l_domain, $l_server="", $l_port="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_server == "")
	  $l_server =$_SESSION['ip'];
  if ($l_port == "")
	  $l_port =$_SESSION['port'];

  $cmd = "custdomget\t$l_domain";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Set custom domain file
function custdomset($l_domain, $vars, $l_server="", $l_port="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_server == "")
	  $l_server =$_SESSION['ip'];
  if ($l_port == "")
	  $l_port =$_SESSION['port'];

  $cmd = "custdomset\t$l_domain";
  $tmp = "";
  if (is_array($vars))
	{
  	foreach($vars as $var) 
  	{
      if (is_array($var))
  			$tmp .= implode("\t", $var) . "\n";
      else
  			$tmp .= $var . "\n";
    }
	} else {
		if ($vars != "")
		  $tmp = $vars."\n";
	}
	
  return ($this->execute($cmd, 2, $l_server, $l_port, $tmp));
} //end function

// List custom domains
function custdomlist() 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "custdomlist";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function


///////////////////
//// User functions

// List handled users
function userlist($l_domain="", $l_editname="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_domain != "")
    $l_domain = "\t".$l_domain;
  if ($l_editname != "")
    $l_editname = "\t".$l_editname;
  $cmd = "userlist".$l_domain.$l_editname;
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Add user / mailinglist	
function useradd($l_domain, $l_editname, $password, $loginnametype) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "useradd\t$l_domain\t$l_editname\t$password\t$loginnametype";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Delete a user / mailinglist
function userdel($l_domain, $l_editname) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "userdel\t$l_domain\t$l_editname";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Change a user password
function userpasswd($l_domain, $l_editname, $password) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

$cmd = "userpasswd\t$l_domain\t$l_editname\t$password";
return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Authenticate a user
function userauth($l_domain, $l_editname, $password) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "userauth\t$l_domain\t$l_editname\t$password";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function
	
// Get user vars
function uservars($l_domain, $l_editname) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "uservars\t$l_domain\t$l_editname";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Set user vars
function uservarsset($l_domain, $l_editname, $vars) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

	$cmd = "uservarsset\t$l_domain\t$l_editname\t$vars";
	return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function
	
// Get user stats
function userstats($l_domain, $l_editname) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "userstat\t$l_domain\t$l_editname";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Get mailproc.tab
function usergetmproc($l_domain, $l_editname) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "usergetmproc\t$l_domain\t$l_editname";
  $ret = $this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']);
  $arr_count = 0;
  while ($arr_count < count($ret))
  {
    if (is_array($ret[$arr_count]))
    {
			if (ereg('#', $ret[$arr_count][0]))
      {
				$ret[$arr_count][0] = str_replace('"', '', $ret[$arr_count][0]);
	  	}
    } else {
			if (ereg('#', $ret[$arr_count]))
      {
				$ret[$arr_count] = str_replace('"', '', $ret[$arr_count]);
			}
    }
    $arr_count++;
  }
	return ($ret);
} //end function
	
// Set mailproc.tab
function usersetmproc($l_domain, $l_editname, $vars) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "usersetmproc\t$l_domain\t$l_editname";
  $tmp = '';
	foreach($vars as $var)
  {
    if ($var[0] != "")
    {
      if (is_array($var))
      {
        $arr_count = 0;
        while ($arr_count < count($var))
        {
          if (($arr_count == 0) && ($var[0][0] == '#'))
          {
            $var[0] = "#\"".substr($var[0], 1)."\"";
          } else {
            $var[$arr_count] = "\"".$var[$arr_count]."\"";
          }
          $arr_count++;
        }
      } else {
        if ($var[0] == '#')
        {
          $var = "#\"".substr($var, 1)."\"";
        } else {
          $var = "\"".$var."\"";
        }
      }
		}
		$tmp .= implode("\t", $var) . "\n";
	}
  return ($this->execute($cmd, 2, $_SESSION['ip'], $_SESSION['port'], $tmp));
} //end function
	

/////////////////// /
//// Alias functions

// Add user alias
function aliasadd($l_domain, $alias, $l_editname) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "aliasadd\t$l_domain\t$alias\t$l_editname";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Delete a user alias
function aliasdel($l_domain, $alias) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "aliasdel\t$l_domain\t$alias";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// List handled aliases
function aliaslist($l_domain="", $alias="*", $l_editname="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

 	if ($l_domain!="")
		$l_domain = "\t".$l_domain;
  if ($alias!="")
  	$alias = "\t".$alias;
  if ($l_editname!="")
  	$l_editname = "\t".$l_editname;

  $cmd = "aliaslist".$l_domain.$alias.$l_editname;
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function
	
	
///////////////////////////
//// Mailing list functions

// Add address to mailing list
function mluseradd($l_domain, $mlusername, $mailaddress, $perms='RW') 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "mluseradd\t$l_domain\t$mlusername\t$mailaddress\t$perms";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function
	
// Remove address from mailing list
function mluserdel($l_domain, $mlusername, $mailaddress) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "mluserdel\t$l_domain\t$mlusername\t$mailaddress";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function
	
// List mailing list addresses
function mluserlist($l_domain, $mlusername) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "mluserlist\t$l_domain\t$mlusername";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function


///////////////////	
//// POP3 functions

// Add a POP3 external link
function poplnkadd($loc_domain, $loc_username, $extrn_domain, $extrn_username, $extrn_password, $authtype) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "poplnkadd\t$loc_domain\t$loc_username\t$extrn_domain\t$extrn_username\t$extrn_password\t$authtype";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

function poplnkdel($loc_domain, $loc_username, $extrn_domain, $extrn_username) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "poplnkdel\t$loc_domain\t$loc_username\t$extrn_domain\t$extrn_username";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

function poplnklist($loc_domain='', $loc_username='') 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "poplnklist\t$loc_domain\t$loc_username";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

function poplnkenable($enable, $loc_domain, $loc_username, $extrn_domain, $extrn_username='') 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "poplnkenable\t$enable\t$loc_domain\t$loc_username\t$extrn_domain\t$extrn_username";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function


/////////////////////
//// Frozen msg functions

// List frozen msgs
function frozlist() 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "frozlist";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Resubmit a frozen msg
function frozsubmit($msgfile, $lev0, $lev1) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "frozsubmit\t$lev0\t$lev1\t$msgfile";
  return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Delete a frozen msg
function frozdel($msgfile, $lev0, $lev1) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "frozdel\t$lev0\t$lev1\t$msgfile";
	return ($this->execute($cmd, 0, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Get frozen msg log file
function frozgetlog($msgfile, $lev0, $lev1) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "frozgetlog\t$lev0\t$lev1\t$msgfile";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

// Get frozen msg
function frozgetmsg($msgfile, $lev0, $lev1) 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  $cmd = "frozgetmsg\t$lev0\t$lev1\t$msgfile";
  return ($this->execute($cmd, 1, $_SESSION['ip'], $_SESSION['port']));
} //end function

/////////////////////
//// Config functions

// Get a config file (relative of Mailroot)
function cfgfileget($file, $l_server="", $l_port="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_server == "")
	  $l_server =$_SESSION['ip'];
  if ($l_port == "")
	  $l_port =$_SESSION['port'];

	$cmd = "cfgfileget\t$file";
	$ret = $this->execute($cmd, 1, $l_server, $l_port);
  $arr_count = 0;
  while ($arr_count < count($ret)) {
	 	if (is_array($ret[$arr_count])) {
  		if (ereg('#', $ret[$arr_count][0])) {
  			$ret[$arr_count][0] = str_replace('"', '', $ret[$arr_count][0]);
		 	}
    } else {
    	if (ereg('#', $ret[$arr_count])) {
    		$ret[$arr_count] = str_replace('"', '', $ret[$arr_count]);
    	}
    }
    $arr_count++;
  }
  return ($ret);
} //end function

// Save a config file (relative of Mailroot)
function cfgfileset($file, $vars, $add_quote = false, $l_server="", $l_port="") 
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_server == "")
	  $l_server =$_SESSION['ip'];
  if ($l_port == "")
	  $l_port =$_SESSION['port'];

  $cmd = "cfgfileset\t$file";
  $tmp = '';
  foreach($vars as $var)
  {
    if (is_array($var))
    {
		  if ($var[0] != "")
			{
        if ($add_quote)
        {
          if (is_array($var))
          {
            $arr_count = 0;
            while ($arr_count < count($var))
            {
              if (($arr_count == 0) && ($var[0][0] == '#'))
              {
                $var[0] = "#\"".substr($var[0], 1)."\"";
              } else {
                $var[$arr_count] = "\"".$var[$arr_count]."\"";
              }
              $arr_count++;
            }
          } else {
            if ($var[0] == '#')
            {
              $var = "#\"".substr($var, 1)."\"";
            } else {
              $var = "\"".$var."\"";
            }
          }
        }
   		  $tmp .= implode("\t", $var) . "\n";
			}
    } else {
	    $tmp .= $var."\n";
		}
	}
	return ($this->execute($cmd, ($add_quote ? 2 : 3), $l_server, $l_port, $tmp));
} //end function

// Update ctrlaccounts.tab (relative of Mailroot)
function upd_ctrlacc($upd_user, $upd_pwd, $l_server="", $l_port="")
{
  global $is_old_php;
  if (isset($is_old_php) && $is_old_php)
    global $_SESSION;

  if ($l_server == "")
	  $l_server = $_SESSION['ip'];
  if ($l_port == "")
	  $l_port = $_SESSION['port'];

  $ret = $this->cfgfileget("ctrlaccounts.tab", $l_server, $l_port);
  $found = false;
  $enc_pwd = "";
  for ($lenctr=0; $lenctr<strlen($upd_pwd); $lenctr++)
  {
    $enc_pwd .= sprintf("%02x", ((ord($upd_pwd[$lenctr]) ^ 101) & 255));
  }

  $arr_count = 0;
  while ($arr_count < count($ret))
  {
    if (!strcasecmp($ret[$arr_count][0], $upd_user))
    {
      if ($upd_pwd != "")
      {
        $ret[$arr_count][1] = $enc_pwd;
      } else {
			  array_splice($ret, $arr_count, 1);
      }
      $found = true;
      break;
    }
    $arr_count++;
  }

  if (!$found && ($upd_pwd != ""))
  {
    $newretline = array();
    $newretline[0] = $upd_user;
    $newretline[1] = $enc_pwd;
    array_push($ret, $newretline);
  }
  return ($this->cfgfileset("ctrlaccounts.tab", $ret, true, $l_server, $l_port));
} //end function

// Update filters.tab (relative of Mailroot)
function upd_filtersin($upd_rcpt, $upd_filter, $upd_status)
{
  $ret = $this->cfgfileget("filters.in.tab");
  $found = false;
  $arr_count = -1;
  while (!$found && (++$arr_count < count($ret))) {
    if (!strcasecmp($ret[$arr_count][1], $upd_rcpt) && !strcasecmp($ret[$arr_count][4], $upd_filter)) {
      if ($upd_status == 1) {
        $ret[$arr_count][0] = "*";
        $ret[$arr_count][1] = $upd_rcpt;
        $ret[$arr_count][2] = "0.0.0.0/0";
        $ret[$arr_count][3] = "0.0.0.0/0";
        $ret[$arr_count][4] = $upd_filter;
      } else {
			  array_splice($ret, $arr_count, 1);
      }
      $found = true;
    }
  }

  if (!$found && ($upd_status == 1)) {
    $newretline = array();
  	if ((count($ret)>1) || (isset($ret[0]))) { 
      $newretline[0] = "*";
      $newretline[1] = $upd_rcpt;
      $newretline[2] = "0.0.0.0/0";
      $newretline[3] = "0.0.0.0/0";
      $newretline[4] = $upd_filter;
  	  array_push($ret, $newretline);
  	} else {
      $newretline[0][0] = "*";
      $newretline[0][1] = $upd_rcpt;
      $newretline[0][2] = "0.0.0.0/0";
      $newretline[0][3] = "0.0.0.0/0";
      $newretline[0][4] = $upd_filter;
  		$ret = $newretline;
  	}
  }

	if ($found || ($upd_status == 1))
    return ($this->cfgfileset("filters.in.tab", $ret, true));
	else
	  return true;
} //end function

function get_filtersin($get_rcpt)
{
  $result = array();
  $ret = $this->cfgfileget("filters.in.tab");
  if ($ret != NULL) {
    $arr_count = -1;
    while (++$arr_count < count($ret)) {
      if ($ret[$arr_count][1] == $get_rcpt) {
  	    array_push($result, $ret[$arr_count][4]);
      }
    }
  }
	return ($result);
} //end function

  function update_IMAP($user_email, $username, $domain, $password, $action, $dolock, $dounlock, $doimap)
  {
    global $is_old_php, $enable_IMAP_file, $enable_IMAP_mysql, $IMAP_mysql_db_server, $IMAP_mysql_db_user, $IMAP_mysql_db_pwd, $IMAP_mysql_db_table;
    if (isset($is_old_php) && $is_old_php)
      global $_SESSION;
  	$result = true;
  	//  Creation of an external file with imap data (for dovecot)  	
  	if ($enable_IMAP_file)
  	{
    	if ($dolock)
    	{
    		$lock_success = false;
        $vars = $this->cfgfileget($_SESSION['IMAPsemaphore']);
    	  if (!is_array($vars))
    		  $lock_success = true;
        else {
    		  echo "IMAP is locked since ".$vars[1]."<br>Try again later!<br>";
    		}
    		
        if ($lock_success)
    		{
      		$vars = array("locked", time());
          $this->cfgfileset($_SESSION['IMAPsemaphore'], $vars);
    		}
    	} else {
    		$lock_success = true;
    	}
    	
    	if ($doimap && $lock_success)
    	{
        $IMAP_data = $this->cfgfileget($_SESSION['IMAPfilename']);
      	
        $IMAP_line = array();
        $IMAP_line[0] = $user_email;
        $IMAP_line[1] = $username;
        $IMAP_line[2] = $domain;
        $IMAP_line[3] = $password;
        $IMAP_line[4] = $action;
      	if (!is_array($IMAP_data))
      	  $IMAP_data = array();
       	array_push($IMAP_data, $IMAP_line);
     		$result = $this->cfgfileset($_SESSION['IMAPfilename'], $IMAP_data, false);
    	}
    	if ($dolock && $lock_success)
    	{
        $vars = array();
      	$this->cfgfileset($_SESSION['IMAPsemaphore'], $vars);
    	}
    	return ($lock_success && $result); 
		} else 
  
  	/* Database update with imap data */
    if ($enable_IMAP_mysql)
    {
    	if ($doimap)
    	{	
    		$db = mysql_connect($IMAP_mysql_db_server, $IMAP_mysql_db_user, $IMAP_mysql_db_pwd);
        if ($db)
    		{
    		  if (mysql_select_db("mailusers"))
    			{
  
         		switch ($action)
        		{
        		  case 'DELETE':
        			  $query = "DELETE FROM ".$IMAP_mysql_db_table." WHERE id = '".$user_email."'";
        			  $result = mysql_query($query);
        			  break;	
        		  case 'CREATE':
        			  $query = "INSERT INTO ".$IMAP_mysql_db_table." VALUES ('".$user_email."', '".$password."', PASSWORD('".$password."'), 'none', '0', '0', '/var/MailRoot/domains/".$domain."/".$username."',  '/var/MailRoot/domains/".$domain."/".$username."/Maildir', '5000000')";
        			  $result = mysql_query($query);
								if (!$result)
								{
								  $query = "UPDATE ".$IMAP_mysql_db_table." SET clear='".$password."', crypt=PASSWORD('".$password."') WHERE id = '".$user_email."'";
          			  $result = mysql_query($query);
								}
    						break;
        		  case 'PASSWORD':
        			  $query = "UPDATE ".$IMAP_mysql_db_table." SET clear='".$password."', crypt=PASSWORD('".$password."') WHERE id = '".$user_email."'";
//								echo $query."<br>"; 
        			  $result = mysql_query($query);
        			  break;	
        		}
						if (!$result)
							echo "mySql-Error:".mysql_errno().": ".mysql_error()."<BR>";
  					
      		} else {
      		  $result = false;
    			}
    		} else {
    		  $result = false;
    		}
				mysql_close($db);
    	}
      return $result;
  	}
	}
} // end class



?>
Return current item: XMail PHP Admin Interface (XPAI)