Add LdapAlias class and use it for list alias

This commit is contained in:
Victor LABORIE 2017-12-17 15:02:34 +01:00
parent 2b0a51d115
commit 4c0c75d6ed
4 changed files with 82 additions and 12 deletions

View file

@ -84,7 +84,6 @@ if (!empty($_POST['isactive']) && $server->isSuperAdmin()) {
</select>
</div>
</form>
<form name="del" method="post" action="admin.php?domain=<?php print $domain->getName(); ?>">
<?php
}
@ -92,8 +91,9 @@ if (!empty($_POST['isactive']) && $server->isSuperAdmin()) {
?>
<h2>Liste des comptes :</h2><hr>
<h2>Liste des comptes :</h2><hr>
<form name="del" method="post" action="admin.php?domain=<?php print $domain->getName(); ?>">
<table class="table table-striped table-condensed">
<thead>
<tr>
@ -136,17 +136,19 @@ if (!empty($_POST['isactive']) && $server->isSuperAdmin()) {
print '<td><button type="submit" name="account" value="'.$account->getUid().'"><span class="glyphicon glyphicon-trash"></span></button></td>';
print '</tr>';
}
print "</tbody></table>";
print "</tbody></table></form>";
} elseif ( (isset($_GET['viewonly'])) && ($_GET['viewonly']==2) ) {
?>
<h2>Liste des alias/groupe de diffusion&nbsp;:</h2>
<h2>Liste des alias/groupe de diffusion&nbsp;:</h2>
<form name="del" method="post" action="admin.php?domain=<?php print $domain->getName(); ?>&viewonly=2">
<table class="table table-striped table-condensed">
<thead>
<tr>
<th><strong>Nom de l'alias/groupe de diffusion</strong></th>
<th width="100px">Actif</th>
<th width="50px">Suppr</th>
</tr>
</thead>
@ -156,14 +158,19 @@ if (!empty($_POST['isactive']) && $server->isSuperAdmin()) {
<?php
$aliases = $domain->getAlias();
foreach ($aliases as $alias) {
print '<tr><td style="text-align:left;"><a href="alias.php?domain='.$domain->getName().'&view='.$alias. '">' .$alias. '</a></td>';
print '<td><a href="alias.php?domain='.$domain->getName().'&del=' .$alias. '"><span class="glyphicon glyphicon-trash"></span></a></td></tr>';
print '<tr><td style="text-align:left;"><a href="alias.php?domain='.$domain->getName().'&alias='.$alias->getName(). '">' .$alias->getname(). '</a></td>';
if ($alias->isActive()) {
print '<td><span class="glyphicon glyphicon-ok"></span></td>';
} else {
print '<td><span class="glyphicon glyphicon-remove"></span></td>';
}
print '<td><button type="submit" name="alias" value="'.$alias->getName().'"><span class="glyphicon glyphicon-trash"></span></button></td>';
print '</tr>';
}
print "</tbody></table></form>";
}
?>
</table>
</form>
</div>
<?php include("inc/fin.php"); ?>

View file

@ -14,6 +14,9 @@ if (empty($_SESSION['login'])) {
if (!empty($_GET['account'])) {
$account = new LdapAccount($domain, Html::clean($_GET['account']));
}
if (!empty($_GET['alias'])) {
$alias = new LdapAlias($domain, Html::clean($_GET['alias']));
}
}
} catch (Exception $e) {
print '<div class="alert alert-danger" role="alert">'.$e->getMessage().'</div>';

View file

@ -0,0 +1,42 @@
<?php
class LdapAlias extends LdapDomain {
protected $domain,$name,$active=false;
private $aliases=array(),$redirections=array();
public function __construct($domain, $name) {
$this->conn = $domain->conn;
$this->domain = $domain->getName();
$this->name = $name;
if ($sr = @ldap_search($this->conn, "cn=".$name.",cn=".$this->domain.",".LDAP_BASE, "(ObjectClass=mailAlias)")) {
$objects = ldap_get_entries($this->conn, $sr);
$object = $objects[0];
$this->active = ($object['isactive'][0] == 'TRUE') ? true : false;
$this->aliases = array_filter($object['mailacceptinggeneralid'], "is_string");
$this->redirections = array_filter($object['maildrop'], "is_string");
} else {
throw new Exception("Cet alias n'existe pas !");
}
}
public function isActive() {
return $this->active;
}
public function getName() {
return $this->name;
}
public function getAliases() {
return preg_replace('/@'.$this->domain.'/', '', $this->aliases);
}
public function getRedirections() {
return $this->redirections;
}
public function __destruct() {
return true;
}
}

View file

@ -63,14 +63,17 @@ class LdapDomain extends LdapServer {
global $conf;
if (count($this->alias) == 0) {
if (! $conf['domaines']['onlyone']) {
$rdn = ($conf['evoadmin']['version'] <= 2) ? "cn=" .$this->domain. "," .LDAP_BASE : "domain=" .$this->domain. "," .LDAP_BASE;
$rdn = ($conf['evoadmin']['version'] > 2) ? "cn=" .$this->domain. "," .LDAP_BASE : "domain=" .$this->domain. "," .LDAP_BASE;
} else {
$rdn = "ou=people," .LDAP_BASE;
}
$sr = ldap_search($this->conn, $rdn, "(objectClass=mailAlias)");
$info = ldap_get_entries($this->conn, $sr);
for ($i=0;$i<$info["count"];$i++) {
array_push($this->alias,$info[$i]["cn"][0]);
$objects = ldap_get_entries($this->conn, $sr);
foreach($objects as $object) {
if(!empty($object["cn"][0])) {
$alias = new LdapAlias($this, $object["cn"][0]);
array_push($this->alias, $alias);
}
}
}
return $this->alias;
@ -115,6 +118,21 @@ class LdapDomain extends LdapServer {
}
}
public function addAlias($name,$active=false,$mailaccept=array(),$maildrop=array()) {
$info["cn"] = $name;
$info["isActive"] = ($active) ? 'TRUE' : 'FALSE';
$info["objectclass"][0] = "mailAlias";
$info["mailacceptinggeneralid"] = $mailaccept;
$info["maildrop"] = array_filter($maildrop, function($value) {
return filter_var($value, FILTER_VALIDATE_EMAIL);
});
if (!@ldap_add($this->conn, "cn=".$name.",cn=".$this->domain.",".LDAP_BASE, $info)) {
$error = ldap_error($this->conn);
throw new Exception("Erreur dans l'ajout de l'alias : $error");
}
}
public function delAccount($uid) {
$dn = "uid=".$uid.",cn=".$this->domain.",".LDAP_BASE;
if ($sr = @ldap_search($this->conn, $dn, "(ObjectClass=mailAccount)")) {