Debian Codegraph @

templates/config/conftypes.php

raw
1 <?php
2
3 namespace LAM\CONFIG;
4
5 use baseType;
6 use htmlTable;
7 use htmlSubTitle;
8 use htmlImage;
9 use htmlOutputText;
10 use htmlSpacer;
11 use htmlButton;
12 use htmlGroup;
13 use htmlDiv;
14 use htmlResponsiveInputCheckbox;
15 use LAM\TYPES\TypeManager;
16 use LAMConfig;
17 use htmlResponsiveRow;
18 use htmlResponsiveInputField;
19 use function LAM\TYPES\getTypes;
20
21 /*
22 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
23 Copyright (C) 2004 - 2026 Roland Gruber
24
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2 of the License, or
28 (at your option) any later version.
29
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 GNU General Public License for more details.
34
35 You should have received a copy of the GNU General Public License
36 along with this program; if not, write to the Free Software
37 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38
39 */
40
41
42 /**
43 * Here the user can select the account types.
44 *
45 * @package configuration
46 * @author Roland Gruber
47 */
48
49
50 /** Access to config functions */
51 include_once __DIR__ . '/../../lib/config.inc';
52 /** Access to account types */
53 include_once __DIR__ . '/../../lib/types.inc';
54 /** common functions */
55 include_once __DIR__ . '/../../lib/configPages.inc';
56
57 // start session
58 if (isFileBasedSession()) {
59 session_save_path("../../sess");
60 }
61 lam_start_session();
62
63 setlanguage();
64
65 // check if config is set
66 // if not: load login page
67 if (!isset($_SESSION['conf_config'])) {
68 /** go back to login if password is invalid */
69 require(__DIR__ . '/conflogin.php');
70 exit;
71 }
72
73 // check if user canceled editing
74 if (isset($_POST['cancelSettings'])) {
75 metaRefresh("../login.php");
76 exit;
77 }
78
79 $conf = &$_SESSION['conf_config'];
80 if (!($conf instanceof LAMConfig)) {
81 die();
82 }
83
84 $errorsToDisplay = checkTypeInput($conf);
85
86 // check if button was pressed and if we have to save the settings or go to another tab
87 if ((isset($_POST['saveSettings']) || isset($_POST['editmodules'])
88 || isset($_POST['edittypes']) || isset($_POST['generalSettingsButton'])
89 || isset($_POST['moduleSettings']) || isset($_POST['jobs']))
90 && (count($errorsToDisplay) === 0)) {
91 // check if all types have modules
92 $activeTypes = $conf->get_ActiveTypes();
93 foreach ($activeTypes as $activeType) {
94 $selectedModules = $conf->get_AccountModules($activeType);
95 if (count($selectedModules) === 0) {
96 // go to module selection
97 metaRefresh("confmodules.php");
98 exit;
99 }
100 }
101 // go to final page
102 if (isset($_POST['saveSettings'])) {
103 metaRefresh("confsave.php");
104 exit;
105 }
106 // go to modules page
107 elseif (isset($_POST['editmodules'])) {
108 metaRefresh("confmodules.php");
109 exit;
110 }
111 // go to general page
112 elseif (isset($_POST['generalSettingsButton'])) {
113 metaRefresh("confmain.php");
114 exit;
115 }
116 // go to module settings page
117 elseif (isset($_POST['moduleSettings'])) {
118 metaRefresh("moduleSettings.php");
119 exit;
120 }
121 // go to jobs page
122 elseif (isset($_POST['jobs'])) {
123 metaRefresh("jobs.php");
124 exit;
125 }
126 }
127
128 $typeSettings = $conf->get_typeSettings();
129 $allScopes = getTypes();
130 $typeManager = new TypeManager($conf);
131 $activeTypes = $typeManager->getConfiguredTypes();
132 $activeScopes = [];
133 foreach ($activeTypes as $activeType) {
134 $activeScopes[] = $activeType->getScope();
135 }
136 $activeScopes = array_unique($activeScopes);
137 $availableScopes = [];
138 foreach ($allScopes as $scope) {
139 $scopeObj = new $scope(null);
140 if (!in_array($scope, $activeScopes) || $scopeObj->supportsMultipleConfigs()) {
141 $availableScopes[] = $scopeObj;
142 }
143 }
144 usort($availableScopes, \LAM\CONFIG\compareTypesByAlias(...));
145
146 echo $_SESSION['header'];
147 printHeaderContents(_("LDAP Account Manager Configuration"), '../..');
148 echo "</head><body>\n";
149 // include all JavaScript files
150 printJsIncludes('../..');
151 printConfigurationPageHeaderBar($conf);
152
153 // print error messages
154 foreach ($errorsToDisplay as $errorToDisplay) {
155 call_user_func_array(StatusMessage(...), $errorToDisplay);
156 }
157
158 echo "<form action=\"conftypes.php\" method=\"post\" novalidate=\"novalidate\">\n";
159
160 printConfigurationPageTabs(ConfigurationPageTab::TYPES);
161
162 $row = new htmlResponsiveRow();
163
164 // show available types
165 if ($availableScopes !== []) {
166 $row->add(new htmlSubTitle(_("Available account types")));
167 foreach ($availableScopes as $availableScope) {
168 $availableLabelGroup = new htmlGroup();
169 $availableLabelGroup->addElement(new htmlImage('../../graphics/' . $availableScope->getIcon(), '16px', '16px'));
170 $availableLabelGroup->addElement(new htmlSpacer('0.5rem', null));
171 $availableLabelGroup->addElement(new htmlOutputText($availableScope->getAlias()));
172 $row->addField($availableLabelGroup);
173 $availableDescriptionRow = new htmlResponsiveRow();
174 $availableDescriptionRow->add(new htmlOutputText($availableScope->getDescription()), 10, 10, 10, 'responsiveField');
175 $button = new htmlButton('add_' . $availableScope->getScope(), 'add.svg', true);
176 $button->setTitle(_("Add"));
177 $button->setCSSClasses(['size16']);
178 $availableDescriptionRow->add($button, 2, 2, 2, 'responsiveField');
179 $row->addField($availableDescriptionRow);
180 $row->addVerticalSpacer('1rem');
181 }
182 $row->addVerticalSpacer('2rem');
183 }
184 parseHtml(null, $row, [], false, 'user');
185
186 $container = new htmlResponsiveRow();
187
188 $_SESSION['conftypes_optionTypes'] = [];
189 // show active types
190 if (count($activeTypes) > 0) {
191 $container->add(new htmlSubTitle(_("Active account types")));
192 $index = 0;
193 foreach ($activeTypes as $activeType) {
194 // title
195 $titleGroup = new htmlGroup();
196 $titleGroup->addElement(new htmlImage('../../graphics/' . $activeType->getIcon(), '16px', '16px'));
197 $titleGroup->addElement(new htmlSpacer('0.5rem', null));
198 $titleText = new htmlOutputText($activeType->getAlias());
199 $titleText->setIsBold(true);
200 $titleGroup->addElement($titleText);
201 $container->addField($titleGroup);
202 $descriptionRow = new htmlResponsiveRow();
203 $descriptionRow->add(new htmlOutputText($activeType->getBaseType()->getDescription()), 12, 12, 9, 'responsiveField');
204 $buttons = new htmlGroup();
205 // move buttons
206 if ($index > 0) {
207 $upButton = new htmlButton('moveup_' . $activeType->getId(), 'up.svg', true);
208 $upButton->setTitle(_("Up"));
209 $upButton->setCSSClasses(['size16']);
210 $buttons->addElement($upButton);
211 }
212 if ($index < (count($activeTypes) - 1)) {
213 $upButton = new htmlButton('movedown_' . $activeType->getId(), 'down.svg', true);
214 $upButton->setTitle(_("Down"));
215 $upButton->setCSSClasses(['size16']);
216 $buttons->addElement($upButton);
217 }
218 // delete button
219 $delButton = new htmlButton('rem_' . $activeType->getId(), 'del.svg', true);
220 $delButton->setTitle(_("Remove this account type"));
221 $delButton->setCSSClasses(['size16']);
222 $buttons->addElement($delButton);
223 $buttonsDiv = new htmlDiv(null, $buttons, ['text-right']);
224 $descriptionRow->add($buttonsDiv, 12, 12, 3, 'responsiveLabel');
225 $container->addField($descriptionRow);
226 $container->addVerticalSpacer('0.5rem');
227 // LDAP suffix
228 $suffix = '';
229 if (!empty($typeSettings['suffix_' . $activeType->getId()])) {
230 $suffix = $typeSettings['suffix_' . $activeType->getId()];
231 }
232 $suffixInput = new htmlResponsiveInputField(_("LDAP suffix"), 'suffix_' . $activeType->getId(), $suffix, '202', true);
233 $container->add($suffixInput);
234 // list attributes
235 $attributes = $typeSettings['attr_' . $activeType->getId()] ?? $activeType->getBaseType()->getDefaultListAttributes();
236 $attrsInput = new htmlResponsiveInputField(_("List attributes"), 'attr_' . $activeType->getId(), $attributes, '206');
237 $attrsInput->setFieldMaxLength(1000);
238 $container->add($attrsInput);
239 // custom label
240 $customLabel = '';
241 if (isset($typeSettings['customLabel_' . $activeType->getId()])) {
242 $customLabel = $typeSettings['customLabel_' . $activeType->getId()];
243 }
244 $customLabelInput = new htmlResponsiveInputField(_('Custom label'), 'customLabel_' . $activeType->getId(), $customLabel, '264');
245 $container->add($customLabelInput);
246 // LDAP filter
247 $filter = '';
248 if (isset($typeSettings['filter_' . $activeType->getId()])) {
249 $filter = $typeSettings['filter_' . $activeType->getId()];
250 }
251 $filterInput = new htmlResponsiveInputField(_("Additional LDAP filter"), 'filter_' . $activeType->getId(), $filter, '260');
252 $container->add($filterInput);
253 // type options
254 $typeConfigOptions = $activeType->getBaseType()->get_configOptions();
255 if (!empty($typeConfigOptions)) {
256 $container->add($typeConfigOptions);
257 // save option types to session
258 ob_start();
259 $typeConfigOptionTypes = parseHtml(null, $typeConfigOptions, [], true, 'user');
260 ob_end_clean();
261 $_SESSION['conftypes_optionTypes'] = array_merge($_SESSION['conftypes_optionTypes'], $typeConfigOptionTypes);
262 }
263 // advanced options
264 $advancedOptions = new htmlResponsiveRow();
265 // read-only
266 if (isLAMProVersion() && ($conf->getAccessLevel() == LAMConfig::ACCESS_ALL)) {
267 $isReadOnly = false;
268 if (isset($typeSettings['readOnly_' . $activeType->getId()])) {
269 $isReadOnly = $typeSettings['readOnly_' . $activeType->getId()];
270 }
271 $readOnly = new htmlResponsiveInputCheckbox('readOnly_' . $activeType->getId(), $isReadOnly, _('Read-only'), '265');
272 $readOnly->setElementsToDisable(['hideNewButton_' . $activeType->getId(), 'hideDeleteButton_' . $activeType->getId()]);
273 $advancedOptions->add($readOnly);
274 }
275 // hidden type
276 $hidden = false;
277 if (isset($typeSettings['hidden_' . $activeType->getId()])) {
278 $hidden = $typeSettings['hidden_' . $activeType->getId()];
279 }
280 $advancedOptions->add(new htmlResponsiveInputCheckbox('hidden_' . $activeType->getId(), $hidden, _('Hidden'), '261'));
281 if (isLAMProVersion() && ($conf->getAccessLevel() == LAMConfig::ACCESS_ALL)) {
282 // hide button to create new accounts
283 $hideNewButton = false;
284 if (isset($typeSettings['hideNewButton_' . $activeType->getId()])) {
285 $hideNewButton = $typeSettings['hideNewButton_' . $activeType->getId()];
286 }
287 $advancedOptions->add(new htmlResponsiveInputCheckbox('hideNewButton_' . $activeType->getId(), $hideNewButton, _('No new entries'), '262'));
288 // hide button to delete accounts
289 $hideDeleteButton = false;
290 if (isset($typeSettings['hideDeleteButton_' . $activeType->getId()])) {
291 $hideDeleteButton = $typeSettings['hideDeleteButton_' . $activeType->getId()];
292 }
293 $advancedOptions->add(new htmlResponsiveInputCheckbox('hideDeleteButton_' . $activeType->getId(), $hideDeleteButton, _('Disallow delete'), '263'));
294 }
295 $container->add($advancedOptions);
296
297 $container->addVerticalSpacer('2rem');
298 $index++;
299 }
300 }
301
302 $dynamicTypeOptions = [];
303 foreach ($_SESSION['conftypes_optionTypes'] as $key => $value) {
304 if (isset($typeSettings[$key])) {
305 $dynamicTypeOptions[$key] = explode(LAMConfig::LINE_SEPARATOR, $typeSettings[$key]);
306 }
307 }
308 parseHtml(null, $container, $dynamicTypeOptions, false, 'user');
309
310 echo "</div></div>";
311
312 echo "<input type=\"hidden\" name=\"postAvailable\" value=\"yes\">\n";
313
314 $buttonContainer = new htmlTable();
315 $buttonContainer->addElement(new htmlSpacer(null, '10px'), true);
316 $saveButton = new htmlButton('saveSettings', _('Save'));
317 $saveButton->setCSSClasses(['lam-primary']);
318 $buttonContainer->addElement($saveButton);
319 $cancelButton = new htmlButton('cancelSettings', _('Cancel'));
320 $buttonContainer->addElement($cancelButton, true);
321 $buttonContainer->addElement(new htmlSpacer(null, '10px'), true);
322 parseHtml(null, $buttonContainer, [], false, 'user');
323
324 echo "</form>\n";
325 echo "</body>\n";
326 echo "</html>\n";
327
328
329 /**
330 * Checks user input and saves the entered settings.
331 *
332 * @param LAMConfig $conf config
333 * @return array<int, string[]> list of errors
334 */
335 function checkTypeInput(LAMConfig $conf): array {
336 if (!isset($_POST['postAvailable'])) {
337 return [];
338 }
339 $errors = [];
340 $typeManager = new TypeManager($conf);
341 $typeSettings = $conf->get_typeSettings();
342 $accountTypes = $conf->get_ActiveTypes();
343 $postKeys = array_keys($_POST);
344 foreach ($postKeys as $key) {
345 // check if remove button was pressed
346 if (str_starts_with($key, "rem_")) {
347 $type = substr($key, 4);
348 $accountTypes = array_flip($accountTypes);
349 unset($accountTypes[$type]);
350 $accountTypes = array_flip($accountTypes);
351 $accountTypes = array_values($accountTypes);
352 }
353 // check if up button was pressed
354 elseif (str_starts_with($key, "moveup_")) {
355 $type = substr($key, 7);
356 $pos = array_search($type, $accountTypes);
357 $temp = $accountTypes[$pos - 1];
358 $accountTypes[$pos - 1] = $accountTypes[$pos];
359 $accountTypes[$pos] = $temp;
360 }
361 // check if down button was pressed
362 elseif (str_starts_with($key, "movedown_")) {
363 $type = substr($key, 9);
364 $pos = array_search($type, $accountTypes);
365 $temp = $accountTypes[$pos + 1];
366 $accountTypes[$pos + 1] = $accountTypes[$pos];
367 $accountTypes[$pos] = $temp;
368 }
369 // set suffixes
370 elseif (str_starts_with($key, "suffix_")) {
371 $typeSettings[$key] = trim($_POST[$key]);
372 $type = $typeManager->getConfiguredType(substr($key, 7));
373 if ($type === null) {
374 continue;
375 }
376 if (strlen($_POST[$key]) < 1) {
377 $errors[] = ["ERROR", _("LDAP Suffix is invalid!"), $type->getAlias()];
378 }
379 }
380 // set attributes
381 elseif (str_starts_with($key, "attr_")) {
382 $typeSettings[$key] = $_POST[$key];
383 $type = $typeManager->getConfiguredType(substr($key, 5));
384 if ($type === null) {
385 continue;
386 }
387 if (!is_string($_POST[$key]) || !preg_match("/^((#[^:;]+)|([^:;]*:[^:;]+))(;((#[^:;]+)|([^:;]*:[^:;]+)))*$/", $_POST[$key])) {
388 $errors[] = ["ERROR", _("List attributes are invalid!"), $type->getAlias()];
389 }
390 }
391 // set filter
392 elseif (substr($key, 0, strlen('filter_')) === "filter_") {
393 $typeSettings[$key] = $_POST[$key];
394 }
395 // set custom label
396 elseif (str_starts_with($key, 'customLabel_')) {
397 $typeSettings[$key] = $_POST[$key];
398 }
399 }
400 $typeConfigOptions = extractConfigOptionsFromPOST($_SESSION['conftypes_optionTypes']);
401 foreach ($accountTypes as $accountType) {
402 // set hidden
403 $key = "hidden_" . $accountType;
404 $typeSettings[$key] = (isset($_POST[$key]) && ($_POST[$key] == 'on'));
405 if (isLAMProVersion() && ($conf->getAccessLevel() == LAMConfig::ACCESS_ALL)) {
406 // set if new entries are allowed
407 $key = "hideNewButton_" . $accountType;
408 $typeSettings[$key] = (isset($_POST[$key]) && ($_POST[$key] == 'on'));
409 // set if deletion of entries is allowed
410 $key = "hideDeleteButton_" . $accountType;
411 $typeSettings[$key] = (isset($_POST[$key]) && ($_POST[$key] == 'on'));
412 // set if account type is read-only
413 $key = "readOnly_" . $accountType;
414 $typeSettings[$key] = (isset($_POST[$key]) && ($_POST[$key] == 'on'));
415 }
416 // check dynamic type settings
417 $configuredType = $typeManager->getConfiguredType($accountType);
418 if ($configuredType === null) {
419 continue;
420 }
421 $typeObj = $configuredType->getBaseType();
422 $typeMessages = $typeObj->check_configOptions($typeConfigOptions);
423 if (!empty($typeMessages)) {
424 $errors = array_merge($errors, $typeMessages);
425 }
426 }
427 // new type
428 foreach ($_POST as $key => $value) {
429 // check if add button was pressed
430 if (str_starts_with($key, "add_")) {
431 $scope = substr($key, 4);
432 $accountTypes[] = $typeManager->generateNewTypeId($scope);
433 }
434 }
435 // add dynamic type settings
436 foreach ($typeConfigOptions as $key => $value) {
437 $typeSettings[$key] = implode(LAMConfig::LINE_SEPARATOR, $value);
438 }
439 // save input
440 $conf->set_typeSettings($typeSettings);
441 $conf->set_ActiveTypes($accountTypes);
442 // check for duplicate type aliases
443 $aliasNames = [];
444 foreach ($typeManager->getConfiguredTypes() as $type) {
445 if (in_array($type->getAlias(), $aliasNames)) {
446 $errors[] = ['ERROR', _('Please set a unique label for the account types.'), htmlspecialchars($type->getAlias())];
447 }
448 $aliasNames[] = $type->getAlias();
449 }
450 return $errors;
451 }
452
453 /**
454 * Compares types by alias for sorting.
455 *
456 * @param baseType $a first type
457 * @param baseType $b second type
458 * @return int comparison result
459 */
460 function compareTypesByAlias(baseType $a, baseType $b): int {
461 return strnatcasecmp($a->getAlias(), $b->getAlias());
462 }