<?php
	require("constants.inc.php");
	include_once("MySql.class.php");
	class Domain {
		var $_id=null;
		var $_domain=null;
		var $_msip=null;
		var $_msport=null;
		var $_msdb=null;
		var $_msuser=null;
		var $_mspwd=null;
		var $_mtable=null;
		var $_fuser=null;
		var $_fdomain=null;
		var $_falias=null;
		var $_fisalias=null;
		var $_fpassword=null;
		var $_pwdcrypt=null;
		var $_exists=false;

		function getId() {
			return $this->_id;
		}

		
		//connect to remote mysql server 
		//because you can work from a central workstation and connect to remote e-mail servers
		function _connectDB(&$dbo) {
			if ($dbo) {
				$dbo->setDbName($this->getMySqlDb());
				$dbo->setPort($this->getMySqlServerPort());
				$dbo->setHost($this->getMySqlServerIp());
				$dbo->setUser($this->getMySqlUser());
				$dbo->setPassword($this->getMySqlPassword());
				$dbo->connect(); //now can connect
			}
		}

		//if id==string init with domain_name else init with id
		function _init($id=null) {
			$this->_id=null;
			$this->_domain=null;
			$this->_msip=null;
			$this->_msport=null;
			$this->_msdb=null;
			$this->_msuser=null;
			$this->_mspwd=null;
			$this->_mtable=null;
			$this->_fuser=null;
			$this->_fdomain=null;
			$this->_falias=null;
			$this->_fisalias=null;
			$this->_fpassword=null;
			$this->_pwdcrypt=null;
			$this->_exists=null;
			if (!(int)$id)
				$this->_domain=$id;
			else
				$this->_id=$id;
			if ($id) {
				$dbo=new MySql();
				if (!(int)$id)
					$dbo->query("select * from ".MMCDOMAINTABLE." where Domain='".$this->getId()."'");
				else
					$dbo->query("select * from ".MMCDOMAINTABLE." where Id=".$this->getId());
				if ($dbo->numRows()) {
					$mr=$dbo->fetchArray();
					$this->_id=$mr["Id"];
					$this->_domain=$mr["Domain"];
					$this->_msip=$mr["MServerIp"];
					$this->_msport=$mr["MServerPort"];
					$this->_msdb=$mr["MServerDb"];
					$this->_msuser=$mr["MServerUser"];
					$this->_mspwd=$mr["MServerPwd"];
					$this->_mtable=$mr["MailTable"];
					$this->_fuser=$mr["FieldUser"];
					$this->_fdomain=$mr["FieldDomain"];
					$this->_falias=$mr["FieldAlias"];
					$this->_fisalias=$mr["FieldIsAlias"];
					$this->_fpassword=$mr["FieldPassword"];
					$this->_pwdcrypt=$mr["PwdCrypt"];
					$this->_exists=true;
					$dbo->freeResult();
				}
				$dbo->close();
			}
		}

		//if id==string init with domain else init with id
		function Domain($id=null) {
			$this->_init($id);
		}

		function isValid() {
			return $this->_exists;
		}

		function getName() {
			return $this->_domain;
		}

		function getDomain() {
			return $this->getName();
		}

		function getMySqlServerIp() {
			return $this->_msip;
		}

		function getMySqlServerPort() {
			return $this->_msport;
		}

		function hasCryptedPasswords() {
			return $this->_pwdcrypt;
		}

		function getMySqlDb() {
			return $this->_msdb;
		}

		function getMySqlUser() {
			return $this->_msuser;
		}

		function getMySqlPassword() {
			return $this->_mspwd;
		}

		function getMailTable() {
			return $this->_mtable;
		}

		function getUserField() {
			return $this->_fuser;
		}

		function getDomainField() {
			return $this->_fdomain;
		}

		function getAliasField() {
			return $this->_falias;
		}

		function getIsAliasField() {
			return $this->_fisalias;
		}

		function getPasswordField() {
			return $this->_fpassword;
		}

		//create("qwert.com","mysqlip",(int)mysqlport,"mysql_db","mysql_user","mysql_password","mailtablename","userfieldname","domainfieldname","aliasfieldname","isaliasfiledname",(int)encrypt_passwords)
		function create($domain=null,$msip=null,$msport=null,$msdb=null,$msuser=null,$mspwd=null,$mtable=null,$fuser=null,$fdomain=null,$falias=null,$fisalias=null,$fpassword=null,$pwdcrypt=null) {
			$retval=0;
			if ($domain) {
				$q="insert into ".MMCDOMAINTABLE." (Id,Domain,MServerIp,MServerPort,MServerDb,MServerUser,MServerPwd,MailTable,FieldUser,FieldDomain,FieldAlias,FieldIsAlias,FieldPassword,PwdCrypt) ";
				$q.="values(null,";
				$q.="'".$domain."',";
				$q.="'".$msip."',";
				$q.=$msport.",";
				$q.="'".$msdb."',";
				$q.="'".$msuser."',";
				$q.="'".$mspwd."',";
				$q.="'".$mtable."',";
				$q.="'".$fuser."',";
				$q.="'".$fdomain."',";
				$q.="'".$falias."',";
				$q.="'".$fisalias."',";
				$q.="'".$fpassword."',";
				$q.=(int)$pwdcrypt.")";

				$dbo=new MySql();
				$dbo->query($q);
				$retval=$dbo->insertid();
				$dbo->close();
				$this->_init($retval);
			}
			return $retval;
		}

		//modify("qwert.com","mysqlip",(int)mysqlport,"mysql_db","mysql_user","mysql_password","mailtablename","userfieldname","domainfieldname","aliasfieldname","isaliasfiledname",(int)encrypt_passwords)
		function modify($domain=null,$msip=null,$msport=null,$msdb=null,$msuser=null,$mspwd=null,$mtable=null,$fuser=null,$fdomain=null,$falias=null,$fisalias=null,$fpassword=null,$pwdcrypt=null) {
			if ($domain) {
				$q="update ".MMCDOMAINTABLE." set ";
				$q.="Domain='".$domain."',";
				$q.="MServerIp='".$msip."',";
				$q.="MServerPort=".$msport.",";
				$q.="MServerDb='".$msdb."',";
				$q.="MServerUser='".$msuser."',";
				if (strlen($mspwd)>0) //if password empty, no change
					$q.="MServerPwd='".$mspwd."',";
				$q.="MailTable='".$mtable."',";
				$q.="FieldUser='".$fuser."',";
				$q.="FieldDomain='".$fdomain."',";
				$q.="FieldAlias='".$falias."',";
				$q.="FieldIsAlias='".$fisalias."',";
				$q.="FieldPassword='".$fpassword."', ";
				$q.="PwdCrypt=".(int)$pwdcrypt." ";
				$q.="where Id=".$this->getId();

				$dbo=new MySql();
				$dbo->query($q);
				$retval=$dbo->insertid();
				$dbo->close();
				$this->_init($this->getId());
			}
		}

		//delete domain only from domain config table
		function kill() {
			$dbo=new MySql();
			$dbo->query("delete from ".MMCDOMAINTABLE." where Id=".$this->getId());
			$dbo->query("delete from ".MMCRIGHTSTABLE." where DomainId=".$this->getId());
			$dbo->close();
			$this->_init($this->getId());
		}


		//delete domain from confi table and !!!!DELETE ALL ***@thisdomain.com from exims usertable.
		//with other words, delete all user accounts too
		function killall() {
			$retval=false;
			// !!! delete all email account on this domain !!!!
			$dbo=new MySql(false); //noconnect
			$this->_connectDB($dbo);
			$dbo->query("select 1+1 as TWO"); //connect check (sure be sure)
			if ($dbo->numRows()) {
				$dbo->freeResult();
				$q="delete from ".$this->getMailTable()." where ".$this->getDomainField()."='".$this->getDomain()."'";
				$dbo->query($q);
				$dbo->query("select * from ".$this->getMailTable()." where ".$this->getDomainField()."='".$this->getDomain()."' limit 1");
				if (!$dbo->numRows())
					$retval=true;
				else
					$dbo->freeResult();
			}
			$dbo->close();
			if ($retval)
				$this->kill();
			return $retval;
		}

		function getCreateDbQuery() {
			$q="create database ".$this->getMySqlDb();
			return $q;
		}

		function getCreateMailTableQuery() {
			$q="create table ";
			$q.=$this->getMySqlDb().".".$this->getMailTable()." (";
			$q.=$this->getUserField()." char(63) not null,";
			$q.=$this->getDomainField()." char(127) not null,";
			$q.=$this->getAliasField()." char(255),";
			$q.=$this->getIsAliasField()." enum('yes','no') default 'no',";
			$q.=$this->getPasswordField()." char(32),";
			$q.="key ".$this->getUserField()." (".$this->getUserField()."),";
			$q.="key ".$this->getDomainField()." (".$this->getDomainField()."))";
			return $q;
		}

		function getCreateRelayIpTableQuery() {
			$q="create table ";
			$q.=$this->getMySqlDb().".relay_ip (ip char(15) not null, ts datetime not null, key ip (ip), key ts (ts))";
			return $q;
		}

		function createDb() {
			$dbo=new MySql(false);
			$this->_connectDb(&$dbo);
			$dbo->query($this->getCreateDbQuery());
			$dbo->query($this->getCreateMailTableQuery());
			$dbo->query($this->getCreateRelayIpTableQuery());
			$dbo->close();
		}
	}
?>