<?

include_once "./setting.php";

include_once $global["root"] . "/include/handle.php";
include_once $global["root"] . "/include/print_array.php";

include_once $global["root"] . "/lib/Site.php";
include_once $global["root"] . "/lib/Item.php";


$Site = new Site;
$Site->init("mail");

include_once $global["lang"] . "/" . $_SESSION["config_lang"] . ".php";

extract($_POST);

if (!$action or trim($action) == "") goprev();

if (($action == "copy" or $action == "move") and
		(! $directory_num or trim($directory_num) == "")) {
	goprev();
}

if ($action != "copy" and $action != "move" and $action != "remove"
		and $action != "cleanup") {
	goprev();
}

if ($action != "cleanup" and (! $items or count($items) < 1)) {
	goprev();
}

$Site->DB->transaction_start(true);

//-------------------------------------------------- Copy
if ($action == "copy") {
	$a = explode(",", $items);

	$Item = new Item();
	while (list($i, $j) = each($a)) {
		$Item->set($j);
		if ($Item->copy($directory_num)) {
			$Site->DB->retval = true;
			$retval = true;
		} else {
			$retval = false;
		}
	}
}

//-------------------------------------------------- Move
if ($action == "move") {
	$a = explode(",", $items);

	$Item = new Item();
	while (list($i, $j) = each($a)) {
		$Item->set($j);
		if ($Item->move($directory_num)) {
			$Site->DB->retval = true;
			$retval = true;
		} else {
			$retval = false;
		}
	}
}

//-------------------------------------------------- Remove
if ($action == "remove") {
	$a = explode(",", $items);

	if ($_SESSION["user_directory"] == $global["directory"]["trash"]) {
		$force = true;
	} else {
		$force = false;
	}

	$Item = new Item();
	while (list($i, $j) = each($a)) {
		$Item->set($j);
		if ($Item->remove($force)) {
			$Site->DB->retval = true;
			$retval = true;
		} else {
			$retval = false;
		}
	}
}

//-------------------------------------------------- cleanup
if ($action == "cleanup") {

	// remove each item.
	$SQL_FIND_ITEMS_IN_TRASH =
		"select SQL_CACHE no, item_num, item_type from item_%s where directory_num = '%s'";

	$Site->DB->query(sprintf($SQL_FIND_ITEMS_IN_TRASH,
		$_SESSION["config_id"], $directory_num), true);

	$Item = new Item();
	for ($i = 0; $i < $Site->DB->num_rows; $i++) {
		$o = $Site->DB->fetch();

		$Item->set($o["no"]);
		if ($Item->remove(true)) {
			$Site->DB->retval = true;
			$retval = true;
		} else {
			$retval = false;
		}
	}
}

//-------------------------------------------------------------------------------
if (! $retval) $Site->DB->retval = false;

$Site->DB->close();

if (! $retval and $action != "cleanup") {
	$Site->end1();

	goprev();
} else {
	$url = "Location: " . $_SESSION["user_www_url"] . "/mail?no=" .
		$_SESSION["user_directory"] . "&pg=" . $_SESSION["user_page_number"];

	$Site->end1();

	Header($url);
	exit;
}

/*
Description
-----------

Author
------
 Spike^ekipS <spike@spikeekips.net>

Changelog
---------

Usage
-----

*/

?>
