| 1 |
<?php |
| 2 |
namespace LAM\TOOLS\MULTI_EDIT; |
| 3 |
use htmlProgressbar; |
| 4 |
use htmlTable; |
| 5 |
use htmlTitle; |
| 6 |
use htmlSelect; |
| 7 |
use htmlOutputText; |
| 8 |
use htmlInputField; |
| 9 |
use htmlSubTitle; |
| 10 |
use htmlButton; |
| 11 |
use htmlStatusMessage; |
| 12 |
use htmlSpacer; |
| 13 |
use htmlHiddenInput; |
| 14 |
use htmlGroup; |
| 15 |
use htmlDiv; |
| 16 |
use htmlJavaScript; |
| 17 |
use htmlLink; |
| 18 |
use htmlInputTextarea; |
| 19 |
use htmlResponsiveRow; |
| 20 |
use htmlResponsiveSelect; |
| 21 |
use htmlResponsiveInputField; |
| 22 |
use htmlResponsiveTable; |
| 23 |
use LAM\TOOLS\TREEVIEW\TreeViewTool; |
| 24 |
use LAM\TYPES\TypeManager; |
| 25 |
use LAMException; |
| 26 |
use LamTemporaryFilesManager; |
| 27 |
|
| 28 |
/* |
| 29 |
|
| 30 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 31 |
Copyright (C) 2013 - 2025 Roland Gruber |
| 32 |
|
| 33 |
This program is free software; you can redistribute it and/or modify |
| 34 |
it under the terms of the GNU General Public License as published by |
| 35 |
the Free Software Foundation; either version 2 of the License, or |
| 36 |
(at your option) any later version. |
| 37 |
|
| 38 |
This program is distributed in the hope that it will be useful, |
| 39 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 |
GNU General Public License for more details. |
| 42 |
|
| 43 |
You should have received a copy of the GNU General Public License |
| 44 |
along with this program; if not, write to the Free Software |
| 45 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 46 |
|
| 47 |
*/ |
| 48 |
|
| 49 |
/** |
| 50 |
* Multi edit tool that allows LDAP operations on multiple entries. |
| 51 |
* |
| 52 |
* @author Roland Gruber |
| 53 |
* @package tools |
| 54 |
*/ |
| 55 |
|
| 56 |
/** security functions */ |
| 57 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 58 |
/** access to configuration data */ |
| 59 |
include_once(__DIR__ . "/../../lib/config.inc"); |
| 60 |
/** access LDAP server */ |
| 61 |
include_once(__DIR__ . "/../../lib/ldap.inc"); |
| 62 |
/** used to print status messages */ |
| 63 |
include_once(__DIR__ . "/../../lib/status.inc"); |
| 64 |
/** multi edit functions */ |
| 65 |
include_once(__DIR__ . "/../../lib/multiEditTool.inc"); |
| 66 |
|
| 67 |
// start session |
| 68 |
startSecureSession(); |
| 69 |
enforceUserIsLoggedIn(); |
| 70 |
|
| 71 |
// die if no write access |
| 72 |
if (!checkIfWriteAccessIsAllowed()) { |
| 73 |
die(); |
| 74 |
} |
| 75 |
|
| 76 |
checkIfToolIsActive('toolMultiEdit'); |
| 77 |
|
| 78 |
setlanguage(); |
| 79 |
|
| 80 |
if (!empty($_POST)) { |
| 81 |
validateSecurityToken(); |
| 82 |
} |
| 83 |
|
| 84 |
define('ADD', 'add'); |
| 85 |
define('MOD', 'mod'); |
| 86 |
define('DEL', 'del'); |
| 87 |
|
| 88 |
define('STAGE_START', 'start'); |
| 89 |
define('STAGE_READ_FINISHED', 'readFinished'); |
| 90 |
define('STAGE_ACTIONS_CALCULATED', 'actionsCalculated'); |
| 91 |
define('STAGE_WRITING', 'writing'); |
| 92 |
define('STAGE_FINISHED', 'finished'); |
| 93 |
|
| 94 |
if (isset($_GET['ajaxStatus'])) { |
| 95 |
runAjaxActions(); |
| 96 |
} |
| 97 |
else { |
| 98 |
displayStartPage(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Displays the main page of the multi edit tool. |
| 103 |
*/ |
| 104 |
function displayStartPage(): void { |
| 105 |
// display main page |
| 106 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 107 |
echo '<div class="smallPaddingContent">'; |
| 108 |
echo "<form action=\"multiEdit.php\" method=\"post\">\n"; |
| 109 |
$errors = []; |
| 110 |
$container = new htmlResponsiveRow(); |
| 111 |
$container->add(new htmlTitle(_("Multi edit"))); |
| 112 |
// LDAP suffix |
| 113 |
$showRules = ['-' => ['otherSuffix']]; |
| 114 |
$hideRules = []; |
| 115 |
$typeManager = new TypeManager(); |
| 116 |
$types = $typeManager->getConfiguredTypes(); |
| 117 |
$suffixes = []; |
| 118 |
foreach ($types as $type) { |
| 119 |
if ($type->isHidden()) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
$suffixes[$type->getAlias()] = $type->getSuffix(); |
| 123 |
$hideRules[$type->getSuffix()] = ['otherSuffix']; |
| 124 |
} |
| 125 |
$treeSuffixes = TreeViewTool::getRootDns(); |
| 126 |
if (!empty($treeSuffixes)) { |
| 127 |
if (count($treeSuffixes) === 1) { |
| 128 |
$suffixes[_('Tree view')] = $treeSuffixes[0]; |
| 129 |
$hideRules[$treeSuffixes[0]] = ['otherSuffix']; |
| 130 |
} |
| 131 |
else { |
| 132 |
foreach ($treeSuffixes as $treeSuffix) { |
| 133 |
$suffixes[_('Tree view') . ' (' . getAbstractDN($treeSuffix) . ')'] = $treeSuffix; |
| 134 |
$hideRules[$treeSuffix] = ['otherSuffix']; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
$suffixes = array_flip($suffixes); |
| 139 |
natcasesort($suffixes); |
| 140 |
$suffixes = array_flip($suffixes); |
| 141 |
$suffixes[_('Other')] = '-'; |
| 142 |
$suffixValues = array_values($suffixes); |
| 143 |
$valSuffix = empty($_POST['suffix']) ? $suffixValues[0] : $_POST['suffix']; |
| 144 |
$suffixSelect = new htmlResponsiveSelect('suffix', $suffixes, [$valSuffix], _('LDAP suffix'), '700'); |
| 145 |
$suffixSelect->setHasDescriptiveElements(true); |
| 146 |
$suffixSelect->setSortElements(false); |
| 147 |
$suffixSelect->setTableRowsToShow($showRules); |
| 148 |
$suffixSelect->setTableRowsToHide($hideRules); |
| 149 |
$container->add($suffixSelect); |
| 150 |
$valOtherSuffix = empty($_POST['otherSuffix']) ? '' : (string) $_POST['otherSuffix']; |
| 151 |
$container->add(new htmlResponsiveInputField(_('Other'), 'otherSuffix', $valOtherSuffix)); |
| 152 |
// LDAP filter |
| 153 |
$valFilter = empty($_POST['filter']) ? '(objectClass=inetOrgPerson)' : (string) $_POST['filter']; |
| 154 |
$container->add(new htmlResponsiveInputField(_('LDAP filter'), 'filter', $valFilter, '701')); |
| 155 |
// operation fields |
| 156 |
$operationsTitle = new htmlSubTitle(_('Operations')); |
| 157 |
$operationsTitle->setHelpId('702'); |
| 158 |
$container->add($operationsTitle); |
| 159 |
$operationsTitles = [_('Type'), _('Attribute name'), _('Value')]; |
| 160 |
$data = []; |
| 161 |
$opCount = empty($_POST['opcount']) ? '3' : $_POST['opcount']; |
| 162 |
if (isset($_POST['addFields'])) { |
| 163 |
$opCount += 3; |
| 164 |
} |
| 165 |
$operations = [_('Add') => ADD, _('Modify') => MOD, _('Delete') => DEL]; |
| 166 |
for ($i = 0; $i < $opCount; $i++) { |
| 167 |
// operation type |
| 168 |
$selOp = empty($_POST['op_' . $i]) ? ADD : $_POST['op_' . $i]; |
| 169 |
$opSelect = new htmlSelect('op_' . $i, $operations, [$selOp]); |
| 170 |
$opSelect->setHasDescriptiveElements(true); |
| 171 |
$data[$i][] = $opSelect; |
| 172 |
// attribute name |
| 173 |
$attrVal = empty($_POST['attr_' . $i]) ? '' : (string) $_POST['attr_' . $i]; |
| 174 |
$data[$i][] = new htmlInputField('attr_' . $i, $attrVal); |
| 175 |
$valVal = empty($_POST['val_' . $i]) ? '' : (string) $_POST['val_' . $i]; |
| 176 |
$data[$i][] = new htmlInputField('val_' . $i, $valVal); |
| 177 |
// check input |
| 178 |
if (($selOp == ADD) && !empty($attrVal) && empty($valVal)) { |
| 179 |
$errors[] = new htmlStatusMessage('ERROR', _('Please enter a value to add.'), htmlspecialchars($attrVal)); |
| 180 |
} |
| 181 |
if (($selOp == MOD) && !empty($attrVal) && empty($valVal)) { |
| 182 |
$errors[] = new htmlStatusMessage('ERROR', _('Please enter a value to modify.'), htmlspecialchars($attrVal)); |
| 183 |
} |
| 184 |
} |
| 185 |
$operationsTable = new htmlResponsiveTable($operationsTitles, $data); |
| 186 |
$container->add($operationsTable); |
| 187 |
// add more fields |
| 188 |
$container->addVerticalSpacer('1rem'); |
| 189 |
$container->add(new htmlButton('addFields', _('Add more fields'))); |
| 190 |
$container->add(new htmlHiddenInput('opcount', $opCount)); |
| 191 |
// error messages |
| 192 |
if ($errors !== []) { |
| 193 |
$container->addVerticalSpacer('5rem'); |
| 194 |
foreach ($errors as $error) { |
| 195 |
$error->colspan = 5; |
| 196 |
$container->add($error); |
| 197 |
} |
| 198 |
} |
| 199 |
// action buttons |
| 200 |
$container->addVerticalSpacer('2rem'); |
| 201 |
$buttonGroup = new htmlGroup(); |
| 202 |
$buttonGroup->colspan = 3; |
| 203 |
$applyButton = new htmlButton('applyChanges', _('Apply changes')); |
| 204 |
$applyButton->setCSSClasses(['lam-primary']); |
| 205 |
$buttonGroup->addElement($applyButton); |
| 206 |
$buttonGroup->addElement(new htmlSpacer('10px', null)); |
| 207 |
$dryRunButton = new htmlButton('dryRun', _('Dry run')); |
| 208 |
$dryRunButton->setCSSClasses(['lam-secondary']); |
| 209 |
$buttonGroup->addElement($dryRunButton); |
| 210 |
$container->add($buttonGroup); |
| 211 |
$container->addVerticalSpacer('1rem'); |
| 212 |
|
| 213 |
// run actions |
| 214 |
if ((count($errors) === 0) && (isset($_POST['dryRun']) || isset($_POST['applyChanges']))) { |
| 215 |
runActions($container); |
| 216 |
} |
| 217 |
|
| 218 |
addSecurityTokenToMetaHTML($container); |
| 219 |
|
| 220 |
parseHtml(null, $container, [], false, 'user'); |
| 221 |
echo "</form>\n"; |
| 222 |
echo '</div>'; |
| 223 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Runs the dry run and change actions. |
| 228 |
* |
| 229 |
* @param htmlResponsiveRow $container container |
| 230 |
*/ |
| 231 |
function runActions(htmlResponsiveRow $container): void { |
| 232 |
// LDAP suffix |
| 233 |
$suffix = ($_POST['suffix'] === '-') ? trim($_POST['otherSuffix']) : $_POST['suffix']; |
| 234 |
if (empty($suffix)) { |
| 235 |
$error = new htmlStatusMessage('ERROR', _('LDAP Suffix is invalid!')); |
| 236 |
$error->colspan = 5; |
| 237 |
$container->add($error); |
| 238 |
return; |
| 239 |
} |
| 240 |
// LDAP filter |
| 241 |
$filter = trim($_POST['filter']); |
| 242 |
// operations |
| 243 |
$operations = []; |
| 244 |
for ($i = 0; $i < $_POST['opcount']; $i++) { |
| 245 |
if (!empty($_POST['attr_' . $i])) { |
| 246 |
$operations[] = [$_POST['op_' . $i], strtolower(trim($_POST['attr_' . $i])), trim($_POST['val_' . $i])]; |
| 247 |
} |
| 248 |
} |
| 249 |
if (count($operations) === 0) { |
| 250 |
$error = new htmlStatusMessage('ERROR', _('Please specify at least one operation.')); |
| 251 |
$error->colspan = 5; |
| 252 |
$container->add($error); |
| 253 |
return; |
| 254 |
} |
| 255 |
$_SESSION['multiEdit_suffix'] = $suffix; |
| 256 |
$_SESSION['multiEdit_filter'] = $filter; |
| 257 |
$_SESSION['multiEdit_operations'] = $operations; |
| 258 |
$_SESSION['multiEdit_status'] = ['stage' => STAGE_START]; |
| 259 |
$_SESSION['multiEdit_dryRun'] = isset($_POST['dryRun']); |
| 260 |
// progress area |
| 261 |
$container->add(new htmlSubTitle(_('Progress'))); |
| 262 |
$container->add(new htmlProgressbar('progressBar')); |
| 263 |
$progressDiv = new htmlDiv('progressArea', new htmlOutputText('')); |
| 264 |
$container->add($progressDiv); |
| 265 |
// JS block for AJAX status update |
| 266 |
$ajaxBlock = ' |
| 267 |
window.lam.multiedit.runActions(); |
| 268 |
'; |
| 269 |
$container->add(new htmlJavaScript($ajaxBlock)); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Performs the modify operations. |
| 274 |
*/ |
| 275 |
function runAjaxActions(): void { |
| 276 |
$jsonReturn = [ |
| 277 |
'status' => STAGE_START, |
| 278 |
'progress' => 0, |
| 279 |
'content' => '' |
| 280 |
]; |
| 281 |
switch ($_SESSION['multiEdit_status']['stage']) { |
| 282 |
case STAGE_START: |
| 283 |
$jsonReturn = readLDAPData(); |
| 284 |
break; |
| 285 |
case STAGE_READ_FINISHED: |
| 286 |
$jsonReturn = generateActions(); |
| 287 |
break; |
| 288 |
case STAGE_ACTIONS_CALCULATED: |
| 289 |
case STAGE_WRITING: |
| 290 |
$jsonReturn = $_SESSION['multiEdit_dryRun'] ? dryRun() : doModify(); |
| 291 |
break; |
| 292 |
} |
| 293 |
echo json_encode($jsonReturn, JSON_THROW_ON_ERROR); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Reads the LDAP entries from the directory. |
| 298 |
* |
| 299 |
* @return array<mixed> status |
| 300 |
*/ |
| 301 |
function readLDAPData(): array { |
| 302 |
$suffix = $_SESSION['multiEdit_suffix']; |
| 303 |
$filter = $_SESSION['multiEdit_filter']; |
| 304 |
if (empty($filter)) { |
| 305 |
$filter = '(objectClass=*)'; |
| 306 |
} |
| 307 |
$operations = $_SESSION['multiEdit_operations']; |
| 308 |
$attributes = []; |
| 309 |
foreach ($operations as $op) { |
| 310 |
if (!in_array(strtolower($op[1]), $attributes)) { |
| 311 |
$attributes[] = strtolower($op[1]); |
| 312 |
$attributes = array_merge($attributes, extractWildcards($op[2])); |
| 313 |
} |
| 314 |
} |
| 315 |
$attributes = array_values(array_unique($attributes)); |
| 316 |
// run LDAP query |
| 317 |
$results = searchLDAP($suffix, $filter, $attributes); |
| 318 |
// print error message if no data returned |
| 319 |
if (empty($results)) { |
| 320 |
$code = ldap_errno($_SESSION['ldap']->server()); |
| 321 |
if ($code !== 0) { |
| 322 |
$msg = new htmlStatusMessage('ERROR', _('Encountered an error while performing search.'), getDefaultLDAPErrorString($_SESSION['ldap']->server())); |
| 323 |
} |
| 324 |
else { |
| 325 |
$msg = new htmlStatusMessage('ERROR', _('No objects found!')); |
| 326 |
} |
| 327 |
$content = getMessageHTML($msg); |
| 328 |
return [ |
| 329 |
'status' => STAGE_FINISHED, |
| 330 |
'progress' => 100, |
| 331 |
'content' => $content |
| 332 |
]; |
| 333 |
} |
| 334 |
// save LDAP data |
| 335 |
$_SESSION['multiEdit_status']['entries'] = $results; |
| 336 |
$_SESSION['multiEdit_status']['stage'] = STAGE_READ_FINISHED; |
| 337 |
return [ |
| 338 |
'status' => STAGE_READ_FINISHED, |
| 339 |
'progress' => 10, |
| 340 |
'content' => '' |
| 341 |
]; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Generates the required actions based on the read LDAP data. |
| 346 |
* |
| 347 |
* @return array<mixed> status |
| 348 |
*/ |
| 349 |
function generateActions(): array { |
| 350 |
$actions = []; |
| 351 |
foreach ($_SESSION['multiEdit_status']['entries'] as $oldEntry) { |
| 352 |
$dn = $oldEntry['dn']; |
| 353 |
$newEntry = $oldEntry; |
| 354 |
foreach ($_SESSION['multiEdit_operations'] as $op) { |
| 355 |
$opType = $op[0]; |
| 356 |
$attr = $op[1]; |
| 357 |
$val = replaceWildcards($op[2], $oldEntry); |
| 358 |
switch ($opType) { |
| 359 |
case ADD: |
| 360 |
if (empty($oldEntry[$attr]) || !in_array_ignore_case($val, $oldEntry[$attr])) { |
| 361 |
$newEntry[$attr][] = $val; |
| 362 |
} |
| 363 |
break; |
| 364 |
case MOD: |
| 365 |
if (empty($oldEntry[$attr]) || !in_array_ignore_case($val, $oldEntry[$attr])) { |
| 366 |
// attribute not yet exists, add it |
| 367 |
$newEntry[$attr] = [$val]; |
| 368 |
} |
| 369 |
break; |
| 370 |
case DEL: |
| 371 |
if (empty($val) && !empty($oldEntry[$attr])) { |
| 372 |
unset($newEntry[$attr]); |
| 373 |
} |
| 374 |
elseif (!empty($val) && isset($oldEntry[$attr]) && in_array($val, $oldEntry[$attr])) { |
| 375 |
$newEntry[$attr] = array_delete([$val], $newEntry[$attr]); |
| 376 |
} |
| 377 |
break; |
| 378 |
} |
| 379 |
} |
| 380 |
unset($oldEntry['dn']); |
| 381 |
unset($newEntry['dn']); |
| 382 |
// cleanup |
| 383 |
foreach ($newEntry as $name => &$values) { |
| 384 |
// remove empty values |
| 385 |
$values = array_values($values); |
| 386 |
$count = count($values); |
| 387 |
for ($i = 0; $i < $count; $i++) { |
| 388 |
if ($values[$i] === '') { |
| 389 |
unset($values[$i]); |
| 390 |
} |
| 391 |
} |
| 392 |
$values = array_values($values); |
| 393 |
// remove empty list of values |
| 394 |
if (count($values) === 0) { |
| 395 |
unset($newEntry[$name]); |
| 396 |
} |
| 397 |
} |
| 398 |
// find deleted attributes (in $oldEntry but no longer in $newEntry) |
| 399 |
foreach ($oldEntry as $name => $value) { |
| 400 |
if (!isset($newEntry[$name])) { |
| 401 |
$actions[$dn][$name] = []; |
| 402 |
} |
| 403 |
} |
| 404 |
// find changed attributes |
| 405 |
foreach ($newEntry as $name => $value) { |
| 406 |
if (!isset($oldEntry[$name]) || !areArrayContentsEqual($value, $oldEntry[$name])) { |
| 407 |
$actions[$dn][$name] = $value; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
// save actions |
| 412 |
$_SESSION['multiEdit_status']['actions'] = $actions; |
| 413 |
$_SESSION['multiEdit_status']['stage'] = STAGE_ACTIONS_CALCULATED; |
| 414 |
return [ |
| 415 |
'status' => STAGE_ACTIONS_CALCULATED, |
| 416 |
'progress' => 20, |
| 417 |
'content' => '' |
| 418 |
]; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Prints the dryRun output. |
| 423 |
* |
| 424 |
* @return array<mixed> status |
| 425 |
* @throws LAMException error simulating actions |
| 426 |
*/ |
| 427 |
function dryRun(): array { |
| 428 |
$pro = isLAMProVersion() ? ' Pro' : ''; |
| 429 |
$ldif = '# LDAP Account Manager' . $pro . ' ' . LAMVersion() . "\n\nversion: 1\n\n"; |
| 430 |
$log = ''; |
| 431 |
// fill LDIF and log file |
| 432 |
foreach ($_SESSION['multiEdit_status']['actions'] as $dn => $changes) { |
| 433 |
$log .= $dn . "\r\n"; |
| 434 |
$ldif .= 'dn: ' . $dn . "\n"; |
| 435 |
$ldif .= 'changetype: modify' . "\n"; |
| 436 |
$isFirstChange = true; |
| 437 |
foreach ($changes as $attr => $values) { |
| 438 |
$log .= '* ' . $attr . '=' . implode(', ', $values) . "\r\n"; |
| 439 |
if (!$isFirstChange) { |
| 440 |
$ldif .= "-\n"; |
| 441 |
} |
| 442 |
$ldif .= 'replace: ' . $attr . "\n"; |
| 443 |
foreach ($values as $value) { |
| 444 |
$ldif .= $attr . ': ' . $value . "\n"; |
| 445 |
} |
| 446 |
$isFirstChange = false; |
| 447 |
} |
| 448 |
$ldif .= "\n"; |
| 449 |
$log .= "\r\n"; |
| 450 |
} |
| 451 |
// build meta HTML |
| 452 |
$container = new htmlTable(); |
| 453 |
$container->addElement(new htmlOutputText(_('Dry run finished.')), true); |
| 454 |
$container->addVerticalSpace('20px'); |
| 455 |
// store LDIF |
| 456 |
$tempFilesManager = new LamTemporaryFilesManager(); |
| 457 |
$fileName = $tempFilesManager->registerTemporaryFile('.ldif', 'ldif_'); |
| 458 |
$out = $tempFilesManager->openTemporaryFileForWrite($fileName); |
| 459 |
fwrite($out, $ldif); |
| 460 |
$container->addElement(new htmlOutputText(_('LDIF file')), true); |
| 461 |
$ldifLink = new htmlLink($fileName, $tempFilesManager->getDownloadLink($fileName)); |
| 462 |
$ldifLink->setTargetWindow('_blank'); |
| 463 |
$container->addElement($ldifLink, true); |
| 464 |
$container->addVerticalSpace('20px'); |
| 465 |
$container->addElement(new htmlOutputText(_('Log output')), true); |
| 466 |
$container->addElement(new htmlInputTextarea('log', $log, 100, 30), true); |
| 467 |
// generate HTML |
| 468 |
fclose ($out); |
| 469 |
ob_start(); |
| 470 |
parseHtml(null, $container, [], true, 'user'); |
| 471 |
$content = ob_get_contents(); |
| 472 |
ob_end_clean(); |
| 473 |
return [ |
| 474 |
'status' => STAGE_FINISHED, |
| 475 |
'progress' => 100, |
| 476 |
'content' => $content |
| 477 |
]; |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Error handler |
| 482 |
* |
| 483 |
* @param int $errno error number |
| 484 |
* @param string $errstr error message |
| 485 |
* @param string $errfile error file |
| 486 |
* @param int $errline error line |
| 487 |
* @return bool stop internal error handler |
| 488 |
*/ |
| 489 |
function multiEditLdapErrorHandler($errno, $errstr, $errfile, $errline): bool { |
| 490 |
if (($errno === E_USER_ERROR) || ($errno === E_ERROR)) { |
| 491 |
logNewMessage(LOG_ERR, 'Error occurred: ' . $errstr . " ($errfile: $errline)"); |
| 492 |
$_REQUEST['multiEdit_error'] = true; |
| 493 |
} |
| 494 |
elseif (($errno === E_USER_WARNING) || ($errno === E_WARNING)) { |
| 495 |
logNewMessage(LOG_WARNING, 'Error occurred: ' . $errstr . " ($errfile: $errline)"); |
| 496 |
$_REQUEST['multiEdit_error'] = true; |
| 497 |
} |
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Runs the actual modifications. |
| 503 |
* |
| 504 |
* @return array<mixed> status |
| 505 |
*/ |
| 506 |
function doModify(): array { |
| 507 |
set_error_handler(\LAM\TOOLS\MULTI_EDIT\multiEditLdapErrorHandler(...)); |
| 508 |
// initial action index |
| 509 |
if (!isset($_SESSION['multiEdit_status']['index'])) { |
| 510 |
$_SESSION['multiEdit_status']['index'] = 0; |
| 511 |
$_SESSION['multiEdit_status']['dnList'] = array_keys($_SESSION['multiEdit_status']['actions']); |
| 512 |
} |
| 513 |
// initial content |
| 514 |
if (!isset($_SESSION['multiEdit_status']['modContent'])) { |
| 515 |
$_SESSION['multiEdit_status']['modContent'] = ''; |
| 516 |
} |
| 517 |
// run 10 modifications in each call |
| 518 |
$localCount = 0; |
| 519 |
while (($localCount < 10) && ($_SESSION['multiEdit_status']['index'] < count($_SESSION['multiEdit_status']['dnList']))) { |
| 520 |
$dn = $_SESSION['multiEdit_status']['dnList'][$_SESSION['multiEdit_status']['index']]; |
| 521 |
$changes = $_SESSION['multiEdit_status']['actions'][$dn]; |
| 522 |
$_SESSION['multiEdit_status']['modContent'] .= htmlspecialchars($dn) . "<br>"; |
| 523 |
// run LDAP commands |
| 524 |
$success = ldap_modify($_SESSION['ldap']->server(), $dn, $changes); |
| 525 |
if (!$success || isset($_REQUEST['multiEdit_error'])) { |
| 526 |
$msg = new htmlStatusMessage('ERROR', getDefaultLDAPErrorString($_SESSION['ldap']->server())); |
| 527 |
$_SESSION['multiEdit_status']['modContent'] .= getMessageHTML($msg); |
| 528 |
} |
| 529 |
$localCount++; |
| 530 |
$_SESSION['multiEdit_status']['index']++; |
| 531 |
} |
| 532 |
// check if finished |
| 533 |
if ($_SESSION['multiEdit_status']['index'] == count($_SESSION['multiEdit_status']['actions'])) { |
| 534 |
$_SESSION['multiEdit_status']['modContent'] .= '<br><br>' . _('Finished all operations.'); |
| 535 |
return [ |
| 536 |
'status' => STAGE_FINISHED, |
| 537 |
'progress' => 100, |
| 538 |
'content' => $_SESSION['multiEdit_status']['modContent'] |
| 539 |
]; |
| 540 |
} |
| 541 |
// return current status |
| 542 |
return [ |
| 543 |
'status' => STAGE_WRITING, |
| 544 |
'progress' => 20 + (($_SESSION['multiEdit_status']['index'] / count($_SESSION['multiEdit_status']['actions'])) * 80), |
| 545 |
'content' => $_SESSION['multiEdit_status']['modContent'] |
| 546 |
]; |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* Returns the HTML code for a htmlStatusMessage |
| 551 |
* |
| 552 |
* @param htmlStatusMessage $msg message |
| 553 |
* @return string HTML code |
| 554 |
*/ |
| 555 |
function getMessageHTML(htmlStatusMessage $msg): string { |
| 556 |
ob_start(); |
| 557 |
parseHtml(null, $msg, [], true, 'user'); |
| 558 |
$content = ob_get_contents(); |
| 559 |
ob_end_clean(); |
| 560 |
if ($content === false) { |
| 561 |
return ''; |
| 562 |
} |
| 563 |
return $content; |
| 564 |
} |