<?php
/**
* This file handles the LDAP operation for Ms Active Directory
*
* @package Ldap
* @subpackage MsActiveDirectory
*/

/**
* Include the base ldap class.
*/
require_once(dirname(__FILE__).'/Ldap.php');

class MsActiveDirectory extends Ldap
{
	/**
	* MsActiveDirectory
	* Constructor for MsActiveDirectory class
	*
	* @param String		$ldapHost 		LDAP Server Host (hostname or ip address)
	* @param Integer	$ldapPort 		LDAP Server Port
	* @param String		$username 		LDAP Connect Username - Authorized User to connect to LDAP Server
	* @param String		$password 		Password for Connect Username
	* @param String		$baseDn 		The Base DN where our LDAP will operate from
	* @param Boolean	$ldapVersion3 	The Indication flag whether this LDAP Connection is using LDAP Version 3 connection.
	* @param Boolean 	$negotiateTLS 	The Indication flag whether this LDAP Connection is using TLS Negotiation.
	* @param Integer	$followReferral The option for Referral for this LDAP Connection
	*
	* @return Mixed		return not success return null 
	*/
	function MsActiveDirectory ($ldapHost, $ldapPort, $username, $password, $baseDn , $ldapVersion3, $negotiateTLS, $followReferral)
	{
		parent::Ldap($ldapHost, $ldapPort, $username, $password, $baseDn , $ldapVersion3, $negotiateTLS, $followReferral);
	}

	/**
	* setAccountStatus
	* Set the account status for an entry in LDAP
	*
	* @param String		$dn			Distinguish name - unique name for ldap entry	
	* @param Boolean	$enableFlg	if true, the status will be set to Active. Otherwise, the status will be set as Disable
	* @param interger	$userCurrentStatus	Current status of the user
	*
	* @return Boolean	return the update status
	*/
	function setAccountStatus ($dn, $enableFlg, $userCurrentStatus) {
		$status = ($userCurrentStatus | 2);
		if ($enableFlg) {
			$status = ($status & ~2);
		}
		$statusAttribute = array($this->getAccountStatusAttribute()=>$status);
		if(!$this-> modify($dn, $statusAttribute)) {
			return false;
		}
		return true;
	}

	/**
	* getAccountStatus
	* Get the status from the specified LDAP entry
	*
	* @param	String		$searchString		Filter criteria - used for query the ldap entries	
	*
	* @return	Array		if there is a result, return the result in array form. Otherwise, return an empty array
	*/
	function getAccountStatus ($searchString)
	{
		return $this->search("$searchString", array($this->getAccountStatusAttribute()));
	}

	/**
	* isStatusEnable
	* To check if the returned status is Enable or Disable
	*
	* @param	Mixed		$status		The account status return from LDAP server
	*
	* @return	Boolean		If the user's account is enable or disable
	*/
	function isStatusEnable($status)
	{
		$decodeStatus =($status & 2);
		if ($decodeStatus == 2) {
			return false;
		}
		return true;
	}

	/**
	* getAccountStatusAttribute
	* Get the Attribute name that hold the user account status
	*
	* @see getAccountStatus
	* @see setAccountStatus
	* 
	* @return	String	Return the attribute of user account status
	*/
	function getAccountStatusAttribute()
	{
		return 'useraccountcontrol';
	}

}
?>