| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\CONFIG; |
| 4 |
|
| 5 |
use htmlInputField; |
| 6 |
use htmlJavaScript; |
| 7 |
use htmlTable; |
| 8 |
use htmlOutputText; |
| 9 |
use htmlHelpLink; |
| 10 |
use htmlHiddenInput; |
| 11 |
use htmlButton; |
| 12 |
use htmlSpacer; |
| 13 |
use htmlElement; |
| 14 |
use htmlImage; |
| 15 |
use htmlSortableList; |
| 16 |
use htmlSubTitle; |
| 17 |
use htmlDiv; |
| 18 |
use htmlResponsiveRow; |
| 19 |
use htmlGroup; |
| 20 |
use LAM\TYPES\ConfiguredType; |
| 21 |
use LAM\TYPES\TypeManager; |
| 22 |
use LAMConfig; |
| 23 |
|
| 24 |
/* |
| 25 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 26 |
Copyright (C) 2004 - 2026 Roland Gruber |
| 27 |
|
| 28 |
This program is free software; you can redistribute it and/or modify |
| 29 |
it under the terms of the GNU General Public License as published by |
| 30 |
the Free Software Foundation; either version 2 of the License, or |
| 31 |
(at your option) any later version. |
| 32 |
|
| 33 |
This program is distributed in the hope that it will be useful, |
| 34 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 35 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 36 |
GNU General Public License for more details. |
| 37 |
|
| 38 |
You should have received a copy of the GNU General Public License |
| 39 |
along with this program; if not, write to the Free Software |
| 40 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 41 |
|
| 42 |
*/ |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* confmodules lets the user select the account modules |
| 47 |
* |
| 48 |
* @package configuration |
| 49 |
* @author Roland Gruber |
| 50 |
*/ |
| 51 |
|
| 52 |
|
| 53 |
/** Access to config functions */ |
| 54 |
include_once(__DIR__ . '/../../lib/config.inc'); |
| 55 |
/** Access to module lists */ |
| 56 |
include_once(__DIR__ . '/../../lib/modules.inc'); |
| 57 |
/** common functions */ |
| 58 |
include_once __DIR__ . '/../../lib/configPages.inc'; |
| 59 |
|
| 60 |
// start session |
| 61 |
if (isFileBasedSession()) { |
| 62 |
session_save_path("../../sess"); |
| 63 |
} |
| 64 |
lam_start_session(); |
| 65 |
|
| 66 |
setlanguage(); |
| 67 |
|
| 68 |
|
| 69 |
// check if config is set |
| 70 |
// if not: load login page |
| 71 |
if (!isset($_SESSION['conf_config'])) { |
| 72 |
/** go back to login if password is invalid */ |
| 73 |
require(__DIR__ . '/conflogin.php'); |
| 74 |
exit; |
| 75 |
} |
| 76 |
|
| 77 |
// check if user canceled editing |
| 78 |
if (isset($_POST['cancelSettings'])) { |
| 79 |
metaRefresh("../login.php"); |
| 80 |
exit; |
| 81 |
} |
| 82 |
|
| 83 |
$conf = &$_SESSION['conf_config']; |
| 84 |
if (!($conf instanceof LAMConfig)) { |
| 85 |
die(); |
| 86 |
} |
| 87 |
|
| 88 |
$errorsToDisplay = checkModuleInput($conf); |
| 89 |
|
| 90 |
// check if button was pressed and if we have to save the settings or go to another tab |
| 91 |
if ((isset($_POST['saveSettings']) || isset($_POST['editmodules']) |
| 92 |
|| isset($_POST['edittypes']) || isset($_POST['generalSettingsButton']) |
| 93 |
|| isset($_POST['moduleSettings']) || isset($_POST['jobs'])) |
| 94 |
&& ($errorsToDisplay === [])) { |
| 95 |
// go to final page |
| 96 |
if (isset($_POST['saveSettings'])) { |
| 97 |
metaRefresh("confsave.php"); |
| 98 |
exit; |
| 99 |
} |
| 100 |
// go to types page |
| 101 |
elseif (isset($_POST['edittypes'])) { |
| 102 |
metaRefresh("conftypes.php"); |
| 103 |
exit; |
| 104 |
} |
| 105 |
// go to general page |
| 106 |
elseif (isset($_POST['generalSettingsButton'])) { |
| 107 |
metaRefresh("confmain.php"); |
| 108 |
exit; |
| 109 |
} |
| 110 |
// go to module settings page |
| 111 |
elseif (isset($_POST['moduleSettings'])) { |
| 112 |
metaRefresh("moduleSettings.php"); |
| 113 |
exit; |
| 114 |
} |
| 115 |
// go to jobs page |
| 116 |
elseif (isset($_POST['jobs'])) { |
| 117 |
metaRefresh("jobs.php"); |
| 118 |
exit; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
echo $_SESSION['header']; |
| 123 |
printHeaderContents(_("LDAP Account Manager Configuration"), '../..'); |
| 124 |
echo "</head><body>\n"; |
| 125 |
// include all JavaScript files |
| 126 |
printJsIncludes('../..'); |
| 127 |
printConfigurationPageHeaderBar($conf); |
| 128 |
|
| 129 |
// print error messages |
| 130 |
foreach ($errorsToDisplay as $errorToDisplay) { |
| 131 |
call_user_func_array(StatusMessage(...), $errorToDisplay); |
| 132 |
} |
| 133 |
|
| 134 |
echo "<form id=\"inputForm\" action=\"confmodules.php\" method=\"post\" onSubmit=\"window.lam.utility.saveScrollPosition('inputForm')\" novalidate=\"novalidate\">\n"; |
| 135 |
|
| 136 |
printConfigurationPageTabs(ConfigurationPageTab::MODULES); |
| 137 |
|
| 138 |
$typeManager = new TypeManager($conf); |
| 139 |
$types = $typeManager->getConfiguredTypes(); |
| 140 |
|
| 141 |
$container = new htmlResponsiveRow(); |
| 142 |
foreach ($types as $type) { |
| 143 |
config_showAccountModules($type, $container); |
| 144 |
} |
| 145 |
|
| 146 |
$legendContainer = new htmlGroup(); |
| 147 |
$legendContainer->addElement(new htmlOutputText("* " . _("Base module"))); |
| 148 |
$legendContainer->addElement(new htmlSpacer('2rem', null)); |
| 149 |
$legendContainer->addElement(new htmlHelpLink('237')); |
| 150 |
$container->add($legendContainer); |
| 151 |
$container->add(new htmlHiddenInput('postAvailable', 'yes')); |
| 152 |
|
| 153 |
parseHtml(null, $container, [], false, 'user'); |
| 154 |
|
| 155 |
echo "</div></div>"; |
| 156 |
|
| 157 |
$buttonContainer = new htmlTable(); |
| 158 |
$buttonContainer->addElement(new htmlSpacer(null, '10px'), true); |
| 159 |
$saveButton = new htmlButton('saveSettings', _('Save')); |
| 160 |
$saveButton->setCSSClasses(['lam-primary']); |
| 161 |
$buttonContainer->addElement($saveButton); |
| 162 |
$cancelButton = new htmlButton('cancelSettings', _('Cancel')); |
| 163 |
$buttonContainer->addElement($cancelButton, true); |
| 164 |
$buttonContainer->addElement(new htmlSpacer(null, '10px'), true); |
| 165 |
|
| 166 |
if (empty($errorsToDisplay) && isset($_POST['scrollPositionTop']) && get_preg($_POST['scrollPositionTop'], 'digit') |
| 167 |
&& isset($_POST['scrollPositionLeft']) && get_preg($_POST['scrollPositionLeft'], 'digit')) { |
| 168 |
// scroll to last position |
| 169 |
$buttonContainer->addElement(new htmlJavaScript('window.lam.utility.restoreScrollPosition(' . $_POST['scrollPositionTop'] . ', ' . $_POST['scrollPositionLeft'] . ')')); |
| 170 |
} |
| 171 |
|
| 172 |
parseHtml(null, $buttonContainer, [], false, 'user'); |
| 173 |
|
| 174 |
echo "</form>\n"; |
| 175 |
echo "</body>\n"; |
| 176 |
echo "</html>\n"; |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Displays the module selection boxes and checks if dependencies are fulfilled. |
| 181 |
* |
| 182 |
* @param ConfiguredType $type account type |
| 183 |
* @param htmlResponsiveRow $container meta HTML container |
| 184 |
*/ |
| 185 |
function config_showAccountModules($type, $container): void { |
| 186 |
// account modules |
| 187 |
$available = getAvailableModules($type->getScope(), true); |
| 188 |
$selected = $type->getModules(); |
| 189 |
$sortedAvailable = []; |
| 190 |
foreach ($available as $availableModule) { |
| 191 |
$sortedAvailable[$availableModule] = getModuleAlias($availableModule, $type->getScope()); |
| 192 |
} |
| 193 |
natcasesort($sortedAvailable); |
| 194 |
|
| 195 |
// build options for selected and available modules |
| 196 |
$selOptions = []; |
| 197 |
foreach ($selected as $selectedModule) { |
| 198 |
if (in_array($selectedModule, $available)) { // selected modules must be available |
| 199 |
if (is_base_module($selectedModule, $type->getScope())) { // mark base modules |
| 200 |
$selOptions[getModuleAlias($selectedModule, $type->getScope()) . " (" . $selectedModule . ")*"] = $selectedModule; |
| 201 |
} |
| 202 |
else { |
| 203 |
$selOptions[getModuleAlias($selectedModule, $type->getScope()) . " (" . $selectedModule . ")"] = $selectedModule; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
$availOptions = []; |
| 208 |
foreach ($sortedAvailable as $key => $value) { |
| 209 |
if (!in_array($key, $selected)) { // display non-selected modules |
| 210 |
if (is_base_module($key, $type->getScope())) { // mark base modules |
| 211 |
$availOptions[$value . " (" . $key . ")*"] = $key; |
| 212 |
} |
| 213 |
else { |
| 214 |
$availOptions[$value . " (" . $key . ")"] = $key; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// add account module selection |
| 220 |
$container->add(new htmlSubTitle($type->getAlias(), '../../graphics/' . $type->getIcon())); |
| 221 |
if ($selOptions !== []) { |
| 222 |
$container->add(new htmlOutputText(_("Selected modules")), 12, 6); |
| 223 |
} |
| 224 |
if ($availOptions !== []) { |
| 225 |
$container->add(new htmlOutputText(_("Available modules")), 0, 6); |
| 226 |
} |
| 227 |
$container->addVerticalSpacer('1rem'); |
| 228 |
// selected modules |
| 229 |
if ($selOptions !== []) { |
| 230 |
$listElements = []; |
| 231 |
foreach ($selOptions as $key => $value) { |
| 232 |
$el = new htmlTable('100%'); |
| 233 |
$mod = new $value($type->getScope()); |
| 234 |
$availModImage = new htmlImage('../../graphics/' . $mod->getIcon()); |
| 235 |
$availModImage->setCSSClasses(['size16', 'margin-right5-mobile-only']); |
| 236 |
$el->addElement($availModImage); |
| 237 |
$el->addElement(new htmlOutputText($key)); |
| 238 |
$delButton = new htmlButton('del_' . $type->getId() . '_' . $value, 'del.svg', true); |
| 239 |
$delButton->alignment = htmlElement::ALIGN_RIGHT; |
| 240 |
$el->addElement($delButton); |
| 241 |
$listElements[] = $el; |
| 242 |
} |
| 243 |
$selSortable = new htmlSortableList($listElements, $type->getId() . '_selected'); |
| 244 |
$selSortable->alignment = htmlElement::ALIGN_TOP; |
| 245 |
$selSortable->setCSSClasses(['module-list']); |
| 246 |
$selSortable->setOnUpdate('function() {updateModulePositions(\'positions_' . $type->getId() . '\', \'' . $type->getId() . '_selected' . '\');}'); |
| 247 |
$container->add($selSortable, 12, 6); |
| 248 |
} |
| 249 |
else { |
| 250 |
$container->add(new htmlOutputText(''), 12, 6); |
| 251 |
} |
| 252 |
// available modules |
| 253 |
if ($availOptions !== []) { |
| 254 |
$container->add(new htmlSpacer(null, '2rem'), 12, 0, 0, 'hide-on-tablet'); |
| 255 |
$container->add(new htmlOutputText(_("Available modules")), 12, 0, 0, 'hide-on-tablet'); |
| 256 |
$container->add(new htmlSpacer(null, '1rem'), 12, 0, 0, 'hide-on-tablet'); |
| 257 |
$availTable = new htmlTable(); |
| 258 |
foreach ($availOptions as $text => $key) { |
| 259 |
$mod = new $key($type->getScope()); |
| 260 |
$availModImage = new htmlImage('../../graphics/' . $mod->getIcon()); |
| 261 |
$availModImage->setCSSClasses(['size16', 'margin10']); |
| 262 |
$availTable->addElement($availModImage); |
| 263 |
$availTable->addElement(new htmlOutputText($text)); |
| 264 |
$addButton = new htmlButton('add_' . $type->getId() . '_' . $key, 'add.svg', true); |
| 265 |
$addButton->alignment = htmlElement::ALIGN_RIGHT; |
| 266 |
$availTable->addElement($addButton, true); |
| 267 |
} |
| 268 |
$availRow = new htmlResponsiveRow(); |
| 269 |
$availDiv = new htmlDiv(null, $availTable); |
| 270 |
$availDiv->alignment = htmlElement::ALIGN_TOP; |
| 271 |
$availDiv->setCSSClasses(['confModList']); |
| 272 |
$availRow->add($availDiv); |
| 273 |
if (count($availOptions) >= 10) { |
| 274 |
$availRow->addVerticalSpacer('1rem'); |
| 275 |
$filterGroup = new htmlGroup(); |
| 276 |
$filterGroup->addElement(new htmlOutputText(_('Filter'))); |
| 277 |
$filterInput = new htmlInputField('filter_' . $type->getId()); |
| 278 |
$filterInput->setOnInput('window.lam.config.updateModuleFilter(this); return false;'); |
| 279 |
$filterInput->setTransient(true); |
| 280 |
$filterGroup->addElement($filterInput); |
| 281 |
$availRow->add($filterGroup); |
| 282 |
} |
| 283 |
$container->add($availRow, 12, 6); |
| 284 |
} |
| 285 |
$positions = []; |
| 286 |
$selOptionsCount = count($selOptions); |
| 287 |
for ($i = 0; $i < $selOptionsCount; $i++) { |
| 288 |
$positions[] = $i; |
| 289 |
} |
| 290 |
$container->add(new htmlHiddenInput('positions_' . $type->getId(), implode(',', $positions))); |
| 291 |
// spacer to next account type |
| 292 |
$container->addVerticalSpacer('2rem'); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Checks user input and saves the entered settings. |
| 297 |
* |
| 298 |
* @param LAMConfig $conf config |
| 299 |
* @return array<int, string[]> list of errors |
| 300 |
*/ |
| 301 |
function checkModuleInput(LAMConfig $conf): array { |
| 302 |
if (!isset($_POST['postAvailable'])) { |
| 303 |
return []; |
| 304 |
} |
| 305 |
$errors = []; |
| 306 |
$typeSettings = $conf->get_typeSettings(); |
| 307 |
$typeManager = new TypeManager($conf); |
| 308 |
$accountTypes = $typeManager->getConfiguredTypes(); |
| 309 |
foreach ($accountTypes as $type) { |
| 310 |
$scope = $type->getScope(); |
| 311 |
$typeId = $type->getId(); |
| 312 |
$available = getAvailableModules($scope, true); |
| 313 |
$selected_temp = $typeSettings['modules_' . $typeId] ?? ''; |
| 314 |
$selected_temp = explode(',', $selected_temp); |
| 315 |
$selected = []; |
| 316 |
// only use available modules as selected |
| 317 |
foreach ($selected_temp as $selectedName) { |
| 318 |
if (in_array($selectedName, $available)) { |
| 319 |
$selected[] = $selectedName; |
| 320 |
} |
| 321 |
} |
| 322 |
// reorder based on sortable list |
| 323 |
$sorting = $_POST['positions_' . $typeId]; |
| 324 |
if (!empty($sorting)) { |
| 325 |
$sorting = explode(',', $sorting); |
| 326 |
$sortTmp = []; |
| 327 |
foreach ($sorting as $pos) { |
| 328 |
$sortTmp[] = $selected[intval($pos)]; |
| 329 |
} |
| 330 |
$selected = $sortTmp; |
| 331 |
} |
| 332 |
// remove modules from selection |
| 333 |
$new_selected = []; |
| 334 |
foreach ($selected as $selectedName) { |
| 335 |
if (!isset($_POST['del_' . $typeId . '_' . $selectedName])) { |
| 336 |
$new_selected[] = $selectedName; |
| 337 |
} |
| 338 |
} |
| 339 |
$selected = $new_selected; |
| 340 |
$typeSettings['modules_' . $typeId] = implode(',', $selected); |
| 341 |
// add modules to selection |
| 342 |
foreach ($available as $modName) { |
| 343 |
if (isset($_POST['add_' . $typeId . '_' . $modName])) { |
| 344 |
$selected[] = $modName; |
| 345 |
$typeSettings['modules_' . $typeId] = implode(',', $selected); |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
// check dependencies |
| 350 |
$depends = check_module_depends($selected, getModulesDependencies($scope)); |
| 351 |
if ($depends !== false) { |
| 352 |
foreach ($depends as $dependency) { |
| 353 |
$errors[] = ['ERROR', $type->getAlias(), _("Unsolved dependency:") . ' ' . |
| 354 |
$dependency[0] . " (" . $dependency[1] . ")"]; |
| 355 |
} |
| 356 |
} |
| 357 |
// check conflicts |
| 358 |
$conflicts = check_module_conflicts($selected, getModulesDependencies($scope)); |
| 359 |
if ($conflicts !== false) { |
| 360 |
foreach ($conflicts as $conflict) { |
| 361 |
$errors[] = ['ERROR', $type->getAlias(), _("Conflicting module:") . ' ' . |
| 362 |
$conflict[0] . " (" . $conflict[1] . ")"]; |
| 363 |
} |
| 364 |
} |
| 365 |
// check for base module |
| 366 |
$baseCount = 0; |
| 367 |
foreach ($selected as $selectedName) { |
| 368 |
if (is_base_module($selectedName, $scope)) { |
| 369 |
$baseCount++; |
| 370 |
} |
| 371 |
} |
| 372 |
if ($baseCount !== 1) { |
| 373 |
$errors[] = ['ERROR', $type->getAlias(), _("No or more than one base module selected!")]; |
| 374 |
} |
| 375 |
} |
| 376 |
$conf->set_typeSettings($typeSettings); |
| 377 |
|
| 378 |
return $errors; |
| 379 |
} |