| 1 |
<?php |
| 2 |
/* |
| 3 |
* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 4 |
* Copyright (C) 2017 Lars Althof |
| 5 |
* 2017 - 2026 Roland Gruber |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* Enable the account for Courier Mail Service Aliases |
| 24 |
* |
| 25 |
* @package modules |
| 26 |
* |
| 27 |
* @author Lars Althof |
| 28 |
* @author Roland Gruber |
| 29 |
*/ |
| 30 |
|
| 31 |
use LAM\TYPES\ConfiguredType; |
| 32 |
|
| 33 |
/** |
| 34 |
* Enable the account for Courier Mail Service Aliases |
| 35 |
* |
| 36 |
* @package modules |
| 37 |
* |
| 38 |
* @author Lars Althof |
| 39 |
* @author Roland Gruber |
| 40 |
*/ |
| 41 |
class courierMailAlias extends baseModule { |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritDoc} |
| 45 |
*/ |
| 46 |
public function can_manage(): bool { |
| 47 |
return $this->get_scope() === 'mailAlias'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* {@inheritdoc} |
| 52 |
*/ |
| 53 |
public function get_metaData(): array { |
| 54 |
$return = []; |
| 55 |
// icon |
| 56 |
$return['icon'] = 'mail.svg'; |
| 57 |
// alias name |
| 58 |
$return["alias"] = _("Courier"); |
| 59 |
// LDAP filter |
| 60 |
$return["ldap_filter"] = [ |
| 61 |
'or' => "(objectClass=courierMailAlias)" |
| 62 |
]; |
| 63 |
// RDN attribute |
| 64 |
$return["RDN"] = [ |
| 65 |
"mail" => "low" |
| 66 |
]; |
| 67 |
// module dependencies |
| 68 |
$return['dependencies'] = [ |
| 69 |
'depends' => ['nisMailAlias'], |
| 70 |
'conflicts' => [] |
| 71 |
]; |
| 72 |
// managed object classes |
| 73 |
$return['objectClasses'] = ['CourierMailAlias']; |
| 74 |
// managed attributes |
| 75 |
$return['attributes'] = ['mail', 'maildrop', 'mailsource', 'description']; |
| 76 |
// help Entries |
| 77 |
$return['help'] = [ |
| 78 |
'mail' => [ |
| 79 |
"Headline" => _("Email address"), |
| 80 |
"attr" => 'mail', |
| 81 |
"Text" => _("This is the email address of the alias.") |
| 82 |
], |
| 83 |
'maildrop' => [ |
| 84 |
"Headline" => _("Recipient address"), |
| 85 |
"attr" => 'maildrop', |
| 86 |
"Text" => _("This is the recipient address of the mail alias. There can be more than one.") |
| 87 |
], |
| 88 |
'mailsource' => [ |
| 89 |
"Headline" => _("Mail source"), |
| 90 |
"attr" => 'mailsource', |
| 91 |
"Text" => _("The mail source can be 'local' or 'estmp'.") |
| 92 |
], |
| 93 |
'description' => [ |
| 94 |
"Headline" => _("Description"), |
| 95 |
"attr" => 'description', |
| 96 |
"Text" => _('This is an optional description for this entry.') |
| 97 |
] |
| 98 |
]; |
| 99 |
$return['PDF_fields'] = [ |
| 100 |
'mail' => _('Email address'), |
| 101 |
'maildrop' => _('Recipient address'), |
| 102 |
'mailsource' => _('Mail source'), |
| 103 |
'description' => _('Description') |
| 104 |
]; |
| 105 |
$return['upload_columns'] = [ |
| 106 |
[ |
| 107 |
'name' => 'courierMailAlias_mail', |
| 108 |
'description' => _('Email address'), |
| 109 |
'help' => 'mail', |
| 110 |
'example' => _('[email protected]'), |
| 111 |
'required' => true |
| 112 |
], |
| 113 |
[ |
| 114 |
'name' => 'courierMailAlias_maildrop', |
| 115 |
'description' => _('Recipient address'), |
| 116 |
'help' => 'maildrop', |
| 117 |
'example' => _('[email protected],[email protected]'), |
| 118 |
'required' => true |
| 119 |
], |
| 120 |
[ |
| 121 |
'name' => 'courierMailAlias_mailsource', |
| 122 |
'description' => _('Mail source'), |
| 123 |
'help' => 'mailsource', |
| 124 |
'values' => 'esmtp, local' |
| 125 |
], |
| 126 |
[ |
| 127 |
'name' => 'courierMailAlias_description', |
| 128 |
'description' => _('Description'), |
| 129 |
'help' => 'description', |
| 130 |
], |
| 131 |
]; |
| 132 |
return $return; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* {@inheritDoc} |
| 137 |
*/ |
| 138 |
protected function load_Messages(): void { |
| 139 |
$this->messages['maildrop'][0] = ['ERROR', _('Recipient address'), _('Please enter a valid email address!')]; |
| 140 |
$this->messages['maildrop'][1] = ['ERROR', _('Account %s:') . ' courierMailAlias_maildrop', _('Please enter a valid email address!')]; |
| 141 |
$this->messages['mailsource'][0] = ['ERROR', _('Account %s:') . ' courierMailAlias_maildrop', _('Please enter a valid mail source.')]; |
| 142 |
$this->messages['mail'][0] = ['ERROR', _('Email address'), _('Please enter a valid email address!')]; |
| 143 |
$this->messages['mail'][1] = ['ERROR', _('Account %s:') . ' courierMailAlias_mail', _('Please enter a valid email address!')]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* {@inheritDoc} |
| 148 |
*/ |
| 149 |
public function display_html_attributes(): htmlElement|array { |
| 150 |
$return = new htmlResponsiveRow(); |
| 151 |
$this->addSimpleInputTextField($return, 'mail', _('Email address'), true); |
| 152 |
$this->addMultiValueInputTextField($return, 'maildrop', _('Recipient address'), true); |
| 153 |
$return->addVerticalSpacer('0.5rem'); |
| 154 |
$selectedSource = (empty($this->attributes['mailsource'][0])) ? "-" : $this->attributes['mailsource'][0]; |
| 155 |
$return->add(new htmlResponsiveSelect('mailsource', ['-', 'esmtp', 'local'], [$selectedSource], _('Mail source'), 'mailsource')); |
| 156 |
$description = (empty($this->attributes['description'][0])) ? '' : $this->attributes['description'][0]; |
| 157 |
$boxInput = new htmlResponsiveInputField(_('Description'), 'description', $description, 'description'); |
| 158 |
$boxInput->setFieldMaxLength(100); |
| 159 |
$return->add($boxInput); |
| 160 |
return $return; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* {@inheritDoc} |
| 165 |
*/ |
| 166 |
public function process_attributes(): array { |
| 167 |
$errors = []; |
| 168 |
$this->attributes['mail'][0] = $_POST['mail']; |
| 169 |
if (empty($this->attributes['mail'][0]) || !get_preg($this->attributes['mail'][0], 'email')) { |
| 170 |
$errors[] = $this->messages['mail'][0]; |
| 171 |
} |
| 172 |
$this->processMultiValueInputTextField('maildrop', $errors, 'mailLocalAddress'); |
| 173 |
if (empty($this->attributes['maildrop'])) { |
| 174 |
$errors[] = $this->messages['maildrop'][0]; |
| 175 |
} |
| 176 |
$this->attributes['mailsource'] = []; |
| 177 |
if ($_POST['mailsource'] !== "-") { |
| 178 |
$this->attributes['mailsource'][0] = $_POST['mailsource']; |
| 179 |
} |
| 180 |
$this->attributes['description'][0] = $_POST['description']; |
| 181 |
return $errors; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* {@inheritDoc} |
| 186 |
*/ |
| 187 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 188 |
$return = []; |
| 189 |
$this->addSimplePDFField($return, 'mail', _('Email address')); |
| 190 |
$this->addSimplePDFField($return, 'maildrop', _('Recipient address')); |
| 191 |
$this->addSimplePDFField($return, 'mailsource', _('Mail source')); |
| 192 |
$this->addSimplePDFField($return, 'description', _('Description')); |
| 193 |
return $return; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* {@inheritDoc} |
| 198 |
*/ |
| 199 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 200 |
$messages = []; |
| 201 |
$possibleSources = ['esmtp', 'local']; |
| 202 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 203 |
if (!in_array("courierMailAlias", $partialAccounts[$i]['objectClass'])) { |
| 204 |
$partialAccounts[$i]['objectClass'][] = "courierMailAlias"; |
| 205 |
} |
| 206 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'courierMailAlias_mail', 'mail', 'email', $this->messages['mail'][1], $messages); |
| 207 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'courierMailAlias_maildrop', 'maildrop', 'mailLocalAddress', $this->messages['maildrop'][1], $messages, '/,[ ]?/'); |
| 208 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'courierMailAlias_description', 'description'); |
| 209 |
if (!empty($rawAccounts[$i][$ids['courierMailAlias_mailsource']])) { |
| 210 |
if (in_array($rawAccounts[$i][$ids['courierMailAlias_mailsource']], $possibleSources)) { |
| 211 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'courierMailAlias_mailsource', 'mailsource'); |
| 212 |
} |
| 213 |
else { |
| 214 |
$error = $this->messages['mailsource'][0]; |
| 215 |
$error[] = [$i]; |
| 216 |
$messages[] = $error; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return $messages; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @inheritDoc |
| 225 |
*/ |
| 226 |
public function getListAttributeDescriptions(ConfiguredType $type): array { |
| 227 |
return [ |
| 228 |
'mail' => _('Email address'), |
| 229 |
'maildrop' => _('Recipient address'), |
| 230 |
'mailsource' => _('Mail source'), |
| 231 |
'description' => _('Description') |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @inheritDoc |
| 237 |
*/ |
| 238 |
public function getListRenderFunction(string $attributeName, ConfiguredType $type): ?callable { |
| 239 |
if ($attributeName === 'mail') { |
| 240 |
return function(array $entry, string $attribute): htmlElement { |
| 241 |
$group = new htmlGroup(); |
| 242 |
if (isset($entry[$attribute][0]) && ($entry[$attribute][0] != '')) { |
| 243 |
for ($i = 0; $i < count($entry[$attribute]); $i++) { |
| 244 |
if ($i > 0) { |
| 245 |
$group->addElement(new htmlOutputText(", ")); |
| 246 |
} |
| 247 |
$group->addElement(new htmlLink($entry[$attribute][$i], "mailto:" . $entry[$attribute][$i])); |
| 248 |
} |
| 249 |
} |
| 250 |
return $group; |
| 251 |
}; |
| 252 |
} |
| 253 |
return null; |
| 254 |
} |
| 255 |
|
| 256 |
} |