| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 48 |
|
| 49 |
include_once(__DIR__ . "/../../lib/config.inc"); |
| 50 |
|
| 51 |
include_once(__DIR__ . "/../../lib/ldap.inc"); |
| 52 |
|
| 53 |
include_once(__DIR__ . "/../../lib/status.inc"); |
| 54 |
|
| 55 |
|
| 56 |
startSecureSession(); |
| 57 |
enforceUserIsLoggedIn(); |
| 58 |
|
| 59 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 122 |
if (isset($_POST['abort'])) { |
| 123 |
display_main(null, null, $optionsToInsert, $optionsToDelete); |
| 124 |
exit; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if (isset($_POST['createOU']) || isset($_POST['deleteOU'])) { |
| 129 |
$validDeletableDns = flattenArray($optionsToDelete); |
| 130 |
|
| 131 |
if (isset($_POST['createOU'])) { |
| 132 |
|
| 133 |
$validParentDns = flattenArray($optionsToInsert); |
| 134 |
if (preg_match("/^[a-z0-9 _\\-]+$/i", $_POST['newOU']) && in_array_ignore_case($_POST['parentOU'], $validParentDns)) { |
| 135 |
|
| 136 |
$new_dn = "ou=" . ldap_escape($_POST['newOU'], '', LDAP_ESCAPE_DN) . "," . $_POST['parentOU']; |
| 137 |
$found = ldapGetDN($new_dn); |
| 138 |
if ($found === null) { |
| 139 |
|
| 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 |
|
| 157 |
else { |
| 158 |
$error = _("OU is invalid!") . "<br>" . htmlspecialchars($_POST['newOU']); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 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 |
|
| 173 |
elseif (isset($_POST['deleteOU']) && in_array_ignore_case($_POST['deleteableOU'], $validDeletableDns)) { |
| 174 |
|
| 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 |
|
| 183 |
include_once __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); |
| 206 |
echo "</form>"; |
| 207 |
echo '</div>'; |
| 208 |
include_once __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 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
function display_main(?string $message, ?string $error, array $optionsToInsert, array $optionsToDelete): void { |
| 229 |
|
| 230 |
include_once __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 |
|
| 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 |
|
| 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); |
| 276 |
echo "</form>\n"; |
| 277 |
echo '</div>'; |
| 278 |
include_once __DIR__ . '/../../lib/adminFooter.inc'; |
| 279 |
} |