<?class ldap{ var $defaultServerHost='<EXCHANGE SERVER>'; var $defaultServerPort='<EXCHANGE SERVER PORT:default = 389>'; var $_dieOnFailure=false; function ldap($host='',$port='') { define("CLASS_LDAP",1); if ($host=='') $this->_host=$this->defaultServerHost; else $this->_host=$host; if ($port=='') $this->_port=$this->defaultServerPort; else $this->_port=$port; $this->ldap_connection=@ldap_connect($this->_host,$this->_port); if (!$this->ldap_connection) $this->_doError(0,"", "Connection to Server"); } function connect($userName, $passWord, $domain='<DOMAIN>', $country='nl') { $this->dn = $this->createDN($userName, $domain, $country); if (!isset($this->ldap_connection)) $this->ldap($_host,$_port); $this->_connect=@ldap_bind($this->ldap_connection,$this->dn,$passWord); if (!isset($this->_connect) || empty($this->_connect)) { $this->_doError(ldap_errno($this->ldap_connection),ldap_error($this->ldap_connection), "Login failed for dn= ". $this->dn . "\npass=". $passWord); return false; } else return true; } function search($filter, $baseDN='') { if (!isset($this->ldap_connection)) $this->ldap($_host,$_port); if ($baseDN) $dn=$baseDN; else $dn=$this->baseDN; $this->rs=@ldap_search($this->ldap_connection,$dn,$filter,$this->_reqAttr,$this->_attrOnly,$this->_sizeLimit, $this->_timeLimit); if (!isset($this->rs)) $this->_doError(ldap_errno($this->ldap_connection),ldap_error($this->ldap_connection), "Search failed for filter= ". $filter);}function fetch_records($oneRecord=false){ if ($oneRecord) { $record=ldap_get_entries($this->ldap_connection, $this->rs); if (isset($record)) return $record[0]; } else return (ldap_get_entries($this->ldap_connection, $this->rs));} function createDN($userName, $domain, $country) { $dn=sprintf("cn= %s, cn=users, dc=%s",$userName,$this->_host); if ($domain) $dn.=sprintf(", dc=", $domain); if ($country) $dn.=sprintf(", dc=", $country); return $dn; } function _doError($errno, $errString, $when='') { if (headers_sent()) { printf ("<!-- \n\n\n Error Occurred:"); printf ("\nWhen: %s\n ", $when); printf ("\nErrorno: %d", $errono); printf ("\nText: %s--> ",$errString); } if ($this->_dieOnFailure) die("ERROR with LDAP!"); } var $ldap_connection; var $ldap_result; var $_connect; var $dn; var $_host; var $_port; var $_reqAttr=array(); var $_attrOnly=0; var $_sizeLimit=99; var $_timeLimit=NULL; var $rs;} ?>