Debian Codegraph @

lib/baseModule.inc

raw
1 <?php
2
3 use LAM\JOB\Job;
4 use LAM\PDF\PDFEntry;
5 use LAM\PDF\PDFLabelValue;
6 use LAM\PDF\PDFTable;
7 use LAM\TYPES\ConfiguredType;
8 use LAM\TYPES\TypeManager;
9 use function LAM\TYPES\getScopeFromTypeId;
10 use LAM\PDF\PDFImage;
11
12 /*
13
14 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
15 Copyright (C) 2003 - 2026 Roland Gruber
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
31
32 */
33
34 /**
35 * This is the parent class for all account modules.
36 *
37 * It implements the complete module interface and uses meta-data
38 * provided by the account modules for its functions.
39 *
40 * @package modules
41 * @author Roland Gruber
42 * @see baseModule
43 */
44
45 /** PDF functions */
46 include_once(__DIR__ . '/pdf.inc');
47
48 /**
49 * Parent class of all account modules.
50 * It implements the complete module interface and uses meta-data
51 * provided by the account modules for its functions.<br>
52 * <br>
53 * <b>Location and naming of modules</b><br>
54 * All LAM modules are placed in lib/modules/ and are named "<class name>.inc".
55 * E.g. if you create a new module and its class name is "qmail" then the filename would be "qmail.inc".
56 * The class name of a module must contain only a-z, A-Z, 0-9, -, and _.<br>
57 * <br>
58 * You can avoid to override many functions by using {@link get_metaData()}.<br>
59 * <br>
60 * All module classes should extend the baseModule class.
61 *
62 * @package modules
63 * @author Roland Gruber
64 */
65 abstract class baseModule {
66
67 /**
68 * These attributes will be ignored by default if a new account is copied from an existing one.
69 */
70 public const ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT = ['uid', 'uidNumber', 'gid', 'gidNumber',
71 'cn', 'userpassword', 'sn', 'givenName', 'initials', 'telephoneNumber',
72 'homePhone', 'mobile', 'facsimileTelephoneNumber', 'pager', 'mail',
73 'employeeNumber', 'userCertificate;binary', 'userCertificate',
74 'homeDirectory', 'unixHomeDirectory', 'jpegPhoto'];
75
76 /** @var array<string, mixed>|null $meta includes all meta data provided by the sub class */
77 protected ?array $meta = null;
78
79 /** the account type of this module (user, group, host) */
80 private ?string $scope;
81
82 /** @var array<string, string[]>|null $moduleSettings configuration settings of all modules */
83 protected ?array $moduleSettings = null;
84
85 /**
86 * @var selfServiceProfile|null self-service profile with settings of all modules
87 */
88 protected ?selfServiceProfile $selfServiceSettings = null;
89
90 /** name of parent accountContainer ($_SESSION[$base]) */
91 private ?string $base = null;
92
93 /** @var array<string, string[]|string>|null contains all ldap attributes which should be written */
94 protected ?array $attributes = null;
95
96 /** @var array<string, string[]|string>|null contains all ldap attributes which are loaded from LDAP */
97 protected ?array $orig = null;
98
99 /** @var array<string, array<int, array<string|string[]>>>|null $messages contains all error messages of a module */
100 protected ?array $messages = null;
101
102 /** if true, managed object classes are added when an account is created or loaded (default: true) */
103 protected bool $autoAddObjectClasses = true;
104
105 /**
106 * Creates a new base module class
107 *
108 * @param string|null $scope the account type (user, group, host)
109 */
110 public function __construct(?string $scope) {
111 $this->scope = $scope;
112 // load configuration
113 if ($this->can_manage() || ($scope === null)) {
114 if (isset($_SESSION['config'])) {
115 $this->moduleSettings = $_SESSION['config']->get_moduleSettings();
116 }
117 if (isset($_SESSION['selfServiceProfile'])) {
118 $this->selfServiceSettings = $_SESSION['selfServiceProfile'];
119 }
120 // initialize module
121 $this->load_Messages();
122 $this->meta = $this->get_metaData();
123 }
124 }
125
126 /**
127 * This function fills the $messages variable with output messages from this module.
128 *
129 * Calling this method requires the existence of an enclosing {@link accountContainer}.
130 */
131 protected function load_Messages(): void {
132 }
133
134 /**
135 * Initializes the module after it became part of an {@link accountContainer}
136 *
137 * Calling this method requires the existence of an enclosing {@link accountContainer}.
138 *
139 * @param string $base the name of the {@link accountContainer} object ($_SESSION[$base])
140 */
141 public function init(string $base): void {
142 $this->base = $base;
143 $this->attributes = [];
144 $this->orig = [];
145 // add object classes if needed
146 $this->attributes['objectClass'] = [];
147 $this->orig['objectClass'] = [];
148 if ($this->autoAddObjectClasses) {
149 $objectClasses = $this->getManagedObjectClasses($this->getAccountContainer()->get_type()->getId());
150 for ($i = 0; $i < count($objectClasses); $i++) {
151 if (!in_array($objectClasses[$i], $this->attributes['objectClass'])) {
152 $this->attributes['objectClass'][] = $objectClasses[$i];
153 }
154 }
155 }
156 }
157
158
159 /**
160 * This function loads the LDAP attributes when an account should be loaded.
161 *
162 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
163 * <br>
164 * By default this method loads the object classes and accounts which are specified in {@link getManagedObjectClasses()}
165 * and {@link getManagedAttributes()}.
166 *
167 * @param array<string, string[]|string> $attributes array like the array returned by get_ldap_attributes(dn of account) but without count indices
168 */
169 public function load_attributes(array $attributes): void {
170 $this->attributes = [];
171 // load object classes
172 if (isset($attributes['objectClass'])) {
173 $this->attributes['objectClass'] = $attributes['objectClass'];
174 $this->orig['objectClass'] = $attributes['objectClass'];
175 }
176 else {
177 $this->attributes['objectClass'] = [];
178 $this->orig['objectClass'] = [];
179 }
180 $typeId = $this->getAccountContainer()->get_type()->getId();
181 // add object classes if needed
182 if ($this->autoAddObjectClasses) {
183 $objectClasses = $this->getManagedObjectClasses($typeId);
184 for ($i = 0; $i < count($objectClasses); $i++) {
185 if (!in_array($objectClasses[$i], $this->attributes['objectClass'])) {
186 $this->attributes['objectClass'][] = $objectClasses[$i];
187 }
188 }
189 }
190 // load attributes
191 $attributeNames = array_merge($this->getManagedAttributes($typeId), $this->getManagedHiddenAttributes($typeId));
192 $attributeNames = array_unique($attributeNames);
193 $attributeNames = array_values($attributeNames);
194 for ($i = 0; $i < count($attributeNames); $i++) {
195 if (isset($attributes[$attributeNames[$i]])) {
196 $this->attributes[$attributeNames[$i]] = $attributes[$attributeNames[$i]];
197 $this->orig[$attributeNames[$i]] = $attributes[$attributeNames[$i]];
198 }
199 }
200 }
201
202 /**
203 * Loads the LDAP data from an account to copy.
204 *
205 * @param array<string, string[]|string> $ldapAttributes LDAP attributes of copy
206 * @param string[] $attributesToIgnore list of attributes to ignore during load (defaults to self::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT)
207 */
208 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
209 if (empty($attributesToIgnore)) {
210 $attributesToIgnore = self::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT;
211 }
212 $typeId = $this->getAccountContainer()->get_type()->getId();
213 $validAttributeNames = array_merge($this->getManagedAttributes($typeId), $this->getManagedHiddenAttributes($typeId));
214 foreach ($ldapAttributes as $ldapAttribute => $values) {
215 if (in_array_ignore_case($ldapAttribute, $attributesToIgnore)) {
216 continue;
217 }
218 if (!in_array($ldapAttribute, $validAttributeNames)) {
219 continue;
220 }
221 $this->attributes[$ldapAttribute] = $values;
222 }
223 $objectClassesToCopy = $ldapAttributes['objectClass'];
224 $validObjectClasses = $this->getManagedObjectClasses($typeId);
225 foreach ($validObjectClasses as $validObjectClass) {
226 if (in_array_ignore_case($validObjectClass, $objectClassesToCopy) &&
227 (empty($this->attributes['objectClass']) || !in_array_ignore_case($validObjectClass, $this->attributes['objectClass']))) {
228 $this->attributes['objectClass'][] = $validObjectClass;
229 }
230 }
231 }
232
233 /**
234 * This function provides meta data which is interpreted by baseModule.
235 * Only subclasses will return real data.<br>
236 * <br>
237 * The aim of the meta data is to reduce the number
238 * of functions in the subclasses. All major data is centralized in one place.<br>
239 * <br>
240 * The returned array contains a list of key-value pairs for the different functions.<br>
241 * <ul>
242 *
243 * <li><b>{@link is_base_module()}</b><br>
244 * <br>
245 * <b>Key:</b> is_base<br>
246 * <b>Value:</b> boolean<br>
247 * <br>
248 * <b>Example:</b> "is_base" => true
249 * <br><br>
250 * </li>
251 *
252 * <li><b>{@link get_ldap_filter()}</b><br>
253 * <br>
254 * <b>Key:</b> ldap_filter<br>
255 * <b>Value:</b> array of filters<br>
256 * <br>
257 * <b>Example:</b> "ldap_filter" => array('or' => 'objectClass=posixAccount', 'and' => '(!(uid=*$))')
258 * <br><br>
259 * </li>
260 *
261 * <li><b>{@link getManagedObjectClasses()}</b><br>
262 * <br>
263 * <b>Key:</b> objectClasses<br>
264 * <b>Value:</b> array of object classes<br>
265 * <br>
266 * <b>Example:</b> "objectClasses" => array('posixAccount')
267 * <br><br>
268 * </li>
269 *
270 * <li><b>{@link getLDAPAliases()}</b><br>
271 * <br>
272 * <b>Key:</b> LDAPaliases<br>
273 * <b>Value:</b> array of aliases<br>
274 * <br>
275 * <b>Example:</b> "LDAPaliases" => array('commonName' => 'cn')
276 * <br><br>
277 * </li>
278 *
279 * <li><b>{@link get_RDNAttributes()}</b><br>
280 * <br>
281 * <b>Key:</b> RDN<br>
282 * <b>Value:</b> array of RDNs<br>
283 * <br>
284 * <b>Example:</b> "RDN" => array('uid' => 'normal', 'cn' => 'low')
285 * <br><br>
286 * </li>
287 *
288 * <li><b>{@link get_dependencies()}</b><br>
289 * <br>
290 * <b>Key:</b> dependencies<br>
291 * <b>Value:</b> array of dependencies<br>
292 * <br>
293 * <b>Example:</b> "dependencies" => array("depends" => array("posixAccount", array("qmail", "sendmail")), "conflicts" => array("exim"))
294 * <br><br>
295 * </li>
296 *
297 * <li><b>{@link get_profileOptions()}</b><br>
298 * <br>
299 * <b>Key:</b> profile_options<br>
300 * <b>Value:</b> array of profile options<br>
301 * <br>
302 * The syntax for the value array is the same as for the return value of get_profileOptions().
303 * <br><br>
304 * </li>
305 *
306 * <li><b>{@link check_profileOptions()}</b><br>
307 * <br>
308 * <b>Key:</b> profile_checks<br>
309 * <b>Value:</b> array of checks (array("optionName" => []))<br>
310 * <br>
311 * The "optionName" keys of the value array are the names of the option identifiers.<br>
312 * Each array element is an array itself containing these values:
313 * <ul>
314 * <li><b>type:</b> determines how to check input<br>
315 * Possible values:
316 * <ul>
317 * <li><b>regex:</b> check with regular expression from regex variable, case sensitive</li>
318 * <li><b>regex_i:</b> check with regular expression from regex variable, case insensitive</li>
319 * <li><b>int_greater:</b> integer value of cmp_name1 must be greater than the integer value from the option cmp_name2</li>
320 * <li><b>int_greaterOrEqual:</b> integer value of cmp_name1 must be greater or equal than the integer value from the option cmp_name2</li>
321 * </ul>
322 * </li>
323 * <li><b>error_message:</b> message that is displayed if input value was syntactically incorrect<br>
324 * error_message is an array to build StatusMessages (message type, message head, message text, additional variables)
325 * <li><b>regex:</b> regular expression string (only if type is regex/regex_i)</li>
326 * <li><b>cmp_name1:</b> name of first input variable that is used for comparison (only if type is int_greater/int_greaterOrEqual)</li>
327 * <li><b>cmp_name2:</b> name of second input variable that is used for comparison (only if type is int_greater/int_greaterOrEqual)</li>
328 * <li><b>required:</b> true or false, if this input field must be filled set to true (optional)
329 * <li><b>required_message:</b> message that is displayed if no input value was given (only if required == true)<br>
330 * required_message is an array to build StatusMessages (message type, message head, message text, additional variables)
331 * </li>
332 * </ul>
333 * <br><br>
334 * </li>
335 *
336 * <li><b>{@link load_profile()}</b><br>
337 * <br>
338 * <b>Key:</b> profile_mappings<br>
339 * <b>Value:</b> array('profile_identifier1' => 'LDAP_attribute1', 'profile_identifier2' => 'LDAP_attribute2')<br>
340 * <br>
341 * The mapped values are stored directly in $this->attributes.
342 * <br>
343 * <b>Example:</b> "profile_mappings" => array('inetOrgPerson_title' => 'title')
344 * <br><br>
345 * </li>
346 *
347 * <li><b>{@link get_configOptions()}</b><br>
348 * <br>
349 * <b>Key:</b> config_options<br>
350 * <b>Value:</b> array('user' => array, 'host' => array, 'all' => array)<br>
351 * <br>
352 * The values from 'all' are always returned, the other values only if they are inside the $scopes array.<br>
353 * The syntax for sub arrays is the same as for the return value of {@link get_configOptions()}.
354 * <br><br>
355 * </li>
356 *
357 * <li><b>{@link check_configOptions()}</b><br>
358 * <br>
359 * <b>Key:</b> config_checks<br>
360 * <b>Value:</b> array('user' => array, 'host' => 'array', 'all' => array)<br>
361 * <br>
362 * The values from 'all' are always used for checking, the other values only if they are inside the $scopes array.
363 * The syntax for sub arrays is the same as for {@link check_configOptions()}.
364 * <br><br>
365 * </li>
366 *
367 * <li><b>{@link get_uploadColumns()}</b><br>
368 * <br>
369 * <b>Key:</b> upload_columns<br>
370 * <b>Value:</b> array<br>
371 * <br>
372 * The syntax for array is the same as for the return value of {@link get_uploadColumns()}.
373 * <br><br>
374 * </li>
375 *
376 * <li><b>{@link get_uploadPreDepends()}</b><br>
377 * <br>
378 * <b>Key:</b> upload_preDepends<br>
379 * <b>Value:</b> array<br>
380 * <br>
381 * The syntax for array is the same as for the return value of {@link get_uploadPreDepends()}.
382 * <br><br>
383 * </li>
384 *
385 * <li><b>{@link get_help()}</b><br>
386 * <br>
387 * <b>Key:</b> help<br>
388 * <b>Value:</b> hashtable of help entries<br>
389 * <br>
390 * The hashtable is an array which maps help IDs to help entries.<br>
391 * <br>
392 * <b>Example:</b> 'help' => array('myEntry' => array('Headline' => 'This is the head line', 'Text' => 'Help content'))
393 * <br><br>
394 * </li>
395 *
396 * <li><b>{@link getSelfServiceSearchAttributes()}</b><br>
397 * <br>
398 * <b>Key:</b> selfServiceSearchAttributes<br>
399 * <b>Value:</b> array of attribute names<br>
400 * <br>
401 * <b>Example:</b> "selfServiceSearchAttributes" => array('uid')
402 * <br><br>
403 * </li>
404 *
405 * <li><b>{@link getSelfServiceFields()}</b><br>
406 * <br>
407 * <b>Key:</b> selfServiceFieldSettings<br>
408 * <b>Value:</b> array of self-service fields<br>
409 * <br>
410 * <b>Example:</b> "selfServiceFieldSettings" => array('pwd' => 'Password')
411 * <br><br>
412 * </li>
413 *
414 * </ul>
415 * <b>Example:</b> return array("is_base" => true);
416 *
417 * @return array<string, mixed> meta data
418 */
419 public function get_metaData(): array {
420 return [];
421 }
422
423 /**
424 * Returns the account type of this module (user, group, host)
425 *
426 * @return string account type
427 */
428 public function get_scope() {
429 return $this->scope;
430 }
431
432 /**
433 * Returns true if this module can manage accounts of the current type, otherwise false.
434 *
435 * Calling this method does not require the existence of an enclosing {@link accountContainer}.
436 *
437 * @return bool true if module fits
438 *
439 * @see baseModule::get_metaData()
440 */
441 abstract public function can_manage(): bool;
442
443 /**
444 * Returns true if your module is a base module and otherwise false.
445 *
446 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
447 * <br>
448 * Every account type needs exactly one base module. A base module manages a structural object class.
449 * E.g. the inetOrgPerson module is a base module since its object class is structural.
450 *
451 * @return boolean true if base module (defaults to false if no meta data is provided)
452 *
453 * @see baseModule::get_metaData()
454 */
455 public function is_base_module() {
456 return (isset($this->meta['is_base']) && ($this->meta['is_base']));
457 }
458
459 /**
460 * Returns an LDAP filter for the account lists
461 *
462 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
463 * <br>
464 * Returns an array('or' => '...', 'and' => '...') that is used to build the LDAP filter. Usually, this is used to filter object classes.
465 * All "or" filter parts of the base modules are combined with OR and then combined with the "and" parts.<br>
466 * The resulting LDAP filter will look like this: (&(|(OR1)(OR2)(OR3))(AND1)(AND2)(AND3))<br>
467 * <br>
468 * <b>Example:</b> return array('or' => '(objectClass=posixAccount)', 'and' => '(!(uid=*$))')
469 *
470 * @param string $typeId account type id
471 * @return array<string, string> LDAP filter
472 *
473 * @see baseModule::get_metaData()
474 */
475 public function get_ldap_filter($typeId) {
476 return $this->meta['ldap_filter'] ?? [];
477 }
478
479 /**
480 * Returns an alias name for the module.
481 *
482 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
483 * <br>
484 * This function returns a more descriptive string than the class name. Alias names are used for the buttons on the account pages and the module selection in the configuration wizard.<br>
485 * Please take care that your alias name is not too long. It may contain any character but should not include parts that may be interpreted by the browser (e.g. '<' or '>').
486 * If you use different aliases dependent on the account type please make sure that there is a general alias for unknown types.
487 *
488 * @return string alias name
489 *
490 * @see baseModule::get_metaData()
491 */
492 public function get_alias() {
493 return $this->meta['alias'] ?? static::class;
494 }
495
496 /**
497 * Returns a hash array containing a list of possible LDAP attributes that can be used to form the RDN (Relative Distinguished Name).
498 *
499 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
500 * <br>
501 * The returned elements have this form: <attribute> => <priority>
502 * <br> <attribute> is the name of the LDAP attribute
503 * <br> <priority> defines the priority of the attribute (can be "low", "normal", "high")<br>
504 * <br>
505 * <b>Example:</b> return array('uid' => 'normal', 'cn' => 'low')
506 *
507 * @param string $typeId account type
508 * @return array<string, string> list of attributes
509 *
510 * @see baseModule::get_metaData()
511 */
512 public function get_RDNAttributes($typeId): array {
513 return $this->meta['RDN'] ?? [];
514 }
515
516 /**
517 * This function returns a list with all depending and conflicting modules.
518 *
519 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
520 * <br>
521 * The return value is an array with two sub arrays, "depends" and "conflicts".
522 * All values of the conflict array are string values with module names. All values of the depends
523 * array are either string values with module names or arrays which include only string values with
524 * module names.<br>
525 * If an element of the depends array is itself an array, this means that your module
526 * depends on one of these modules.<br>
527 * <br>
528 * <b>Example:</b> return array("depends" => array("posixAccount", array("qmail", "sendmail")), "conflicts" => array("exim"))
529 *
530 * @return array<string, array<string|string[]>> list of dependencies and conflicts
531 *
532 * @see baseModule::get_metaData()
533 */
534 public function get_dependencies(): array {
535 return $this->meta['dependencies'] ?? ['depends' => [], 'conflicts' => []];
536 }
537
538 /**
539 * This function defines what attributes will be used in the account profiles and their appearance in the profile editor.
540 *
541 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
542 * <br>
543 * The return value is an object implementing htmlElement.<br>
544 * The field name are used as keywords to load
545 * and save profiles. We recommend to use the module name as prefix for them
546 * (e.g. posixAccount_homeDirectory) to avoid naming conflicts.
547 *
548 * @param string $typeId type id (user, group, host, ...)
549 * @return htmlResponsiveRow|null meta HTML object
550 *
551 * @see baseModule::get_metaData()
552 * @see htmlResponsiveRow
553 */
554 public function get_profileOptions(string $typeId): ?htmlResponsiveRow {
555 return $this->meta['profile_options'] ?? null;
556 }
557
558 /**
559 * Checks input values of account profiles.
560 *
561 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
562 * <br>
563 * $options is an hash array (option name => value) that contains the user input.
564 * The option values are all arrays containing one or more elements.<br>
565 * If the input data is invalid the return value is an array that contains arrays
566 * to build StatusMessages (message type, message head, message text). If no errors occurred
567 * the function returns an empty array.
568 *
569 * @param array<string, string[]> $options a hash array (name => value) containing the user input
570 * @param string $typeId type id (user, group, host)
571 * @return string[][] list of error messages (array(type, title, text)) to generate StatusMessages, if any
572 *
573 * @see baseModule::get_metaData()
574 */
575 public function check_profileOptions(array $options, string $typeId): array {
576 $errors = [];
577 if (isset($this->meta['profile_checks'])) {
578 foreach ($this->meta['profile_checks'] as $identifier => $check) {
579 // check if option is required
580 if (isset($check['required']) && $check['required']
581 && (!isset($options[$identifier][0]) || ($options[$identifier][0] == ''))) {
582 $errors[] = $check['required_message'];
583 continue;
584 }
585 switch ($check['type']) {
586 // check by regular expression (from account.inc)
587 case "ext_preg":
588 // ignore empty fields
589 if (!empty($options[$identifier][0])
590 && !get_preg($options[$identifier][0], $check['regex'])) {
591 $errors[] = $check['error_message'];
592 }
593 break;
594 // check by regular expression (case insensitive)
595 case 'regex_i':
596 // ignore empty fields
597 if (!empty($options[$identifier][0])
598 && !preg_match('/' . $check['regex'] . '/i', $options[$identifier][0])) {
599 $errors[] = $check['error_message'];
600 }
601 break;
602 // check by regular expression (case sensitive)
603 case 'regex':
604 // ignore empty fields
605 if (!empty($options[$identifier][0])
606 && !preg_match('/' . $check['regex'] . '/', $options[$identifier][0])) {
607 $errors[] = $check['error_message'];
608 }
609 break;
610 // check by integer comparison (greater)
611 case 'int_greater':
612 $val1 = $options[$check['cmp_name1']][0] ?? '';
613 $val2 = $options[$check['cmp_name2']][0] ?? '';
614 // ignore if both fields or minimum are empty
615 if (!empty($val1)
616 && ($val2 !== '')
617 && (intval($val1) <= intval($val2))) {
618 $errors[] = $check['error_message'];
619 }
620 break;
621 // check by integer comparison (greater or equal)
622 case 'int_greaterOrEqual':
623 $val1 = $options[$check['cmp_name1']][0] ?? '';
624 $val2 = $options[$check['cmp_name2']][0] ?? '';
625 // ignore if both fields are empty
626 if (!empty($val1)
627 && ($val2 !== '')
628 && (intval($val1) < intval($val2))) {
629 $errors[] = $check['error_message'];
630 }
631 break;
632 // print error message for invalid types
633 default:
634 StatusMessage("ERROR", "Unsupported type!", $check['type']);
635 break;
636 }
637 }
638 }
639 return $errors;
640 }
641
642 /**
643 * This function loads the values from an account profile to the module's internal data structures.
644 *
645 * Calling this method does not require the existence of an enclosing {@link accountContainer}.
646 *
647 * @param array<string, string[]> $profile hash array with profile values (identifier => value)
648 *
649 * @see baseModule::get_metaData()
650 */
651 public function load_profile(array $profile): void {
652 if (isset($this->meta['profile_mappings'])) {
653 $identifiers = array_keys($this->meta['profile_mappings']);
654 for ($i = 0; $i < count($identifiers); $i++) {
655 if (isset($profile[$identifiers[$i]])) {
656 $this->attributes[$this->meta['profile_mappings'][$identifiers[$i]]] = $profile[$identifiers[$i]];
657 }
658 }
659 }
660 }
661
662 /**
663 * Returns a list of configuration options.
664 *
665 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
666 * <br>
667 * The field names are used as keywords to load and save settings.
668 * We recommend to use the module name as prefix for them (e.g. posixAccount_homeDirectory) to avoid naming conflicts.
669 *
670 * @param string[] $scopes account types (user, group, host)
671 * @param array<string, string[]> $allScopes list of all active account modules and their account type id (module => array(type id))
672 * @return htmlElement[]|htmlElement htmlElement or array of htmlElement
673 *
674 * @see baseModule::get_metaData()
675 * @see htmlElement
676 */
677 public function get_configOptions(array $scopes, array $allScopes): htmlElement|array {
678 $return = [];
679 for ($i = 0; $i < count($scopes); $i++) {
680 if (isset($this->meta['config_options'][$scopes[$i]])) {
681 if (is_array($this->meta['config_options'][$scopes[$i]])) {
682 $return = array_merge($return, $this->meta['config_options'][$scopes[$i]]);
683 }
684 elseif (isset($return[0]) && ($return[0] instanceof htmlTable) && ($this->meta['config_options'][$scopes[$i]] instanceof htmlTable)) {
685 $return[0]->mergeTableElements($this->meta['config_options'][$scopes[$i]]);
686 }
687 else {
688 $return[] = $this->meta['config_options'][$scopes[$i]];
689 }
690 }
691 }
692 if (isset($this->meta['config_options']['all'])) {
693 if (is_array($this->meta['config_options']['all'])) {
694 $return = array_merge($return, $this->meta['config_options']['all']);
695 }
696 elseif (isset($return[0]) && ($return[0] instanceof htmlTable) && ($this->meta['config_options']['all'] instanceof htmlTable)) {
697 $return[0]->mergeTableElements($this->meta['config_options']['all']);
698 }
699 else {
700 $return[] = $this->meta['config_options']['all'];
701 }
702 }
703 return $return;
704 }
705
706 /**
707 * Checks input values of module settings.
708 *
709 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
710 * <br>
711 * If the input data is invalid the return value is an array that contains subarrays to build StatusMessages ('message type', 'message head', 'message text').
712 * <br>If no errors occurred the function returns an empty array.
713 *
714 * @param string[] $typeIds list of account type ids which are used
715 * @param array<string, string[]> $options hash array (option name => value) that contains the input. The option values are all arrays containing one or more elements.
716 * @return string[][] list of error messages
717 *
718 * @see baseModule::get_metaData()
719 */
720 public function check_configOptions(array $typeIds, array &$options): array {
721 $messages = [];
722 // add checks that are independent of scope
723 $scopes = ['all'];
724 foreach ($typeIds as $typeId) {
725 $scopes[] = getScopeFromTypeId($typeId);
726 }
727 $scopes = array_unique($scopes);
728 foreach ($scopes as $scope) {
729 if (isset($this->meta['config_checks'][$scope]) && is_array($this->meta['config_checks'][$scope])) {
730 $identifiers = array_keys($this->meta['config_checks'][$scope]);
731 for ($i = 0; $i < count($identifiers); $i++) {
732 // check if option is required
733 if (isset($this->meta['config_checks'][$scope][$identifiers[$i]]['required']) && ($this->meta['config_checks'][$scope][$identifiers[$i]]['required']) && ($options[$identifiers[$i]][0] == '')) {
734 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['required_message'];
735 }
736 switch ($this->meta['config_checks'][$scope][$identifiers[$i]]['type']) {
737 // check by regular expression (from account.inc)
738 case "ext_preg":
739 // ignore empty fields
740 if ($options[$identifiers[$i]][0] == '') {
741 break;
742 }
743 if (!get_preg($options[$identifiers[$i]][0], $this->meta['config_checks'][$scope][$identifiers[$i]]['regex'])) {
744 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
745 }
746 break;
747 // check by regular expression (case insensitive)
748 case "regex_i":
749 // ignore empty fields
750 if ($options[$identifiers[$i]][0] == '') {
751 break;
752 }
753 if (!preg_match('/' . $this->meta['config_checks'][$scope][$identifiers[$i]]['regex'] . '/i', $options[$identifiers[$i]][0])) {
754 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
755 }
756 break;
757 // check by regular expression (case sensitive)
758 case "regex":
759 // ignore empty fields
760 if ($options[$identifiers[$i]][0] == '') {
761 break;
762 }
763 if (!preg_match('/' . $this->meta['config_checks'][$scope][$identifiers[$i]]['regex'] . '/', $options[$identifiers[$i]][0])) {
764 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
765 }
766 break;
767 // check by integer comparison (greater)
768 case "int_greater":
769 // ignore if both fields are empty
770 if (($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0] == '') && ($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0] == '')) {
771 break;
772 }
773 // print error message if only one field is empty
774 if (($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0] == '') || ($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0] == '')) {
775 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
776 break;
777 }
778 // compare
779 if (intval($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0]) <= intval($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0])) {
780 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
781 }
782 break;
783 // check by integer comparison (greater or equal)
784 case "int_greaterOrEqual":
785 // ignore if both fields are empty
786 if (($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0] == '') && ($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0] == '')) {
787 break;
788 }
789 // print error message if only one field is empty
790 if (($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0] == '') || ($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0] == '')) {
791 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
792 break;
793 }
794 // compare
795 if (intval($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name1']][0]) < intval($options[$this->meta['config_checks'][$scope][$identifiers[$i]]['cmp_name2']][0])) {
796 $messages[] = $this->meta['config_checks'][$scope][$identifiers[$i]]['error_message'];
797 }
798 break;
799 // print error message on undefined type
800 default:
801 StatusMessage("ERROR", "Unsupported type!", $this->meta['config_checks'][$scope][$identifiers[$i]]['type']);
802 break;
803 }
804 }
805 }
806 }
807 return $messages;
808 }
809
810 /**
811 * Returns a list of config options for LAM's main configuration.
812 *
813 * @param array<string, array<string, string>> $currentSettings current settings
814 * @return htmlElement[] config options
815 */
816 public function getGlobalConfigOptions(array $currentSettings): array {
817 return [];
818 }
819
820 /**
821 * Checks the global config options.
822 *
823 * @param array<string, array<string, string>> $options config options
824 * @param string[] $messages info messages can be added here
825 * @param string[] $errors error messages can be added here
826 */
827 public function checkGlobalConfigOptions(array &$options, array &$messages, array &$errors): void {
828 // to be implemented by modules if needed
829 }
830
831 /**
832 * Specifies if the module supports global cron job actions.
833 *
834 * @return bool supports cron
835 */
836 public function supportsGlobalCronJob(): bool {
837 return false;
838 }
839
840 /**
841 * Runs any global cron actions.
842 *
843 * @param bool $isDryRun dry-run active
844 */
845 public function runGlobalCronActions(bool $isDryRun): void {
846 // needs to be implemented by submodule if needed
847 }
848
849 /**
850 * Returns a hashtable with all entries that may be printed out in the PDF.
851 *
852 * @param string $typeId type id (user, group, host)
853 * @return array<string, string> PDF entries as key => label
854 *
855 * @see baseModule::get_metaData()
856 */
857 public function get_pdfFields(string $typeId): array {
858 return $this->meta['PDF_fields'] ?? [];
859 }
860
861 /**
862 * Returns the PDF entries for this module.
863 *
864 * @param string[] $pdfKeys list of PDF keys that are included in document
865 * @param string $typeId type id (user, group, host)
866 * @return array<string, PDFEntry[]> list of key => PDFEntry
867 */
868 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
869 return [];
870 }
871
872 /**
873 * Adds a simple PDF entry to the given array.
874 *
875 * @param array<string, PDFEntry[]> $result result array (entry will be added here)
876 * @param string $name ID
877 * @param string $label label name
878 * @param string|null $attrName attribute name (default: =$name)
879 * @param string $delimiter delimiter if multiple attribute values exist (default: ", ")
880 */
881 protected function addSimplePDFField(array &$result, string $name, string $label, ?string $attrName = null, string $delimiter = ', '): void {
882 if ($attrName == null) {
883 $attrName = $name;
884 }
885 $value = '';
886 if (isset($this->attributes[strtolower($attrName)][0])) {
887 $attrName = strtolower($attrName);
888 }
889 if (isset($this->attributes[$attrName][0])) {
890 natcasesort($this->attributes[$attrName]);
891 $value = implode($delimiter, $this->attributes[$attrName]);
892 $value = trim($value);
893 }
894 $result[static::class . '_' . $name][] = new PDFLabelValue($label, $value);
895 }
896
897 /**
898 * Adds a simple PDF entry with the given key and value.
899 *
900 * @param array<string, PDFEntry[]> $result result array (entry will be added here)
901 * @param string $name ID
902 * @param string $label label name
903 * @param string|string[] $value value as string or array
904 * @param string $delimiter delimiter if value is array (default: ", ")
905 */
906 public function addPDFKeyValue(&$result, $name, $label, $value, $delimiter = ', '): void {
907 if (is_array($value)) {
908 natcasesort($value);
909 $value = implode($delimiter, $value);
910 }
911 $result[static::class . '_' . $name][] = new PDFLabelValue($label, $value);
912 }
913
914 /**
915 * Adds a table entry to the PDF.
916 *
917 * @param array<string, PDFEntry[]> $result result array (entry will be added here)
918 * @param string $name ID
919 * @param PDFTable $table table
920 */
921 public function addPDFTable(array &$result, string $name, PDFTable $table): void {
922 if (empty($table->rows)) {
923 return;
924 }
925 $result[static::class . '_' . $name][] = $table;
926 }
927
928 /**
929 * Adds an image to the PDF.
930 *
931 * @param array<string, PDFEntry[]> $result result array (entry will be added here)
932 * @param string $attrName attribute name
933 */
934 public function addPDFImage(array &$result, string $attrName): void {
935 if (isset($this->attributes[$attrName]) && (count($this->attributes[$attrName]) > 0)) {
936 $result[static::class . '_' . $attrName][] = new PDFImage($this->attributes[$attrName][0]);
937 }
938 }
939
940 /**
941 * Returns an array containing all input columns for the file upload.
942 *
943 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
944 * <br>
945 * This function returns an array which contains subarrays which represent an upload column.
946 * <b>Syntax of column arrays:</b>
947 * <br>
948 * <br> array(
949 * <br> string: name, // fixed non-translated name which is used as column name (should be of format: <module name>_<column name>)
950 * <br> string: description, // short descriptive name
951 * <br> string: help, // help ID
952 * <br> string: example, // example value
953 * <br> string: values, // possible input values (optional)
954 * <br> string: default, // default value (optional)
955 * <br> boolean: required // true, if user must set a value for this column
956 * <br> boolean: unique // true if all values of this column must be different values (optional, default: "false")
957 * <br> )
958 *
959 * @param string[] $selectedModules list of selected account modules
960 * @param ConfiguredType $type account type
961 * @return array<string, string|bool>[] column list
962 *
963 * @see baseModule::get_metaData()
964 */
965 public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array {
966 return $this->meta['upload_columns'] ?? [];
967 }
968
969 /**
970 * Returns a list of module names which must be processed in building the account before this module.
971 *
972 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
973 * <br>
974 * The named modules may not be active, LAM will check this automatically.
975 *
976 * @return string[] list of module names
977 *
978 * @see baseModule::get_metaData()
979 */
980 public function get_uploadPreDepends(): array {
981 return $this->meta['upload_preDepends'] ?? [];
982 }
983
984 /**
985 * In this function the LDAP accounts are built.
986 *
987 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
988 * <br>
989 * Returns an array which contains subarrays to generate StatusMessages if any errors occurred.
990 *
991 * @param array<int, string>[] $rawAccounts the user input data, contains one subarray for each account.
992 * @param array<string, int> $ids list of IDs for column position (e.g. "posixAccount_uid" => 5)
993 * @param array<string, string[]|string>[] $partialAccounts list of hash arrays (name => value) which are later added to LDAP
994 * @param string[] $selectedModules list of selected account modules
995 * @param ConfiguredType $type account type
996 * @return string[][] list of error messages if any
997 */
998 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
999 // must be implemented in submodules
1000 return [];
1001 }
1002
1003 /**
1004 * Maps simple upload fields directly to LDAP attribute values.
1005 *
1006 * @param array<int, string>[] $rawAccounts the user input data, contains one subarray for each account.
1007 * @param array<string, int> $ids list of IDs for column position (e.g. "posixAccount_uid" => 5)
1008 * @param array<string, string[]|string>[] $partialAccounts list of hash arrays (name => value) which are later added to LDAP
1009 * @param int $position current position in CSV
1010 * @param string $colName column name
1011 * @param string $attrName LDAP attribute name
1012 * @param string|string[] $regex for get_preg() (e.g. 'ascii')
1013 * @param string[] $message error message to add if regex does not match
1014 * @param array<string|string[]>[] $errors list of error messages if any
1015 * @param string $regexSplit multiple values are separated and can be split with this preg_split expression (e.g. "/;[ ]?/")
1016 */
1017 protected function mapSimpleUploadField(&$rawAccounts, &$ids, &$partialAccounts, $position, $colName, $attrName, $regex = null, $message = [], &$errors = [], $regexSplit = null): void {
1018 if (!isset($ids[$colName])) {
1019 return;
1020 }
1021 if (!empty($rawAccounts[$position][$ids[$colName]])) {
1022 $regexIDs = is_array($regex) ? $regex : [$regex];
1023 // single value
1024 if ($regexSplit == null) {
1025 if (!empty($regex)) {
1026 $this->checkUploadRegex($regexIDs, $rawAccounts[$position][$ids[$colName]], $message, $position, $errors);
1027 }
1028 $partialAccounts[$position][$attrName] = [trim($rawAccounts[$position][$ids[$colName]])];
1029 }
1030 // multi-value
1031 else {
1032 $list = preg_split($regexSplit, trim($rawAccounts[$position][$ids[$colName]]));
1033 $partialAccounts[$position][$attrName] = $list;
1034 if (!empty($regex)) {
1035 for ($x = 0; $x < count($list); $x++) {
1036 if (!$this->checkUploadRegex($regexIDs, $list[$x], $message, $position, $errors)) {
1037 break;
1038 }
1039 }
1040 }
1041 }
1042 }
1043 }
1044
1045 /**
1046 * Checks the upload value against a list of regular expressions.
1047 *
1048 * @param string[] $regexIDs regular expression IDs for get_preg()
1049 * @param string $value value to check
1050 * @param string[] $message error message array if not matching
1051 * @param int $position upload position
1052 * @param array<string|string[]>[] $errors error messages
1053 * @return bool value is ok
1054 * @see get_preg()
1055 */
1056 private function checkUploadRegex($regexIDs, $value, $message, $position, &$errors) {
1057 $matched = false;
1058 foreach ($regexIDs as $regexID) {
1059 if (get_preg($value, $regexID)) {
1060 $matched = true;
1061 break;
1062 }
1063 }
1064 if (!$matched) {
1065 $errMsg = $message;
1066 $errMsg[] = [(string) $position];
1067 $errors[] = $errMsg;
1068 return false;
1069 }
1070 return true;
1071 }
1072
1073 /**
1074 * This function returns the help entry array for a specific help id.
1075 *
1076 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
1077 * <br>
1078 * The result is an hashtable with the following keys:<br>
1079 * <ul>
1080 * <li><b>Headline (required)</b><br>
1081 * The headline of this help entry. Can consist of any alphanumeric characters. No HTML/CSS elements are allowed.</li>
1082 * <li><b>Text (required)</b><br>
1083 * The text of the help entry which may contain any alphanumeric characters.</li>
1084 * <li><b>SeeAlso (optional)</b><br>
1085 * A reference to another related web site. It must be an array containing a field called "text" with the link text
1086 * that should be displayed and a field called "link" which is the link target.</li>
1087 * </ul>
1088 * <br>
1089 * <b>Example:</b><br>
1090 * <br>
1091 * array('Headline' => 'This is the head line', 'Text' => 'Help content', 'SeeAlso' => array('text' => 'LAM homepage', 'link' => 'http://www.ldap-account-manager.org/'))
1092 *
1093 * @param string $id The id string for the help entry needed.
1094 * @return array<string, string>|null The desired help entry.
1095 *
1096 * @see baseModule::get_metaData()
1097 */
1098 public function get_help(string $id): ?array {
1099 if (isset($this->meta['help'][$id])) {
1100 return $this->meta['help'][$id];
1101 }
1102 else {
1103 return null;
1104 }
1105 }
1106
1107 /**
1108 * This function is used to check if this module page can be displayed.
1109 *
1110 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1111 * <br>
1112 * Your module might depend on input of other modules. This function determines if the user
1113 * can change to your module page or not. The return value is true if your module accepts
1114 * input, otherwise false.<br>
1115 * This method's return value defaults to true.
1116 *
1117 * @return bool true, if page can be displayed
1118 */
1119 public function module_ready(): bool {
1120 return true;
1121 }
1122
1123 /**
1124 * This function is used to check if all settings for this module have been made.
1125 *
1126 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1127 * <br>
1128 * This function tells LAM if it can create/modify the LDAP account. If your module needs any
1129 * additional input then set this to false. The user will be notified that your module needs
1130 * more input.<br>
1131 * This method's return value defaults to true.
1132 *
1133 * @return bool true, if settings are complete
1134 */
1135 public function module_complete(): bool {
1136 return true;
1137 }
1138
1139 /**
1140 * Controls if the module button the account page is visible and activated.
1141 *
1142 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1143 * <br>
1144 * <b>Possible return values:</b>
1145 * <ul>
1146 * <li><b>enabled:</b> button is visible and active</li>
1147 * <li><b>disabled:</b> button is visible and deactivated (greyed)</li>
1148 * <li><b>hidden:</b> no button will be shown</li>
1149 * </ul>
1150 *
1151 * @return string status ("enabled", "disabled", "hidden")
1152 */
1153 public function getButtonStatus(): string {
1154 return "enabled";
1155 }
1156
1157 /**
1158 * Runs any actions that need to be done before an LDAP entry is created.
1159 *
1160 * @param array<string, string[]|string> $attributes LDAP attributes of this entry (attributes are provided as reference, handle modifications of $attributes with care)
1161 * @param ConfiguredType $type account type
1162 * @return array<string|string[]>[] array which contains status messages. Each entry is an array containing the status message parameters.
1163 */
1164 public function doUploadPreActions($attributes, $type) {
1165 return [];
1166 }
1167
1168 /**
1169 * This function is responsible to do additional tasks after the account has been created in LDAP (e.g. modifying group memberships, adding Quota etc..).
1170 *
1171 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
1172 * <br>
1173 * This function is called as long as the returned status is 'finished'. Please make sure
1174 * that one function call lasts no longer than 3-4 seconds. Otherwise the upload may fail
1175 * because the time limit is exceeded. You should not make more than one LDAP operation in
1176 * each call.
1177 *
1178 * @param array<int, array<int, string>> $data array containing one account in each element
1179 * @param array<string, int> $ids maps the column names to keys for the sub arrays (array(<column_name> => <column number>))
1180 * @param int[] $failed list of account numbers which could not be successfully uploaded to LDAP
1181 * @param array<string, int|string|string[]|string[][]|array<string, string[]>|mixed> $temp variable to store temporary data between two post actions
1182 * @param array<string, string[]|string>[] $accounts list of LDAP entries
1183 * @param string[] $selectedModules selected account modules
1184 * @param ConfiguredType $type account type
1185 * @return array<string, string|int|string[][]|htmlStatusMessage[]> current status
1186 * <br> array (
1187 * <br> 'status' => 'finished' | 'inProgress' // defines if all operations are complete
1188 * <br> 'progress' => 0..100 // the progress of the operations in percent
1189 * <br> 'errors' => array // list of arrays which are used to generate StatusMessages
1190 * <br> )
1191 */
1192 public function doUploadPostActions(array &$data, array $ids, array $failed, array &$temp, array &$accounts,
1193 array $selectedModules, ConfiguredType $type): array {
1194 return [
1195 'status' => 'finished',
1196 'progress' => 100,
1197 'errors' => []
1198 ];
1199 }
1200
1201 /**
1202 * Returns a list of modifications which have to be made to the LDAP account.
1203 *
1204 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1205 * <br>
1206 *
1207 * <br>This function returns an array with 3 entries:
1208 * <br>array( DN1 ('add' => array($attr), 'remove' => array($attr), 'modify' => array($attr)), DN2 .... )
1209 * <br>DN is the DN to change. It is possible to change several DNs (e.g. create a new user and add him
1210 * to some groups via attribute memberUid)<br>
1211 * <br><b>"add"</b> are attributes which have to be added to the LDAP entry
1212 * <br><b>"remove"</b> are attributes which have to be removed from the LDAP entry
1213 * <br><b>"modify"</b> are attributes which have to be modified in the LDAP entry
1214 * <br><b>"notchanged"</b> are attributes which stay unchanged
1215 * <br><b>"info"</b> values with informational value (e.g. to be used later by pre/postModify actions)
1216 * <br>
1217 * <br>This builds the required commands from $this-attributes and $this->orig.
1218 *
1219 * @return array<string, array<string, array<string, string[]>>> list of modifications
1220 */
1221 public function save_attributes(): array {
1222 $this->getAccountContainer()->replaceWildcardsInArray($this->getWildcardTargetAttributeNames(), $this->attributes);
1223 return $this->getAccountContainer()->save_module_attributes($this->attributes, $this->orig);
1224 }
1225
1226 /**
1227 * Allows the module to run commands before the LDAP entry is changed or created.
1228 *
1229 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1230 * <br>
1231 * The modification is aborted if an error message is returned.
1232 *
1233 * @param boolean $newAccount new account
1234 * @param array<string, string[]|string> $attributes LDAP attributes of this entry
1235 * @return array<string|string[]>[] array which contains status messages. Each entry is an array containing the status message parameters.
1236 */
1237 public function preModifyActions(bool $newAccount, array $attributes): array {
1238 return [];
1239 }
1240
1241 /**
1242 * Allows the module to run commands after the LDAP entry is changed or created.
1243 *
1244 * Calling this method requires the existence of an enclosing {@link accountContainer}.
1245 *
1246 * @param bool $newAccount new account
1247 * @param array<string, string[]|string> $attributes LDAP attributes of this entry
1248 * @return string[][] array which contains status messages. Each entry is an array containing the status message parameters.
1249 */
1250 public function postModifyActions(bool $newAccount, array $attributes): array {
1251 return [];
1252 }
1253
1254 /**
1255 * Allows the module to run commands before the LDAP entry is deleted.
1256 *
1257 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1258 *
1259 * @return string[][] array which contains status messages. Each entry is an array containing the status message parameters.
1260 */
1261 public function preDeleteActions(): array {
1262 return [];
1263 }
1264
1265 /**
1266 * Allows the module to run commands after the LDAP entry is deleted.
1267 *
1268 * Calling this method requires the existence of an enclosing {@link accountContainer}.
1269 *
1270 * @return array<string|string[]>[] array which contains status messages. Each entry is an array containing the status message parameters.
1271 */
1272 public function postDeleteActions(): array {
1273 return [];
1274 }
1275
1276 /**
1277 * This function returns an array with the same syntax as save_attributes().
1278 *
1279 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1280 * <br>
1281 * It allows additional LDAP changes when an account is deleted.
1282 *
1283 * @return array<string, array<string, array<string|string[]>>> of LDAP operations, same as for save_attributes()
1284 */
1285 public function delete_attributes(): array {
1286 return [];
1287 }
1288
1289 /**
1290 * This function creates meta HTML code which will be displayed when an account should be deleted.
1291 *
1292 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1293 * <br>
1294 * This can be used to interact with the user, e.g. should the home directory be deleted? The output
1295 * of all modules is displayed on a single page.
1296 *
1297 * @return htmlElement|null meta HTML object
1298 * @see htmlElement
1299 */
1300 public function display_html_delete() {
1301 return null;
1302 }
1303
1304 /**
1305 * Defines if the LDAP entry has only virtual child entries. This is the case for e.g. LDAP views.
1306 *
1307 * @return boolean has only virtual children
1308 */
1309 public function hasOnlyVirtualChildren() {
1310 return false;
1311 }
1312
1313 /**
1314 * This function processes user input of the primary module page.
1315 *
1316 * Calling this method requires the existence of an enclosing {@link accountContainer}.<br>
1317 * <br>
1318 * It checks the user input and saves changes in the module's data structures.<br>
1319 * <br>
1320 * <b>Example:</b> return [[('ERROR', 'Invalid input!', 'This is not allowed here.']];
1321 *
1322 * @return string[][] Array which contains status messages. Each entry is an array containing the status message parameters.
1323 */
1324 abstract public function process_attributes(): array;
1325
1326 /**
1327 * This function creates meta HTML code to display the module page.
1328 *
1329 * Calling this method requires the existence of an enclosing {@link accountContainer}.
1330 *
1331 * @return htmlElement|htmlElement[] meta HTML object
1332 *
1333 * @see htmlElement
1334 */
1335 abstract public function display_html_attributes(): htmlElement|array;
1336
1337 /**
1338 * Adds a simple text input field to the given htmlResponsiveRow.
1339 * The field name will be the same as the attribute name. There must also be a help entry with the attribute name as ID.
1340 * A new line will also be added after this entry so multiple calls will show the fields one below the other.
1341 *
1342 * @param htmlResponsiveRow $container parent container
1343 * @param string $attrName attribute name
1344 * @param string $label label name
1345 * @param bool $required this is a required field (default false)
1346 * @param int $length field length
1347 * @param bool $isTextArea show as text area (default false)
1348 * @param string[]|null $autoCompleteValues values for auto-completion
1349 * @return htmlResponsiveInputField|htmlResponsiveInputTextarea reference to htmlResponsiveInputField/htmlResponsiveInputTextarea
1350 */
1351 protected function &addSimpleInputTextField(&$container, $attrName, $label, $required = false, $length = null, $isTextArea = false, $autoCompleteValues = null) {
1352 $value = '';
1353 if (isset($this->attributes[$attrName][0])) {
1354 $value = $this->attributes[$attrName][0];
1355 }
1356 if ($isTextArea) {
1357 $cols = 30;
1358 if ($length != null) {
1359 $cols = $length;
1360 }
1361 $input = new htmlResponsiveInputTextarea($attrName, $value, $cols, 3, $label, $attrName);
1362 }
1363 else {
1364 $input = new htmlResponsiveInputField($label, $attrName, $value, $attrName);
1365 if ($length != null) {
1366 $input->setFieldSize($length);
1367 }
1368 if (!empty($autoCompleteValues)) {
1369 $input->enableAutocompletion($autoCompleteValues);
1370 }
1371 }
1372 $input->setRequired($required);
1373 $container->add($input);
1374 return $input;
1375 }
1376
1377 /**
1378 * Adds a simple read-only field to the given container.
1379 *
1380 * @param htmlResponsiveRow $container parent container
1381 * @param string $attrName attribute name
1382 * @param string $label field label
1383 */
1384 protected function addSimpleReadOnlyField(&$container, $attrName, $label): void {
1385 $val = '&nbsp;';
1386 if (!empty($this->attributes[$attrName][0])) {
1387 $values = $this->attributes[$attrName];
1388 array_map(htmlspecialchars(...), $values);
1389 $val = implode('<br>', $values);
1390 }
1391 $labelBox = new htmlOutputText($label);
1392 if (!empty($this->attributes[$attrName]) && (count($this->attributes[$attrName]) > 1)) {
1393 $labelBox->alignment = htmlElement::ALIGN_TOP;
1394 }
1395 $container->addLabel($labelBox);
1396 $container->addField(new htmlOutputText($val, false));
1397 }
1398
1399 /**
1400 * Validates a text field.
1401 * If validation is used then there must exist a message named [{attribute name}][0] (e.g. $this->messages['street'][0]).
1402 *
1403 * @param string $attrName attribute name
1404 * @param array<string|string[]>[] $errors errors array where to put validation errors
1405 * @param bool $required value required
1406 * @param string $validationID validation ID for function get_preg() (default: null, null means no validation)
1407 */
1408 protected function processSimpleTextInput($attrName, &$errors, $required = false, $validationID = null): void {
1409 if ((!isset($_POST[$attrName]) || ($_POST[$attrName] === '')) && isset($this->attributes[$attrName][0])) {
1410 unset($this->attributes[$attrName][0]);
1411 if ($required) {
1412 $errors[] = $this->messages[$attrName][0];
1413 }
1414 return;
1415 }
1416 $this->attributes[$attrName][0] = $_POST[$attrName] ?? '';
1417 if ($required && (!isset($_POST[$attrName]) || ($_POST[$attrName] === ''))) {
1418 $errors[] = $this->messages[$attrName][0];
1419 }
1420 elseif (($validationID !== null)
1421 && (isset($_POST[$attrName]) && ($_POST[$attrName] !== ''))
1422 && !get_preg($_POST[$attrName], $validationID)) {
1423 $errors[] = $this->messages[$attrName][0];
1424 }
1425 }
1426
1427 /**
1428 * Adds a text input field that may contain multiple values to the given htmlResponsiveRow.
1429 * The field name will be the same as the attribute name plus a counting number (e.g. street_0).
1430 * The last field will be followed by a button to add a new value. This is named add_{attribute name} (e.g. add_street).
1431 * There must be a help entry with the attribute name as ID.
1432 * A new line will also be added after this entry so multiple calls will show the fields one below the other.
1433 *
1434 * @param htmlResponsiveRow $container parent container
1435 * @param string $attrName attribute name
1436 * @param string|null $label label name
1437 * @param bool $required this is a required field (default false)
1438 * @param int|null $length field length
1439 * @param bool $isTextArea show as text area (default false)
1440 * @param string[]|null $autoCompleteValues values for auto-completion
1441 * @param integer $fieldSize field size
1442 * @param string[] $htmlIDs reference to array where to add the generated HTML IDs of the input fields
1443 * @param string $cssClasses additional CSS classes of input fields
1444 * @param string|null $helpId help ID (attribute name if null)
1445 */
1446 protected function addMultiValueInputTextField(&$container, $attrName, $label, $required = false, $length = null, $isTextArea = false,
1447 $autoCompleteValues = null, $fieldSize = null, &$htmlIDs = null, $cssClasses = '',
1448 ?string $helpId = null): void {
1449 $values = [];
1450 if (isset($this->attributes[$attrName][0])) {
1451 $values = $this->attributes[$attrName];
1452 }
1453 if (count($values) === 0) {
1454 $values[] = '';
1455 }
1456 natcasesort($values);
1457 $first = array_shift($values);
1458 // shift empty value to the end
1459 if ($first === '') {
1460 $values[] = $first;
1461 }
1462 else {
1463 array_unshift($values, $first);
1464 }
1465 $values = array_values($values);
1466 if ($label !== null) {
1467 $labelTextOut = new htmlOutputText($label, true, $required);
1468 $labelTextOut->alignment = htmlElement::ALIGN_TOP;
1469 $container->addLabel($labelTextOut);
1470 }
1471 $help = new htmlHelpLink($helpId ?? $attrName);
1472 $help->alignment = htmlElement::ALIGN_TOP;
1473 $subContainer = new htmlTable();
1474 $subContainer->setCSSClasses(['fullwidth']);
1475 $subContainer->alignment = htmlElement::ALIGN_TOP;
1476 for ($i = 0; $i < count($values); $i++) {
1477 if (!$isTextArea) {
1478 $input = new htmlInputField($attrName . '_' . $i, $values[$i]);
1479 $input->setAccessibilityLabel($label);
1480 if (!empty($cssClasses)) {
1481 $input->setCSSClasses([$cssClasses]);
1482 }
1483 if (!empty($length)) {
1484 $input->setFieldMaxLength($length);
1485 }
1486 if (!empty($fieldSize)) {
1487 $input->setFieldSize($fieldSize);
1488 }
1489 if (!empty($autoCompleteValues)) {
1490 $input->enableAutocompletion($autoCompleteValues);
1491 }
1492 $subContainer->addElement($input);
1493 }
1494 else {
1495 $cols = 30;
1496 if ($length != null) {
1497 $cols = $length;
1498 }
1499 $textArea = new htmlInputTextarea($attrName . '_' . $i, $values[$i], $cols, 3);
1500 if (!empty($cssClasses)) {
1501 $textArea->setCSSClasses([$cssClasses]);
1502 }
1503 $subContainer->addElement($textArea);
1504 }
1505 if (!empty($htmlIDs)) {
1506 $htmlIDs[] = $attrName . '_' . $i;
1507 }
1508 if (!empty($values[$i])) {
1509 $delButton = new htmlButton('del_' . $attrName . '_' . $i, 'del.svg', true);
1510 $delButton->setCSSClasses(['noMarginSides']);
1511 $delButton->setTitle(_('Delete'));
1512 $subContainer->addElement($delButton);
1513 }
1514 if ($i === 0) {
1515 $addButton = new htmlButton('add_' . $attrName, 'add.svg', true);
1516 $addButton->setCSSClasses(['noMarginSides']);
1517 $addButton->setTitle(_('Add'));
1518 $subContainer->addElement($addButton);
1519 $subContainer->addElement($help);
1520 }
1521 $subContainer->addNewLine();
1522 }
1523 if ($label !== null) {
1524 $container->addField($subContainer);
1525 }
1526 else {
1527 $container->add($subContainer);
1528 }
1529 }
1530
1531 /**
1532 * Validates a multi-value text field.
1533 * The input fields must be created with function addMultiValueInputTextField().
1534 * If validation is used then there must exist a message named [{attribute name}][0] (e.g. $this->messages['street'][0]).
1535 *
1536 * @param string $attrName attribute name
1537 * @param array<string|string[]>[] $errors errors array where to put validation errors
1538 * @param string|null $validationID validation ID for function get_preg() (default: null, null means no validation)
1539 * @param bool $required the field is required (default: false)
1540 */
1541 protected function processMultiValueInputTextField($attrName, &$errors, $validationID = null, $required = false): void {
1542 $counter = 0;
1543 while (isset($_POST[$attrName . '_' . $counter])) {
1544 $this->attributes[$attrName][$counter] = trim($_POST[$attrName . '_' . $counter]);
1545 if (($this->attributes[$attrName][$counter] === '') || isset($_POST['del_' . $attrName . '_' . $counter])) {
1546 unset($this->attributes[$attrName][$counter]);
1547 }
1548 elseif (($validationID !== null) && !get_preg($this->attributes[$attrName][$counter], $validationID)) {
1549 $msg = $this->messages[$attrName][0];
1550 if (count($msg) < 3) {
1551 $msg[] = htmlspecialchars($this->attributes[$attrName][$counter]);
1552 }
1553 else {
1554 $msg[] = [htmlspecialchars($this->attributes[$attrName][$counter])];
1555 }
1556 $errors[] = $msg;
1557 }
1558 $counter++;
1559 }
1560 if (isset($_POST['add_' . $attrName])) {
1561 $this->attributes[$attrName][] = '';
1562 }
1563 if (!empty($this->attributes[$attrName])) {
1564 $this->attributes[$attrName] = array_values(array_unique($this->attributes[$attrName]));
1565 }
1566 if ($required && empty($this->attributes[$attrName])) {
1567 $errors[] = $this->messages[$attrName][0];
1568 }
1569 }
1570
1571 /**
1572 * Adds a select field type that may contain multiple values to the given htmlTable.
1573 * The field name will be the same as the attribute name plus a counting number (e.g. street_0).
1574 * The last field will be followed by a button to add a new value. This is named add_{attribute name} (e.g. add_street).
1575 * There must be a help entry with the attribute name as ID.
1576 * A new line will also be added after this entry so multiple calls will show the fields one below the other.
1577 *
1578 * @param htmlResponsiveRow $container parent container
1579 * @param string $attrName attribute name
1580 * @param string|null $label label name
1581 * @param string[]|array<string, string>|array<string, string[]|array<string, string>> $options options for the selects
1582 * @param bool $hasDescriptiveOptions has descriptive options
1583 * @param bool $required this is a required field (default false)
1584 * @param integer $fieldSize field size
1585 * @param string[] $htmlIDs reference to array where to add the generated HTML IDs of the input fields
1586 */
1587 protected function addMultiValueSelectField(&$container, $attrName, $label, $options, $hasDescriptiveOptions = false,
1588 $required = false, $fieldSize = 1, &$htmlIDs = null): void {
1589 $values = [];
1590 if (isset($this->attributes[$attrName][0])) {
1591 $values = $this->attributes[$attrName];
1592 }
1593 if (count($values) === 0) {
1594 $values[] = '';
1595 }
1596 natcasesort($values);
1597 $values = array_values($values);
1598 if ($label !== null) {
1599 $labelTextOut = new htmlOutputText($label);
1600 $labelTextOut->setMarkAsRequired($required);
1601 $container->addLabel($labelTextOut);
1602 }
1603 $subContainer = new htmlTable();
1604 $subContainer->alignment = htmlElement::ALIGN_TOP;
1605 for ($i = 0; $i < count($values); $i++) {
1606 $input = new htmlSelect($attrName . '_' . $i, $options, [$values[$i]], $fieldSize);
1607 $input->setHasDescriptiveElements($hasDescriptiveOptions);
1608 $subContainer->addElement($input);
1609 if (!empty($htmlIDs)) {
1610 $htmlIDs[] = $attrName . '_' . $i;
1611 }
1612 if (!empty($values[$i])) {
1613 $subContainer->addElement(new htmlButton('del_' . $attrName . '_' . $i, 'del.svg', true));
1614 }
1615 if ($i === 0) {
1616 $subContainer->addElement(new htmlButton('add_' . $attrName, 'add.svg', true));
1617 $subContainer->addElement(new htmlHelpLink($attrName));
1618 }
1619 $subContainer->addNewLine();
1620 }
1621 $container->addField($subContainer);
1622 }
1623
1624 /**
1625 * Validates a multi-value select field.
1626 * The select fields must be created with function addMultiValueSelectField().
1627 *
1628 * @param string $attrName attribute name
1629 */
1630 protected function processMultiValueSelectField(string $attrName): void {
1631 $counter = 0;
1632 while (isset($_POST[$attrName . '_' . $counter])) {
1633 $this->attributes[$attrName][$counter] = trim($_POST[$attrName . '_' . $counter]);
1634 if (($this->attributes[$attrName][$counter] === '') || isset($_POST['del_' . $attrName . '_' . $counter])) {
1635 unset($this->attributes[$attrName][$counter]);
1636 }
1637 $counter++;
1638 }
1639 if (isset($_POST['add_' . $attrName])) {
1640 $this->attributes[$attrName][] = '';
1641 }
1642 $this->attributes[$attrName] = array_values(array_unique($this->attributes[$attrName]));
1643 }
1644
1645 /**
1646 * Adds an area with two multi-select fields with buttons to move items from right to left and vice-versa.
1647 * The options of the selects must be presorted.
1648 * <br>Names:
1649 * <ul>
1650 * <li>First select: $namePrefix_1
1651 * <li>Second select: $namePrefix_2
1652 * <li>Button move left: $namePrefix_left
1653 * <li>Button move right: $namePrefix_right
1654 * </ul>
1655 *
1656 * @param htmlResponsiveRow $container row
1657 * @param string $labelFirst label of first select
1658 * @param string $labelSecond label of second select
1659 * @param string[] $optionsFirst options of first select ('label' => 'value')
1660 * @param string[] $selectedFirst selected options of first select
1661 * @param string[] $optionsSecond options of first select ('label' => 'value')
1662 * @param string[] $selectedSecond selected options of second select
1663 * @param string $namePrefix prefix for select field and button names
1664 * @param bool $rightToLeftText sets the text direction in select to right to left
1665 * @param bool $showFilter displays a live filter
1666 */
1667 protected function addDoubleSelectionArea(&$container, $labelFirst, $labelSecond, $optionsFirst, $selectedFirst,
1668 $optionsSecond, $selectedSecond, $namePrefix, $rightToLeftText = false, $showFilter = false): void {
1669 $forceTablet = false;
1670 foreach ($optionsFirst as $label => $value) {
1671 if (mb_strlen($label) > 75) {
1672 $forceTablet = true;
1673 break;
1674 }
1675 }
1676 if (!$forceTablet) {
1677 foreach ($optionsSecond as $label => $value) {
1678 if (mb_strlen($label) > 75) {
1679 $forceTablet = true;
1680 break;
1681 }
1682 }
1683 }
1684 // first select
1685 $firstRow = new htmlResponsiveRow();
1686 $firstRow->add(new htmlOutputText($labelFirst));
1687 $firstSelect = new htmlSelect($namePrefix . '_1', $optionsFirst, $selectedFirst, 15);
1688 $firstSelect->setHasDescriptiveElements(true);
1689 $firstSelect->setMultiSelect(true);
1690 $firstSelect->setRightToLeftTextDirection($rightToLeftText);
1691 $firstSelect->setSortElements(false);
1692 $firstSelect->enableDynamicScrolling();
1693 $firstRow->add($firstSelect);
1694 if ($showFilter) {
1695 $firstFilterGroup = new htmlGroup();
1696 $firstFilterGroup->addElement(new htmlOutputText(_('Filter')));
1697 $firstFilterInput = new htmlInputField($namePrefix . '_filterFirst');
1698 $firstFilterInput->filterSelectBox($namePrefix . '_1');
1699 $firstFilterGroup->addElement($firstFilterInput);
1700 $firstRow->add($firstFilterGroup);
1701 }
1702 $firstContainerDesktop = ($forceTablet) ? 12 : 5;
1703 $container->add($firstRow, 12, 12, $firstContainerDesktop);
1704 // buttons
1705 $buttonActiveDesktop = ($forceTablet) ? 0 : 12;
1706 $buttonInactiveDesktop = ($forceTablet) ? 6 : 0;
1707 $buttonRow = new htmlResponsiveRow();
1708 $buttonRow->setCSSClasses(['text-center']);
1709 $buttonRow->add(new htmlSpacer(null, '1rem'), 0, 0, $buttonActiveDesktop);
1710 $buttonRow->add(new htmlButton($namePrefix . '_left', 'go-previous.svg', true), 0, 0, $buttonActiveDesktop);
1711 $buttonRow->add(new htmlButton($namePrefix . '_left', 'up.svg', true), 6, 6, $buttonInactiveDesktop);
1712 $buttonRow->add(new htmlButton($namePrefix . '_right', 'go-next.svg', true), 0, 0, $buttonActiveDesktop);
1713 $buttonRow->add(new htmlButton($namePrefix . '_right', 'down.svg', true), 6, 6, $buttonInactiveDesktop);
1714 $buttonRowDesktop = ($forceTablet) ? 12 : 1;
1715 $container->add($buttonRow, 12, 12, $buttonRowDesktop);
1716 // second select
1717 $secondRow = new htmlResponsiveRow();
1718 $secondRow->add(new htmlOutputText($labelSecond));
1719 $secondSelect = new htmlSelect($namePrefix . '_2', $optionsSecond, $selectedSecond, 15);
1720 $secondSelect->setHasDescriptiveElements(true);
1721 $secondSelect->setMultiSelect(true);
1722 $secondSelect->setRightToLeftTextDirection($rightToLeftText);
1723 $secondSelect->setSortElements(false);
1724 $secondSelect->enableDynamicScrolling();
1725 $secondRow->add($secondSelect);
1726 if ($showFilter) {
1727 $secondFilterGroup = new htmlGroup();
1728 $secondFilterGroup->addElement(new htmlOutputText(_('Filter')));
1729 $secondFilterInput = new htmlInputField($namePrefix . '_filterSecond');
1730 $secondFilterInput->filterSelectBox($namePrefix . '_2');
1731 $secondFilterGroup->addElement($secondFilterInput);
1732 $secondRow->add($secondFilterGroup);
1733 }
1734 $secondContainerDesktop = ($forceTablet) ? 12 : 6;
1735 $container->add($secondRow, 12, 12, $secondContainerDesktop);
1736 }
1737
1738 /**
1739 * Adds a simple text input field for the self-service.
1740 * The field name will be the same as the class name plus "_" plus attribute name (e.g. posixAccount_cn).
1741 *
1742 * @param array<string, htmlResponsiveRow> $container array that is used as return value for getSelfServiceOptions()
1743 * @param string $name attribute name (== field name)
1744 * @param string $label label to display in front of input field
1745 * @param string[] $fields list of active fields
1746 * @param array<string, string[]|string> $attributes attributes of LDAP account
1747 * @param string[] $readOnlyFields list of read-only fields
1748 * @param bool $required field is required
1749 * @param bool $isTextArea display as text area
1750 * @param string|null $attributeName attribute name (defaults to $name)
1751 */
1752 protected function addSimpleSelfServiceTextField(&$container, $name, $label, &$fields, &$attributes, &$readOnlyFields, $required = false, $isTextArea = false, $attributeName = null): void {
1753 if (!in_array($name, $fields)) {
1754 return;
1755 }
1756 $ldapAttrName = $attributeName ?? $name;
1757 $value = '';
1758 if (isset($attributes[$ldapAttrName][0])) {
1759 $value = $attributes[$ldapAttrName][0];
1760 }
1761 if (!$isTextArea && !in_array($name, $readOnlyFields)) {
1762 $field = new htmlInputField(static::class . '_' . $name, $value);
1763 $field->setRequired($required);
1764 $field->setFieldSize(null);
1765 }
1766 elseif ($isTextArea && !in_array($name, $readOnlyFields)) {
1767 $field = new htmlInputTextarea(static::class . '_' . $name, $value, 100, 3);
1768 }
1769 elseif (!$isTextArea) {
1770 $field = new htmlOutputText($value);
1771 }
1772 else {
1773 $value = htmlspecialchars($value);
1774 $value = str_replace("\n", '<br>', $value);
1775 $field = new htmlOutputText($value, false);
1776 }
1777 $row = new htmlResponsiveRow();
1778 $fieldLabel = new htmlLabel(static::class . '_' . $name, $this->getSelfServiceLabel($name, $label));
1779 $fieldLabel->setMarkAsRequired($required && !in_array($name, $readOnlyFields));
1780 $row->addLabel($fieldLabel);
1781 $row->addField($field);
1782 $container[$name] = $row;
1783 }
1784
1785 /**
1786 * Checks the input value of a self-service text field.
1787 * The field name must be the same as the class name plus "_" plus attribute name (e.g. posixAccount_cn).
1788 * If validation is used then there must exist a message named [{attribute name}][0] (e.g. $this->messages['street'][0]).
1789 *
1790 * @param array<string, array<string|string[]>[]|array<string, string[]|string>> $container return value of checkSelfServiceOptions()
1791 * @param string $name attribute name
1792 * @param array<string, string[]|string> $attributes LDAP attributes
1793 * @param string[] $fields input fields
1794 * @param string[] $readOnlyFields list of read-only fields
1795 * @param string $validationID validation ID for get_preg()
1796 * @param array<string|string[]> $validationMessage validation message data (defaults to $this->messages[$name][0])
1797 * @param array<string|string[]> $requiredMessage message data when no value is set by user (no check if null)
1798 * @param string $attributeName attribute name (defaults to $name)
1799 */
1800 protected function checkSimpleSelfServiceTextField(&$container, $name, &$attributes, $fields, &$readOnlyFields,
1801 $validationID = null, $validationMessage = null, $requiredMessage = null, $attributeName = null): void {
1802 if (in_array($name, $fields) && !in_array($name, $readOnlyFields)) {
1803 $fieldName = static::class . '_' . $name;
1804 $ldapAttrName = $attributeName ?? $name;
1805 if (!empty($_POST[$fieldName])) {
1806 if (($validationID !== null) && !get_preg($_POST[$fieldName], $validationID)) {
1807 $container['messages'][] = $validationMessage ?? $this->messages[$name][0];
1808 }
1809 elseif (isset($attributes[$ldapAttrName]) && ($attributes[$ldapAttrName][0] != $_POST[$fieldName])) {
1810 $container['mod'][$ldapAttrName] = [(string) $_POST[$fieldName]];
1811 }
1812 elseif (!isset($attributes[$ldapAttrName])) {
1813 $container['add'][$ldapAttrName] = [(string) $_POST[$fieldName]];
1814 }
1815 }
1816 elseif ($requiredMessage !== null) {
1817 $container['messages'][] = $requiredMessage;
1818 }
1819 elseif (isset($attributes[$ldapAttrName])) {
1820 $container['del'][$ldapAttrName] = $attributes[$ldapAttrName];
1821 }
1822 }
1823 }
1824
1825 /**
1826 * Adds a simple text input field for the self-service.
1827 * The field name will be the same as the class name plus "_" plus attribute name (e.g. posixAccount_cn).
1828 *
1829 * @param array<string, htmlResponsiveRow> $container array that is used as return value for getSelfServiceOptions()
1830 * @param string $name attribute name (== field name)
1831 * @param string $label label to display in front of input field
1832 * @param string[] $fields list of active fields
1833 * @param array<string, string[]|string> $attributes attributes of LDAP account
1834 * @param string[] $readOnlyFields list of read-only fields
1835 * @param bool $required field is required
1836 * @param bool $isTextArea display as text area
1837 * @param string $attributeName attribute name (defaults to $name)
1838 */
1839 protected function addMultiValueSelfServiceTextField(&$container, $name, $label, &$fields, &$attributes,
1840 &$readOnlyFields, $required = false, $isTextArea = false, $attributeName = null): void {
1841 if (!in_array($name, $fields)) {
1842 return;
1843 }
1844 $ldapAttrName = $attributeName ?? $name;
1845 $values = [];
1846 if (isset($attributes[$ldapAttrName][0])) {
1847 $values = $attributes[$ldapAttrName];
1848 }
1849 $readOnly = in_array($name, $readOnlyFields);
1850 $field = new htmlResponsiveRow();
1851 $fieldNamePrefix = static::class . '_' . $name . '_';
1852 if (!$readOnly) {
1853 if (empty($values)) {
1854 $values[] = '';
1855 }
1856 for ($i = 0; $i < count($values); $i++) {
1857 $fieldRow = new htmlResponsiveRow();
1858 $value = $values[$i];
1859 if (!$isTextArea) {
1860 $inputField = new htmlInputField($fieldNamePrefix . $i, $value);
1861 $inputField->setRequired($required);
1862 $inputField->setFieldSize(null);
1863 }
1864 else {
1865 $inputField = new htmlInputTextarea($fieldNamePrefix . $i, $value, 100, 3);
1866 }
1867 $inputField->setAccessibilityLabel($this->getSelfServiceLabel($name, $label));
1868 $fieldRow->add($inputField, 9, 10);
1869 $linkGroup = new htmlGroup();
1870 if ($value !== '') {
1871 $delLink = new htmlLink(null, '#', '../../graphics/del.svg');
1872 $delLink->setOnClick('window.lam.selfservice.delMultiValue(\'' . $fieldNamePrefix . '\', this); return false;');
1873 $delLink->setCSSClasses(['del-link icon']);
1874 $delLink->setTitle(_('Delete'));
1875 $linkGroup->addElement($delLink);
1876 }
1877 if ($i === (count($values) - 1)) {
1878 $addLink = new htmlLink(null, '#', '../../graphics/add.svg');
1879 $addLink->setOnClick('window.lam.selfservice.addMultiValue(\'' . $fieldNamePrefix . '\', this); return false;');
1880 $addLink->setCSSClasses(['add-link icon', 'margin-left5']);
1881 $addLink->setTitle(_('Add'));
1882 $linkGroup->addElement($addLink);
1883 }
1884 $fieldRow->add($linkGroup, 3, 2);
1885 $field->add($fieldRow);
1886 }
1887 }
1888 else {
1889 foreach ($values as $value) {
1890 if (!$isTextArea) {
1891 $inputField = new htmlOutputText($value);
1892 }
1893 else {
1894 $inputField = new htmlOutputText($value);
1895 $inputField->setPreformatted();
1896 }
1897 $field->add($inputField);
1898 }
1899 }
1900 $row = new htmlResponsiveRow();
1901 $fieldLabel = new htmlLabel($fieldNamePrefix . '0', $this->getSelfServiceLabel($name, $label));
1902 $fieldLabel->setMarkAsRequired($required);
1903 $row->addLabel($fieldLabel);
1904 $row->addField($field);
1905 $container[$name] = $row;
1906 }
1907
1908 /**
1909 * Checks the input value of a self-service multi-value text field.
1910 * The field name must be the same as the class name plus "_" plus attribute name (e.g. posixAccount_cn).
1911 * If validation is used then there must exist a message named [{attribute name}][0] (e.g. $this->messages['street'][0]).
1912 *
1913 * @param array<string, array<string|string[]>[]|array<string, string[]|string>> $container return value of checkSelfServiceOptions()
1914 * @param string $name attribute name
1915 * @param array<string, string[]|string> $attributes LDAP attributes
1916 * @param string[] $fields input fields
1917 * @param string[] $readOnlyFields list of read-only fields
1918 * @param string $validationID validation ID for get_preg()
1919 * @param array<string|string[]> $validationMessage validation message data (defaults to $this->messages[$name][0])
1920 * @param array<string|string[]> $requiredMessage message data when no value is set by user (no check if null)
1921 * @param string $attributeName attribute name (defaults to $name)
1922 */
1923 protected function checkMultiValueSelfServiceTextField(&$container, $name, &$attributes, $fields, &$readOnlyFields,
1924 $validationID = null, $validationMessage = null, $requiredMessage = null, $attributeName = null): void {
1925 if (in_array($name, $fields) && !in_array($name, $readOnlyFields)) {
1926 $fieldName = static::class . '_' . $name;
1927 $ldapAttrName = $attributeName ?? $name;
1928 $valuesNew = [];
1929 foreach ($_POST as $postKey => $postValue) {
1930 if (!str_contains($postKey, $fieldName)) {
1931 continue;
1932 }
1933 if ($postValue === '') {
1934 continue;
1935 }
1936 if (($validationID != null) && !get_preg($postValue, $validationID)) {
1937 $container['messages'][] = $validationMessage ?? $this->messages[$name][0];
1938 return;
1939 }
1940 $valuesNew[] = (string) $postValue;
1941 }
1942 if (empty($valuesNew) && ($requiredMessage !== null)) {
1943 $container['messages'][] = $requiredMessage;
1944 }
1945 $valuesOld = $attributes[$ldapAttrName] ?? [];
1946 if (!areArrayContentsEqual($valuesOld, $valuesNew)) {
1947 $container['mod'][$ldapAttrName] = $valuesNew;
1948 }
1949 }
1950 }
1951
1952 /**
1953 * Returns a list of managed object classes for this module.
1954 *
1955 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
1956 * <br>
1957 * This is used to fix spelling errors in LDAP-Entries (e.g. if "posixACCOUNT" is read instead of "posixAccount" from LDAP).<br>
1958 * <br>
1959 * <b>Example:</b> return array('posixAccount')
1960 *
1961 * @param string $typeId type id (user, group, host)
1962 * @return string[] list of object classes
1963 *
1964 * @see baseModule::get_metaData()
1965 */
1966 public function getManagedObjectClasses(string $typeId): array {
1967 if (isset($this->meta['objectClasses']) && is_array($this->meta['objectClasses'])) {
1968 return $this->meta['objectClasses'];
1969 }
1970 return [];
1971 }
1972
1973 /**
1974 * Returns a list of aliases for LDAP attributes.
1975 *
1976 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
1977 * <br>
1978 * All alias attributes will be renamed to the given attribute names.
1979 *
1980 * @param string $typeId type id (user, group, host)
1981 * @return array<string, string> list of aliases like array("alias name" => "attribute name")
1982 *
1983 * @see baseModule::get_metaData()
1984 */
1985 public function getLDAPAliases(string $typeId): array {
1986 if (isset($this->meta['LDAPaliases']) && is_array($this->meta['LDAPaliases'])) {
1987 return $this->meta['LDAPaliases'];
1988 }
1989 return [];
1990 }
1991
1992 /**
1993 * Returns a list of LDAP attributes which are managed by this module.
1994 * All attribute names will be renamed to match the given spelling.
1995 *
1996 * @param string $typeId type id (user, group, host)
1997 * @return string[] list of attributes
1998 *
1999 * @see baseModule::get_metaData()
2000 */
2001 public function getManagedAttributes(string $typeId): array {
2002 if (isset($this->meta['attributes']) && is_array($this->meta['attributes'])) {
2003 return $this->meta['attributes'];
2004 }
2005 return [];
2006 }
2007
2008 /**
2009 * Returns a list of operational LDAP attributes which are managed by this module and need to be explicitly set for LDAP search.
2010 *
2011 * @param string $typeId account type id
2012 * @return string[] list of hidden attributes
2013 *
2014 * @see baseModule::get_metaData()
2015 */
2016 public function getManagedHiddenAttributes(string $typeId): array {
2017 if (isset($this->meta['hiddenAttributes']) && is_array($this->meta['hiddenAttributes'])) {
2018 return $this->meta['hiddenAttributes'];
2019 }
2020 return [];
2021 }
2022
2023 /**
2024 * This function returns a list of possible LDAP attributes (e.g. uid, cn, ...) which can be used to search for LDAP objects.
2025 *
2026 * Calling this method does not require the existence of an enclosing {@link accountContainer}.
2027 *
2028 * @return string[] attributes
2029 *
2030 * @see baseModule::get_metaData()
2031 */
2032 public function getSelfServiceSearchAttributes(): array {
2033 if (isset($this->meta['selfServiceSearchAttributes']) && is_array($this->meta['selfServiceSearchAttributes'])) {
2034 return $this->meta['selfServiceSearchAttributes'];
2035 }
2036 return [];
2037 }
2038
2039 /**
2040 * Returns a list of possible input fields and their descriptions.
2041 *
2042 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
2043 * <br>
2044 * <b>Format:</b> array(<field identifier> => <field description>)
2045 *
2046 * @return array<string, string> fields
2047 *
2048 * @see baseModule::get_metaData()
2049 */
2050 public function getSelfServiceFields(): array {
2051 if (isset($this->meta['selfServiceFieldSettings']) && is_array($this->meta['selfServiceFieldSettings'])) {
2052 return $this->meta['selfServiceFieldSettings'];
2053 }
2054 return [];
2055 }
2056
2057 /**
2058 * Returns if a given self-service field can be set in read-only mode.
2059 *
2060 * @param string $fieldID field identifier
2061 * @param selfServiceProfile $profile currently edited profile
2062 * @return bool may be set read-only
2063 */
2064 public function canSelfServiceFieldBeReadOnly(string $fieldID, selfServiceProfile $profile): bool {
2065 if (isset($this->meta['selfServiceReadOnlyFields']) && is_array($this->meta['selfServiceReadOnlyFields'])) {
2066 return in_array($fieldID, $this->meta['selfServiceReadOnlyFields']);
2067 }
2068 return false;
2069 }
2070
2071 /**
2072 * Returns if a self-service field can be relabeled.
2073 *
2074 * @param string $fieldID field ID
2075 * @param selfServiceProfile $profile currently edited profile
2076 * @return bool may be relabeled
2077 */
2078 public function canSelfServiceFieldBeRelabeled(string $fieldID, selfServiceProfile $profile): bool {
2079 if (isset($this->meta['selfServiceNoRelabelFields']) && is_array($this->meta['selfServiceNoRelabelFields'])) {
2080 return !in_array($fieldID, $this->meta['selfServiceNoRelabelFields']);
2081 }
2082 return true;
2083 }
2084
2085 /**
2086 * Returns the field label. This can be either the given default label or an override value from profile.
2087 *
2088 * @param string $fieldID field ID
2089 * @param string $defaultLabel default label text
2090 * @return string label
2091 */
2092 protected function getSelfServiceLabel(string $fieldID, string $defaultLabel): string {
2093 if (!$this->canSelfServiceFieldBeRelabeled($fieldID, $this->selfServiceSettings)) {
2094 return $defaultLabel;
2095 }
2096 $key = static::class . '_' . $fieldID;
2097 return empty($this->selfServiceSettings->relabelFields[$key]) ? $defaultLabel : $this->selfServiceSettings->relabelFields[$key];
2098 }
2099
2100 /**
2101 * Returns the meta HTML code for each input field.
2102 *
2103 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
2104 * <br>
2105 * It is not possible to display help links.
2106 *
2107 * @param string[] $fields list of active fields
2108 * @param array<string, string[]|string> $attributes attributes of LDAP account
2109 * @param bool $passwordChangeOnly indicates that the user is only allowed to change his password and no LDAP content is readable
2110 * @param string[] $readOnlyFields list of read-only fields
2111 * @return array<string, htmlResponsiveRow> list of meta HTML elements (field name => htmlResponsiveRow)
2112 *
2113 * @see htmlElement
2114 */
2115 public function getSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array {
2116 // this function must be overwritten by subclasses.
2117 return [];
2118 }
2119
2120 /**
2121 * Checks if all input values are correct and returns the LDAP attributes which should be changed.
2122 * <br>Return values:
2123 * <br>messages: array of parameters to create status messages
2124 * <br>add: array of attributes to add
2125 * <br>del: array of attributes to remove
2126 * <br>mod: array of attributes to modify
2127 * <br>info: array of values with informational value (e.g. to be used later by pre/postModify actions)
2128 *
2129 * Calling this method does not require the existence of an enclosing {@link accountContainer}.
2130 *
2131 * @param string[] $fields input fields
2132 * @param array<string, string[]|string> $attributes LDAP attributes
2133 * @param bool $passwordChangeOnly indicates that the user is only allowed to change his password and no LDAP content is readable
2134 * @param string[] $readOnlyFields list of read-only fields
2135 * @return array<string, array<string, string[]>|string[]> messages and attributes (array('messages' => [], 'add' => array('mail' => array('[email protected]')), 'del' => [], 'mod' => [], 'info' => []))
2136 */
2137 public function checkSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array {
2138 return ['messages' => [], 'add' => [], 'del' => [], 'mod' => [], 'info' => []];
2139 }
2140
2141 /**
2142 * Returns a list of self-service configuration settings.
2143 *
2144 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
2145 * <br>
2146 * The name attributes are used as keywords to load
2147 * and save settings. We recommend to use the module name as prefix for them
2148 * (e.g. posixAccount_homeDirectory) to avoid naming conflicts.
2149 *
2150 * @param selfServiceProfile $profile currently edited profile
2151 * @return htmlElement|null meta HTML object
2152 *
2153 * @see baseModule::get_metaData()
2154 * @see htmlElement
2155 */
2156 public function getSelfServiceSettings(selfServiceProfile $profile): ?htmlElement {
2157 return $this->meta['selfServiceSettings'] ?? null;
2158 }
2159
2160 /**
2161 * Checks if the self-service settings are valid.
2162 *
2163 * Calling this method does not require the existence of an enclosing {@link accountContainer}.<br>
2164 * <br>
2165 * If the input data is invalid, the return value is an array that contains arrays
2166 * to build StatusMessages (message type, message head, message text). If no errors
2167 * occurred, the function returns an empty array.
2168 *
2169 * @param array<string, string[]> $options hash array (option name => value) that contains the input. The option values are all arrays containing one or more elements.
2170 * @param selfServiceProfile $profile self-service profile
2171 * @return string[][] error messages
2172 */
2173 public function checkSelfServiceSettings(array &$options, selfServiceProfile &$profile): array {
2174 // needs to be implemented by the subclasses, if needed
2175 return [];
2176 }
2177
2178 /**
2179 * Allows the module to run commands before the LDAP entry is changed or created.
2180 *
2181 * An error message should be printed if the function returns false.
2182 *
2183 * @param boolean $newAccount is new account or existing one
2184 * @param array<string, string[]|string> $attributes LDAP attributes of this entry
2185 * @return bool true, if no problems occurred
2186 */
2187 public function preModifySelfService(bool $newAccount, array $attributes): bool {
2188 return true;
2189 }
2190
2191 /**
2192 * Allows the module to run commands after the LDAP entry is changed or created.
2193 *
2194 * @param bool $newAccount is new account or existing one
2195 * @param array<string, string[]|string> $attributes LDAP attributes of this entry
2196 * @return bool true, if no problems occurred
2197 */
2198 public function postModifySelfService(bool $newAccount, array $attributes): bool {
2199 return true;
2200 }
2201
2202 /**
2203 * This allows modules to create a link to a module specific page
2204 * for the self-service.
2205 * The link is shown on the login page of the self-service. You
2206 * can use this to provide e.g. a page to reset passwords.
2207 *
2208 * @param array<string, string[]> $settings self-service settings
2209 * @return string|null link text (null if no special page used)
2210 */
2211 public function getLinkToSpecialSelfServicePage(array $settings): ?string {
2212 return null;
2213 }
2214
2215 /**
2216 * This function creates meta HTML code to display the module specific page
2217 * for the self-service.
2218 *
2219 * @param selfServiceProfile $profile self-service settings
2220 * @return htmlElement|null meta HTML object
2221 *
2222 * @see htmlElement
2223 */
2224 public function displaySpecialSelfServicePage($profile) {
2225 return null;
2226 }
2227
2228 /**
2229 * Returns the {@link accountContainer} object.
2230 *
2231 * @return ?accountContainer accountContainer object
2232 *
2233 * @see accountContainer
2234 */
2235 protected function getAccountContainer(): ?accountContainer {
2236 if ($this->base !== null && isset($_SESSION[$this->base])) {
2237 return $_SESSION[$this->base];
2238 }
2239 else {
2240 return null;
2241 }
2242 }
2243
2244 /**
2245 * Returns the LDAP attributes which are managed in this module.
2246 *
2247 * @return array<string, string[]|string>|null attributes
2248 */
2249 public function getAttributes(): ?array {
2250 return $this->attributes;
2251 }
2252
2253 /**
2254 * Returns the LDAP attributes which are managed in this module (with unchanged values).
2255 *
2256 * @return array<string, string[]|string>|null attributes
2257 */
2258 public function getOriginalAttributes(): ?array {
2259 return $this->orig;
2260 }
2261
2262 /**
2263 * Returns the path to the module icon.
2264 * The path must be relative to graphics (e.g. key.svg) or a URL (/icons/icon.svg or http://server/icon.svg).
2265 * You can also set $this->meta['icon']. The preferred size is 32x32px, format SVG.
2266 *
2267 * @return string icon
2268 *
2269 * @see baseModule::get_metaData()
2270 */
2271 public function getIcon(): string {
2272 if (!empty($this->meta['icon']) && is_string($this->meta['icon'])) {
2273 return $this->meta['icon'];
2274 }
2275 return 'configure.svg';
2276 }
2277
2278 /**
2279 * Manages AJAX requests.
2280 * This function may be called with or without an account container.
2281 */
2282 public function handleAjaxRequest(): void {
2283 // modules that use AJAX need to implement this function
2284 }
2285
2286 /**
2287 * Specifies if this module supports the LAM admin interface.
2288 * The LAM admin interface are the pages that allow to manage e.g. users and groups.
2289 * In contrast there is also the LAM self-service interface. Most modules support
2290 * the admin interface.
2291 *
2292 * @return boolean support admin interface
2293 */
2294 public function supportsAdminInterface() {
2295 return true;
2296 }
2297
2298 /**
2299 * Returns a list of jobs that can be run.
2300 *
2301 * @param LAMConfig $config configuration
2302 * @return Job[] jobs
2303 */
2304 public function getSupportedJobs(LAMConfig $config): array {
2305 return [];
2306 }
2307
2308
2309
2310 // helper functions
2311
2312 /**
2313 * Returns if the given configuration option is set.
2314 * This function returns false if the configuration options cannot be read.
2315 *
2316 * @param String $optionName name of the option
2317 * @param boolean $default default value if config option is not set at all (default: false)
2318 * @return boolean true if option is set
2319 */
2320 protected function isBooleanConfigOptionSet($optionName, $default = false) {
2321 // abort if configuration is not available
2322 if (!is_array($this->moduleSettings) || !isset($this->moduleSettings[$optionName][0])) {
2323 return $default;
2324 }
2325 return ($this->moduleSettings[$optionName][0] == 'true');
2326 }
2327
2328 /**
2329 * Returns a list of wildcards that can be replaced in input fields.
2330 * E.g. "$firstname" is replaced with "givenName" attribute value.
2331 *
2332 * @return array<string, string> replacements as wildcard => value
2333 */
2334 public function getWildCardReplacements(): array {
2335 return [];
2336 }
2337
2338 /**
2339 * Returns a list of attribute names that may contain wildcards.
2340 *
2341 * @return string[] list of attribute names
2342 */
2343 public function getWildcardTargetAttributeNames(): array {
2344 return [];
2345 }
2346
2347 /**
2348 * Returns a callable if there should be a custom filtering for the given attribute name.
2349 *
2350 * @param string $attributeName attribute name
2351 * @return callable|null custom function for filtering (?array $values, ?string $filterValue)
2352 */
2353 public function getListFilterFunction(string $attributeName): ?callable {
2354 return null;
2355 }
2356
2357 /**
2358 * Returns a callable if there should be a custom display for the given attribute name.
2359 *
2360 * @param string $attributeName attribute name
2361 * @param ConfiguredType $type type
2362 * @return callable|null custom function for rendering (array $entry, string $attribute)
2363 */
2364 public function getListRenderFunction(string $attributeName, ConfiguredType $type): ?callable {
2365 return null;
2366 }
2367
2368 /**
2369 * Returns a list of attribute descriptions for the account list.
2370 *
2371 * @param ConfiguredType $type type
2372 * @return array<string, string> attribute name => description label
2373 */
2374 public function getListAttributeDescriptions(ConfiguredType $type): array {
2375 return [];
2376 }
2377
2378 /**
2379 * Renders a list of DNs with links to edit page.
2380 *
2381 * @param array<string, string[]|string> $entry LDAP entry
2382 * @param string $attribute attribute name
2383 * @return htmlElement content
2384 */
2385 protected function listRenderDnList(array $entry, string $attribute): htmlElement {
2386 if (!empty($entry[$attribute][0])) {
2387 $typeManager = new TypeManager();
2388 $values = $entry[$attribute];
2389 usort($values, compareDN(...));
2390 $count = count($values);
2391 $showMoreLess = ($count > htmlShowMore::DEFAULT_MAXIMUM_SIZE);
2392 for ($i = 0; $i < $count; $i++) {
2393 $replaced = false;
2394 $className = '';
2395 if ($showMoreLess && $i >= htmlShowMore::DEFAULT_MAXIMUM_SIZE) {
2396 $className = htmlShowMore::CLASS_SHOW_MORE;
2397 }
2398 foreach ($typeManager->getConfiguredTypes() as $type) {
2399 if ((stripos($values[$i], $type->getSuffix()) > 0) && !isAccountTypeHidden($type->getId())) {
2400 $values[$i] = new htmlLink(getAbstractDN($values[$i]), '../account/edit.php?type=' . $type->getId() . '&DN=\'' . $values[$i] . '\'');
2401 $replaced = true;
2402 break;
2403 }
2404 }
2405 if (!$replaced) {
2406 $values[$i] = new htmlOutputText(getAbstractDN($values[$i]));
2407 }
2408 $valueGroup = new htmlGroup([$values[$i], new htmlLineBreak()]);
2409 if ($showMoreLess && ($i === (htmlShowMore::DEFAULT_MAXIMUM_SIZE - 1))) {
2410 $valueGroup->addElement(new htmlShowMore());
2411 }
2412 $values[$i] = new htmlSpan($valueGroup, [$className]);
2413 }
2414 if ($showMoreLess) {
2415 $values[] = new htmlShowLess();
2416 }
2417 return new htmlDiv(null, new htmlGroup($values), ['rightToLeftText', htmlShowMore::CLASS_SHOW_MORE_ROOT]);
2418 }
2419 return new htmlGroup();
2420 }
2421
2422 }