Location: PHPKode > projects > Webextreme > webextreme/functions.php
<?php
require_once("config.php");
require_once("i18n.php");

session_start();

// Display the Weblogininterface
function display_login()
{
    
 	global $lang;
    header("WWW-Authenticate: Basic realm=\"" . $lang['webextreme_login'] . "\"");
    header("HTTP/1.0 401 Unauthorized");
	$ret = "\n" . '<html>';
	$ret .= "\n" . '<head>';
	$ret .= "\n" . '<title>' . $lang['no_access'] . '</title>';
	$ret .= "\n" . '<link rel="stylesheet"                  href="style.css">';
	$ret .= "\n" . '<meta http-equiv="pragma"               content="no-cache">';
	$ret .= "\n" . '<meta http-equiv="content-type"         content="text/html; charset=iso-8859-1">';
	$ret .= "\n" . '</head>';
	$ret .= "\n" . '<body>';
	$ret .= "\n" . '<div align="center">';
  	$ret .= "\n" . '<h1>'. $lang['must_enter_valid_login'] . '</h1>';
	$ret .= "\n" . '</div>';
	$ret .= "\n" . '</body>';
	$ret .= "\n" . '</html>';
	
	return  $ret;
 }
 
 
 //Log User in an create Cookie
 function authenticate()
 {
 	global $dbhost, $dbusername, $dbpassword, $dbname, $dbusertable;
	$ret = false;
 	$connection = @mysql_connect($dbhost, $dbusername, $dbpassword) or die (db_error("connection"));
    $sql = "SELECT * FROM $dbusertable WHERE username = '" . $_SERVER['PHP_AUTH_USER'] . "' AND password = MD5('" . $_SERVER['PHP_AUTH_PW'] . "');";
    $result = @mysql($dbname, $sql) or die(db_error("query"));
    $num = mysql_numrows($result);
    $row = mysql_fetch_array($result);
    mysql_close($connection);

    if($num == 1)
    {
      $_SESSION['admin'] = $row["admin"];
      $ret = true;
    }
    return $ret;
 }
 
  //Checks if user is still logged in
  function check_login()
 {

	 if(!isset($_SERVER['PHP_AUTH_USER']))
	  {
	  	display_login();
	  	$ret = false;
	  }
	  elseif (authenticate()!=true)
	  {
		display_login();
		$ret = false;
	  }else
	  {
	  	$ret = true;
	  }

 	
    return $ret;
 }
 
 
// A function that at first checks, whether eXtremail mail server lives or not
if(!(@fsockopen($mailserver, $port)))
{
  $error['title'] = $lang['error'];
  $error['message'] = $lang['extremail_down'];
  $error['back'] = 0;
  require("error.php");
  exit;
}

// A function that returns either yes or no, depending on whether or not $arg is true or false (or 1 or 0)
function yesnoformat($arg)
{
  global $language, $lang;
  if($arg)
    return $lang['yes'];	// if 1
  else
    return $lang['no']; 	// else 0
}

// The generic catch all. Just send an eXtremail command to this function and it will return the results in an array. 
function command($arg)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "$arg\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0260 User(s) listed", "", $results);
  $results = str_replace("OK0360 Domain listed", "", $results);
  $results = str_replace("+OK QUEUE command correct", "", $results);	// FIX ME, YET
  $results = str_replace("OK5300 Status displayed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  $resultsarray = explode("\n", $results);
  return $resultsarray;
}

// Returns the results of a user list command in an array.
function get_mailuserinfo($name, $domain)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "user list name=$name domain=$domain\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0260 User(s) listed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  if (eregi("ER0260 User not found", $results))
  {
    $resultsarray[0] = "User has not been found";
  }
  else
  {
    $resultsarray = explode("\n", $results);
  }
  return $resultsarray;
}

// A simple function that returns the username from an email address.
function get_username($emailaddr)
{
  $temp = explode("@", $emailaddr);
  $username = $temp[0];
  return $username;
}

// Takes a result from a user list command and parses the results and puts them into variables.
function parse_mailuserinfo($array)
{
    $temp["User"] = trim(str_replace("User", "",$array[0]));
    $temp["Directory"] = trim(str_replace("Directory", "",$array[1]));
    $temp["Forward"] = trim(str_replace("Forward", "",$array[2]));
    $temp["Copy"] = trim(str_replace("Copy", "",$array[3]));
    $temp["AccountMap"] = trim(str_replace("AccountMap", "",$array[4]));
    $temp["DiskQuota"] = trim(str_replace("DiskQuota", "",$array[5]));
    $temp["DiskUsed"] = trim(str_replace("DiskUsed", "",$array[6]));
    $temp["MaxInMailSize"] = trim(str_replace("MaxInMailSize", "",$array[7]));
    $temp["MaxOutMailSize"] = trim(str_replace("MaxOutMailSize", "",$array[8]));
    $temp["FileAutoReplay"] = trim(str_replace("FileAutoReplay", "",$array[9]));
    $temp["AutoReplay"] = trim(str_replace("AutoReplay", "",$array[10]));
    $temp["MailboxAccess"] = trim(str_replace("MailboxAccess", "",$array[11]));
    $temp["Date"] = trim(str_replace("Date", "",$array[12]));
    $temp["Status"] = trim(str_replace("Status", "",$array[13]));
  

  return $temp;
}

