| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\TOOLS\OU_EDIT; |
| 4 |
|
| 5 |
use \htmlSpacer; |
| 6 |
use \htmlOutputText; |
| 7 |
use \htmlButton; |
| 8 |
use \htmlHiddenInput; |
| 9 |
use \htmlTitle; |
| 10 |
use \htmlSubTitle; |
| 11 |
use \htmlStatusMessage; |
| 12 |
use \htmlResponsiveRow; |
| 13 |
use \htmlResponsiveSelect; |
| 14 |
use \htmlResponsiveInputField; |
| 15 |
use \htmlGroup; |
| 16 |
use LAM\TYPES\TypeManager; |
| 17 |
|
| 18 |
/* |
| 19 |
|
| 20 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 21 |
Copyright (C) 2003 - 2025 Roland Gruber |
| 22 |
|
| 23 |
This program is free software; you can redistribute it and/or modify |
| 24 |
it under the terms of the GNU General Public License as published by |
| 25 |
the Free Software Foundation; either version 2 of the License, or |
| 26 |
(at your option) any later version. |
| 27 |
|
| 28 |
This program is distributed in the hope that it will be useful, |
| 29 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 |
GNU General Public License for more details. |
| 32 |
|
| 33 |
You should have received a copy of the GNU General Public License |
| 34 |
along with this program; if not, write to the Free Software |
| 35 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 36 |
|
| 37 |
*/ |
| 38 |
|
| 39 |
/** |
| 40 |
* This is an editor for organizational units. |
| 41 |
* |
| 42 |
* @author Roland Gruber |
| 43 |
* @package tools |
| 44 |
*/ |
| 45 |
|
| 46 |
/** security functions */ |
| 47 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 48 |
/** access to configuration data */ |
| 49 |
include_once(__DIR__ . "/../../lib/config.inc"); |
| 50 |
/** access LDAP server */ |
| 51 |
include_once(__DIR__ . "/../../lib/ldap.inc"); |
| 52 |
/** used to print status messages */ |
| 53 |
include_once(__DIR__ . "/../../lib/status.inc"); |
| 54 |
|
| 55 |
// start session |
| 56 |
startSecureSession(); |
| 57 |
enforceUserIsLoggedIn(); |
| 58 |
|
| 59 |
// die if no write access |
| 60 |
if (!checkIfWriteAccessIsAllowed()) { |
| 61 |
die(); |
| 62 |
} |
| 63 |
|
| 64 |
checkIfToolIsActive('toolOUEditor'); |
| 65 |
|
| 66 |
setlanguage(); |
| 67 |
|
| 68 |
if (!empty($_POST)) { |
| 69 |
validateSecurityToken(); |
| 70 |
} |
| 71 |
|
| 72 |
$error = null; |
| 73 |
$message = null; |
| 74 |
|
| 75 |
$optionsToDelete = []; |
| 76 |
$optionsToInsert = []; |
| 77 |
|
| 78 |
/** |
| 79 |
* Refreshes the possible OUs. |
| 80 |
* |
| 81 |
* @param array<string, array<string, string>> $optionsToInsert OUs that can be used for adding |
| 82 |
* @param array<string, array<string, string>> $optionsToDelete OUs that can be deleted |
| 83 |
*/ |
| 84 |
function refreshOus(array &$optionsToInsert, array &$optionsToDelete): void { |
| 85 |
$typeManager = new TypeManager(); |
| 86 |
$typeList = $typeManager->getConfiguredTypes(); |
| 87 |
$types = []; |
| 88 |
foreach ($typeList as $type) { |
| 89 |
if ($type->isHidden() || !checkIfWriteAccessIsAllowed($type->getId())) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
$types[$type->getId()] = $type->getAlias(); |
| 93 |
} |
| 94 |
natcasesort($types); |
| 95 |
foreach ($types as $typeId => $title) { |
| 96 |
$type = $typeManager->getConfiguredType($typeId); |
| 97 |
if ($type === null) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
$elements = []; |
| 101 |
$units = searchLDAP($type->getSuffix(), '(|(objectclass=organizationalunit)(objectclass=organization))', ['dn']); |
| 102 |
foreach ($units as $unit) { |
| 103 |
if (is_string($unit['dn'])) { |
| 104 |
$elements[getAbstractDN($unit['dn'])] = $unit['dn']; |
| 105 |
} |
| 106 |
} |
| 107 |
if (!empty($elements)) { |
| 108 |
$optionsToDelete[$title] = $elements; |
| 109 |
uasort($optionsToDelete[$title], compareDn(...)); |
| 110 |
} |
| 111 |
$optionsToInsert[$title] = $elements; |
| 112 |
if (empty($optionsToInsert[$title])) { |
| 113 |
$optionsToInsert[$title] = [getAbstractDN($type->getSuffix()) => $type->getSuffix()]; |
| 114 |
} |
| 115 |
uasort($optionsToInsert[$title], compareDn(...)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
refreshOus($optionsToInsert, $optionsToDelete); |
| 120 |
|
| 121 |
// check if deletion was canceled |
| 122 |
if (isset($_POST['abort'])) { |
| 123 |
display_main(null, null, $optionsToInsert, $optionsToDelete); |
| 124 |
exit; |
| 125 |
} |
| 126 |
|
| 127 |
// check if the submit button was pressed |
| 128 |
if (isset($_POST['createOU']) || isset($_POST['deleteOU'])) { |
| 129 |
$validDeletableDns = flattenArray($optionsToDelete); |
| 130 |
// new ou |
| 131 |
if (isset($_POST['createOU'])) { |
| 132 |
// create ou if valid |
| 133 |
$validParentDns = flattenArray($optionsToInsert); |
| 134 |
if (preg_match("/^[a-z0-9 _\\-]+$/i", $_POST['newOU']) && in_array_ignore_case($_POST['parentOU'], $validParentDns)) { |
| 135 |
// check if ou already exists |
| 136 |
$new_dn = "ou=" . ldap_escape($_POST['newOU'], '', LDAP_ESCAPE_DN) . "," . $_POST['parentOU']; |
| 137 |
$found = ldapGetDN($new_dn); |
| 138 |
if ($found === null) { |
| 139 |
// add new ou |
| 140 |
$ou = []; |
| 141 |
$ou['objectClass'] = "organizationalunit"; |
| 142 |
$ou['ou'] = $_POST['newOU']; |
| 143 |
$ret = @ldap_add($_SESSION['ldap']->server(), $new_dn, $ou); |
| 144 |
if ($ret) { |
| 145 |
$message = _("New OU created successfully."); |
| 146 |
refreshOus($optionsToInsert, $optionsToDelete); |
| 147 |
} |
| 148 |
else { |
| 149 |
$error = _("Unable to create new OU!"); |
| 150 |
} |
| 151 |
} |
| 152 |
else { |
| 153 |
$error = _("OU already exists!"); |
| 154 |
} |
| 155 |
} |
| 156 |
// show errormessage if ou is invalid |
| 157 |
else { |
| 158 |
$error = _("OU is invalid!") . "<br>" . htmlspecialchars($_POST['newOU']); |
| 159 |
} |
| 160 |
} |
| 161 |
// delete ou, user was sure |
| 162 |
elseif (isset($_POST['deleteOU']) && isset($_POST['sure']) && in_array_ignore_case($_POST['deletename'], $validDeletableDns)) { |
| 163 |
$ret = ldap_delete($_SESSION['ldap']->server(), $_POST['deletename']); |
| 164 |
if ($ret) { |
| 165 |
$message = _("OU deleted successfully."); |
| 166 |
refreshOus($optionsToInsert, $optionsToDelete); |
| 167 |
} |
| 168 |
else { |
| 169 |
$error = _("Unable to delete OU!"); |
| 170 |
} |
| 171 |
} |
| 172 |
// ask if the user is sure to delete |
| 173 |
elseif (isset($_POST['deleteOU']) && in_array_ignore_case($_POST['deleteableOU'], $validDeletableDns)) { |
| 174 |
// check for subentries |
| 175 |
$sr = ldap_list($_SESSION['ldap']->server(), $_POST['deleteableOU'], "(objectClass=*)", [""]); |
| 176 |
if ($sr === false) { |
| 177 |
$error = _("OU is not empty or invalid!"); |
| 178 |
} |
| 179 |
else { |
| 180 |
$info = ldap_get_entries($_SESSION['ldap']->server(), $sr); |
| 181 |
if (($info !== false) && ($info['count'] === 0)) { |
| 182 |
// print header |
| 183 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 184 |
echo '<div class="smallPaddingContent">'; |
| 185 |
echo "<form action=\"ou_edit.php\" method=\"post\">\n"; |
| 186 |
$container = new htmlResponsiveRow(); |
| 187 |
$label = new htmlOutputText(_("Do you really want to delete this OU?")); |
| 188 |
$label->colspan = 5; |
| 189 |
$container->add($label, 12); |
| 190 |
$container->addVerticalSpacer('1rem'); |
| 191 |
$dnLabel = new htmlOutputText(getAbstractDN($_POST['deleteableOU'])); |
| 192 |
$dnLabel->colspan = 5; |
| 193 |
$container->add($dnLabel, 12); |
| 194 |
$container->addVerticalSpacer('1rem'); |
| 195 |
$buttonGroup = new htmlGroup(); |
| 196 |
$deleteButton = new htmlButton('sure', _("Delete")); |
| 197 |
$deleteButton->setCSSClasses(['lam-danger']); |
| 198 |
$buttonGroup->addElement($deleteButton); |
| 199 |
$buttonGroup->addElement(new htmlSpacer('0.5rem', null)); |
| 200 |
$buttonGroup->addElement(new htmlButton('abort', _("Cancel"))); |
| 201 |
$container->add($buttonGroup, 12); |
| 202 |
$container->add(new htmlHiddenInput('deleteOU', 'submit'), 12); |
| 203 |
$container->add(new htmlHiddenInput('deletename', $_POST['deleteableOU']), 12); |
| 204 |
addSecurityTokenToMetaHTML($container); |
| 205 |
parseHtml(null, $container, [], false, 'user'); |
| 206 |
echo "</form>"; |
| 207 |
echo '</div>'; |
| 208 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 209 |
exit(); |
| 210 |
} |
| 211 |
else { |
| 212 |
$error = _("OU is not empty or invalid!"); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
display_main($message, $error, $optionsToInsert, $optionsToDelete); |
| 219 |
|
| 220 |
/** |
| 221 |
* Displays the main page of the OU editor |
| 222 |
* |
| 223 |
* @param string|null $message info message |
| 224 |
* @param string|null $error error message |
| 225 |
* @param array<string, array<string, string>> $optionsToInsert options where new OU can be inserted |
| 226 |
* @param array<string, array<string, string>> $optionsToDelete OUs that can be deleted |
| 227 |
*/ |
| 228 |
function display_main(?string $message, ?string $error, array $optionsToInsert, array $optionsToDelete): void { |
| 229 |
// display main page |
| 230 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 231 |
echo '<div class="smallPaddingContent">'; |
| 232 |
echo "<form action=\"ou_edit.php\" method=\"post\">\n"; |
| 233 |
|
| 234 |
$container = new htmlResponsiveRow(); |
| 235 |
$container->add(new htmlTitle(_("OU editor")), 12); |
| 236 |
if ($error !== null) { |
| 237 |
$msg = new htmlStatusMessage("ERROR", "", $error); |
| 238 |
$msg->colspan = 5; |
| 239 |
$container->add($msg, 12); |
| 240 |
} |
| 241 |
elseif ($message !== null) { |
| 242 |
$msg = new htmlStatusMessage("INFO", "", $message); |
| 243 |
$msg->colspan = 5; |
| 244 |
$container->add($msg, 12); |
| 245 |
} |
| 246 |
|
| 247 |
if (!empty($optionsToInsert)) { |
| 248 |
// new OU |
| 249 |
$container->add(new htmlSubTitle(_("New organisational unit"))); |
| 250 |
$parentOUSelect = new htmlResponsiveSelect('parentOU', $optionsToInsert, [], _('Parent DN'), '601'); |
| 251 |
$parentOUSelect->setContainsOptgroups(true); |
| 252 |
$parentOUSelect->setHasDescriptiveElements(true); |
| 253 |
$parentOUSelect->setRightToLeftTextDirection(true); |
| 254 |
$parentOUSelect->setSortElements(false); |
| 255 |
$container->add($parentOUSelect); |
| 256 |
$container->add(new htmlResponsiveInputField(_('Name'), 'newOU')); |
| 257 |
$container->addLabel(new htmlOutputText(' ', false)); |
| 258 |
$container->addField(new htmlButton('createOU', _("Ok"))); |
| 259 |
$container->addVerticalSpacer('2rem'); |
| 260 |
} |
| 261 |
if (!empty($optionsToDelete)) { |
| 262 |
// delete OU |
| 263 |
$container->add(new htmlSubTitle(_("Delete organisational unit"))); |
| 264 |
$deleteableOUSelect = new htmlResponsiveSelect('deleteableOU', $optionsToDelete, [], _('Organisational unit'), '602'); |
| 265 |
$deleteableOUSelect->setContainsOptgroups(true); |
| 266 |
$deleteableOUSelect->setHasDescriptiveElements(true); |
| 267 |
$deleteableOUSelect->setRightToLeftTextDirection(true); |
| 268 |
$deleteableOUSelect->setSortElements(false); |
| 269 |
$container->add($deleteableOUSelect); |
| 270 |
$container->addLabel(new htmlOutputText(' ', false)); |
| 271 |
$container->addField(new htmlButton('deleteOU', _("Ok"))); |
| 272 |
} |
| 273 |
|
| 274 |
addSecurityTokenToMetaHTML($container); |
| 275 |
parseHtml(null, $container, [], false, 'user'); |
| 276 |
echo "</form>\n"; |
| 277 |
echo '</div>'; |
| 278 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 279 |
} |