Debian Codegraph @

lib/modules/inetLocalMailRecipient.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2004 - 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 * Provides mail routing for user accounts.
24 *
25 * @package modules
26 * @author Roland Gruber
27 */
28
29 use LAM\TYPES\ConfiguredType;
30
31 /**
32 * Provides mail routing for users.
33 *
34 * @package modules
35 */
36 class inetLocalMailRecipient extends baseModule {
37
38 /**
39 * These attributes will be ignored by default if a new account is copied from an existing one.
40 */
41 private const ATTRIBUTES_TO_IGNORE_ON_COPY = ['mailLocalAddress', 'mailRoutingAddress'];
42
43 /**
44 * Creates a new mitKerberos object.
45 *
46 * @param string $scope account type (user, group, host)
47 */
48 public function __construct($scope) {
49 // call parent constructor
50 parent::__construct($scope);
51 $this->autoAddObjectClasses = false;
52 }
53
54 /**
55 * {@inheritDoc}
56 */
57 public function can_manage(): bool {
58 return in_array($this->get_scope(), ['user', 'group']);
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 public function get_metaData(): array {
65 $return = [];
66 // icon
67 $return['icon'] = 'mail.svg';
68 // alias name
69 $return["alias"] = _("Mail routing");
70 // module dependencies
71 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
72 // managed object classes
73 $return['objectClasses'] = ['inetLocalMailRecipient'];
74 // managed attributes
75 $return['attributes'] = ['mailLocalAddress', 'mailHost', 'mailRoutingAddress'];
76 // help Entries
77 $return['help'] = [
78 'mailRoutingAddress' => [
79 "Headline" => _("Routing address"), 'attr' => 'mailRoutingAddress',
80 "Text" => _("This is the target email address for the user's mails.")
81 ],
82 'mailLocalAddress' => [
83 "Headline" => _("Local address"), 'attr' => 'mailLocalAddress',
84 "Text" => _("This is one of the users public email addresses.")
85 ],
86 'mailLocalAddressList' => [
87 "Headline" => _("Local address list"), 'attr' => 'mailLocalAddress',
88 "Text" => _("This is a comma separated list of the users public email addresses.")
89 ],
90 'mailHost' => [
91 "Headline" => _("Mail server"), 'attr' => 'mailHost',
92 "Text" => _("This is the mail server for the user.")
93 ]];
94 // profile options
95 $profileContainer = new htmlResponsiveRow();
96 $profileContainer->add(new htmlResponsiveInputCheckbox('inetLocalMailRecipient_addExt', false, _('Automatically add this extension'), 'autoAdd'));
97 $profileContainer->add(new htmlResponsiveInputField(_('Routing address'), 'inetLocalMailRecipient_mailRoutingAddress', null, 'mailRoutingAddress'));
98 $profileContainer->add(new htmlResponsiveInputField(_('Local address'), 'inetLocalMailRecipient_mailLocalAddress', null, 'mailLocalAddress'));
99 $profileContainer->add(new htmlResponsiveInputField(_('Mail server'), 'inetLocalMailRecipient_host', null, 'mailHost'));
100 $return['profile_options'] = $profileContainer;
101 // profile checks
102 $return['profile_checks']['inetLocalMailRecipient_host'] = [
103 'type' => 'ext_preg',
104 'regex' => 'DNSname',
105 'error_message' => $this->messages['mailHost'][0]];
106 // profile mappings
107 $return['profile_mappings'] = [
108 'inetLocalMailRecipient_host' => 'mailHost',
109 'inetLocalMailRecipient_mailRoutingAddress' => 'mailRoutingAddress',
110 'inetLocalMailRecipient_mailLocalAddress' => 'mailLocalAddress',
111 ];
112 // upload fields
113 $return['upload_columns'] = [
114 [
115 'name' => 'inetLocalMailRecipient_routingAdr',
116 'description' => _('Routing address'),
117 'help' => 'mailRoutingAddress',
118 'example' => _('[email protected]')
119 ],
120 [
121 'name' => 'inetLocalMailRecipient_mailLocalAddress',
122 'description' => _('Local address list'),
123 'help' => 'mailLocalAddressList',
124 'example' => _('[email protected]')
125 ],
126 [
127 'name' => 'inetLocalMailRecipient_server',
128 'description' => _('Mail server'),
129 'help' => 'mailHost',
130 'example' => _('mail.yourdomain.org')
131 ]
132 ];
133 // available PDF fields
134 $return['PDF_fields'] = [
135 'routingAdr' => _('Routing address'),
136 'mailLocalAddress' => _('Local address list'),
137 'host' => _('Mail server')
138 ];
139 // self-service
140 $return['selfServiceFieldSettings'] = [
141 'mailLocalAddress' => _('Local address (read-only)'),
142 'mailRoutingAddress' => _('Routing address (read-only)'),
143 ];
144 return $return;
145 }
146
147 /**
148 * {@inheritDoc}
149 */
150 protected function load_Messages(): void {
151 $this->messages['mailRoutingAddress'][0] = ['ERROR', 'Routing address is invalid!']; // third array value is set dynamically
152 $this->messages['mailRoutingAddress'][1] = ['ERROR', _('Account %s:') . ' inetLocalMailRecipient_routingAdr', 'Routing address is invalid!'];
153 $this->messages['mailLocalAddress'][0] = ['ERROR', 'Local address is invalid!']; // third array value is set dynamically
154 $this->messages['mailLocalAddress'][1] = ['ERROR', _('Account %s:') . ' inetLocalMailRecipient_mailLocalAddress', 'Local address is invalid!'];
155 $this->messages['mailHost'][0] = ['ERROR', 'Mail server is invalid!']; // third array value is set dynamically
156 $this->messages['mailHost'][1] = ['ERROR', _('Account %s:') . ' inetLocalMailRecipient_server', 'Mail server is invalid!'];
157 }
158
159 /**
160 * {@inheritDoc}
161 */
162 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
163 parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY));
164 }
165
166 /**
167 * {@inheritDoc}
168 */
169 public function load_profile(array $profile): void {
170 // profile mappings in meta data
171 parent::load_profile($profile);
172 // add extension
173 if (isset($profile['inetLocalMailRecipient_addExt'][0])
174 && ($profile['inetLocalMailRecipient_addExt'][0] == "true")
175 && !in_array('inetLocalMailRecipient', $this->attributes['objectClass'])) {
176 $this->attributes['objectClass'][] = 'inetLocalMailRecipient';
177 }
178 }
179
180 /**
181 * {@inheritdoc}
182 */
183 public function getWildcardTargetAttributeNames(): array {
184 return ['mailLocalAddress', 'mailRoutingAddress'];
185 }
186
187 /**
188 * {@inheritDoc}
189 */
190 public function display_html_attributes(): htmlElement|array {
191 $return = new htmlResponsiveRow();
192 if (in_array('inetLocalMailRecipient', $this->attributes['objectClass'])) {
193 $this->getAccountContainer()->replaceWildcardsInArray($this->getWildcardTargetAttributeNames(), $this->attributes);
194 // mail routing address
195 $this->addSimpleInputTextField($return, 'mailRoutingAddress', _('Routing address'));
196 // mail server
197 $this->addSimpleInputTextField($return, 'mailHost', _('Mail server'));
198 // list current local addresses
199 $this->addMultiValueInputTextField($return, 'mailLocalAddress', _('Local address'));
200
201 $return->addVerticalSpacer('2rem');
202 $deleteButton = new htmlButton('remObjectClass', _('Remove mail routing extension'));
203 $deleteButton->setCSSClasses(['lam-danger']);
204 $return->add($deleteButton, 12, 12, 12, 'text-center');
205 }
206 else {
207 $return->add(new htmlButton('addObjectClass', _('Add mail routing extension')));
208 }
209 return $return;
210 }
211
212 /**
213 * {@inheritDoc}
214 */
215 public function process_attributes(): array {
216 if (isset($_POST['addObjectClass'])) {
217 $this->attributes['objectClass'][] = 'inetLocalMailRecipient';
218 return [];
219 }
220 elseif (isset($_POST['remObjectClass'])) {
221 $this->attributes['objectClass'] = array_delete(['inetLocalMailRecipient'], $this->attributes['objectClass']);
222 for ($i = 0; $i < count($this->meta['attributes']); $i++) {
223 if (isset($this->attributes[$this->meta['attributes'][$i]])) {
224 unset($this->attributes[$this->meta['attributes'][$i]]);
225 }
226 }
227 return [];
228 }
229 if (!in_array('inetLocalMailRecipient', $this->attributes['objectClass'])) {
230 return [];
231 }
232 $errors = [];
233 $this->getAccountContainer()->replaceWildcardsInPOST($this->getWildcardTargetAttributeNames());
234 $this->attributes['mailRoutingAddress'] = [];
235 $this->attributes['mailLocalAddress'] = [];
236 $this->attributes['mailHost'] = [];
237 // check routing address
238 if (isset($_POST['mailRoutingAddress']) && ($_POST['mailRoutingAddress'] != "")) {
239 // check if address has correct format
240 if (get_preg($_POST['mailRoutingAddress'], 'email')) {
241 $this->attributes['mailRoutingAddress'][0] = $_POST['mailRoutingAddress'];
242 }
243 else {
244 $message = $this->messages['mailRoutingAddress'][0];
245 $message[] = $_POST['mailRoutingAddress'];
246 $errors[] = $message;
247 }
248 }
249 // check mail server
250 if (isset($_POST['mailHost']) && ($_POST['mailHost'] != "")) {
251 // check if address has correct format
252 if (get_preg($_POST['mailHost'], 'DNSname')) {
253 $this->attributes['mailHost'][0] = $_POST['mailHost'];
254 }
255 else {
256 $message = $this->messages['mailHost'][0];
257 $message[] = $_POST['mailHost'];
258 $errors[] = $message;
259 }
260 }
261 $this->processMultiValueInputTextField('mailLocalAddress', $errors, 'mailLocalAddress');
262 return $errors;
263 }
264
265 /**
266 * {@inheritDoc}
267 */
268 public function save_attributes(): array {
269 if (!in_array('inetLocalMailRecipient', $this->attributes['objectClass']) && !in_array('inetLocalMailRecipient', $this->orig['objectClass'])) {
270 // skip saving if the extension was not added/modified
271 return [];
272 }
273 return parent::save_attributes();
274 }
275
276 /**
277 * {@inheritDoc}
278 */
279 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
280 $messages = [];
281 for ($i = 0; $i < count($rawAccounts); $i++) {
282 // add object class
283 if (!in_array("inetLocalMailRecipient", $partialAccounts[$i]['objectClass'])) {
284 $partialAccounts[$i]['objectClass'][] = "inetLocalMailRecipient";
285 }
286 // add local addresses
287 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'inetLocalMailRecipient_mailLocalAddress', 'mailLocalAddress', 'mailLocalAddress', $this->messages['mailLocalAddress'][1], $messages, '/,[ ]*/');
288 // add routing address
289 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'inetLocalMailRecipient_routingAdr', 'mailRoutingAddress',
290 'email', $this->messages['mailRoutingAddress'][1], $messages);
291 // add mail server
292 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'inetLocalMailRecipient_server', 'mailHost',
293 'DNSname', $this->messages['mailHost'][1], $messages);
294 }
295 return $messages;
296 }
297
298 /**
299 * {@inheritDoc}
300 */
301 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
302 $return = [];
303 $this->addSimplePDFField($return, 'routingAdr', _('Routing address'), 'mailRoutingAddress');
304 $this->addSimplePDFField($return, 'mailLocalAddress', _('Local address list'), 'mailLocalAddress');
305 $this->addSimplePDFField($return, 'host', _('Mail server'), 'mailHost');
306 return $return;
307 }
308
309 public function getSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array {
310 if ($passwordChangeOnly) {
311 return []; // no processing as long no LDAP content can be read
312 }
313 $return = [];
314 if (in_array('mailLocalAddress', $fields)) {
315 $mailLocalAddress = [];
316 if (isset($attributes['mailLocalAddress'])) {
317 $mailLocalAddress = $attributes['mailLocalAddress'];
318 }
319 array_map(htmlspecialchars(...), $mailLocalAddress);
320 $row = new htmlResponsiveRow();
321 $row->addLabel(new htmlOutputText($this->getSelfServiceLabel('mailLocalAddress', _('Local address'))));
322 $row->addField(new htmlOutputText(implode('<br>', $mailLocalAddress), false));
323 $return['mailLocalAddress'] = $row;
324 }
325 if (in_array('mailRoutingAddress', $fields)) {
326 $mailRoutingAddress = '';
327 if (isset($attributes['mailRoutingAddress'][0])) {
328 $mailRoutingAddress = $attributes['mailRoutingAddress'][0];
329 }
330 $row = new htmlResponsiveRow();
331 $row->addLabel(new htmlOutputText($this->getSelfServiceLabel('mailRoutingAddress', _('Routing address'))));
332 $row->addField(new htmlOutputText($mailRoutingAddress));
333 $return['mailRoutingAddress'] = $row;
334 }
335 return $return;
336 }
337
338 /**
339 * @inheritDoc
340 */
341 public function getListAttributeDescriptions(ConfiguredType $type): array {
342 return [
343 'mailroutingaddress' => _('Routing address'),
344 'maillocaladdress' => _('Local address list'),
345 'mailhost' => _('Mail server')
346 ];
347 }
348
349 }