// Sends a "stat" command to the eXtremail server. Returns the results as a variable. Pretty simple.
function query_server()
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "stat\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK5300 Status displayed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  return $results;
}

// Displays information, normally at the bottom of the page.
function display_info()
{
  global $lang, $language;
?>
<div align="center">
  <table border="0">
    <tr>
      <td align="center" class="info"><a href="domainview.php">Domain</a> | <a href="mfilterview.php">Mfilter</a> | <a href="status.php">Status</a> | <?php if($_SESSION['admin'] == 1) { ?><a href="admin.php"><?php echo $lang['admin']; ?></a> | <a href="denyfilterview.php">Deny Filter</a> | <a href="eXtremail-2.1-0.pdf"><?php echo $lang['readme']; ?></a> |  <?php } ?><a href="logout.php"><?php echo $lang['logout']; ?></a><br><?php echo $lang['logged_in_as'] . " " . $_SERVER['PHP_AUTH_USER']; ?></td>
    </tr>
  </table>
</div>
<?php
}

// Gets the userlist of a given domain and returns the result in an array.
function get_userlist($domain)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "user list domain=$domain\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0260 User(s) listed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  $resultsarray = explode("\n", $results);
  return $resultsarray;
}

// Gets the alias list of a given domain and returns the result in an array
function get_aliaslist($domain)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "useralias list domain=$domain\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while(!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0460 User alias(es) listed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  $resultsarray = explode("\n", $results);
  return $resultsarray;
}

// Gets the information for a given $domain and returns the result in an array.
function get_domaininfo($domain)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command="domain list name=$domain\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while(!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0360 Domain listed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  $resultsarray = explode("\n", $results);
  return $resultsarray;
}



// Gets the total number of users in a given domain and returns the result as an integer.
function get_totalusers($domain)
{
  $result = get_userlist($domain);
  do
  {
    $emailaddress = array_pop($result);
    if(trim($emailaddress) != "")
      ++$count;
  }
  while(sizeof($result) != 0);
  return $count;
}

// Gets the total number of aliases in a given domain and returns the result as an integer.
function get_totalaliases($domain)
{
  $result = get_aliaslist($domain);
  do
  {
    $emailaddress = array_pop($result);
    if (trim($emailaddress) != "")
      ++$count;
  }
  while(sizeof($result) != 0);
  return $count;
}

// Parses and returns the domain information and returns an array of just the data: parse_domainlist($results);
function parse_domainlist($array)
{

  $temp["Domain"] = trim(str_replace("Domain", "",$array[0]));
  $temp["DefaultMailbox"] = trim(str_replace("DefaultMailbox", "",$array[1]));
  $temp["DefaultDir"] = trim(str_replace("DefaultDir", "",$array[2]));
  $temp["DefaultDiskQuota"] = trim(str_replace("DefaultDiskQuota", "",$array[3]));
  $temp["DefaultMaxInMailSize"] = trim(str_replace("DefaultMaxInMailSize", "",$array[4]));
  $temp["DefaultMaxOutMailSize"] = trim(str_replace("DefaultMaxOutMailSize", "",$array[5]));
  $temp["DefaultMaxMlistMembers"] = trim(str_replace("DefaultMaxMlistMembers", "",$array[6]));
  $temp["DiskQuota"] = trim(str_replace("DiskQuota", "",$array[7]));
  $temp["DiskUsed"] = trim(str_replace("DiskUsed", "",$array[8]));
  $temp["MaxInMailSize"] = trim(str_replace("MaxInMailSize", "",$array[9]));
  $temp["MaxOutMailSize"] = trim(str_replace("MaxOutMailSize", "",$array[10]));
  $temp["MaxNumUsers"] = trim(str_replace("MaxNumUsers", "",$array[11]));
  $temp["NumUsers"] = trim(str_replace("NumUsers", "",$array[12]));
  $temp["MaxNumUserAlias"] = trim(str_replace("MaxNumUserAlias", "",$array[13]));
  $temp["UserAlias"] = trim(str_replace("UserAlias", "",$array[14]));
  $temp["MaxNumDomainAlias"] = trim(str_replace("MaxNumDomainAlias", "",$array[15]));
  $temp["NumDomainAlias"] = trim(str_replace("NumDomainAlias", "",$array[16]));
  $temp["MaxNumMlist"] = trim(str_replace("MaxNumMlist", "",$array[17]));
  $temp["NumMlist"] = trim(str_replace("NumMlist", "",$array[18]));
  $temp["MailInFooter"] = trim(str_replace("MailInFooter", "",$array[19]));
  $temp["MailOutFooter"] = trim(str_replace("MailOutFooter", "",$array[20]));
  $temp["ShowFooter"] = trim(str_replace("ShowFooter", "",$array[21]));
  $temp["Logging"] = trim(str_replace("Logging", "",$array[22]));
  $temp["LoggingMailbox"] = trim(str_replace("LoggingMailbox", "",$array[23]));
  $temp["LoggingFile"] = trim(str_replace("LoggingFile", "",$array[24]));
  $temp["PurgeMail"] = trim(str_replace("PurgeMail", "",$array[25]));
  $temp["PurgeMailPeriod"] = trim(str_replace("PurgeMailPeriod", "",$array[26]));
  $temp["PurgeMailSize"] = trim(str_replace("PurgeMailSize", "",$array[27]));
  $temp["MailboxAccess"] = trim(str_replace("MailboxAccess", "",$array[28]));
  $temp["Permission"] = trim(str_replace("Permission", "",$array[29]));
  $temp["Date"] = trim(str_replace("Date", "",$array[30]));
  $temp["Status"] = trim(str_replace("Status", "",$array[31]));


  return $temp;
}

