Debian Codegraph @

lib/modules/courierMailAccount.inc

raw
1 <?php
2
3 use LAM\TYPES\ConfiguredType;
4 use LAM\TYPES\TypeManager;
5
6 /*
7 * This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
8 * Copyright (C) 2017 Lars Althof
9 * 2017 - 2026 Roland Gruber
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /**
27 * Courier mail extension for users.
28 *
29 * @package modules
30 *
31 * @author Lars Althof
32 * @author Roland Gruber
33 */
34
35 /**
36 * Courier mail extension for users.
37 *
38 * @package modules
39 *
40 * @author Lars Althof
41 * @author Roland Gruber
42 */
43 class courierMailAccount extends baseModule {
44
45 /**
46 * These attributes will be ignored by default if a new account is copied from an existing one.
47 */
48 private const ATTRIBUTES_TO_IGNORE_ON_COPY = ['mailbox'];
49
50 /**
51 * Creates a new courierMailAccount object.
52 *
53 * @param string $scope account type (user, group, host)
54 */
55 public function __construct($scope) {
56 // call parent constructor
57 parent::__construct($scope);
58 $this->autoAddObjectClasses = false;
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 public function can_manage(): bool {
65 return $this->get_scope() === 'user';
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 public function get_metaData(): array {
72 $return = [];
73 // icon
74 $return['icon'] = 'mail.svg';
75 // alias name
76 $return["alias"] = _("Courier");
77 // module dependencies
78 $return['dependencies'] = [
79 'depends' => [
80 'inetOrgPerson'
81 ],
82 'conflicts' => []
83 ];
84 // managed object classes
85 $return['objectClasses'] = [
86 'courierMailAccount'
87 ];
88 // managed attributes
89 $return['attributes'] = [
90 'mailbox', 'mailhost', 'sharedgroup', 'disableimap',
91 'disablepop3', 'disableshared', 'disablewebmail', 'quota'
92 ];
93 // help entries
94 $return['help'] = [
95 'disableimap' => [
96 "Headline" => _("Disable IMAP access"),
97 "attr" => 'disableimap',
98 "Text" => _("Setting this will disallow the user from logging in using IMAP.")
99 ],
100 'disablepop3' => [
101 "Headline" => _("Disable POP3 access"),
102 "attr" => 'disablepop3',
103 "Text" => _("Setting this will disallow the user from logging in using POP3.")
104 ],
105 'disablewebmail' => [
106 "Headline" => _("Disable webmail access"),
107 "attr" => 'disablewebmail',
108 "Text" => _("Setting this will disallow the user from logging in using webmail.")
109 ],
110 'disableshared' => [
111 "Headline" => _("Disable shared folder access"),
112 "attr" => 'disableshared',
113 "Text" => _("Setting this will make shared folders invisible to this user.")
114 ],
115 'mailbox' => [
116 "Headline" => _("Mailbox"),
117 "attr" => 'mailbox',
118 "Text" => _("Path to the user's mailbox. Usually relative to the user's home folder.")
119 ],
120 'mailhost' => [
121 "Headline" => _("Mailbox home server"),
122 "attr" => 'mailhost',
123 "Text" => _("Name of the host that stores the user's email. Must be a FQDN.")
124 ],
125 'quota' => [
126 "Headline" => _("Mailbox quota"),
127 "attr" => 'quota',
128 "Text" => _("The amount of storage available to the user.")
129 ],
130 'quotaUpload' => [
131 "Headline" => _("Mailbox quota"),
132 "attr" => 'quota',
133 "Text" => _("The amount of storage available to the user.") . ' ' . _('Please enter the amount in MB, e.g. 1000 means 1000MB of quota.')
134 ],
135 'autoAdd' => [
136 "Headline" => _("Automatically add this extension"),
137 "Text" => _("This will enable the extension automatically if this profile is loaded.")
138 ],
139 'homeDirectory' => [
140 "Headline" => _("Home directory"),
141 "attr" => 'homeDirectory',
142 "Text" => _("The system home directory of the user. In a virtual user setup this is typically '/' for all users.")
143 ],
144 ];
145 // profile options
146 $profileContainer = new htmlResponsiveRow();
147 $profileContainer->add(new htmlResponsiveInputCheckbox('courierMailAccount_addExt', false, _('Automatically add this extension'), 'autoAdd'));
148 $return['profile_options'] = $profileContainer;
149 // PDF fields
150 $return['PDF_fields'] = [
151 'mailbox' => _('Mailbox'),
152 'mailhost' => _('Mailbox home server'),
153 'quota' => _('Mailbox quota'),
154 'disableimap' => _('Disable IMAP access'),
155 'disablepop3' => _('Disable POP3 access'),
156 'disablewebmail' => _('Disable webmail access'),
157 'disableshared' => _('Disable shared folder access')
158 ];
159 // upload fields
160 $return['upload_columns'] = [
161 [
162 'name' => 'courierMailAccount_mailbox',
163 'description' => _('Mailbox'),
164 'help' => 'mailbox',
165 ],
166 [
167 'name' => 'courierMailAccount_mailhost',
168 'description' => _('Mailbox home server'),
169 'help' => 'mailhost',
170 ],
171 [
172 'name' => 'courierMailAccount_quota',
173 'description' => _('Mailbox quota'),
174 'help' => 'quotaUpload',
175 'example' => '1000'
176 ],
177 [
178 'name' => 'courierMailAccount_disableimap',
179 'description' => _('Disable IMAP access'),
180 'help' => 'disableimap',
181 'values' => 'true, false',
182 'default' => 'false',
183 ],
184 [
185 'name' => 'courierMailAccount_disablepop3',
186 'description' => _('Disable POP3 access'),
187 'help' => 'disablepop3',
188 'values' => 'true, false',
189 'default' => 'false',
190 ],
191 [
192 'name' => 'courierMailAccount_disablewebmail',
193 'description' => _('Disable webmail access'),
194 'help' => 'disablewebmail',
195 'values' => 'true, false',
196 'default' => 'false',
197 ],
198 [
199 'name' => 'courierMailAccount_disableshared',
200 'description' => _('Disable shared folder access'),
201 'help' => 'disableshared',
202 'values' => 'true, false',
203 'default' => 'false',
204 ],
205 ];
206 return $return;
207 }
208
209 /**
210 * {@inheritDoc}
211 */
212 protected function load_Messages(): void {
213 $this->messages['mailhost'][0] = ['ERROR', _('The mail host is not valid.')]; // third array value is set dynamically
214 $this->messages['mailhost'][1] = ['ERROR', _('Account %s:') . ' courierMailAccount_mailhost', _('The mail host is not valid.')];
215 $this->messages['quota'][0] = ['ERROR', _('The quota must be entered as a floating point number.')]; // third array value is set dynamically
216 $this->messages['quota'][1] = ['ERROR', _('Account %s:') . ' courierMailAccount_quota', _('Please enter a number.')];
217 $this->messages['homeDirectory'][0] = ['ERROR', _('Homedirectory contains invalid characters.')]; // third array value is set dynamically
218 $this->messages['homeDirectory'][1] = ['ERROR', _('Account %s:') . ' courierMailAccount_homeDirectory', _('Homedirectory contains invalid characters.')];
219 $this->messages['boolean'][0] = ['ERROR', _('Account %s:') . ' courierMailAccount_%s', _('This value can only be "true" or "false".')];
220 }
221
222 /**
223 * {@inheritDoc}
224 */
225 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
226 parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY));
227 }
228
229 /**
230 * {@inheritDoc}
231 */
232 public function getManagedAttributes(string $typeId): array {
233 $attrs = parent::getManagedAttributes($typeId);
234 $typeManager = new TypeManager();
235 $type = $typeManager->getConfiguredType($typeId);
236 if ($type == null) {
237 return $attrs;
238 }
239 $modules = $type->getModules();
240 if ($this->isUnixDisabled($modules)) {
241 $attrs[] = 'homeDirectory';
242 }
243 return $attrs;
244 }
245
246 /**
247 * {@inheritdoc}
248 */
249 public function getWildcardTargetAttributeNames(): array {
250 return ['homeDirectory', 'mailbox'];
251 }
252
253 /**
254 * {@inheritDoc}
255 */
256 public function display_html_attributes(): htmlElement|array {
257 $return = new htmlResponsiveRow();
258 if ($this->isExtensionEnabled()) {
259 $this->getAccountContainer()->replaceWildcardsInArray($this->getWildcardTargetAttributeNames(), $this->attributes);
260 if ($this->isUnixDisabled($this->getAccountContainer()->get_type()->getModules())) {
261 // home directory
262 $this->addSimpleInputTextField($return, 'homeDirectory', _('Home directory'), true);
263 }
264 // show current mailBox
265 $this->addSimpleInputTextField($return, 'mailbox', _('Mailbox folder'));
266 // mail host input
267 $this->addSimpleInputTextField($return, 'mailhost', _('Mailbox host'));
268 // quota with selection for unit of measure
269 $unitIndex = 2;
270 $value = 0;
271 if (isset($this->attributes['quota'][0]) && ((string) $this->attributes['quota'][0] !== '')) {
272 $unitIndex = floor(log((float) substr($this->attributes['quota'][0], 0, -1), 1000));
273 $value = round((float) (substr($this->attributes['quota'][0], 0, -1)) / (1000 ** $unitIndex), 2);
274 }
275 $return->addLabel(new htmlOutputText(_('Mailbox quota')));
276 $tempTable = new htmlTable();
277 $boxInput = new htmlInputField('quotaValue', (string) $value);
278 $boxInput->setFieldSize(5);
279 $boxInput->setFieldMaxLength(5);
280 $tempTable->addElement($boxInput);
281 $units = [
282 'B', 'kB', 'MB', 'GB',
283 'TB', 'PB', 'EB'
284 ];
285 $selected = [
286 $units[$unitIndex]
287 ];
288 $unitSelect = new htmlSelect('quotaUnit', $units, $selected);
289 $unitSelect->setSortElements(false);
290 $tempTable->addElement($unitSelect);
291 $tempTable->addElement(new htmlHelpLink('quota'));
292 $return->addField($tempTable);
293 // check boxes for account access control
294 $disableimap = isset($this->attributes['disableimap'][0]) && ($this->attributes['disableimap'][0] != 0);
295 $return->add(new htmlResponsiveInputCheckbox('disableimap', $disableimap, _('Disable IMAP access'), 'disableimap'));
296 $disablepop3 = isset($this->attributes['disablepop3'][0]) && ($this->attributes['disablepop3'][0] != 0);
297 $return->add(new htmlResponsiveInputCheckbox('disablepop3', $disablepop3, _('Disable POP3 access'), 'disablepop3'));
298 $disablewebmail = isset($this->attributes['disablewebmail'][0]) && ($this->attributes['disablewebmail'][0] != 0);
299 $return->add(new htmlResponsiveInputCheckbox('disablewebmail', $disablewebmail, _('Disable webmail access'), 'disablewebmail'));
300 $disableshared = isset($this->attributes['disableshared'][0]) && ($this->attributes['disableshared'][0] != 0);
301 $return->add(new htmlResponsiveInputCheckbox('disableshared', $disableshared, _('Disable shared folder access'), 'disableshared'));
302 // remove extension
303 $return->addVerticalSpacer('2rem');
304 $remButton = new htmlButton('remObjectClass', _('Remove Courier mail extension'));
305 $remButton->setCSSClasses(['lam-danger']);
306 $return->add($remButton, 12, 12, 12, 'text-center');
307 }
308 else {
309 $return->add(new htmlButton('addObjectClass', _('Add Courier mail extension')));
310 }
311 return $return;
312 }
313
314 /**
315 * {@inheritDoc}
316 */
317 public function process_attributes(): array {
318 // add extension
319 if (isset($_POST['addObjectClass'])) {
320 $this->attributes['objectClass'][] = 'courierMailAccount';
321 return [];
322 }
323 // remove extension
324 elseif (isset($_POST['remObjectClass'])) {
325 $this->attributes['objectClass'] = array_delete([
326 'courierMailAccount'
327 ], $this->attributes['objectClass']);
328 $attrNames = $this->getManagedAttributes($this->getAccountContainer()->get_type()->getId());
329 foreach ($attrNames as $attrName) {
330 if (isset($this->attributes[$attrName])) {
331 unset($this->attributes[$attrName]);
332 }
333 }
334 return [];
335 }
336 // skip processing if extension is not active
337 if (!$this->isExtensionEnabled()) {
338 return [];
339 }
340 $this->getAccountContainer()->replaceWildcardsInPOST($this->getWildcardTargetAttributeNames());
341 $errors = [];
342 // check new mailbox
343 $this->attributes['mailbox'][0] = $_POST['mailbox'];
344 // quota
345 $units = [
346 'B', 'kB', 'MB',
347 'GB', 'TB', 'PB', 'EB'
348 ];
349 // check new quota
350 if (isset($_POST['quotaValue'])) {
351 if (is_numeric($_POST['quotaValue']) && in_array($_POST['quotaUnit'], $units)) {
352 if ($_POST['quotaValue'] > 0) {
353 $power = array_search($_POST['quotaUnit'], $units);
354 $this->attributes['quota'][0] = $_POST['quotaValue'] * (1000 ** $power) . "S";
355 }
356 elseif (isset($this->attributes['quota'])) {
357 unset($this->attributes['quota']);
358 }
359 }
360 else {
361 $message = $this->messages['quota'][0];
362 $message[] = $_POST['quotaValue'];
363 $errors[] = $message;
364 }
365 }
366 elseif (isset($this->attributes['quota'])) {
367 unset($this->attributes['quota']);
368 }
369 // mail host
370 if (!empty($_POST['mailhost'])) {
371 // must be a valid host name
372 $this->attributes['mailhost'][0] = $_POST['mailhost'];
373 if (!get_preg($this->attributes['mailhost'][0], 'hostname')) {
374 $message = $this->messages['mailhost'][0];
375 $message[] = $_POST['mailhost'];
376 $errors[] = $message;
377 }
378 }
379 elseif (isset($this->attributes['mailhost'])) {
380 unset($this->attributes['mailhost']);
381 }
382 // disable IMAP
383 $this->attributes['disableimap'][0] = 0;
384 if (isset($_POST['disableimap'])) {
385 $this->attributes['disableimap'][0] = 1;
386 }
387 // disable POP3
388 $this->attributes['disablepop3'][0] = 0;
389 if (isset($_POST['disablepop3'])) {
390 $this->attributes['disablepop3'][0] = 1;
391 }
392 // disable Webmail
393 $this->attributes['disablewebmail'][0] = 0;
394 if (isset($_POST['disablewebmail'])) {
395 $this->attributes['disablewebmail'][0] = 1;
396 }
397 // disable shared folder
398 $this->attributes['disableshared'][0] = 0;
399 if (isset($_POST['disableshared'])) {
400 $this->attributes['disableshared'][0] = 1;
401 }
402 // home directory
403 if ($this->isUnixDisabled($this->getAccountContainer()->get_type()->getModules())) {
404 // check new home directory
405 $this->attributes['homeDirectory'][0] = $_POST['homeDirectory'];
406 // check for valid path
407 if (!get_preg($this->attributes['homeDirectory'][0], 'filePath')) {
408 $message = $this->messages['homeDirectory'][0];
409 $message[] = $this->attributes['homeDirectory'][0];
410 $errors[] = $message;
411 }
412 }
413 return $errors;
414 }
415
416 /**
417 * {@inheritdoc}
418 */
419 public function module_ready(): bool {
420 $attrs = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes();
421 return isset($attrs['mail'][0]);
422 }
423
424 /**
425 * Returns if the Courier extension is enabled.
426 *
427 * @return boolean Courier extension is active
428 */
429 private function isExtensionEnabled() {
430 return in_array('courierMailAccount', $this->attributes['objectClass']);
431 }
432
433 /**
434 * {@inheritDoc}
435 */
436 public function get_pdfFields(string $typeId): array {
437 $fields = parent::get_pdfFields($typeId);
438 $typeManager = new TypeManager();
439 $modules = $typeManager->getConfiguredType($typeId)->getModules();
440 if ($this->isUnixDisabled($modules)) {
441 $fields['homeDirectory'] = _('Home directory');
442 }
443 return $fields;
444 }
445
446 /**
447 * {@inheritDoc}
448 */
449 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
450 $return = [];
451 $this->addSimplePDFField($return, 'mailbox', _('Mailbox'));
452 $this->addSimplePDFField($return, 'mailhost', _('Mailbox home server'));
453 $this->addSimplePDFField($return, 'homeDirectory', _('Home directory'));
454 $disableOptions = [
455 'disableimap' => _('Disable IMAP access'),
456 'disablepop3' => _('Disable POP3 access'),
457 'disablewebmail' => _('Disable webmail access'),
458 'disableshared' => _('Disable shared folder access')
459 ];
460 foreach ($disableOptions as $attrName => $label) {
461 $value = _('No');
462 if (isset($this->attributes[$attrName][0]) && ($this->attributes[$attrName][0] == '1')) {
463 $value = _('Yes');
464 }
465 $this->addPDFKeyValue($return, $attrName, $label, $value);
466 }
467 // quota
468 $unitIndex = 2;
469 $value = 0;
470 if (isset($this->attributes['quota'][0]) && ((string) $this->attributes['quota'][0] !== '')) {
471 $unitIndex = floor(log((float) substr($this->attributes['quota'][0], 0, -1), 1000));
472 $value = round((float) (substr($this->attributes['quota'][0], 0, -1)) / (1000 ** $unitIndex), 2);
473 }
474 $units = [
475 'B', 'kB', 'MB', 'GB',
476 'TB', 'PB', 'EB'
477 ];
478 $this->addPDFKeyValue($return, 'quota', _('Mailbox quota'), $value . $units[$unitIndex]);
479 return $return;
480 }
481
482 /**
483 * {@inheritDoc}
484 */
485 public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array {
486 $return = parent::get_uploadColumns($selectedModules, $type);
487 // home directory
488 if ($this->isUnixDisabled($selectedModules)) {
489 $return[] = [
490 'name' => 'courierMailAccount_homeDirectory',
491 'description' => _('Home directory'),
492 'help' => 'homeDirectory',
493 'example' => _('/home/smiller'),
494 'required' => true
495 ];
496 }
497 return $return;
498 }
499
500 /**
501 * {@inheritDoc}
502 */
503 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
504 $messages = [];
505 for ($i = 0; $i < count($rawAccounts); $i++) {
506 // add object class
507 if (!in_array("courierMailAccount", $partialAccounts[$i]['objectClass'])) {
508 $partialAccounts[$i]['objectClass'][] = "courierMailAccount";
509 }
510 // home directory
511 if ($this->isUnixDisabled($selectedModules)) {
512 if (!get_preg($rawAccounts[$i][$ids['courierMailAccount_homeDirectory']], 'homeDirectory')) {
513 $message = $this->messages['homeDirectory'][1];
514 $message[] = [$i];
515 $messages[] = $message;
516 }
517 else {
518 $partialAccounts[$i]['homeDirectory'] = $rawAccounts[$i][$ids['courierMailAccount_homeDirectory']];
519 }
520 }
521 // mailbox
522 if (!empty($rawAccounts[$i][$ids['courierMailAccount_mailbox']])) {
523 $partialAccounts[$i]['mailbox'] = $rawAccounts[$i][$ids['courierMailAccount_mailbox']];
524 }
525 // add mailhost
526 if (!empty($rawAccounts[$i][$ids['courierMailAccount_mailhost']])) {
527 if (!get_preg($rawAccounts[$i][$ids['courierMailAccount_mailhost']], 'hostname')) {
528 $message = $this->messages['mailhost'][1];
529 $message[] = [$i];
530 $messages[] = $message;
531 }
532 else {
533 $partialAccounts[$i]['mailhost'] = $rawAccounts[$i][$ids['courierMailAccount_mailhost']];
534 }
535 }
536 // mail quota
537 if (!empty($rawAccounts[$i][$ids['courierMailAccount_quota']])) {
538 if (is_numeric($rawAccounts[$i][$ids['courierMailAccount_quota']])) {
539 $partialAccounts[$i]['quota'] = ($rawAccounts[$i][$ids['courierMailAccount_quota']] * 1000000) . "S";
540 }
541 else {
542 $message = $this->messages['quota'][1];
543 $message[] = [$i];
544 $messages[] = $message;
545 }
546 }
547 // boolean flags
548 $flags = [
549 'disableimap', 'disablepop3',
550 'disablewebmail', 'disableshared'
551 ];
552 $flagValues = ['true', 'false'];
553 foreach ($flags as $flag) {
554 $partialAccounts[$i][$flag] = 0;
555 if (!empty($rawAccounts[$i][$ids['courierMailAccount_' . $flag]])) {
556 $flagValue = $rawAccounts[$i][$ids['courierMailAccount_' . $flag]];
557 if (!in_array($flagValue, $flagValues)) {
558 $message = $this->messages['boolean'][0];
559 $message[] = [$i, $flag];
560 $messages[] = $message;
561 }
562 elseif ($flagValue == 'true') {
563 $partialAccounts[$i][$flag] = 1;
564 }
565 }
566 }
567 }
568 return $messages;
569 }
570
571 /**
572 * {@inheritDoc}
573 */
574 public function load_profile(array $profile): void {
575 // profile mappings in meta data
576 parent::load_profile($profile);
577 // add extension
578 if (isset($profile['courierMailAccount_addExt'][0])
579 && ($profile['courierMailAccount_addExt'][0] == "true")
580 && !in_array('courierMailAccount', $this->attributes['objectClass'])) {
581 $this->attributes['objectClass'][] = 'courierMailAccount';
582 }
583 }
584
585 /**
586 * Returns if this account is virtual only or also a unix account
587 *
588 * @param string[] $modules module names
589 * @return bool only virtual account
590 */
591 private function isUnixDisabled($modules) {
592 return !in_array('posixAccount', $modules);
593 }
594
595 /**
596 * @inheritDoc
597 */
598 public function getListAttributeDescriptions(ConfiguredType $type): array {
599 return [
600 'mailbox' => _('Mailbox'),
601 'mailhost' => _('Mailbox home server'),
602 'quota' => _('Mailbox quota'),
603 'disableimap' => _('Disable IMAP access'),
604 'disablepop3' => _('Disable POP3 access'),
605 'disablewebmail' => _('Disable webmail access'),
606 'disableshared' => _('Disable shared folder access')
607 ];
608 }
609
610 }