<?php
/**
* @file includes/ldap.inc
* @brief LDAP Library
* @author Kenneth Smith <hide@address.com>
*
* Modularized Information Environment (MIE)
* Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('LDAP_INC') && exit;
define('LDAP_INC', true);
class LDAP{
public $base;
public $option;
public $link;
function __construct($_url, $_option = array()) {
$this->base = parse_url($_url);
$this->base['dn'] = isset($this->base['path']) ? basename($this->base['path']) : '';
$this->option = $_option;
$this->link = null;
}
function __destruct() {
$this->close();
}
function open() {
if($this->link) {
return $this->link;
}
if(isset($this->base['port'])) {
if(!$this->link = ldap_connect($this->base['host'], $this->base['port'])) {
die(ldap_error($this->link));
}
}
else{
if(!$this->link = ldap_connect($this->base['host'])) {
die(ldap_error($this->link));
}
}
if(isset($this->option['protocol'])) {
if(!ldap_set_option($this->link, LDAP_OPT_PROTOCOL_VERSION, $this->option['protocol'])) {
die(ldap_error($this->link));
}
}
if(isset($this->base['user']) && isset($this->base['pass'])) {
if(!ldap_bind($this->link, $this->base['user'], $this->base['pass'])) {
die(ldap_error($this->link));
}
}
}
function close() {
if($this->link == null)return;
if(!ldap_close($this->link)) {
die(ldap_error($this->link));
}
$this->link = null;
}
function bind($_bind_dn, $_bind_pw) {
$this->open();
error_reporting(0);
return @ldap_bind($this->link, $_bind_dn, $_bind_pw);
}
function get_dn($_criteria) {
$this->open();
$search = ldap_search($this->link, $this->base['dn'], $_criteria);
if(ldap_count_entries($this->link, $search) != 1) {
return;
}
return ldap_get_dn($this->link, ldap_first_entry($this->link, $search));
}
function entries($_criteria) {
$this->open();
return ldap_count_entries($this->link, ldap_search($this->link, $this->base['dn'], $_criteria));
}
function search($_criteria, $_maximum = 0) {
$this->open();
$entry = ldap_first_entry($this->link, ldap_search($this->link, $this->base['dn'], $_criteria));
if(!$entry) {
return;
}
$cnt = 0;
do {
$ret[$cnt] = ldap_get_attributes($this->link, $entry);
$cnt++;
if($cnt == $_maximum) {
break;
}
}while($entry = ldap_next_entry($this->link, $entry));
return $ret;
}
}
?>