// Parses and returns the mfilter information and returns an array of just the data: parse_mfilterlist($results);
function parse_mfilterlist($array)
{

  $temp["MFilter"] = trim(str_replace("MFilter", "",$array[1]));
  $temp["Condition"] = trim(str_replace("Condition", "",$array[2]));
  $temp["Operator"] = trim(str_replace("Operator", "",$array[3]));
  $temp["Value"] = trim(str_replace("Value", "",$array[4]));
  $temp["Action"] = trim(str_replace("Action", "",$array[5]));
  $temp["Header"] = trim(str_replace("Header", "",$array[6]));
  $temp["Header Cond"] = trim(str_replace("Header Cond", "",$array[7]));
  $temp["Rewrite"] = trim(str_replace("Rewrite", "",$array[8]));
  $temp["Pipe"] = trim(str_replace("Pipe", "",$array[9]));
  $temp["Data"] = trim(str_replace("Data", "",$array[10]));
  $temp["Description"] = trim(str_replace("Description", "",$array[11]));
  $temp["Order"] = trim(str_replace("Order", "",$array[12]));
  $temp["Options"] = trim(str_replace("Options", "",$array[13]));
  $temp["Date"] = trim(str_replace("Date", "",$array[14]));
  $temp["Status"] = trim(str_replace("Status", "",$array[15]));

  return $temp;
}

// Function that returns all the domains on a specified mail server in an array.
function get_domainlist()
{
  $result = command("domain list all");
  return $result;
}

// Function that returns all the mfilters on a specified mail server in an array.
function get_mfilterlist()
{
  $result = command("mfilter list all");
  return $result;
}

// Returns the results of an alias list command in an array.
function get_mailaliasinfo($name, $domain)
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "useralias list name=$name domain=$domain\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0460 User alias(es) listed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  if(eregi("ER0460 User alias not found", $results))
    $resultsarray[0] = "Alias has not been found";
  else
    $resultsarray = explode("\n", $results);
  return $resultsarray;
}

// Takes a result from an alias list command and parses the results and puts them into variables.
function parse_mailaliasinfo($array)
{
  $temp["Name"] = trim(str_replace("Name", "",$array[0]));
  $temp["Type"] = trim(str_replace("Type", "",$array[1]));
  $temp["Value"] = trim(str_replace("Value", "",$array[2]));
  $temp["Date"] = trim(str_replace("Date", "",$array[3]));
  $temp["Status"] = trim(str_replace("Status", "",$array[4]));

  return $temp;
}

// Returns if 3 if mailaccess is POP and IMAP, 1 if only POP and 2 if IMAP
function parse_mailaccess($access)
{
  if(preg_match("/POP,IMAP/", $access))
    return 3;
  else if(preg_match("/POP/", $access))
    return 1;
  else if(preg_match("/IMAP/", $access))
    return 2;
}

// Returns if 3 is mailacces POP/IMAP, if 1 only POP and if 2 IMAP
function get_mailaccess($access)
{
	if($access == 3)
		return "POP/IMAP";
	else if ($access == 2)
		return "IMAP";
	else if ($access == 1)
		return "POP";	
	
}

