| 1 |
<?php |
| 2 |
|
| 3 |
use LAM\TYPES\TypeManager; |
| 4 |
|
| 5 |
/* |
| 6 |
|
| 7 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 8 |
Copyright (C) 2014 - 2026 Roland Gruber |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation; either version 2 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* Provides NIS mail alias management. |
| 27 |
* |
| 28 |
* @package modules |
| 29 |
* @author Roland Gruber |
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* Provides NIS mail alias management. |
| 34 |
* |
| 35 |
* @package modules |
| 36 |
*/ |
| 37 |
class nisMailAliasUser extends baseModule { |
| 38 |
|
| 39 |
/** @var array<int, array<string, string[]|string>>|null $cachedAliasList alias cache */ |
| 40 |
private ?array $cachedAliasList = null; |
| 41 |
|
| 42 |
/** @var array<string, string>[] $recipientsToDelete recipient entries to delete (list of arrays: dn => attributes) */ |
| 43 |
private array $recipientsToDelete = []; |
| 44 |
/** @var string[] $aliasesToDelete complete alias entries to delete */ |
| 45 |
private array $aliasesToDelete = []; |
| 46 |
/** @var array<string, array<string, string[]|string>> new alias entries (list of arrays: dn => attributes) */ |
| 47 |
private array $aliasesToAdd = []; |
| 48 |
/** @var array<string, string[]> $recipientsToAdd alias entries to extend with new recipients (list of arrays: dn => recipients) */ |
| 49 |
private array $recipientsToAdd = []; |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
*/ |
| 54 |
public function can_manage(): bool { |
| 55 |
return $this->get_scope() === 'user'; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritDoc} |
| 60 |
*/ |
| 61 |
public function get_metaData(): array { |
| 62 |
$return = []; |
| 63 |
// icon |
| 64 |
$return['icon'] = 'mail.svg'; |
| 65 |
// alias name |
| 66 |
$return["alias"] = _("Mail aliases"); |
| 67 |
// module dependencies |
| 68 |
$return['dependencies'] = ['depends' => [['inetOrgPerson', 'posixAccount']], 'conflicts' => []]; |
| 69 |
// help Entries |
| 70 |
$return['help'] = [ |
| 71 |
'aliasUser' => [ |
| 72 |
"Headline" => _("Alias names with user name"), |
| 73 |
"Text" => _('Sets the alias names linked to the current user name.') |
| 74 |
], |
| 75 |
'aliasMail' => [ |
| 76 |
"Headline" => _("Alias names with email address"), |
| 77 |
"Text" => _('Sets the alias names linked to the user\'s email address.') |
| 78 |
], |
| 79 |
'suffix' => [ |
| 80 |
"Headline" => _("Suffix"), |
| 81 |
"Text" => _("Location where new alias is stored.") |
| 82 |
], |
| 83 |
'newAlias' => [ |
| 84 |
"Headline" => _("Alias name"), 'attr' => 'cn', |
| 85 |
"Text" => _("Name of new alias entry.") |
| 86 |
], |
| 87 |
'aliasNames' => [ |
| 88 |
"Headline" => _("Alias names"), 'attr' => 'cn', |
| 89 |
"Text" => _("Select one or more alias entries from the list to add the recipient.") |
| 90 |
], |
| 91 |
'hiddenOptions' => [ |
| 92 |
"Headline" => _("Hidden options"), |
| 93 |
"Text" => _("The selected options will not be managed inside LAM. You can use this to reduce the number of displayed input fields.") |
| 94 |
], |
| 95 |
]; |
| 96 |
// available PDF fields |
| 97 |
$return['PDF_fields'] = [ |
| 98 |
'alias' => _('Alias names'), |
| 99 |
]; |
| 100 |
return $return; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* {@inheritDoc} |
| 105 |
*/ |
| 106 |
protected function load_Messages(): void { |
| 107 |
$this->messages['alias'][0] = ['ERROR', _('Alias name is invalid.')]; |
| 108 |
$this->messages['alias'][1] = ['ERROR', _('Alias name already exists.')]; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* {@inheritDoc} |
| 113 |
*/ |
| 114 |
public function display_html_attributes(): htmlElement|array { |
| 115 |
$return = new htmlResponsiveRow(); |
| 116 |
if (!$this->isMailAliasTypeActive()) { |
| 117 |
$return->add(new htmlStatusMessage('ERROR', _('Please activate the mail aliases type for this server profile.'))); |
| 118 |
return $return; |
| 119 |
} |
| 120 |
$aliases = $this->getMailAliasList(); |
| 121 |
$count = count($aliases); |
| 122 |
$userName = $this->getUserName(); |
| 123 |
$mails = $this->getMailAddresses(); |
| 124 |
if (!$this->isBooleanConfigOptionSet('nisMailAliasUser_hideUserAliases') && ($userName != null)) { |
| 125 |
$return->add(new htmlSubTitle(_('Aliases for user name'))); |
| 126 |
for ($i = 0; $i < $count; $i++) { |
| 127 |
if (empty($aliases[$i]['rfc822mailmember'])) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
$dn = $aliases[$i]['dn']; |
| 131 |
$members = $aliases[$i]['rfc822mailmember']; |
| 132 |
if (in_array($userName, $members) |
| 133 |
&& (!isset($this->recipientsToDelete[$dn]) || !in_array($userName, $this->recipientsToDelete[$dn])) |
| 134 |
&& !in_array($dn, $this->aliasesToDelete)) { |
| 135 |
$return->addLabel(new htmlOutputText($aliases[$i]['cn'][0])); |
| 136 |
$buttonGroup = new htmlGroup(); |
| 137 |
$remButton = new htmlButton('rem_' . $i, 'del.svg', true); |
| 138 |
$remButton->setTitle(_('Remove user from alias entry.')); |
| 139 |
$buttonGroup->addElement($remButton); |
| 140 |
$delButton = new htmlButton('del_' . $i, 'delete.svg', true); |
| 141 |
$delButton->setTitle(sprintf(_('Delete whole alias entry which includes %s recipients.'), count($members))); |
| 142 |
$buttonGroup->addElement($delButton); |
| 143 |
$return->addField($buttonGroup); |
| 144 |
} |
| 145 |
} |
| 146 |
$return->addVerticalSpacer('1rem'); |
| 147 |
$addButton = new htmlAccountPageButton(static::class, 'add', 'user', _('Add')); |
| 148 |
$return->add($addButton, 12, 12, 12, 'text-center'); |
| 149 |
} |
| 150 |
if (!$this->isBooleanConfigOptionSet('nisMailAliasUser_hideMailAliases') && !empty($mails)) { |
| 151 |
$return->add(new htmlSubTitle(_('Aliases for email'))); |
| 152 |
for ($m = 0; $m < count($mails); $m++) { |
| 153 |
if (count($mails) > 1) { |
| 154 |
$label = new htmlOutputText($mails[$m]); |
| 155 |
$return->add($label); |
| 156 |
} |
| 157 |
for ($i = 0; $i < $count; $i++) { |
| 158 |
if (empty($aliases[$i]['rfc822mailmember'])) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
$dn = $aliases[$i]['dn']; |
| 162 |
$members = $aliases[$i]['rfc822mailmember']; |
| 163 |
if (in_array($mails[$m], $members) |
| 164 |
&& (!isset($this->recipientsToDelete[$dn]) || !in_array($mails[$m], $this->recipientsToDelete[$dn])) |
| 165 |
&& !in_array($dn, $this->aliasesToDelete)) { |
| 166 |
$return->addLabel(new htmlOutputText($aliases[$i]['cn'][0])); |
| 167 |
$buttonGroup = new htmlGroup(); |
| 168 |
$remButton = new htmlButton('remMail_' . $i . '_' . $m, 'del.svg', true); |
| 169 |
$remButton->setTitle(_('Remove user from alias entry.')); |
| 170 |
$buttonGroup->addElement($remButton); |
| 171 |
$delButton = new htmlButton('delMail_' . $i . '_' . $m, 'delete.svg', true); |
| 172 |
$delButton->setTitle(sprintf(_('Delete whole alias entry which includes %s recipients.'), count($members))); |
| 173 |
$buttonGroup->addElement($delButton); |
| 174 |
$return->addField($buttonGroup); |
| 175 |
} |
| 176 |
} |
| 177 |
$return->addVerticalSpacer('1rem'); |
| 178 |
$addButton = new htmlAccountPageButton(static::class, 'add', 'mail' . $m, _('Add')); |
| 179 |
$return->add($addButton, 12, 12, 12, 'text-center'); |
| 180 |
if ((count($mails) > 1) && ($m < (count($mails) - 1))) { |
| 181 |
$return->addVerticalSpacer('2rem'); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
return $return; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* {@inheritDoc} |
| 190 |
*/ |
| 191 |
public function process_attributes(): array { |
| 192 |
$errors = []; |
| 193 |
if (!$this->isMailAliasTypeActive()) { |
| 194 |
return $errors; |
| 195 |
} |
| 196 |
$mails = $this->getMailAddresses(); |
| 197 |
foreach (array_keys($_POST) as $key) { |
| 198 |
if (str_starts_with($key, 'rem_')) { |
| 199 |
$index = substr($key, strlen('rem_')); |
| 200 |
$dn = $this->cachedAliasList[$index]['dn']; |
| 201 |
$this->removeRecipient($this->getUserName(), $dn); |
| 202 |
} |
| 203 |
elseif (str_starts_with($key, 'del_')) { |
| 204 |
$index = substr($key, strlen('del_')); |
| 205 |
$dn = $this->cachedAliasList[$index]['dn']; |
| 206 |
$this->deleteAlias($dn); |
| 207 |
} |
| 208 |
elseif (str_starts_with($key, 'remMail_')) { |
| 209 |
$parts = substr($key, strlen('remMail_')); |
| 210 |
$parts = explode('_', $parts); |
| 211 |
$this->removeRecipient($mails[$parts[1]], $this->cachedAliasList[$parts[0]]['dn']); |
| 212 |
} |
| 213 |
elseif (str_starts_with($key, 'delMail_')) { |
| 214 |
$parts = substr($key, strlen('remMail_')); |
| 215 |
$parts = explode('_', $parts); |
| 216 |
$this->deleteAlias($this->cachedAliasList[$parts[0]]['dn']); |
| 217 |
} |
| 218 |
} |
| 219 |
return $errors; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Removes a recipient from the given DN. |
| 224 |
* |
| 225 |
* @param string $recipient recipient as user name or email |
| 226 |
* @param string $dn alias DN |
| 227 |
*/ |
| 228 |
private function removeRecipient($recipient, $dn): void { |
| 229 |
if (!isset($this->aliasesToAdd[$dn])) { |
| 230 |
// no new entry update existing |
| 231 |
if (isset($this->recipientsToAdd[$dn]) && in_array($recipient, $this->recipientsToAdd[$dn])) { |
| 232 |
// undo adding of recipient |
| 233 |
if (count($this->recipientsToAdd[$dn]) === 1) { |
| 234 |
// only one entry as marked for adding, remove whole entry |
| 235 |
unset($this->recipientsToAdd[$dn]); |
| 236 |
} |
| 237 |
else { |
| 238 |
$this->recipientsToAdd[$dn] = array_values(array_delete([$recipient], $this->recipientsToAdd[$dn])); |
| 239 |
} |
| 240 |
} |
| 241 |
else { |
| 242 |
// mark for removal |
| 243 |
$this->recipientsToDelete[$dn][] = $recipient; |
| 244 |
} |
| 245 |
foreach ($this->cachedAliasList as $index => $attrs) { |
| 246 |
if ($dn == $attrs['dn']) { |
| 247 |
$this->cachedAliasList[$index]['rfc822mailmember'] = array_values(array_delete([$recipient], $this->cachedAliasList[$index]['rfc822mailmember'])); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
elseif (count($this->aliasesToAdd[$dn]['rfc822mailmember']) === 1) { |
| 252 |
// new entry |
| 253 |
// single recipient in new entry, do not create new entry at all |
| 254 |
unset($this->aliasesToAdd[$dn]); |
| 255 |
foreach ($this->cachedAliasList as $index => $attrs) { |
| 256 |
if ($dn == $attrs['dn']) { |
| 257 |
unset($this->cachedAliasList[$index]); |
| 258 |
$this->cachedAliasList = array_values($this->cachedAliasList); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
else { |
| 263 |
$this->aliasesToAdd[$dn]['rfc822mailmember'] = array_values(array_delete([$recipient], $this->aliasesToAdd[$dn]['rfc822mailmember'])); |
| 264 |
foreach ($this->cachedAliasList as &$attrs) { |
| 265 |
if ($dn == $attrs['dn']) { |
| 266 |
$attrs['rfc822mailmember'] = array_values(array_delete([$recipient], $attrs['rfc822mailmember'])); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Removes an alias with the given DN. |
| 274 |
* |
| 275 |
* @param string $dn alias DN |
| 276 |
*/ |
| 277 |
private function deleteAlias($dn): void { |
| 278 |
if (!isset($this->aliasesToAdd[$dn])) { |
| 279 |
// no new entry, delete existing entry |
| 280 |
$this->aliasesToDelete[] = $dn; |
| 281 |
} |
| 282 |
else { |
| 283 |
unset($this->aliasesToAdd[$dn]); |
| 284 |
foreach ($this->cachedAliasList as $index => $attrs) { |
| 285 |
if ($dn == $attrs['dn']) { |
| 286 |
unset($this->cachedAliasList[$index]); |
| 287 |
$this->cachedAliasList = array_values($this->cachedAliasList); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
if (isset($this->recipientsToAdd[$dn])) { |
| 292 |
unset($this->recipientsToAdd[$dn]); |
| 293 |
} |
| 294 |
if (isset($this->recipientsToDelete[$dn])) { |
| 295 |
unset($this->recipientsToDelete[$dn]); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Returns the HTML meta data for the add page. |
| 301 |
* |
| 302 |
* @return htmlElement HTML meta data |
| 303 |
*/ |
| 304 |
public function display_html_add(): htmlElement { |
| 305 |
$return = new htmlResponsiveRow(); |
| 306 |
$aliases = $this->getMailAliasList(); |
| 307 |
$userName = $this->getUserName(); |
| 308 |
$mails = $this->getMailAddresses(); |
| 309 |
$recipient = null; |
| 310 |
// get recipient value to add |
| 311 |
if (isset($_POST['recipient'])) { |
| 312 |
$recipient = $_POST['recipient']; |
| 313 |
} |
| 314 |
elseif (isset($_POST['form_subpage_' . static::class . '_add_user'])) { |
| 315 |
$recipient = $userName; |
| 316 |
} |
| 317 |
else { |
| 318 |
for ($m = 0; $m < count($mails); $m++) { |
| 319 |
if (isset($_POST['form_subpage_' . static::class . '_add_mail' . $m])) { |
| 320 |
$recipient = $mails[$m]; |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
$return->addLabel(new htmlOutputText(_('Recipient'))); |
| 326 |
$return->addField(new htmlOutputText($recipient)); |
| 327 |
$return->add(new htmlHiddenInput('recipient', $recipient)); |
| 328 |
// new mail alias |
| 329 |
$return->add(new htmlSubTitle(_('Create new alias'))); |
| 330 |
$typeManager = new TypeManager(); |
| 331 |
$mailAliasTypes = $typeManager->getConfiguredTypesForScope('mailAlias'); |
| 332 |
$ous = []; |
| 333 |
foreach ($mailAliasTypes as $type) { |
| 334 |
$ous = array_merge($ous, $type->getSuffixList()); |
| 335 |
} |
| 336 |
$ous = array_unique($ous); |
| 337 |
usort($ous, compareDN(...)); |
| 338 |
$suffixSelect = new htmlResponsiveSelect('new_ou', $ous, [], _('Suffix'), 'suffix'); |
| 339 |
$suffixSelect->setRightToLeftTextDirection(true); |
| 340 |
$suffixSelect->setSortElements(false); |
| 341 |
$return->add($suffixSelect); |
| 342 |
$newAliasCn = empty($_POST['new_cn']) ? '' : $_POST['new_cn']; |
| 343 |
$return->add(new htmlResponsiveInputField(_('Alias name'), 'new_cn', $newAliasCn, 'newAlias')); |
| 344 |
$return->addVerticalSpacer('1rem'); |
| 345 |
$addButton = new htmlAccountPageButton(static::class, 'attributes', 'create', _('Create')); |
| 346 |
$return->add($addButton, 12, 12, 12, 'text-center'); |
| 347 |
|
| 348 |
$return->addVerticalSpacer('2rem'); |
| 349 |
|
| 350 |
// add to existing alias |
| 351 |
$return->add(new htmlSubTitle(_('Add to existing alias'))); |
| 352 |
$aliasesToAdd = []; |
| 353 |
foreach ($aliases as $index => $attrs) { |
| 354 |
if (!empty($attrs['rfc822mailmember']) && in_array($recipient, $attrs['rfc822mailmember'])) { |
| 355 |
continue; |
| 356 |
} |
| 357 |
$aliasesToAdd[$attrs['cn'][0]] = $index; |
| 358 |
} |
| 359 |
$aliasSelect = new htmlResponsiveSelect('ex_cn', $aliasesToAdd, [], _('Alias names'), 'aliasNames', 20); |
| 360 |
$aliasSelect->setHasDescriptiveElements(true); |
| 361 |
$aliasSelect->setMultiSelect(true); |
| 362 |
$return->add($aliasSelect); |
| 363 |
$return->addVerticalSpacer('1rem'); |
| 364 |
$addButton = new htmlAccountPageButton(static::class, 'attributes', 'recipient', _('Add')); |
| 365 |
$return->add($addButton, 12, 12, 12, 'text-center'); |
| 366 |
|
| 367 |
$return->addVerticalSpacer('2rem'); |
| 368 |
$cancelButton = new htmlAccountPageButton(static::class, 'attributes', 'back', _('Cancel')); |
| 369 |
$return->add($cancelButton); |
| 370 |
return $return; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Processes user input of the primary module page. |
| 375 |
* It checks if all input values are correct and updates the associated LDAP attributes. |
| 376 |
* |
| 377 |
* @return array<string|string[]>[] list of info/error messages |
| 378 |
*/ |
| 379 |
public function process_add(): array { |
| 380 |
$errors = []; |
| 381 |
// create new alias entry |
| 382 |
if (isset($_POST['form_subpage_' . static::class . '_attributes_create'])) { |
| 383 |
if (empty($_POST['new_cn']) || !get_preg($_POST['new_cn'], 'nis_alias')) { |
| 384 |
$errors[] = $this->messages['alias'][0]; |
| 385 |
} |
| 386 |
else { |
| 387 |
// build new alias entry |
| 388 |
$newDN = 'cn=' . $_POST['new_cn'] . ',' . $_POST['new_ou']; |
| 389 |
$found = false; |
| 390 |
foreach ($this->cachedAliasList as $attrs) { |
| 391 |
if ($attrs['dn'] == $newDN) { |
| 392 |
$found = true; |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
if ($found) { |
| 397 |
$errors[] = $this->messages['alias'][1]; |
| 398 |
} |
| 399 |
else { |
| 400 |
$newAttrs = [ |
| 401 |
'dn' => $newDN, |
| 402 |
'cn' => [$_POST['new_cn']], |
| 403 |
'objectclass' => ['nisMailAlias'], |
| 404 |
'rfc822mailmember' => [$_POST['recipient']] |
| 405 |
]; |
| 406 |
$this->aliasesToAdd[$newDN] = $newAttrs; |
| 407 |
$this->cachedAliasList[] = $newAttrs; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
// add recipient to existing entries |
| 412 |
if (isset($_POST['form_subpage_' . static::class . '_attributes_recipient'])) { |
| 413 |
$selectedAliases = $_POST['ex_cn']; |
| 414 |
foreach ($selectedAliases as $index) { |
| 415 |
$dn = $this->cachedAliasList[$index]['dn']; |
| 416 |
$this->cachedAliasList[$index]['rfc822mailmember'][] = $_POST['recipient']; |
| 417 |
if (isset($this->aliasesToAdd[$dn])) { |
| 418 |
$this->aliasesToAdd[$dn]['rfc822mailmember'][] = $_POST['recipient']; |
| 419 |
} |
| 420 |
else { |
| 421 |
$this->recipientsToAdd[$dn][] = $_POST['recipient']; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return $errors; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* {@inheritDoc} |
| 431 |
*/ |
| 432 |
public function postModifyActions(bool $newAccount, array $attributes): array { |
| 433 |
$errors = []; |
| 434 |
// delete complete aliases |
| 435 |
foreach ($this->aliasesToDelete as $dn) { |
| 436 |
$success = @ldap_delete($_SESSION['ldap']->server(), $dn); |
| 437 |
if (!$success) { |
| 438 |
logNewMessage(LOG_ERR, 'Unable to delete ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 439 |
$errors[] = ['ERROR', sprintf(_('Was unable to delete DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 440 |
} |
| 441 |
else { |
| 442 |
logNewMessage(LOG_NOTICE, 'Removed ' . $dn); |
| 443 |
} |
| 444 |
} |
| 445 |
// delete recipient entries |
| 446 |
foreach ($this->recipientsToDelete as $dn => $recipients) { |
| 447 |
$success = @ldap_mod_del($_SESSION['ldap']->server(), $dn, ['rfc822mailmember' => $recipients]); |
| 448 |
if (!$success) { |
| 449 |
logNewMessage(LOG_ERR, 'Unable to remove recipients ' . implode(', ', $recipients) . ' from ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 450 |
$errors[] = ['ERROR', sprintf(_('Was unable to remove attributes from DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 451 |
} |
| 452 |
else { |
| 453 |
logNewMessage(LOG_NOTICE, 'Removed recipients ' . implode(', ', $recipients) . ' from ' . $dn); |
| 454 |
} |
| 455 |
} |
| 456 |
// create new aliases |
| 457 |
foreach ($this->aliasesToAdd as $dn => $attrs) { |
| 458 |
unset($attrs['dn']); |
| 459 |
$success = @ldap_add($_SESSION['ldap']->server(), $dn, $attrs); |
| 460 |
if (!$success) { |
| 461 |
logNewMessage(LOG_ERR, 'Unable to create mail alias ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 462 |
$errors[] = ['ERROR', sprintf(_('Was unable to create DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 463 |
} |
| 464 |
else { |
| 465 |
logNewMessage(LOG_NOTICE, 'Added mail alias with recipients ' . implode(', ', $attrs['rfc822mailmember']) . ' and DN ' . $dn); |
| 466 |
} |
| 467 |
} |
| 468 |
// add recipients |
| 469 |
foreach ($this->recipientsToAdd as $dn => $recipients) { |
| 470 |
$success = @ldap_mod_add($_SESSION['ldap']->server(), $dn, ['rfc822mailmember' => $recipients]); |
| 471 |
if (!$success) { |
| 472 |
logNewMessage(LOG_ERR, 'Unable to add recipients ' . implode(', ', $recipients) . ' to ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 473 |
$errors[] = ['ERROR', sprintf(_('Was unable to add attributes to DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 474 |
} |
| 475 |
else { |
| 476 |
logNewMessage(LOG_NOTICE, 'Added recipients ' . implode(', ', $recipients) . ' to ' . $dn); |
| 477 |
} |
| 478 |
} |
| 479 |
return $errors; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* {@inheritDoc} |
| 484 |
*/ |
| 485 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 486 |
$return = []; |
| 487 |
$aliases = $this->getMailAliasList(); |
| 488 |
$foundAliases = []; |
| 489 |
$mails = $this->getMailAddresses(); |
| 490 |
$user = $this->getUserName(); |
| 491 |
foreach ($aliases as $alias) { |
| 492 |
if (empty($alias['rfc822mailmember'][0])) { |
| 493 |
continue; |
| 494 |
} |
| 495 |
if (!empty($user) && in_array($user, $alias['rfc822mailmember'])) { |
| 496 |
$foundAliases[] = $alias['cn'][0]; |
| 497 |
} |
| 498 |
if (!empty($mails)) { |
| 499 |
foreach ($mails as $mail) { |
| 500 |
if (in_array($mail, $alias['rfc822mailmember'])) { |
| 501 |
$foundAliases[] = $alias['cn'][0]; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
$foundAliases = array_unique($foundAliases); |
| 507 |
$this->addPDFKeyValue($return, 'alias', _('Alias names'), implode(', ', $foundAliases)); |
| 508 |
return $return; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* {@inheritDoc} |
| 513 |
*/ |
| 514 |
public function get_configOptions(array $scopes, array $allScopes): htmlElement|array { |
| 515 |
$configContainer = new htmlResponsiveRow(); |
| 516 |
$hiddenGroup = new htmlGroup(); |
| 517 |
$hiddenGroup->addElement(new htmlOutputText(_('Hidden options'))); |
| 518 |
$hiddenGroup->addElement(new htmlHelpLink('hiddenOptions')); |
| 519 |
$configContainer->addLabel($hiddenGroup); |
| 520 |
$configContainer->addField(new htmlOutputText('')); |
| 521 |
$configContainer->addVerticalSpacer('0.5rem'); |
| 522 |
$configContainer->add(new htmlResponsiveInputCheckbox('nisMailAliasUser_hideUserAliases', false, _('Aliases for user name'))); |
| 523 |
$configContainer->add(new htmlResponsiveInputCheckbox('nisMailAliasUser_hideMailAliases', false, _('Aliases for email'))); |
| 524 |
return $configContainer; |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* Returns a list of existing email aliases. |
| 529 |
* |
| 530 |
* @return array<string, string[]|string>[] email aliases |
| 531 |
*/ |
| 532 |
private function getMailAliasList(): array { |
| 533 |
if ($this->cachedAliasList !== null) { |
| 534 |
return $this->cachedAliasList; |
| 535 |
} |
| 536 |
$this->cachedAliasList = searchLDAPByAttribute('cn', '*', 'nisMailAlias', ['dn', 'cn', 'rfc822MailMember'], ['mailAlias']); |
| 537 |
return $this->cachedAliasList; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Returns the username of this account. |
| 542 |
* |
| 543 |
* @return string|null user name |
| 544 |
*/ |
| 545 |
private function getUserName() { |
| 546 |
if ($this->getAccountContainer()->getAccountModule('posixAccount') != null) { |
| 547 |
$attrs = $this->getAccountContainer()->getAccountModule('posixAccount')->getAttributes(); |
| 548 |
if (!empty($attrs['uid'][0])) { |
| 549 |
return $attrs['uid'][0]; |
| 550 |
} |
| 551 |
} |
| 552 |
elseif ($this->getAccountContainer()->getAccountModule('inetOrgPerson') != null) { |
| 553 |
$attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); |
| 554 |
if (!empty($attrs['uid'][0])) { |
| 555 |
return $attrs['uid'][0]; |
| 556 |
} |
| 557 |
} |
| 558 |
return null; |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Returns the email addresses of this account. |
| 563 |
* |
| 564 |
* @return string[]|null mail addresses |
| 565 |
*/ |
| 566 |
private function getMailAddresses() { |
| 567 |
if ($this->getAccountContainer()->getAccountModule('inetOrgPerson') != null) { |
| 568 |
$attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); |
| 569 |
if (!empty($attrs['mail'])) { |
| 570 |
return $attrs['mail']; |
| 571 |
} |
| 572 |
} |
| 573 |
return null; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Returns if the mail alias type is active. Otherwise, aliases cannot be managed. |
| 578 |
* |
| 579 |
* @return boolean is active |
| 580 |
*/ |
| 581 |
private function isMailAliasTypeActive() { |
| 582 |
$typeManager = new TypeManager(); |
| 583 |
$activeTypes = $typeManager->getConfiguredTypesForScope('mailAlias'); |
| 584 |
return !empty($activeTypes); |
| 585 |
} |
| 586 |
|
| 587 |
} |