piler/webui/system/database/ldap.php

106 lines
2.4 KiB
PHP
Raw Normal View History

2012-02-08 23:14:28 +01:00
<?php
class LDAP {
private $link;
private $bind;
public function __construct($ldaphost, $ldapport, $binddn, $bindpw) {
2012-02-08 23:14:28 +01:00
$this->link = ldap_connect($ldaphost, $ldapport) or exit('Error: ldap_connect()');
2012-02-08 23:14:28 +01:00
ldap_set_option($this->link, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($this->link, LDAP_OPT_REFERRALS, 0);
2012-02-08 23:14:28 +01:00
if(@ldap_bind($this->link, $binddn, $bindpw)) {
$this->bind = 1;
}
else {
$this->bind = 0;
}
return $this->link;
}
public function is_bind_ok() {
return $this->bind;
}
public function query($basedn, $filter, $justthese) {
$i = 0;
$data = array();
$sr = ldap_search($this->link, $basedn, $filter, $justthese);
$results = ldap_get_entries($this->link, $sr);
if(LOG_LEVEL >= NORMAL) { syslog(LOG_INFO, sprintf("ldap query: base dn='%s', filter='%s', attr='%s', %d hits", $basedn, $filter, implode(" ", $justthese), $results['count'])); }
2013-04-03 22:45:05 +02:00
2012-02-08 23:14:28 +01:00
for($i=0; $i < $results['count']; $i++) {
for($k=0; $k < $results[$i]['count']; $k++) {
$attr = $results[$i][$k];
if($results[$i][$attr]['count'] == 1) {
$data[$i][$attr] = isset($results[$i][$attr][0]) ? $results[$i][$attr][0] : "";
}
else {
$data[$i][$attr] = isset($results[$i][$attr]) ? $results[$i][$attr] : "";
}
}
$data[$i]['dn'] = $results[$i]['dn'];
}
$query = new stdClass();
$query->filter = $filter;
2012-02-08 23:14:28 +01:00
$query->row = isset($data[0]) ? $data[0] : array();
$query->dn = isset($results[0]['dn']) ? $results[0]['dn'] : "";
$query->rows = $data;
$query->num_rows = $results['count'];
unset($data);
return $query;
}
public function add($dn, $entry) {
return ldap_add($this->link, $dn, $entry);
}
public function modify($dn, $entry) {
return ldap_modify($this->link, $dn, $entry);
}
public function rename($dn, $newrdn, $newparent) {
return ldap_rename($this->link, $dn, $newrdn, $newparent, TRUE);
}
public function replace($dn, $entry) {
return ldap_mod_replace($this->link, $dn, $entry);
}
public function delete($dn) {
return ldap_delete($this->link, $dn);
}
public function __destruct() {
ldap_unbind($this->link);
}
}
?>