Location: PHPKode > projects > ACollab > group_admin/group_delete.php
<?php
/****************************************************************************************/
/* ACollab                                                                              */
/****************************************************************************************/
/* Copyright (c) 2002-2004  Adaptive Technology Resource Centre / University of Toronto */
/*                                                                                      */
/* http://atutor.ca/acollab                                                             */
/*                                                                                      */
/* This program is free software. You may 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.                                               */
/*                                                                                      */
/* 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.                                 */
/*                                                                                      */
/* You may access the GNU General Public License at:                                    */
/* http://www.opensource.org/licenses/gpl-license.php                                   */
/*                                                                                      */
/* You may contact the Adaptive Technology Resource Centre at                           */
/* Robarts Library, University of Toronto                                               */
/* 130 St. George Street, Toronto, Ontario, Canada M5S 1A5                              */
/* Further contact information is available at http://www.utoronto.ca/atrc/             */
/****************************************************************************************/
/* Programmer:                                                                          */
/* Joel Kronenberg - ATRC                                                               */
/****************************************************************************************/
// $Id: group_delete.php 383 2005-02-09 21:32:05Z shozubq $

define('AC_INCLUDE_PATH', '../include/');

require(AC_INCLUDE_PATH.'vitals.inc.php');
authenticate(USER_GROUP_ADMIN);

$id  = intval($_REQUEST['id']);

if ($_POST['cancel']) {
	Header('Location: index.php');
	exit;
}



// Enables deletion of directory if not empty
function clr_dir($dir) {
	if(!$opendir = @opendir($dir)) {
		return false;
	}
	
	while(($readdir=readdir($opendir)) !== false) {
		if (($readdir !== '..') && ($readdir !== '.')) {
			$readdir = trim($readdir);

			clearstatcache(); /* especially needed for Windows machines: */

			if (is_file($dir.'/'.$readdir)) {
				if(!@unlink($dir.'/'.$readdir)) {
					return false;
				}
			} else if (is_dir($dir.'/'.$readdir)) {
				/* calls itself to clear subdirectories */
				if(!clr_dir($dir.'/'.$readdir)) {
					return false;
				}
			}
		}
	} /* end while */

	closedir($opendir);
	
	if(!@rmdir($dir)) {
		return false;
	}
	return true;
}

if ($_POST['submit']) {

	/* delete this group */
	$sql	= "DELETE FROM ".TABLE_PREFIX."groups WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* unlink members from this group */
	$sql	= "DELETE FROM ".TABLE_PREFIX."groups_members WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete news */
	$sql	= "DELETE FROM ".TABLE_PREFIX."news WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete events */
	$sql	= "SELECT event_id FROM ".TABLE_PREFIX."events WHERE group_id=$_SESSION[group_id]";
	$result = mysql_query($sql, $db);
	while ($row = mysql_fetch_assoc($result)) {
		mysql_query("DELETE FROM ".TABLE_PREFIX."events_files WHERE event_id=$row[event_id]", $db);
	}

	$sql	= "DELETE FROM ".TABLE_PREFIX."events WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete library/drafting room files */
	$sql	= "SELECT R.revision_id FROM ".TABLE_PREFIX."files_revisions R, ".TABLE_PREFIX."files F WHERE F.group_id=$_SESSION[group_id] AND F.file_id=R.file_id ORDER BY F.file_id";
	$result = mysql_query($sql, $db);
	while ($row = mysql_fetch_assoc($result)) {
		$char = substr($row['revision_id'], 0, 1).'/';
		@unlink(UPLOAD_DIR.$char.$row['revision_id']);

		$sql = "DELETE FROM ".TABLE_PREFIX."revisions_comments WHERE revision_id=$row[revision_id]";
		mysql_query($sql, $db);
	}

	/*Also Remove files and file revision from database !!*/
	$sql = "DELETE ".TABLE_PREFIX."files_revisions, ".TABLE_PREFIX."files 
			FROM ".TABLE_PREFIX."files_revisions R, ".TABLE_PREFIX."files F	
			WHERE F.file_id=R.file_id AND F.group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete folder from this group */
	$sql	= "DELETE FROM ".TABLE_PREFIX."folders WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete forums */
	$sql	= "SELECT forum_id FROM ".TABLE_PREFIX."forums WHERE group_id=$_SESSION[group_id]";
	$result = mysql_query($sql, $db);
	while ($row = mysql_fetch_assoc($result)) {
		mysql_query("DELETE FROM ".TABLE_PREFIX."forums_threads WHERE forum_id=$row[forum_id]", $db);
	}

	$sql	= "DELETE FROM ".TABLE_PREFIX."forums WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete messages */
	$sql	= "DELETE FROM ".TABLE_PREFIX."messages WHERE group_id=$_SESSION[group_id]";
	mysql_query($sql, $db);

	/* delete chat */
	clr_dir('../chat/msgs/'.$_SESSION['group_id']);
	clr_dir('../chat/tran/'.$_SESSION['group_id']);
	@unlink('../chat/'.$_SESSION['group_id'].'.admin.settings');	

	if (authenticate(USER_ADMIN, USER_RETURN_CHECK)) {
		header('Location: ../admin/groups.php?f='.F_GROUP_DELETED);
	} else {
		header('Location: ../index.php?f='.F_GROUP_DELETED);
	}

	exit;
}

