evoadmin-mail/htdocs/lib/class.ldapserver.php

132 lines
4.9 KiB
PHP
Raw Normal View History

2017-12-13 17:47:38 +01:00
<?php
class LdapServer {
protected $conn=NULL,$login,$dn,$superadmin=false;
private $domains=array();
public function __construct($login) {
global $conf;
$this->login = $login;
if (!$this->conn = ldap_connect(LDAP_URI)) {
throw new Exception("Impossible de se connecter au serveur LDPA ".LDAP_URI);
}
2017-12-13 17:47:38 +01:00
if (!ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3)) {
throw new Exception("Impossible de modifier la version du protocole LDAP à 3");
}
if (!ldap_bind($this->conn, LDAP_ADMIN_DN, LDAP_ADMIN_PASS)) {
throw new Exception("Authentification LDAP échoué !");
2017-12-13 17:47:38 +01:00
}
if (in_array($this->login, $conf['admin']['logins'])) {
$this->superadmin = true;
}
return $this;
}
public function login($password) {
global $conf;
$sr=ldap_search($this->conn, LDAP_BASE, "(&(uid=".$this->login.")(isAdmin=TRUE))");
$info = ldap_get_entries($this->conn, $sr);
if ($info['count']) {
if (@ldap_bind($this->conn, $info[0]['dn'], $password)) {
unset($password);
$this->dn = $info[0]['dn'];
# EvoLog::log("Login success for " . $this->login);
return true;
} else {
$this->__destruct();
# EvoLog::log("Password failed : " . $this->login);
return false;
}
} else {
$this->__destruct();
# EvoLog::log("Login failed : " . $this->login);
return false;
}
}
public function getDomains() {
global $conf;
if (count($this->domains) == 0) {
if ($this->superadmin) {
$sr = ldap_search($this->conn, LDAP_BASE, LdapDomain::getClassFilter());
2017-12-13 17:47:38 +01:00
$objects = ldap_get_entries($this->conn, $sr);
foreach($objects as $object) {
if(!empty($object["cn"][0])) {
$domain = new LdapDomain($this, $object["cn"][0]);
array_push($this->domains, $domain);
}
}
sort($this->domains);
} else {
$auid = explode('@', $this->login);
$domain = new LdapDomain($this, $auid[1]);
array_push($this->domains, $domain);
2017-12-13 17:47:38 +01:00
}
}
return $this->domains;
}
2017-12-15 15:36:22 +01:00
public function addDomain($name,$active=false) {
2017-12-13 17:47:38 +01:00
global $conf;
$info["cn"]=$name;
$info["objectclass"] = LdapDomain::$objectClass;
2017-12-15 15:36:22 +01:00
$info["isActive"] = ($active) ? 'TRUE' : 'FALSE';
2017-12-13 17:47:38 +01:00
$info["gidNumber"]= getfreegid();
2017-12-14 00:32:58 +01:00
if (!@ldap_add($this->conn, "cn=".$name.",".LDAP_BASE, $info)) {
$error = ldap_error($this->conn);
throw new Exception("Erreur dans l'ajout du domaine : $error");
2017-12-13 17:47:38 +01:00
}
}
2017-12-15 11:42:04 +01:00
public function delDomain($name) {
if ($sr = @ldap_search($this->conn, "cn=".$name.",".LDAP_BASE, "(ObjectClass=*)")) {
$objects = ldap_get_entries($this->conn, $sr);
// Delete aliases
foreach($objects as $object) {
if (!empty($object['objectclass']) && !in_array(LdapDomain::$objectClass[0], $object['objectclass']) && in_array(LdapAlias::$objectClass[0], $object['objectclass'])) {
2017-12-15 11:42:04 +01:00
$dn = "cn=".$object['cn'][0]. ",cn=".$name.",".LDAP_BASE;
if (!ldap_delete($this->conn, $dn)) {
$error = ldap_error($this->conn);
throw new Exception("Erreur dans la suppression de l'alias $dn : $error");
}
}
}
// Delete accounts
foreach($objects as $object) {
if (!empty($object['objectclass']) && !in_array(LdapDomain::$objectClass[0], $object['objectclass']) && !in_array(LdapAlias::$objectClass[0], $object['objectclass'])) {
2017-12-15 11:42:04 +01:00
$dn = "uid=".$object['cn'][0]. ",cn=".$name.",".LDAP_BASE;
if (!ldap_delete($this->conn, $dn)) {
$error = ldap_error($this->conn);
throw new Exception("Erreur dans la suppression du compte $dn : $error");
}
}
}
// Delete domain
$dn = "cn=".$name.",".LDAP_BASE;
if (!ldap_delete($this->conn, $dn)) {
$error = ldap_error($this->conn);
throw new Exception("Erreur dans la suppression du domaine $dn : $error");
}
} else {
throw new Exception("Ce domaine n'existe pas !");
}
}
2017-12-13 17:47:38 +01:00
public function isSuperAdmin() {
return $this->superadmin;
}
public function getLogin() {
return $this->login;
}
public function getDn() {
return $this->dn;
}
public function __destruct() {
ldap_unbind($this->conn);
}
}