<?php
/*
	File     : Admin.class.php
	Version  : 0.2 Beta
	Copyright: Attila Soki 2001
	e-mail   : atiware@gmx.net
	Date     : 2001-05-12
*/

	include_once("MySql.class.php");

	class Admin {
		var $_id=null;
		var $_user=null;
		var $_password=null;
		var $_email=null;
		var $_root=false;
		var $_exists=false;
		var $_cookie=null;

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

		function _init($id=null) {
			$this->_id=$id;
			$this->_exists=false;
			$this->_user=null;
			$this->_password=null;
			$this->_email=null;
			$this->_root=false;
			$this->_cookie=null;
			if ($id) {
				$dbo=new MySql();
				$dbo->query("select * from ".MMCADMINSTABLE." where Id=".$this->getId());
				if ($dbo->numRows()) {
					$mr=$dbo->fetchArray();
					$this->_user=$mr["Username"];
					$this->_password=$mr["Pwd"];
					$this->_email=$mr["Email"];
					$this->_root=$mr["Root"];
					$this->_exists=true;
					$dbo->query("select password('".$this->_user.$this->_password."') as COOKIE");
					$mr=$dbo->fetchArray();
					$this->_cookie=$mr["COOKIE"];
					$dbo->freeResult();
				}
				$dbo->close();
			}
		}

		function Admin($id=null) {
			$this->_init($id);
		}

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

		function login($user,$pwd) {
			$loginid=0;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCADMINSTABLE." where Username='".$user."' and Pwd=password('".$pwd."')");
			if ($dbo->numRows()) {
				$mr=$dbo->fetchArray();
				$loginid=$mr["Id"];
				$dbo->freeResult();
			}
			$dbo->close();
			if ($loginid) {
				$this->_init($loginid);
			}
			return $loginid;
		}

		function getUsername() {
			return $this->_user;
		}

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

		function getEmail() {
			return $this->_email;
		}

		function isRoot() {
			return $this->_root;
		}

		function haveRightToDomain($id=null) {
			$retval=false;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCRIGHTSTABLE." where AdminId=".$this->getId()." and DomainId=".$id);
			if ($dbo->numRows()) {
				$retval=true;
				$dbo->freeResult();
			}
			$dbo->close();
			return $retval;
		}

		function create($user,$pwd,$email,$root,$adomain) {
			$newid=0;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCADMINSTABLE." where Username='".$user."'");
			if (!$dbo->numRows()) {
				//username free
				$dbo->freeResult();
				$q="insert into ".MMCADMINSTABLE." (Id,Username,Pwd,Email,Root) values(null,";
				$q.="'".$user."',";
				$q.="password('".$pwd."'),";
				$q.="'".$email."',";
				$q.=$root.")";
				$dbo->query($q);
				$newid=$dbo->insertid();
				//admin rights
				for ($i=0;$i<sizeof($adomain);$i++) {
					$q="insert into ".MMCRIGHTSTABLE." (Id,AdminId,DomainId) values(null,";
					$q.=$newid.",";
					$q.=$adomain[$i]["id"].")";
					$dbo->query($q);
				}
			}
			$dbo->close();
			if ($newid)
				$this->_init($newid);
			return $newid;
		}

		function modify($user,$pwd,$email,$root,$adomain) {
			$retval=true;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCADMINSTABLE." where Username='".$user."'");
			if ($dbo->numRows()) {
				while (($retval) and ($mr=$dbo->fetchArray())) {
					if ($mr["Id"]!=$this->getId())
						$retval=false;
				}
				$dbo->freeResult();
			}
			if ($retval) {
				$q="update ".MMCADMINSTABLE." set ";
				$q.="Username='".$user."',";
				if (strlen($pwd)>0) //if password empty, no password change
					$q.="Pwd=password('".$pwd."'),";
				$q.="Email='".$email."',";
				$q.="Root=".$root;
				$q.=" where Id=".$this->getId();
				$dbo->query($q);
				//admin rights
				$dbo->query("delete from ".MMCRIGHTSTABLE." where AdminId=".$this->getId());
				for ($i=0;$i<sizeof($adomain);$i++) {
					$q="insert into ".MMCRIGHTSTABLE." (Id,AdminId,DomainId) values(null,";
					$q.=$this->getId().",";
					$q.=$adomain[$i]["id"].")";
					$dbo->query($q);
				}
			}
			$dbo->close();
			if ($retval)
				$this->_init($this->getId());
			return $retval;
		}

		function getCookie() {
			return $this->_cookie;
		}

		function chkPassword($pass=null) {
			$retval=false;
			$dbo=new MySql();
			$dbo->query("select Id from ".MMCADMINSTABLE." where Id=".$this->getId()." and Pwd=password('".$pass."')");
			if ($dbo->numRows()) {
				$dbo->freeResult();
				$retval=true;
			}
			$dbo->close();
			return $retval;
		}

		function checkPassword($pass=null) {
			return $this->chkPassword($pass);
		}

		function setPassword($pass=null) {
			$dbo=new MySql();
			$dbo->query("update ".MMCADMINSTABLE." set Pwd=password('".$pass."') where Id=".$this->getId());
			$dbo->close();
			$this->_init($this->getId());
		}

		function kill() {
			$dbo=new MySql();
			$dbo->query("delete from ".MMCADMINSTABLE." where Id=".$this->getId());
			$dbo->query("delete from ".MMCRIGHTSTABLE." where AdminId=".$this->getId());
			$dbo->close();
			$this->_init($this->getId());
		}
	}
?>