| 1 |
<?php |
| 2 |
namespace LAM\TOOLS\PROFILE_EDITOR; |
| 3 |
|
| 4 |
use htmlResponsiveRow; |
| 5 |
use htmlTitle; |
| 6 |
use htmlResponsiveInputField; |
| 7 |
use htmlResponsiveSelect; |
| 8 |
use htmlButton; |
| 9 |
use htmlHiddenInput; |
| 10 |
use htmlSubTitle; |
| 11 |
use LAM\PROFILES\AccountProfilePersistenceManager; |
| 12 |
use LAM\TYPES\TypeManager; |
| 13 |
use LAMException; |
| 14 |
|
| 15 |
/* |
| 16 |
|
| 17 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 18 |
Copyright (C) 2003 - 2025 Roland Gruber |
| 19 |
|
| 20 |
This program is free software; you can redistribute it and/or modify |
| 21 |
it under the terms of the GNU General Public License as published by |
| 22 |
the Free Software Foundation; either version 2 of the License, or |
| 23 |
(at your option) any later version. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, |
| 26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
GNU General Public License for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU General Public License |
| 31 |
along with this program; if not, write to the Free Software |
| 32 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 33 |
|
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* Manages creating/changing of profiles. |
| 38 |
* |
| 39 |
* @package profiles |
| 40 |
* @author Roland Gruber |
| 41 |
*/ |
| 42 |
|
| 43 |
/** security functions */ |
| 44 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 45 |
/** helper functions for profiles */ |
| 46 |
include_once(__DIR__ . "/../../lib/profiles.inc"); |
| 47 |
/** access to LDAP server */ |
| 48 |
include_once(__DIR__ . "/../../lib/ldap.inc"); |
| 49 |
/** access to configuration options */ |
| 50 |
include_once(__DIR__ . "/../../lib/config.inc"); |
| 51 |
/** access to account modules */ |
| 52 |
include_once(__DIR__ . "/../../lib/modules.inc"); |
| 53 |
/** Used to display status messages */ |
| 54 |
include_once(__DIR__ . "/../../lib/status.inc"); |
| 55 |
|
| 56 |
// start session |
| 57 |
startSecureSession(); |
| 58 |
enforceUserIsLoggedIn(); |
| 59 |
|
| 60 |
// die if no write access |
| 61 |
if (!checkIfWriteAccessIsAllowed()) { |
| 62 |
die(); |
| 63 |
} |
| 64 |
|
| 65 |
checkIfToolIsActive('toolProfileEditor'); |
| 66 |
|
| 67 |
setlanguage(); |
| 68 |
|
| 69 |
if (!empty($_POST)) { |
| 70 |
validateSecurityToken(); |
| 71 |
} |
| 72 |
|
| 73 |
// check if user is logged in, if not go to login |
| 74 |
if (!$_SESSION['ldap'] || !$_SESSION['ldap']->server()) { |
| 75 |
metaRefresh("../login.php"); |
| 76 |
exit; |
| 77 |
} |
| 78 |
|
| 79 |
// copy type and profile name from POST to GET |
| 80 |
if (isset($_POST['profname'])) { |
| 81 |
$_GET['edit'] = $_POST['profname']; |
| 82 |
} |
| 83 |
if (isset($_POST['accounttype'])) { |
| 84 |
$_GET['type'] = $_POST['accounttype']; |
| 85 |
} |
| 86 |
|
| 87 |
$typeManager = new TypeManager(); |
| 88 |
$type = $typeManager->getConfiguredType($_GET['type']); |
| 89 |
if (($type === null) || $type->isHidden() || !checkIfWriteAccessIsAllowed($_GET['type'])) { |
| 90 |
logNewMessage(LOG_ERR, 'User tried to access hidden account type profile: ' . $_GET['type']); |
| 91 |
die(); |
| 92 |
} |
| 93 |
|
| 94 |
// abort button was pressed |
| 95 |
// back to profile editor |
| 96 |
if (isset($_POST['abort'])) { |
| 97 |
metaRefresh("profilemain.php"); |
| 98 |
exit; |
| 99 |
} |
| 100 |
|
| 101 |
$accountProfilePersistenceManager = new AccountProfilePersistenceManager(); |
| 102 |
|
| 103 |
$errors = []; |
| 104 |
|
| 105 |
// save button was pressed |
| 106 |
if (isset($_POST['save'])) { |
| 107 |
// create option array to check and save |
| 108 |
$options = []; |
| 109 |
$opt_keys = array_keys($_SESSION['profile_types']); |
| 110 |
foreach ($opt_keys as $element) { |
| 111 |
// text fields |
| 112 |
if ($_SESSION['profile_types'][$element] == "text") { |
| 113 |
$options[$element] = [$_POST[$element]]; |
| 114 |
} |
| 115 |
// checkboxes |
| 116 |
elseif ($_SESSION['profile_types'][$element] == "checkbox") { |
| 117 |
$options[$element] = (isset($_POST[$element]) && ($_POST[$element] == "on")) ? ['true'] : ['false']; |
| 118 |
} |
| 119 |
// dropdownbox |
| 120 |
elseif ($_SESSION['profile_types'][$element] == "select") { |
| 121 |
$options[$element] = [$_POST[$element]]; |
| 122 |
} |
| 123 |
// multiselect |
| 124 |
elseif ($_SESSION['profile_types'][$element] == "multiselect") { |
| 125 |
$options[$element] = $_POST[$element] ?? []; |
| 126 |
} |
| 127 |
// textareas |
| 128 |
if ($_SESSION['profile_types'][$element] == "textarea") { |
| 129 |
$options[$element] = explode("\r\n", $_POST[$element]); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// check options |
| 134 |
$errors = checkProfileOptions($_POST['accounttype'], $options); |
| 135 |
if (count($errors) === 0) { // input data is valid, save profile |
| 136 |
// save profile |
| 137 |
try { |
| 138 |
$accountProfilePersistenceManager->writeAccountProfile($_POST['accounttype'], $_POST['profname'], $_SESSION['config']->getName(), $options); |
| 139 |
metaRefresh('profilemain.php?savedSuccessfully=' . $_POST['profname']); |
| 140 |
exit(); |
| 141 |
} |
| 142 |
catch (LAMException $e) { |
| 143 |
logNewMessage(LOG_ERR, $e->getTitle()); |
| 144 |
$errors[] = ["ERROR", _("Unable to save profile!")]; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// print header |
| 150 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 151 |
echo '<div class="smallPaddingContent">'; |
| 152 |
|
| 153 |
// print error messages if any |
| 154 |
if (count($errors) > 0) { |
| 155 |
echo "<br>\n"; |
| 156 |
foreach ($errors as $error) { |
| 157 |
call_user_func_array(StatusMessage(...), $error); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// empty list of attribute types |
| 162 |
$_SESSION['profile_types'] = []; |
| 163 |
|
| 164 |
// get module options |
| 165 |
$options = getProfileOptions($type->getId()); |
| 166 |
|
| 167 |
// load old profile or POST values if needed |
| 168 |
$old_options = []; |
| 169 |
if (isset($_POST['save'])) { |
| 170 |
foreach ($_POST as $key => $value) { |
| 171 |
$old_options[$key] = is_array($value) ? $value : [$value]; |
| 172 |
} |
| 173 |
} |
| 174 |
elseif (isset($_GET['edit'])) { |
| 175 |
try { |
| 176 |
$old_options = $accountProfilePersistenceManager->loadAccountProfile($type->getId(), $_GET['edit'], $_SESSION['config']->getName()); |
| 177 |
} |
| 178 |
catch (LAMException $e) { |
| 179 |
StatusMessage('ERROR', $e->getTitle(), $e->getMessage()); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// display formular |
| 184 |
echo "<form id=\"profilepage\" action=\"profilepage.php?type=" . $type->getId() . "\" method=\"post\">\n"; |
| 185 |
echo '<input type="hidden" name="' . getSecurityTokenName() . '" value="' . getSecurityTokenValue() . '">'; |
| 186 |
|
| 187 |
$profName = ''; |
| 188 |
if (isset($_GET['edit'])) { |
| 189 |
$profName = (string) $_GET['edit']; |
| 190 |
} |
| 191 |
|
| 192 |
$container = new htmlResponsiveRow(); |
| 193 |
$container->add(new htmlTitle(_("Profile editor"))); |
| 194 |
|
| 195 |
// general options |
| 196 |
$container->add(new htmlSubTitle(_("General settings"), '../../graphics/logo.svg', null, true)); |
| 197 |
$profileNameField = new htmlResponsiveInputField(_("Profile name"), 'profname', $profName, '360', true); |
| 198 |
$profileNameField->setTransient(true); |
| 199 |
$container->add($profileNameField); |
| 200 |
$container->addVerticalSpacer('1rem'); |
| 201 |
// suffix box |
| 202 |
// get root suffix |
| 203 |
$rootsuffix = $type->getSuffix(); |
| 204 |
// get subsuffixes |
| 205 |
$suffixes = ['-' => '-']; |
| 206 |
$possibleSuffixes = $type->getSuffixList(); |
| 207 |
foreach ($possibleSuffixes as $suffix) { |
| 208 |
$suffixes[getAbstractDN($suffix)] = $suffix; |
| 209 |
} |
| 210 |
$selectedSuffix = []; |
| 211 |
if (isset($old_options['ldap_suffix'][0])) { |
| 212 |
$selectedSuffix[] = $old_options['ldap_suffix'][0]; |
| 213 |
} |
| 214 |
$suffixSelect = new htmlResponsiveSelect('ldap_suffix', $suffixes, $selectedSuffix, _("LDAP suffix"), '361'); |
| 215 |
$suffixSelect->setHasDescriptiveElements(true); |
| 216 |
$suffixSelect->setSortElements(false); |
| 217 |
$suffixSelect->setRightToLeftTextDirection(true); |
| 218 |
$container->add($suffixSelect); |
| 219 |
// RDNs |
| 220 |
$rdns = getRDNAttributes($type->getId()); |
| 221 |
$selectedRDN = []; |
| 222 |
if (isset($old_options['ldap_rdn'][0])) { |
| 223 |
$selectedRDN[] = $old_options['ldap_rdn'][0]; |
| 224 |
} |
| 225 |
$rdnSelect = new htmlResponsiveSelect('ldap_rdn', $rdns, $selectedRDN, _("RDN identifier"), '301'); |
| 226 |
$rdnSelect->setSortElements(false); |
| 227 |
$container->add($rdnSelect); |
| 228 |
|
| 229 |
$container->addVerticalSpacer('2rem'); |
| 230 |
|
| 231 |
$_SESSION['profile_types'] = parseHtml(null, $container, $old_options, false, $type->getScope()); |
| 232 |
|
| 233 |
// display module options |
| 234 |
foreach ($options as $moduleName => $moduleOptions) { |
| 235 |
$module = new $moduleName($type->getScope()); |
| 236 |
$icon = $module->getIcon(); |
| 237 |
if (!empty($icon) && !(str_starts_with($icon, 'http')) && !(str_starts_with($icon, '/'))) { |
| 238 |
$icon = '../../graphics/' . $icon; |
| 239 |
} |
| 240 |
$modContainer = new htmlResponsiveRow(); |
| 241 |
$modContainer->add(new htmlSubTitle($module->get_alias(), $icon, null, true)); |
| 242 |
$modContainer->add($moduleOptions); |
| 243 |
$modContainer->addVerticalSpacer('2rem'); |
| 244 |
$_SESSION['profile_types'] = array_merge($_SESSION['profile_types'], parseHtml($moduleName, $modContainer, $old_options, false, $type->getScope())); |
| 245 |
} |
| 246 |
|
| 247 |
// profile name and submit/abort buttons |
| 248 |
$buttonTable = new htmlResponsiveRow(); |
| 249 |
$saveButton = new htmlButton('save', _('Save')); |
| 250 |
$saveButton->setCSSClasses(['lam-primary fullwidth-mobile-only']); |
| 251 |
$buttonTable->addLabel($saveButton); |
| 252 |
$cancelButton = new htmlButton('abort', _('Cancel')); |
| 253 |
$cancelButton->setCSSClasses(['fullwidth-mobile-only']); |
| 254 |
$cancelButton->disableFormValidation(); |
| 255 |
$buttonTable->addField($cancelButton); |
| 256 |
$buttonTable->add(new htmlHiddenInput('accounttype', $type->getId()), 0); |
| 257 |
|
| 258 |
$_SESSION['profile_types'] = array_merge($_SESSION['profile_types'], parseHtml(null, $buttonTable, $old_options, false, $type->getScope())); |
| 259 |
|
| 260 |
?> |
| 261 |
</form> |
| 262 |
</div> |
| 263 |
<?php |
| 264 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 265 |
|
| 266 |
?> |