<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}
global $admin_file;
$aid = substr("$aid", 0,25);
$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `radminsuper` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'"));
$acces = $GLOBALS['db']->XMFbin($row['radminsuper']);
if ($acces == 1) {

	/*********************************************************/
	/* Comments Delete Function                              */
	/*********************************************************/

	/* Thanks to Oleg [Dark Pastor] Martos from http://www.rolemancer.ru */
	/* to code the comments childs deletion function!                    */

	function removeSubComments($tid) {
		global $prefix;
		$tid = intval($tid);
		$result = $GLOBALS['db']->sql_query("SELECT `tid` from `" . $prefix . "_comments` where `pid`='$tid'");
		$numrows = $GLOBALS['db']->sql_numrows($result);
		if($numrows>0) {
			while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
				$stid = intval($row['tid']);
				removeSubComments($stid);
				$stid = intval($stid);
				$GLOBALS['db']->sql_query("delete from " . $prefix . "_comments where tid='$stid'");
			}
		}
		$GLOBALS['db']->sql_query("delete from " . $prefix . "_comments where tid='$tid'");
	}

	function removeComment ($tid, $sid, $ok=0) {
		global $ultramode, $prefix, $admin_file;
		if($ok) {
			$tid = intval($tid);
			$result = $GLOBALS['db']->sql_query("SELECT date from " . $prefix . "_comments where pid='$tid'");
			$numresults = $GLOBALS['db']->sql_numrows($result);
			$sid = intval($sid);
			$GLOBALS['db']->sql_query("update ".TABLE_STORIES." set comments=comments-1-'$numresults' where sid='$sid'");
			/* Call recursive delete function to delete the comment and all its childs */
			removeSubComments($tid);
			if ($ultramode) {
				ultramode();
			}
			Header("Location: modules.php?name=News&file=article&sid=$sid");
		} else {
			incoh();
			Max_OpenTable();
			echo "<center><font class='title'><b>" . _REMOVECOMMENTS . "</b></font></center>";
			Max_CloseTable();
			br('1');
			Max_OpenTable();
			echo "<center>" . _SURETODELCOMMENTS . "";
			echo "<br /><br />[ <a href=\"javascript:history.go(-1)\">" . _NO . "</a> | <a href=\"".$admin_file.".php?op=RemoveComment&tid=$tid&sid=$sid&ok=1\">" . _YES . "</a> ]</center>";
			Max_CloseTable();
			incof();
		}
	}

	function removePollSubComments($tid) {
		global $prefix;
		$tid = intval($tid);
		$result = $GLOBALS['db']->sql_query("SELECT `tid`, `pollID` from `" . $prefix . "_pollcomments` where `pid`='$tid'");
		$numrows = $GLOBALS['db']->sql_numrows($result);
		if($numrows>0) {
			while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
				$stid = intval($row['tid']);
				removePollSubComments($stid);
				$GLOBALS['db']->sql_query("delete from " . $prefix . "_pollcomments where tid='$stid'");
				$GLOBALS['db']->sql_query("update " . $prefix . "_poll_desc set comments=comments-1 where pollID='$row[pollID]'");
			}
		}
		$GLOBALS['db']->sql_query("delete from " . $prefix . "_pollcomments where tid='$tid'");
		$GLOBALS['db']->sql_query("update " . $prefix . "_poll_desc set comments=comments-1 where pollID='$row[pollID]'");
	}

	function RemovePollComment ($tid, $pollID, $ok=0) {
		global $admin_file, $prefix;
		if($ok) {
			$GLOBALS['db']->sql_query("update " . $prefix . "_poll_desc set comments=comments-1 where pollID='$pollID'");
			removePollSubComments($tid);
			Header("Location: modules.php?name=Surveys&op=results&pollID=$pollID");
		} else {
			incoh();
			Max_OpenTable();
			echo "<center><font class='title'><b>" . _REMOVECOMMENTS . "</b></font></center>";
			Max_CloseTable();
  			br('1');
			Max_OpenTable();
			echo "<center>" . _SURETODELCOMMENTS . "";
			echo "<br /><br />[ <a href=\"javascript:history.go(-1)\">" . _NO . "</a> | <a href=\"".$admin_file.".php?op=RemovePollComment&tid=$tid&pollID=$pollID&ok=1\">" . _YES . "</a> ]</center>";
			Max_CloseTable();
			incof();
		}
	}
if($admintest && $adminsecu)  {
	switch ($op) {

	case "RemoveComment":removeComment ($tid, $sid, $ok);break;
	case "removeSubComments":removeSubComments($tid);break;
	case "removePollSubComments":removePollSubComments($tid);break;
	case "RemovePollComment":RemovePollComment($tid, $pollID, $ok);break;

	}
}
} else {
	echo "Access Denied";
}