Debian Codegraph @

templates/delete.php

raw
1 <?php
2 namespace LAM\DELETE;
3 use accountContainer;
4 use DateTime;
5 use htmlGroup;
6 use htmlJavaScript;
7 use htmlResponsiveRow;
8 use htmlButton;
9 use htmlSpacer;
10 use htmlHiddenInput;
11 use htmlOutputText;
12 use htmlStatusMessage;
13 use LAM\TYPES\TypeManager;
14 use moduleCache;
15
16 /*
17
18 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
19 Copyright (C) 2003 - 2006 Tilo Lutz
20 Copyright (C) 2007 - 2025 Roland Gruber
21
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
36 */
37
38 /**
39 * Used to delete accounts from LDAP tree.
40 *
41 * @author Tilo Lutz
42 * @author Roland Gruber
43 * @package main
44 */
45
46
47 /** security functions */
48 include_once(__DIR__ . "/../lib/security.inc");
49 /** account functions */
50 include_once(__DIR__ . '/../lib/account.inc');
51 /** current configuration options */
52 include_once(__DIR__ . '/../lib/config.inc');
53 /** message displaying */
54 include_once(__DIR__ . '/../lib/status.inc');
55 /** LDAP connection */
56 include_once(__DIR__ . '/../lib/ldap.inc');
57 /** remote interface */
58 include_once(__DIR__ . '/../lib/remote.inc');
59 /** module interface */
60 include_once(__DIR__ . '/../lib/modules.inc');
61
62 // Start session
63 startSecureSession();
64 enforceUserIsLoggedIn();
65
66 if (!checkIfWriteAccessIsAllowed()) {
67 die();
68 }
69
70 // Redirect to startpage if user is not logged in
71 if (!isLoggedIn()) {
72 metaRefresh("login.php");
73 exit;
74 }
75
76 // Set correct language, codepages, ....
77 setlanguage();
78
79 if (!empty($_POST)) {
80 validateSecurityToken();
81 }
82
83 $sessionAccountPrefix = 'deleteContainer';
84 foreach ($_SESSION as $key => $value) {
85 if (str_starts_with($key, $sessionAccountPrefix)) {
86 unset($_SESSION[$key]);
87 logNewMessage(LOG_NOTICE, "del " . $key);
88 }
89 }
90
91 $typeManager = new TypeManager();
92
93 if (isset($_POST['type']) && ($typeManager->getConfiguredType($_POST['type']) === null)) {
94 logNewMessage(LOG_ERR, 'Invalid type: ' . $_POST['type']);
95 die();
96 }
97
98 if (isset($_GET['type']) && isset($_SESSION['delete_dn'])) {
99 $typeId = $_GET['type'];
100 $type = $typeManager->getConfiguredType($typeId);
101 if ($type === null) {
102 logNewMessage(LOG_ERR, 'Invalid type id: ' . $typeId);
103 die();
104 }
105 if (!checkIfDeleteEntriesIsAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) {
106 logNewMessage(LOG_ERR, 'User tried to delete entries of forbidden type '. $type->getId());
107 die();
108 }
109 // Create account list
110 $users = [];
111 foreach ($_SESSION['delete_dn'] as $dn) {
112 $start = strpos ($dn, "=")+1;
113 $end = strpos ($dn, ",");
114 $users[] = substr($dn, $start, $end-$start);
115 }
116
117 $sessionKey = $sessionAccountPrefix . (new DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText();
118 //load account
119 $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
120 // Show HTML Page
121 include __DIR__ . '/../lib/adminHeader.inc';
122 echo "<div class=\"smallPaddingContent\">";
123 echo "<br>\n";
124 echo "<form action=\"delete.php\" method=\"post\">\n";
125 $container = new htmlResponsiveRow();
126 $container->add(new htmlOutputText(_("Do you really want to remove the following accounts?")));
127 $container->addVerticalSpacer('2rem');
128 $userCount = count($users);
129 for ($i = 0; $i < $userCount; $i++) {
130 $container->addLabel(new htmlOutputText(_("Account name:")));
131 $container->addField(new htmlOutputText($users[$i]));
132 $container->addLabel(new htmlOutputText(_('DN') . ':'));
133 $container->addField(new htmlOutputText((string) $_SESSION['delete_dn'][$i]));
134 $_SESSION[$sessionKey]->load_account($_SESSION['delete_dn'][$i]);
135 if (!$_SESSION[$sessionKey]->hasOnlyVirtualChildren()) {
136 $childCount = getChildCount($_SESSION['delete_dn'][$i]);
137 if ($childCount > 0) {
138 $container->addLabel(new htmlOutputText(_('Number of child entries') . ':'));
139 $container->addField(new htmlOutputText((string) $childCount));
140 }
141 }
142 $container->addVerticalSpacer('0.5rem');
143 }
144 addSecurityTokenToMetaHTML($container);
145 $container->add(new htmlHiddenInput('type', $type->getId()));
146 $container->addVerticalSpacer('1rem');
147 parseHtml(null, $container, [], false, $type->getScope());
148 // Print delete rows from modules
149 $modules = $_SESSION['config']->get_AccountModules($type->getId());
150 $values = [];
151 foreach ($modules as $module) {
152 $module = moduleCache::getModule($module, $type->getScope());
153 if ($module === null) {
154 continue;
155 }
156 parseHtml($module::class, $module->display_html_delete(), $values, true, $type->getScope());
157 }
158 $buttonContainer = new htmlResponsiveRow();
159 $buttonContainer->addVerticalSpacer('1rem');
160 $buttonGroup = new htmlGroup();
161 $delButton = new htmlButton('delete', _('Delete'));
162 $delButton->setCSSClasses(['lam-danger']);
163 $buttonGroup->addElement($delButton);
164 $buttonGroup->addElement(new htmlSpacer('0.5rem', null));
165 $cancelButton = new htmlButton('cancel', _('Cancel'));
166 $buttonGroup->addElement($cancelButton);
167 $buttonContainer->add($buttonGroup);
168 $buttonContainer->addVerticalSpacer('1rem');
169 parseHtml(null, $buttonContainer, [], false, $type->getScope());
170 echo "</form>\n";
171 echo "</div>\n";
172 include __DIR__ . '/../lib/adminFooter.inc';
173 }
174
175 if (isset($_POST['cancel'])) {
176 if (isset($_SESSION['delete_dn'])) {
177 unset($_SESSION['delete_dn']);
178 }
179 metaRefresh("lists/list.php?type=" . $_POST['type']);
180 }
181 elseif (isset($_POST['cancelAllOk'])) {
182 if (isset($_SESSION['delete_dn'])) {
183 unset($_SESSION['delete_dn']);
184 }
185 metaRefresh("lists/list.php?type=" . $_POST['type'] . '&deleteAllOk=1');
186 }
187
188 if (isset($_POST['delete'])) {
189 $typeId = $_POST['type'];
190 $type = $typeManager->getConfiguredType($typeId);
191 if (($type === null) || !checkIfDeleteEntriesIsAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) {
192 logNewMessage(LOG_ERR, 'User tried to delete entries of forbidden type '. $typeId);
193 die();
194 }
195 // Show HTML Page
196 include __DIR__ . '/../lib/adminHeader.inc';
197 echo "<form action=\"delete.php\" method=\"post\">\n";
198 echo "<div class=\"smallPaddingContent\"><br>\n";
199 $container = new htmlResponsiveRow();
200 addSecurityTokenToMetaHTML($container);
201 $container->add(new htmlHiddenInput('type', $type->getId()));
202
203 $sessionKey = $sessionAccountPrefix . (new DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText();
204 $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
205 // Delete dns
206 $allOk = true;
207 $allErrors = [];
208 foreach ($_SESSION['delete_dn'] as $deleteDN) {
209 // Set to true if a real error has happened
210 $stopProcessing = false;
211 // First load DN.
212 $_SESSION[$sessionKey]->load_account($deleteDN);
213 // get commands and changes of each attribute
214 $moduleNames = array_keys($_SESSION[$sessionKey]->getAccountModules());
215 $modules = $_SESSION[$sessionKey]->getAccountModules();
216 $attributes = [];
217 $errors = [];
218 // predelete actions
219 foreach ($moduleNames as $singlemodule) {
220 $success = true;
221 $messages = $modules[$singlemodule]->preDeleteActions();
222 foreach ($messages as $message) {
223 $errors[] = $message;
224 if ($message[0] === 'ERROR') {
225 $success = false;
226 $allOk = false;
227 }
228 elseif ($message[0] === 'WARN') {
229 $allOk = false;
230 }
231 }
232 if (!$success) {
233 $stopProcessing = true;
234 break;
235 }
236 }
237 if (!$stopProcessing) {
238 // load attributes
239 foreach ($moduleNames as $singlemodule) {
240 // load changes
241 $temp = $modules[$singlemodule]->delete_attributes();
242 // merge changes
243 $DNs = array_keys($temp);
244 $attributes = array_merge_recursive($temp, $attributes);
245 foreach ($DNs as $dn) {
246 $ops = array_keys($temp[$dn]);
247 foreach ($ops as $op) {
248 $attrs = array_keys($temp[$dn][$op]);
249 foreach ($attrs as $attribute) {
250 $attributes[$dn][$op][$attribute] = array_unique($attributes[$dn][$op][$attribute]);
251 }
252 }
253 }
254 }
255 $DNs = array_keys($attributes);
256 foreach ($DNs as $dn) {
257 if (isset($attributes[$dn]['errors'])) {
258 foreach ($attributes[$dn]['errors'] as $singleerror) {
259 $errors[] = $singleerror;
260 if ($singleerror[0] == 'ERROR') {
261 $stopProcessing = true;
262 $allOk = false;
263 }
264 }
265 }
266 if (!$stopProcessing) {
267 // modify attributes
268 if (isset($attributes[$dn]['modify'])) {
269 $success = ldap_mod_replace($_SESSION['ldap']->server(), $dn, $attributes[$dn]['modify']);
270 if (!$success) {
271 $errors[] = ['ERROR', sprintf(_('Was unable to modify attributes from DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())];
272 $stopProcessing = true;
273 $allOk = false;
274 }
275 }
276 // add attributes
277 if (isset($attributes[$dn]['add']) && !$stopProcessing) {
278 $success = ldap_mod_add($_SESSION['ldap']->server(), $dn, $attributes[$dn]['add']);
279 if (!$success) {
280 $errors[] = ['ERROR', sprintf(_('Was unable to add attributes to DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())];
281 $stopProcessing = true;
282 $allOk = false;
283 }
284 }
285 // remove attributes
286 if (isset($attributes[$dn]['remove']) && !$stopProcessing) {
287 $success = ldap_mod_del($_SESSION['ldap']->server(), $dn, $attributes[$dn]['remove']);
288 if (!$success) {
289 $errors[] = ['ERROR', sprintf(_('Was unable to remove attributes from DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())];
290 $stopProcessing = true;
291 $allOk = false;
292 }
293 }
294 }
295 }
296 }
297 if (!$stopProcessing) {
298 $recursive = !$_SESSION[$sessionKey]->hasOnlyVirtualChildren();
299 $messages = deleteDN($deleteDN, $recursive);
300 foreach ($messages as $message) {
301 $errors[] = $message;
302 if ($message[0] === 'ERROR') {
303 $stopProcessing = true;
304 $allOk = false;
305 }
306 elseif ($message[0] === 'WARN') {
307 $allOk = false;
308 }
309 }
310 }
311 // post delete actions
312 if (!$stopProcessing) {
313 foreach ($moduleNames as $singlemodule) {
314 $messages = $modules[$singlemodule]->postDeleteActions();
315 foreach ($messages as $message) {
316 $errors[] = $message;
317 if (($message[0] === 'ERROR') || ($message[0] === 'WARN')) {
318 $allOk = false;
319 }
320 }
321 }
322 }
323 if (!$stopProcessing) {
324 $container->add(new htmlOutputText(sprintf(_('Deleted DN: %s'), $deleteDN)));
325 }
326 else {
327 $container->add(new htmlOutputText(sprintf(_('Error while deleting DN: %s'), $deleteDN)));
328 }
329 foreach ($errors as $error) {
330 $container->add(htmlStatusMessage::fromParamArray($error));
331 }
332 $allErrors = [...$allErrors, ...$errors];
333 }
334 $container->addVerticalSpacer('2rem');
335 $buttonName = $allOk ? 'cancelAllOk' : 'cancel';
336 $container->add(new htmlButton($buttonName, _('Back to list')));
337 $container->addVerticalSpacer('1rem');
338 if ($allOk) {
339 $_SESSION['listRedirectMessages'] = $allErrors;
340 $container->add(new htmlJavaScript('document.getElementById("btn_cancelAllOk").click();'));
341 }
342 parseHtml(null, $container, [], false, $type->getScope());
343 echo "</div>\n";
344 echo "</form>\n";
345 include __DIR__ . '/../lib/adminFooter.inc';
346
347 }
348
349 /**
350 * Returns the number of child entries of a DN.
351 *
352 * @param string $dn DN of parent
353 * @return integer number of children
354 */
355 function getChildCount($dn) {
356 $entries = searchLDAP($dn, '(objectClass=*)', ['dn']);
357 return (count($entries) - 1);
358 }