LDAPAccountManager/lam/lib/modules/nisMailAlias.inc

518 lines
18 KiB
PHP
Raw Normal View History

<?php
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2019-08-15 12:37:05 +00:00
Copyright (C) 2004 - 2019 Roland Gruber
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
*/
/**
* Provides NIS mail alias management.
*
* @package modules
* @author Roland Gruber
*/
/**
* Provides NIS mail alias management.
*
* @package modules
*/
class nisMailAlias extends baseModule {
2006-08-13 12:58:19 +00:00
2013-01-14 17:10:55 +00:00
/** mail cache */
private $cachedMailList = null;
2014-01-14 18:50:54 +00:00
/** user cache */
private $cachedUserList = null;
/** display limit */
const DISPLAY_LIMIT = 50;
2015-08-16 08:47:48 +00:00
/**
* Returns true if this module can manage accounts of the current type, otherwise false.
2015-08-16 08:47:48 +00:00
*
* @return boolean true if module fits
*/
public function can_manage() {
return in_array($this->get_scope(), array('mailAlias'));
}
/**
* Returns meta data that is interpreted by parent class
*
* @return array array with meta data
2015-08-16 08:47:48 +00:00
*
2008-02-03 14:28:28 +00:00
* @see baseModule::get_metaData()
*/
function get_metaData() {
$return = array();
2007-11-19 18:42:03 +00:00
// icon
2007-12-01 12:34:52 +00:00
$return['icon'] = 'mailBig.png';
// base module
$return["is_base"] = true;
// LDAP filter
$return["ldap_filter"] = array('or' => "(objectClass=nisMailAlias)");
// alias name
$return["alias"] = _("Mail aliases");
2005-08-26 08:53:16 +00:00
// RDN attribute
$return["RDN"] = array("cn" => "normal");
// module dependencies
$return['dependencies'] = array('depends' => array(), 'conflicts' => array());
// managed object classes
$return['objectClasses'] = array('nisMailAlias');
2006-05-13 08:55:31 +00:00
// managed attributes
$return['attributes'] = array('cn', 'rfc822MailMember');
// help Entries
$return['help'] = array(
'alias' => array(
2012-02-04 15:56:31 +00:00
"Headline" => _("Alias name"), 'attr' => 'cn',
"Text" => _("Mails to this name are forwarded to the recipients.")
),
'recipient' => array(
"Headline" => _("Recipients"), 'attr' => 'rfc822MailMember',
"Text" => _("Please enter the recipients for this alias.")
),
'recipientList' => array(
2012-02-04 15:56:31 +00:00
"Headline" => _("Recipient list"), 'attr' => 'rfc822MailMember',
"Text" => _("This is a comma separated list of recipients.")
2013-01-14 17:10:55 +00:00
),
'filter' => array(
"Headline" => _("Filter"),
"Text" => _("Here you can enter a filter value. Only entries which contain the filter text will be shown.")
),
);
// upload fields
$return['upload_columns'] = array(
array(
'name' => 'nisMailAlias_alias',
'description' => _('Alias name'),
'help' => 'alias',
'example' => 'root',
'required' => true
),
array(
'name' => 'nisMailAlias_recipients',
'description' => _('Recipient list'),
'help' => 'recipientList',
'example' => _('smiller')
)
);
// available PDF fields
$return['PDF_fields'] = array(
2010-04-05 12:38:23 +00:00
'alias' => _('Alias name'),
'recipients' => _('Recipient list')
);
return $return;
}
/**
* This function fills the error message array with messages
*/
function load_Messages() {
2010-04-03 13:31:47 +00:00
$this->messages['alias'][0] = array('ERROR', _('Alias is empty or invalid!')); // third array value is set dynamically
$this->messages['alias'][1] = array('ERROR', _('Account %s:') . ' nisMailAlias_alias', _('Alias is empty or invalid!'));
$this->messages['recipient'][0] = array('ERROR', _('Recipient is invalid!')); // third array value is set dynamically
$this->messages['recipient'][1] = array('ERROR', _('Account %s:') . ' nisMailAlias_recipient', _('Recipient is invalid!'));
}
2006-08-13 12:58:19 +00:00
/**
* Returns a list of modifications which have to be made to the LDAP account.
*
* @return array list of modifications
* <br>This function returns an array with 3 entries:
* <br>array( DN1 ('add' => array($attr), 'remove' => array($attr), 'modify' => array($attr)), DN2 .... )
* <br>DN is the DN to change. It may be possible to change several DNs (e.g. create a new user and add him to some groups via attribute memberUid)
* <br>"add" are attributes which have to be added to LDAP entry
* <br>"remove" are attributes which have to be removed from LDAP entry
* <br>"modify" are attributes which have to been modified in LDAP entry
2011-02-26 13:14:10 +00:00
* <br>"info" are values with informational value (e.g. to be used later by pre/postModify actions)
*/
function save_attributes() {
2005-08-26 08:53:16 +00:00
// skip saving if account is based on another structural object class
2007-10-03 18:02:10 +00:00
if (!$this->getAccountContainer()->isNewAccount && !in_array('nisMailAlias', $this->getAccountContainer()->attributes_orig['objectClass'])) {
2005-08-26 08:53:16 +00:00
return array();
}
return parent::save_attributes();
}
/**
2007-11-03 14:17:19 +00:00
* Returns the HTML meta data for the main account page.
2015-08-16 08:47:48 +00:00
*
2010-09-20 17:41:20 +00:00
* @return htmlElement HTML meta data
2007-11-03 14:17:19 +00:00
*/
2006-08-13 12:58:19 +00:00
function display_html_attributes() {
2019-09-27 17:00:05 +00:00
$return = new htmlResponsiveRow();
// alias name
2010-09-20 17:41:20 +00:00
$alias = '';
if (isset($this->attributes['cn'][0])) {
$alias = $this->attributes['cn'][0];
}
2019-09-27 17:00:05 +00:00
$nameInput = new htmlResponsiveInputField(_('Alias name'), 'cn', $alias, 'alias');
2010-09-20 17:41:20 +00:00
$nameInput->setRequired(true);
2019-09-27 17:00:05 +00:00
$nameInput->setCSSClasses(array('maxwidth20'));
$return->add($nameInput, 12);
// list current recipients
2013-01-14 17:10:55 +00:00
$mailList = $this->getMailList();
2014-01-14 18:50:54 +00:00
$userList = $this->getUserList();
$autoList = array();
if ((sizeof($userList) + sizeof($mailList)) < 300) {
$autoList = array_merge($userList, $mailList);
}
2010-09-20 17:41:20 +00:00
$recipientCount = 0;
if (isset($this->attributes['rfc822MailMember'])) {
2011-02-13 12:34:24 +00:00
natcasesort($this->attributes['rfc822MailMember']);
$this->attributes['rfc822MailMember'] = array_values($this->attributes['rfc822MailMember']);
2010-09-20 17:41:20 +00:00
$recipientCount = sizeof($this->attributes['rfc822MailMember']);
if ($recipientCount < nisMailAlias::DISPLAY_LIMIT) {
for ($i = 0; $i < $recipientCount; $i++) {
if (($i == 0) && ($recipientCount == 1)) {
2019-09-27 17:00:05 +00:00
$return->addLabel(new htmlOutputText(_('Recipient')));
}
elseif (($i == 0) && ($recipientCount > 1)) {
2019-09-27 17:00:05 +00:00
$return->addLabel(new htmlOutputText(_('Recipients')));
}
else {
2019-09-27 17:00:05 +00:00
$return->addLabel(new htmlOutputText('&nbsp;', false));
}
$mailField = new htmlInputField('rfc822MailMember' . $i, $this->attributes['rfc822MailMember'][$i]);
if (sizeof($autoList) > 0) {
$mailField->enableAutocompletion($autoList);
}
2019-09-27 17:00:05 +00:00
$mailField->setCSSClasses(array('maxwidth20'));
$mailGroup = new htmlGroup();
$mailGroup->addElement($mailField);
$mailGroup->addElement(new htmlAccountPageButton(get_class($this), 'selectMail', 'recipient' . $i, 'mailAlias.png', true, _('Select mail')));
$mailGroup->addElement(new htmlAccountPageButton(get_class($this), 'selectUser', 'recipient' . $i, 'user.png', true, _('Select user')));
$mailGroup->addElement(new htmlButton('delRec' . $i, 'del.png', true));
$mailGroup->addElement(new htmlHelpLink('recipient'));
$return->addField($mailGroup);
2013-01-14 17:10:55 +00:00
}
}
else {
$memberText = implode("\r\n", $this->attributes['rfc822MailMember']);
2019-09-27 17:00:05 +00:00
$return->add(new htmlResponsiveInputTextarea('rfc822MailMember', $memberText, 50, 30, _('Recipients'), 'recipient'), 12);
2010-09-20 17:41:20 +00:00
}
}
if ($recipientCount < nisMailAlias::DISPLAY_LIMIT) {
// input box for new recipient
2019-09-27 17:00:05 +00:00
$return->addLabel(new htmlOutputText(_('New recipient')));
$newMailField = new htmlInputField('rfc822MailMember');
$newMailField->setOnKeyPress('SubmitForm(\'addRec\', event);');
if (sizeof($autoList) > 0) {
$newMailField->enableAutocompletion($autoList);
}
2019-09-27 17:00:05 +00:00
$newMailField->setCSSClasses(array('maxwidth20'));
$newGroup = new htmlGroup();
$newGroup->addElement($newMailField);
$newGroup->addElement(new htmlAccountPageButton(get_class($this), 'selectMail', 'recipient' . 'New', 'mailAlias.png', true, _('Select mail')));
$newGroup->addElement(new htmlAccountPageButton(get_class($this), 'selectUser', 'recipient' . 'New', 'user.png', true, _('Select user')));
$newGroup->addElement(new htmlButton('addRec', 'add.png', true));
$newGroup->addElement(new htmlHelpLink('recipient'));
$newGroup->addElement(new htmlHiddenInput('rec_number', $recipientCount));
$return->addField($newGroup);
2013-01-14 17:10:55 +00:00
}
return $return;
}
/**
* Processes user input of the primary module page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
2006-08-13 12:58:19 +00:00
function process_attributes() {
2006-05-16 15:38:55 +00:00
$errors = array();
$this->attributes['cn'] = array();
$recipientCount = !empty($this->attributes['rfc822MailMember']) ? sizeof($this->attributes['rfc822MailMember']) : 0;
$this->attributes['rfc822MailMember'] = array();
// check alias name
2006-08-13 12:58:19 +00:00
if (isset($_POST['cn']) && ($_POST['cn'] != "") && get_preg($_POST['cn'], 'nis_alias')) {
$this->attributes['cn'][] = $_POST['cn'];
}
else {
2011-04-19 17:47:41 +00:00
$this->attributes['cn'][] = $_POST['cn'];
$message = $this->messages['alias'][0];
2006-08-13 12:58:19 +00:00
$message[] = $_POST['cn'];
2006-08-16 17:42:35 +00:00
$errors[] = $message;
}
if ($recipientCount < nisMailAlias::DISPLAY_LIMIT) {
// check old recipients
if (isset($_POST['rec_number'])) {
for ($i = 0; $i < $_POST['rec_number']; $i++) {
if (isset($_POST['delRec' . $i])) continue;
if (isset($_POST['rfc822MailMember' . $i]) && ($_POST['rfc822MailMember' . $i] != "")) {
// check if address has correct format
if (!get_preg($_POST['rfc822MailMember' . $i], 'nis_recipient') && !get_preg($_POST['rfc822MailMember' . $i], 'email')) {
$message = $this->messages['recipient'][0];
$message[] = $_POST['rfc822MailMember' . $i];
$errors[] = $message;
}
$this->attributes['rfc822MailMember'][] = $_POST['rfc822MailMember' . $i];
}
}
}
// check new recipient
if (isset($_POST['rfc822MailMember']) && ($_POST['rfc822MailMember'] != "")) {
// check if recipient has correct format
if (get_preg($_POST['rfc822MailMember'], 'nis_recipient') || get_preg($_POST['rfc822MailMember'], 'email')) {
$this->attributes['rfc822MailMember'][] = $_POST['rfc822MailMember'];
}
else {
$message = $this->messages['recipient'][0];
$message[] = $_POST['rfc822MailMember'];
2006-08-16 17:42:35 +00:00
$errors[] = $message;
}
}
}
else {
$recipients = explode("\r\n", $_POST['rfc822MailMember']);
foreach ($recipients as $recipient) {
$recipient = trim($recipient);
if (empty($recipient)) {
continue;
}
$this->attributes['rfc822MailMember'][] = $recipient;
// check if recipient has correct format
if (!get_preg($recipient, 'nis_recipient') && !get_preg($recipient, 'email')) {
$message = $this->messages['recipient'][0];
$message[] = htmlspecialchars($recipient);
2006-08-16 17:42:35 +00:00
$errors[] = $message;
}
}
}
$this->attributes['rfc822MailMember'] = array_unique($this->attributes['rfc822MailMember']);
2006-05-16 15:38:55 +00:00
return $errors;
}
2013-01-14 17:10:55 +00:00
/**
2014-01-14 18:50:54 +00:00
* Displays the mail selection.
*
* @return htmlElement meta HTML code
*/
function display_html_selectMail() {
return $this->display_html_select(true);
}
2015-08-16 08:47:48 +00:00
2014-01-14 18:50:54 +00:00
/**
* Processes user input of the host/user selection page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
function process_selectMail() {
return $this->process_select();
}
2015-08-16 08:47:48 +00:00
2014-01-14 18:50:54 +00:00
/**
* Displays the user selection.
*
* @return htmlElement meta HTML code
*/
function display_html_selectUser() {
return $this->display_html_select(false);
}
2015-08-16 08:47:48 +00:00
2014-01-14 18:50:54 +00:00
/**
* Processes user input of the host/user selection page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
function process_selectUser() {
return $this->process_select();
}
2015-08-16 08:47:48 +00:00
2014-01-14 18:50:54 +00:00
/**
* Displays the user/mail selection.
2013-01-14 17:10:55 +00:00
*
2014-01-14 18:50:54 +00:00
* @param boolean $isMail mail selection (user selection if false)
2013-01-14 17:10:55 +00:00
* @return htmlElement meta HTML code
*/
2014-01-14 18:50:54 +00:00
function display_html_select($isMail) {
$options = array();
if ($isMail) {
$regex = 'email';
$options = $this->getMailList();
$suffix = 'Mail';
$label = _('Email');
}
else {
$regex = 'username';
$options = $this->getUserList();
$suffix = 'User';
$label = _('User');
}
2019-09-27 17:00:05 +00:00
$return = new htmlResponsiveRow();
2013-01-14 17:10:55 +00:00
$postKeys = array_keys($_POST);
$position = 'New';
for ($i = 0; $i < sizeof($postKeys); $i++) {
2014-01-14 18:50:54 +00:00
if (strpos($postKeys[$i], 'form_subpage_' . get_class($this) . '_select' . $suffix . '_recipient') === 0) {
$position = substr($postKeys[$i], strlen('form_subpage_' . get_class($this) . '_select' . $suffix . '_recipient'));
2013-01-14 17:10:55 +00:00
break;
}
}
// load list with all mail addresses
$count = sizeof($options);
for ($i = 0; $i < $count; $i++) {
2019-09-27 17:00:05 +00:00
if (!get_preg($options[$i], $regex)) {
2013-01-14 17:10:55 +00:00
unset($options[$i]);
}
}
$options = array_values($options);
2019-09-27 17:00:05 +00:00
$return->addLabel(new htmlOutputText(_('Filter')));
$filterGroup = new htmlGroup();
$filterInput = new htmlInputField('filter', null);
$filterInput->setCSSClasses(array('maxwidth10'));
$filterInput->filterSelectBox('selectBox');
$filterGroup->addElement($filterInput);
$filterGroup->addElement(new htmlHelpLink('filter'));
$return->addField($filterGroup);
$return->addLabel(new htmlOutputText($label));
2014-01-14 18:50:54 +00:00
$mailSelect = new htmlSelect('selectBox', $options, array(), 15);
$mailSelect->setMultiSelect($position === 'New');
2013-01-14 17:10:55 +00:00
$mailSelect->colspan = 5;
2019-09-27 17:00:05 +00:00
$return->addField($mailSelect);
$return->addVerticalSpacer('2rem');
$return->addLabel(new htmlAccountPageButton(get_class($this), 'attributes', 'select', _('Ok')));
$return->addField(new htmlAccountPageButton(get_class($this), 'attributes', 'back', _('Cancel')));
$return->add(new htmlHiddenInput('position', $position), 12);
2013-01-14 17:10:55 +00:00
return $return;
}
2015-08-16 08:47:48 +00:00
2013-01-14 17:10:55 +00:00
/**
* Processes user input of the host/user selection page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
function process_select() {
if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_select'])) {
2014-01-14 18:50:54 +00:00
if (!isset($this->attributes['rfc822MailMember'])) {
$this->attributes['rfc822MailMember'] = array();
}
2013-01-14 17:10:55 +00:00
$position = $_POST['position'];
if ($position == 'New') {
2014-01-14 18:50:54 +00:00
$this->attributes['rfc822MailMember'] = array_merge($this->attributes['rfc822MailMember'], $_POST['selectBox']);
2013-01-14 17:10:55 +00:00
}
else {
$this->attributes['rfc822MailMember'][$_POST['position']] = $_POST['selectBox'];
}
return array();
}
return array();
}
2015-08-16 08:47:48 +00:00
2005-08-26 08:53:16 +00:00
/**
* Controls if the module button the account page is visible and activated.
*
* @return string status ("enabled", "disabled", "hidden")
*/
function getButtonStatus() {
2007-10-03 18:02:10 +00:00
if (!$this->getAccountContainer()->isNewAccount) {
2005-08-26 08:53:16 +00:00
// check if account is based on our object class
2007-10-03 18:02:10 +00:00
$objectClasses = $this->getAccountContainer()->attributes_orig['objectClass'];
2005-08-26 08:53:16 +00:00
if (is_array($objectClasses) && !in_array('nisMailAlias', $objectClasses)) {
return "disabled";
}
}
return "enabled";
}
/**
2017-05-20 09:46:49 +00:00
* {@inheritDoc}
* @see baseModule::build_uploadAccounts()
*/
function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules, &$type) {
$messages = array();
for ($i = 0; $i < sizeof($rawAccounts); $i++) {
// add object class
if (!in_array("nisMailAlias", $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = "nisMailAlias";
// add alias name
2014-04-15 19:49:47 +00:00
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'nisMailAlias_alias', 'cn',
'nis_alias', $this->messages['alias'][1], $messages);
// add recipients
if ($rawAccounts[$i][$ids['nisMailAlias_recipients']] != "") {
$aliases = explode(',', $rawAccounts[$i][$ids['nisMailAlias_recipients']]);
// check format
for ($a = 0; $a < sizeof($aliases); $a++) {
2011-04-19 17:47:41 +00:00
if (get_preg($aliases[$a], 'nis_recipient') || get_preg($aliases[$a], 'email')) {
$partialAccounts[$i]['rfc822MailMember'][] = $aliases[$a];
}
else {
$errMsg = $this->messages['recipient'][1];
array_push($errMsg, array($i));
$messages[] = $errMsg;
}
}
}
}
return $messages;
}
/**
2017-02-19 08:14:11 +00:00
* {@inheritDoc}
* @see baseModule::get_pdfEntries()
2015-01-07 17:16:35 +00:00
*/
2017-04-01 07:57:03 +00:00
function get_pdfEntries($pdfKeys, $typeId) {
$return = array();
2013-05-06 19:16:47 +00:00
$this->addSimplePDFField($return, 'alias', _('Alias name'), 'cn');
$this->addSimplePDFField($return, 'recipients', _('Recipient list'), 'rfc822MailMember');
return $return;
}
2013-01-14 17:10:55 +00:00
/**
* Returns a list of existing email addresses.
*
* @return array email addresses
*/
private function getMailList() {
if ($this->cachedMailList != null) {
return $this->cachedMailList;
}
2017-03-21 18:26:31 +00:00
$this->cachedMailList = searchLDAPByAttribute('mail', '*', null, array('mail'), array('user'));
2013-01-14 17:10:55 +00:00
for ($i = 0; $i < sizeof($this->cachedMailList); $i++) {
$this->cachedMailList[$i] = $this->cachedMailList[$i]['mail'][0];
}
$this->cachedMailList = array_values(array_unique($this->cachedMailList));
return $this->cachedMailList;
}
2014-01-14 18:50:54 +00:00
/**
* Returns a list of existing user names.
*
* @return array user names
*/
private function getUserList() {
if ($this->cachedUserList != null) {
return $this->cachedUserList;
}
$this->cachedUserList = searchLDAPByFilter('(|(objectClass=posixAccount)(objectClass=inetOrgPerson))', array('uid'), array('user'));
$count = sizeof($this->cachedUserList);
for ($i = 0; $i < $count; $i++) {
if (empty($this->cachedUserList[$i]['uid'][0])) {
unset($this->cachedUserList[$i]);
continue;
}
$this->cachedUserList[$i] = $this->cachedUserList[$i]['uid'][0];
}
$this->cachedUserList = array_values(array_unique($this->cachedUserList));
return $this->cachedUserList;
}
}
?>