LDAPAccountManager/lam/templates/delete.php

337 lines
11 KiB
PHP
Raw Normal View History

<?php
/*
2005-03-26 11:27:15 +00:00
$Id$
2006-08-14 17:29:45 +00:00
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2006-03-03 17:30:35 +00:00
Copyright (C) 2003 - 2006 Tilo Lutz
2011-02-26 13:14:10 +00:00
Copyright (C) 2007 - 2011 Roland Gruber
2006-08-14 17:29:45 +00:00
2005-03-26 11:27:15 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2006-08-14 17:29:45 +00:00
2005-03-26 11:27:15 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2006-08-14 17:29:45 +00:00
2005-03-26 11:27:15 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2006-08-14 17:29:45 +00:00
2005-03-26 11:27:15 +00:00
*/
2005-03-26 11:27:15 +00:00
/**
* Used to delete accounts from LDAP tree.
*
* @author Tilo Lutz
* @author Roland Gruber
2005-08-17 19:17:15 +00:00
* @package main
*/
2004-01-10 11:47:48 +00:00
2006-03-26 17:51:25 +00:00
/** security functions */
include_once("../lib/security.inc");
2005-08-17 19:17:15 +00:00
/** account functions */
include_once('../lib/account.inc');
/** current configuration options */
include_once('../lib/config.inc');
/** message displaying */
include_once('../lib/status.inc');
/** LDAP connection */
include_once('../lib/ldap.inc');
/** lamdaemon interface */
include_once('../lib/lamdaemon.inc');
/** module interface */
include_once('../lib/modules.inc');
2004-01-10 11:47:48 +00:00
// Start session
2006-03-26 17:51:25 +00:00
startSecureSession();
2004-01-10 11:47:48 +00:00
2007-12-30 16:08:54 +00:00
if (!checkIfWriteAccessIsAllowed()) {
die();
}
// Redirect to startpage if user is not loged in
2008-05-15 17:32:59 +00:00
if (!isset($_SESSION['loggedIn']) || ($_SESSION['loggedIn'] !== true)) {
metaRefresh("login.php");
2004-01-10 11:47:48 +00:00
exit;
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
// Set correct language, codepages, ....
setlanguage();
2003-04-25 16:14:09 +00:00
2007-04-21 11:04:50 +00:00
if (isset($_GET['type']) && isset($_SESSION['delete_dn'])) {
2004-01-10 11:47:48 +00:00
// Create account list
foreach ($_SESSION['delete_dn'] as $dn) {
$start = strpos ($dn, "=")+1;
$end = strpos ($dn, ",");
$users[] = substr($dn, $start, $end-$start);
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
//load account
$_SESSION['account'] = new accountContainer($_GET['type'], 'account');
// Show HTML Page
2010-01-01 17:21:46 +00:00
include 'main_header.php';
echo "<div class=\"".$_GET['type']."list-bright smallPaddingContent\">";
2010-08-21 09:43:52 +00:00
echo "<br>\n";
2004-01-10 11:47:48 +00:00
echo "<form action=\"delete.php\" method=\"post\">\n";
echo "<input name=\"type\" type=\"hidden\" value=\"" . $_GET['type'] . "\">\n";
2005-03-26 11:27:15 +00:00
echo "<b>" . _("Do you really want to remove the following accounts?") . "</b>";
echo "<br><br>\n";
2005-11-26 12:49:48 +00:00
echo "<table border=0>\n";
2004-01-10 11:47:48 +00:00
for ($i=0; $i<count($users); $i++) {
echo "<tr>\n";
2005-03-26 11:27:15 +00:00
echo "<td><b>" . _("Account name:") . "</b> $users[$i]</td>\n";
2005-11-26 12:49:48 +00:00
echo "<td>&nbsp;&nbsp;<b>" . _('DN') . ":</b> " . $_SESSION['delete_dn'][$i] . "</td>\n";
$childCount = getChildCount($_SESSION['delete_dn'][$i]);
if ($childCount > 0) {
echo "<td>&nbsp;&nbsp;<b>" . _('Number of child entries') . ":</b> " . $childCount . "</td>\n";
}
2004-01-10 11:47:48 +00:00
echo "</tr>\n";
2005-03-26 11:27:15 +00:00
}
echo "</table>\n";
2004-01-10 11:47:48 +00:00
echo "<br>\n";
// Print delete rows from modules
echo "<table border=0 width=\"100%\">\n<tr><td valign=\"top\" width=\"15%\" >";
2005-04-04 16:55:34 +00:00
$modules = $_SESSION['config']->get_AccountModules($_GET['type']);
$values = array();
$tabindex = 100;
2005-04-04 16:55:34 +00:00
foreach ($modules as $module) {
$module = new $module($_GET['type']);
2009-02-07 19:25:22 +00:00
parseHtml(get_class($module), $module->display_html_delete(), $values, true, $tabindex, $_GET['type']);
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
echo "</table>\n";
echo "<br>\n";
echo "<button class=\"smallPadding\" name=\"delete\" id=\"submitButton\">" . _('Delete') . "</button>&nbsp;\n";
echo "<button class=\"smallPadding\" name=\"cancel\" id=\"cancelButton\">" . _('Cancel') . "</button>\n";
2004-01-10 11:47:48 +00:00
echo "</form>\n";
echo "</div>\n";
?>
<script type="text/javascript" language="javascript">
jQuery(document).ready(function() {
jQuery('#submitButton').button();
jQuery('#cancelButton').button();
});
</script>
<?php
2010-08-21 09:43:52 +00:00
include 'main_footer.php';
2005-03-26 11:27:15 +00:00
}
2003-05-02 07:59:13 +00:00
2010-09-26 11:11:01 +00:00
if (isset($_POST['cancel'])) {
2004-01-10 11:47:48 +00:00
if (isset($_SESSION['delete_dn'])) unset($_SESSION['delete_dn']);
2006-01-06 10:47:43 +00:00
metaRefresh("lists/list.php?type=" . $_POST['type']);
2005-03-26 11:27:15 +00:00
}
elseif (isset($_POST['cancelAllOk'])) {
if (isset($_SESSION['delete_dn'])) unset($_SESSION['delete_dn']);
metaRefresh("lists/list.php?type=" . $_POST['type'] . '&amp;deleteAllOk=1');
}
2010-09-26 11:11:01 +00:00
if (isset($_POST['delete'])) {
2004-01-10 11:47:48 +00:00
// Show HTML Page
2010-01-01 17:21:46 +00:00
include 'main_header.php';
2004-01-10 11:47:48 +00:00
echo "<form action=\"delete.php\" method=\"post\">\n";
echo "<input name=\"type\" type=\"hidden\" value=\"" . $_POST['type'] . "\">\n";
echo "<div class=\"".$_POST['type']."list-bright smallPaddingContent\"><br>\n";
echo "<br>\n";
2004-01-10 11:47:48 +00:00
// Delete dns
$allOk = true;
2004-01-10 11:47:48 +00:00
for ($m=0; $m<count($_SESSION['delete_dn']); $m++) {
// Set to true if an real error has happened
$stopprocessing = false;
// First load DN.
$_SESSION['account']->load_account($_SESSION['delete_dn'][$m]);
// get commands and changes of each attribute
2007-10-26 14:10:10 +00:00
$moduleNames = array_keys($_SESSION['account']->getAccountModules());
$modules = $_SESSION['account']->getAccountModules();
2004-01-10 11:47:48 +00:00
$attributes = array();
$errors = array();
2007-02-25 13:51:40 +00:00
// predelete actions
if (!$stopprocessing) {
2007-10-26 14:10:10 +00:00
foreach ($moduleNames as $singlemodule) {
$success = true;
$messages = $modules[$singlemodule]->preDeleteActions();
for ($i = 0; $i < sizeof($messages); $i++) {
$errors[] = $messages[$i];
if ($messages[$i][0] == 'ERROR') {
$success = false;
$allOk = false;
}
elseif ($messages[$i][0] == 'WARN') {
$allOk = false;
}
}
2007-02-25 13:51:40 +00:00
if (!$success) {
$stopprocessing = true;
break;
2004-01-10 11:47:48 +00:00
}
}
2009-06-14 12:31:51 +00:00
}
2007-02-25 13:51:40 +00:00
if (!$stopprocessing) {
// load attributes
2007-10-26 14:10:10 +00:00
foreach ($moduleNames as $singlemodule) {
2007-02-25 13:51:40 +00:00
// load changes
2007-10-26 14:10:10 +00:00
$temp = $modules[$singlemodule]->delete_attributes();
2007-02-25 13:51:40 +00:00
if (is_array($temp)) {
// merge changes
$DNs = array_keys($temp);
$attributes = array_merge_recursive($temp, $attributes);
for ($i=0; $i<count($DNs); $i++) {
$ops = array_keys($temp[$DNs[$i]]);
for ($j=0; $j<count($ops); $j++) {
$attrs = array_keys($temp[$DNs[$i]][$ops[$j]]);
for ($k=0; $k<count($attrs); $k++)
$attributes[$DNs[$i]][$ops[$j]][$attrs[$k]] = array_unique($attributes[$DNs[$i]][$ops[$j]][$attrs[$k]]);
}
}
2004-01-10 11:47:48 +00:00
}
2005-03-26 11:27:15 +00:00
}
2007-02-25 13:51:40 +00:00
$DNs = array_keys($attributes);
for ($i=0; $i<count($DNs); $i++) {
if (isset($attributes[$DNs[$i]]['errors'])) {
foreach ($attributes[$DNs[$i]]['errors'] as $singleerror) {
$errors[] = $singleerror;
if ($singleerror[0] == 'ERROR') {
$stopprocessing = true;
$allOk = false;
}
}
2005-03-26 11:27:15 +00:00
}
2007-02-25 13:51:40 +00:00
if (!$stopprocessing) {
// modify attributes
if (isset($attributes[$DNs[$i]]['modify']) && !$stopprocessing) {
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['modify']);
if (!$success) {
2011-05-11 15:04:24 +00:00
$errors[] = array ('ERROR', sprintf(_('Was unable to modify attributes from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
2007-02-25 13:51:40 +00:00
$stopprocessing = true;
$allOk = false;
2007-02-25 13:51:40 +00:00
}
2003-04-26 21:58:00 +00:00
}
2007-02-25 13:51:40 +00:00
// add attributes
if (isset($attributes[$DNs[$i]]['add']) && !$stopprocessing) {
$success = @ldap_mod_add($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['add']);
if (!$success) {
2011-05-11 15:04:24 +00:00
$errors[] = array ('ERROR', sprintf(_('Was unable to add attributes to DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
2007-02-25 13:51:40 +00:00
$stopprocessing = true;
$allOk = false;
2007-02-25 13:51:40 +00:00
}
}
2011-02-26 13:14:10 +00:00
// remove attributes
2007-02-25 13:51:40 +00:00
if (isset($attributes[$DNs[$i]]['remove']) && !$stopprocessing) {
$success = @ldap_mod_del($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['remove']);
if (!$success) {
2011-05-11 15:04:24 +00:00
$errors[] = array ('ERROR', sprintf(_('Was unable to remove attributes from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
2007-02-25 13:51:40 +00:00
$stopprocessing = true;
$allOk = false;
2007-02-25 13:51:40 +00:00
}
2004-01-10 11:47:48 +00:00
}
}
}
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
if (!$stopprocessing) {
2005-11-26 12:49:48 +00:00
$errors = deleteDN($_SESSION['delete_dn'][$m]);
if (sizeof($errors) > 0) {
$stopprocessing = true;
$allOk = false;
}
2005-03-26 11:27:15 +00:00
}
2007-02-25 13:51:40 +00:00
// post delete actions
if (!$stopprocessing) {
2007-10-26 14:10:10 +00:00
foreach ($moduleNames as $singlemodule) {
$messages = $modules[$singlemodule]->postDeleteActions();
for ($i = 0; $i < sizeof($messages); $i++) {
$errors[] = $messages[$i];
if (($messages[$i][0] == 'ERROR') || ($messages[$i][0] == 'WARN')) {
$allOk = false;
}
}
}
}
2004-01-10 11:47:48 +00:00
if (!$stopprocessing) {
2005-03-26 11:27:15 +00:00
echo sprintf(_('Deleted DN: %s'), $_SESSION['delete_dn'][$m]) . "<br>\n";
foreach ($errors as $error) {
call_user_func_array('StatusMessage', $error);
}
2005-03-26 11:27:15 +00:00
echo "<br>\n";
flush();
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
else {
2005-03-26 11:27:15 +00:00
echo sprintf(_('Error while deleting DN: %s'), $_SESSION['delete_dn'][$m]) . "<br>\n";
foreach ($errors as $error) {
call_user_func_array('StatusMessage', $error);
}
2005-03-26 11:27:15 +00:00
echo "<br>\n";
2003-08-20 19:16:03 +00:00
}
2005-03-26 11:27:15 +00:00
}
2004-01-10 11:47:48 +00:00
echo "<br>\n";
echo "<br><button class=\"smallPadding\" name=\"cancel\" id=\"backButton\">" . _('Back to list') . "</button>\n";
echo "<br><button class=\"hidden\" name=\"cancelAllOk\" id=\"backButtonAllOk\"> </button>\n";
echo "</div>\n";
2004-01-10 11:47:48 +00:00
echo "</form>\n";
?>
<script type="text/javascript" language="javascript">
jQuery(document).ready(function() {
jQuery('#backButton').button();
<?php
if ($allOk) {
echo "jQuery('#backButtonAllOk').click();";
}
?>
});
</script>
<?php
2010-08-21 09:43:52 +00:00
include 'main_footer.php';
2003-05-02 07:59:13 +00:00
2005-03-26 11:27:15 +00:00
}
2003-05-02 07:59:13 +00:00
2005-11-26 12:49:48 +00:00
/**
* Returns the number of child entries of a DN.
*
* @param string $dn DN of parent
* @return interger number of childs
*/
function getChildCount($dn) {
$entries = searchLDAP(escapeDN($dn), 'objectClass=*', array('dn'));
return (sizeof($entries) - 1);
2005-11-26 12:49:48 +00:00
}
/**
* Deletes a DN and all child entries.
2006-08-14 17:29:45 +00:00
*
2005-11-26 12:49:48 +00:00
* @param string $dn DN to delete
* @return array error messages
*/
function deleteDN($dn) {
$errors = array();
2010-02-15 17:36:45 +00:00
$sr = @ldap_list($_SESSION['ldap']->server(), $dn, 'objectClass=*', array('dn'), 0, 0, 0, LDAP_DEREF_NEVER);
2005-11-26 12:49:48 +00:00
if ($sr) {
2007-07-08 10:51:01 +00:00
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
2009-12-19 16:28:16 +00:00
$entries = cleanLDAPResult($entries);
for ($i = 0; $i < sizeof($entries); $i++) {
2005-11-26 12:49:48 +00:00
// delete recursively
$subErrors = deleteDN($entries[$i]['dn']);
for ($e = 0; $e < sizeof($subErrors); $e++) $errors[] = $subErrors[$e];
}
}
else {
$errors[] = array ('ERROR', sprintf(_('Was unable to delete DN: %s.'), $dn), ldap_error($_SESSION['ldap']->server()));
return $errors;
2005-11-26 12:49:48 +00:00
}
// delete parent DN
$success = @ldap_delete($_SESSION['ldap']->server(), $dn);
2006-05-16 15:18:24 +00:00
$ldapUser = $_SESSION['ldap']->decrypt_login();
$ldapUser = $ldapUser[0];
2005-11-26 12:49:48 +00:00
if (!$success) {
2006-05-16 15:18:24 +00:00
logNewMessage(LOG_ERR, '[' . $ldapUser .'] Unable to delete DN: ' . $dn . ' (' . ldap_err2str(ldap_errno($_SESSION['ldap']->server())) . ').');
2005-11-26 12:49:48 +00:00
$errors[] = array ('ERROR', sprintf(_('Was unable to delete DN: %s.'), $dn), ldap_error($_SESSION['ldap']->server()));
}
2006-05-16 15:18:24 +00:00
else {
logNewMessage(LOG_NOTICE, '[' . $ldapUser .'] Deleted DN: ' . $dn);
}
2005-11-26 12:49:48 +00:00
return $errors;
}
2003-04-25 16:14:09 +00:00
?>