// Makes an error page if there's a MySQL error
function db_error($type)
{
  global $language, $lang;
  $error['title'] = $lang['error'];
  $error['back'] = 0;
  if($type == "connection")
    $error['message'] = $lang['mysql_connection_error'];
  else
    $error['message'] = $lang['mysql_query_error'];
  require("error.php");
  exit;
}

function get_status()
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "stat\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK5300 Status displayed", "", $results);
  $results = str_replace("OK0103 Bye", "", $results);
  $array = explode("\n", $results);
  
  return $array;
}

function parse_status($array)
{
  
  $temp["Version"] = trim(str_replace("Version", "",$array[0]));
  $temp["Date"] = trim(str_replace("Date", "",$array[1]));
  $temp["UpTime"] = trim(str_replace("UpTime", "",$array[2]));
  $temp["Mailsent"] = trim(str_replace("Mail sent", "",$array[3]));
  $temp["Mailrecv"] = trim(str_replace("Mail recv", "",$array[4]));
  $temp["Users"] = trim(str_replace("Users", "",$array[5]));
  $temp["SmtpIn"] = trim(str_replace("SmtpIn", "",$array[6]));
  $temp["SmtpOut"] = trim(str_replace("SmtpOut", "",$array[7]));
  $temp["PopIn"] = trim(str_replace("PopIn", "",$array[8]));
  $temp["Threads"] = trim(str_replace("Threads", "",$array[9]));
  $temp["TempFiles"] = trim(str_replace("TempFiles", "",$array[10]));


  return $temp;	
	
}


// Returns always a valid number, so there's only a 0 (zero) if a field is empty
function print_number($number)
{
  if(!is_numeric($number))
    $number = 0;
  return $number; 
}

// Checks a string (e-mail, forward, alias, username, domain) whether it's valid or not
function check($type, $content, $errorpage)
{
  global $lang, $language;
  $content = trim($content);

  // Status by error page
  if(($type == "alias" || $type == "username") && $errorpage == 1)
  {
    if(!preg_match("/^[a-zA-Z0-9][a-zA-Z0-9_.-]+$/", $content))
    {
      $error['title'] = $lang['error'];
      if($type == "alias")
        $error['message'] = $lang['invalid_alias'];
      else
        $error['message'] = $lang['invalid_username'];
      $error['back'] = 1;
      require("error.php");
      exit;
    }
  }
  else if(($type == "forward" || $type == "e-mail") && $errorpage == 1)
  {
    if(!preg_match("/^([a-zA-Z0-9][a-zA-Z0-9_.-]+)@([a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5})$/", $content))
    {
      $error['title'] = $lang['error'];
      if($type == "forward")
        $error['message'] = $lang['invalid_forward'];
      else
        $error['message'] = $lang['invalid_email'];
      $error['back'] = 1;
      require("error.php");
      exit;
    }
  }
  else if($type == "domain" && $errorpage == 1)
  {
    if(!preg_match("/^([a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5})$/", $content))
    {
      $error['title'] = $lang['error'];
      $error['message'] = $lang['invalid_domain'];
      $error['back'] = 1;
      require("error.php");
      exit;
    }
  }
  // Only status by true/false
  else if(($type == "alias" || $type == "username") && $errorpage == 0)
  {
    if(preg_match("/^[a-zA-Z0-9][a-zA-Z0-9_.-]+$/", $content))
      return true;
    else
      return false;
  }
  else if(($type == "forward" || $type == "e-mail") && $errorpage == 0)
  {
    if(preg_match("/^([a-zA-Z0-9][a-zA-Z0-9_.-]+)@([a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5})$/", $content))
      return true;
    else
      return false;
  }
  else if($type == "domain" && $errorpage == 0)
  {
    if(preg_match("/^([a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5})$/", $content))
      return true;
    else
      return false;
  }
}
function get_denyfilterlist()
{
  global $mailserver, $port, $remotepw;
  $fp = fsockopen($mailserver, $port);
  fgets($fp, 128);
  fputs($fp, "login $remotepw\n");
  fgets($fp, 128);
  $command = "DENY LIST ALL\n";
  fputs($fp, $command);
  fputs($fp, "quit\n");
  while (!feof($fp))
  {
    $results .= fgets($fp, 128);
  }
  fclose($fp);
  $results = str_replace("OK0660 Deny entries listed\r\n", "", $results);
  $results = str_replace("OK0103 Bye\r\n", "", $results);
  $resultsarray = explode("\n", $results);

  return $resultsarray;
}

function parse_denyfilterlist($line)
{
	$temparray = explode("\t",$line);
  $temp["Condition"] = $temparray[0];
  $temp["Value"] = $temparray[1];

  return $temp;
}
?>
Return current item: Webextreme