Create LdapServer and LdapDomain class
This commit is contained in:
parent
4bd05bef03
commit
5b8ab54e33
123
htdocs/lib/class.ldapdomain.php
Normal file
123
htdocs/lib/class.ldapdomain.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
class LdapDomain extends LdapServer {
|
||||
protected $domain,$active=false;
|
||||
private $quota="0M/0M",$mail_accounts=array(),$mail_alias=array(),$posix_accounts=array(),$smb_accounts=array();
|
||||
|
||||
public function __construct($server, $name) {
|
||||
$this->conn = $server->conn;
|
||||
$this->login = $server->login;
|
||||
$this->superadmin = $server->superadmin;
|
||||
$this->dn = $server->dn;
|
||||
|
||||
$this->domain = $name;
|
||||
|
||||
$sr = ldap_search($this->conn, "cn=".$this->domain.",".LDAP_BASE, "(ObjectClass=*)");
|
||||
$objects = ldap_get_entries($this->conn, $sr);
|
||||
|
||||
foreach($objects as $object) {
|
||||
if (!empty($object['objectclass'])) {
|
||||
if (in_array("postfixDomain",$object['objectclass'])) {
|
||||
$this->active = $object['isactive'][0];
|
||||
}
|
||||
if (in_array("posixAccount",$object['objectclass'])) {
|
||||
array_push($this->posix_accounts,$object['uid'][0]);
|
||||
}
|
||||
if (in_array("mailAccount",$object['objectclass'])) {
|
||||
array_push($this->mail_accounts,$object['uid'][0]);
|
||||
}
|
||||
if (in_array("mailAlias",$object['objectclass'])) {
|
||||
array_push($this->mail_alias,$object['cn'][0]);
|
||||
}
|
||||
if (in_array("sambaSamAccount",$object['objectclass'])) {
|
||||
array_push($this->smb_accounts,$object['uid'][0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// $this->quota = getquota($this->domain,'group');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function del() {
|
||||
$del = ldap_delete($this->conn, "cn=".$this->domain.",".LDAP_BASE);
|
||||
if ($del) {
|
||||
# EvoLog::log("Del domain ".$this->domain);
|
||||
} else {
|
||||
# EvoLog::log("Delete $this->domain failed");
|
||||
}
|
||||
return $del;
|
||||
}
|
||||
|
||||
public function addAccount($name,$active=false,$admin=false,$accountactive=false,$courieractive=false,$webmailactive=false,$authsmtpactive=false,$amavisBypassSpamChecks=false) {
|
||||
global $conf;
|
||||
$mail = $name.'@'.$this->name;
|
||||
$info["uid"] = $mail;
|
||||
$info["cn"] = $name;
|
||||
$info["homeDirectory"] = "/home/vmail/" .$this->name. "/" .$name. "/";
|
||||
$info["uidNumber"]= $conf['unix']['uid'];
|
||||
$info["gidNumber"]= getgid($this->name);
|
||||
$info["isActive"] = $active;
|
||||
$info["isAdmin"] = $admin;
|
||||
$info["objectclass"][0] = "posixAccount";
|
||||
$info["objectclass"][1] = "organizationalRole";
|
||||
$info["objectclass"][2] = "mailAccount";
|
||||
#$info["objectclass"][3] = "amavisAccount";
|
||||
$info["maildrop"] = $mail;
|
||||
$info["mailacceptinggeneralid"] = $mail;
|
||||
$info["accountActive"] = $accountactive;
|
||||
$info["courierActive"] = $courieractive;
|
||||
$info["webmailActive"] = $webmailactive;
|
||||
$info["authsmtpActive"] = $authsmtpactive;
|
||||
#$info["amavisBypassSpamChecks"] = $amavisBypassSpamChecks;
|
||||
$info["userPassword"] = "{SSHA}" .Ldap::ssha($_POST['pass1']);
|
||||
|
||||
if (ldap_add($this->conn, "uid=".$mail.",cn=".$this->domain.",".LDAP_BASE, $info)) {
|
||||
mail($name, 'Premier message',"Mail d'initialisation du compte.");
|
||||
mailnotify($info,$_GET['domain'],$_POST['pass1']);
|
||||
# EvoLog::log("Add user ".$name);
|
||||
return TRUE;
|
||||
} else {
|
||||
# EvoLog::log("Add $name failed");
|
||||
var_dump($info);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
public function getNbAccounts() {
|
||||
return count($this->posix_accounts)+count($this->mail_alias);
|
||||
}
|
||||
|
||||
public function getNbMailAccounts() {
|
||||
return count($this->mail_accounts);
|
||||
}
|
||||
|
||||
public function getNbSmbAccounts() {
|
||||
return count($this->smb_accounts);
|
||||
}
|
||||
|
||||
public function getNbMailAlias() {
|
||||
return count($this->mail_alias);
|
||||
}
|
||||
|
||||
public function getQuota() {
|
||||
return $this->quota;
|
||||
}
|
||||
|
||||
public function getMailAccounts() {
|
||||
return $this->mail_accounts;
|
||||
}
|
||||
|
||||
public function getMailAlias() {
|
||||
return $this->mail_alias;
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
return true;
|
||||
}
|
||||
}
|
98
htdocs/lib/class.ldapserver.php
Normal file
98
htdocs/lib/class.ldapserver.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
class LdapServer {
|
||||
protected $conn=NULL,$login,$dn,$superadmin=false;
|
||||
private $domains=array();
|
||||
|
||||
public function __construct($login) {
|
||||
global $conf;
|
||||
$this->login = $login;
|
||||
$this->conn = ldap_connect(LDAP_URI) or die ("Impossible de se connecter au serveur LDAP ".LDAP_URI);
|
||||
if (!ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3)) {
|
||||
echo 'Impossible de modifier la version du protocole à 3';
|
||||
}
|
||||
ldap_bind($this->conn, LDAP_ADMIN_DN, LDAP_ADMIN_PASS) or die ("Authentification LDAP échoué !");
|
||||
|
||||
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) {
|
||||
$filter = ($conf['evoadmin']['version'] == 1) ? '(objectClass=ldapDomain)' : '(objectClass=postfixDomain)';
|
||||
$sr = ldap_search($this->conn, LDAP_BASE, $filter);
|
||||
$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 {
|
||||
$filter = ($conf['evoadmin']['version'] <= 2) ? ',domain=((?:(?:[0-9a-zA-Z_\-]+)\.){1,}(?:[0-9a-zA-Z_\-]+)),' : ',cn=((?:(?:[0-9a-zA-Z_\-]+)\.){1,}(?:[0-9a-zA-Z_\-]+)),';
|
||||
$mydomain = preg_replace("/uid=".$login.$filter.LDAP_BASE."/",'$1',$this->dn);
|
||||
array_push($this->domains,$mydomain);
|
||||
}
|
||||
}
|
||||
return $this->domains;
|
||||
}
|
||||
|
||||
public function addDomain($name,$active=false) {
|
||||
global $conf;
|
||||
$info["cn"]=$name;
|
||||
$info["objectclass"][0] = ($conf['evoadmin']['version'] == 1) ? 'ldapDomain' : 'postfixDomain';
|
||||
$info["objectclass"][1] = "posixGroup";
|
||||
$info["postfixTransport"] = "virtual:";
|
||||
$info["isActive"] = $active;
|
||||
$info["gidNumber"]= getfreegid();
|
||||
|
||||
if (ldap_add($this->conn, "cn=".$name.",".LDAP_BASE, $info)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue