All pastes #951500 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-03-21 13:06 UTC
rendered paste body
<?php
require_once('class_ConfigReader.php');
require_once('class_Exception.php');

class LdapUtil {
	private $conn_;
	public function __construct($server = null) {
		if ($server == null) {
			// take it from the config file
			$constr = ConfigReader::getValue('ldap');
			$org = 'o=' . ConfigReader::getValue('ldapOrg');
			$this->conn_ = @ldap_connect($constr);
			if (!$this->conn_) throw new DataAccessException('Invalid server');
		}
	}
	public function authenticate($uName, $pwd) {
		$ldaprdn  = $this->createDN($uName);
		$r = ldap_bind($this->conn_, $ldaprdn, $pwd);
		echo ldap_error($this->conn_);
		if (!$r) throw new DataAccessException('Invalid user credentials');
	}
	public function add($uName, $info) {
		
		$ldapAdmin = ConfigReader::getValue('ldapAdmin');
		$ldapPwd = ConfigReader::getValue('ldapAdminPwd');
		
		$this->authenticate($ldapAdmin, $ldapPwd);
		$ldaprdn  = $this->createDN($uName);
		$r = @ldap_add($this->conn_, $ldaprdn, $info);
		if (!$r) throw new DataAccessException('Invalid parameters');
	}
	private function createDN(&$uName) {
		$uName = 'cn=' . $uName;
		$org = 'o=' . ConfigReader::getValue('ldapOrg');
		$ldaprdn = $uName . ',' . $org;
		return $ldaprdn;
	}
	public function __destruct() {
		@ldap_unbind($this->conn_);
	}
}
?>