if (authenticate(USER_ADMIN, USER_RETURN_CHECK)) {
	$group_info = get_group($_SESSION['group_id']);

	$_SECTION[0][0] = _AC('administration');
	$_SECTION[0][1] = 'admin/index.php';
	$_SECTION[1][0] = _AC('groups');
	$_SECTION[1][1] = 'admin/groups.php';
	$_SECTION[2][0] = $group_info['title'];
	$_SECTION[2][1] = 'group_admin/index.php';
	$_SECTION[3][0] = _AC('delete_group');
	$_SECTION[3][1] = 'admin/groups_delete.php';

	require(AC_INCLUDE_PATH.'admin/header.inc.php');
} else {
	$_SECTION[0][0] = _AC('home');
	$_SECTION[0][1] = 'home.php';
	$_SECTION[1][0] = _AC('administration');
	$_SECTION[1][1] = 'group_admin/';
	$_SECTION[2][0] = _AC('delete_group');
	$_SECTION[2][1] = 'admin/groups_delete.php';

	require(AC_INCLUDE_PATH.'header.inc.php');
}

	$sql = "SELECT * FROM ".TABLE_PREFIX."groups WHERE group_id=$_SESSION[group_id]";
	$result = mysql_query($sql,$db);
	if (!($row = mysql_fetch_array($result))) {
		$errors[] = E_GROUP_NOT_FOUND;
		print_errors($errors);
		require (AC_INCLUDE_PATH.'admin/footer.inc.php');
		exit;
	}
?>
<br />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<input type="hidden" name="id" value="<?php echo $id; ?>" />
	<table border="0" cellspacing="0" cellpadding="2" align="center" class="box2">
	<tr>
		<th colspan="3" class="box"><h3><?php echo _AC('delete_group'); ?></h3></th>
	</tr>
	<tr bgcolor="white">
		<td>&nbsp;</td>
		<td><b><?php echo _AC('title'); ?>:</b> <?php echo $row['title']; ?></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1"><img src="images/clr.gif" alt="" height="1" width="1" /><br /><?php echo _AC('confirm_delete_group'); ?><br /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr bgcolor="white">
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right"><br /><input type="submit" name="submit" value="<?php echo _AC('delete'); ?>" class="submitY" onfocus="this.className='submitY highlight'" onblur="this.className='submitY'" /> &nbsp; <input type="submit" name="cancel" value="<?php echo _AC('cancel'); ?>" class="submitN" onfocus="this.className='submitN highlight'" onblur="this.className='submitN'" /><br /><br /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	</table>
</form>
<?php
	require(AC_INCLUDE_PATH.'footer.inc.php');
?>
Return current item: ACollab