<?
require_once('include/global.php');
require_once('include/mail_lib.php');
require_once('include/file.php');
require_once('conf.inc.php');

set_time_limit(1200);
page_loading_view("편지함을 비우고 있습니다.");

// check
	if (!$_GET['mb_id'])
		msg('올바른 URL로 접근하여 주십시오');


// data
	// set data
		$mail_file_table = $AUTH[auth_m_mf_table];

	// [2006-01-05][custmize] 보낸편지함/수신확인 보관기능.
	$q_where = " AND ( mfext1 != 'Y' OR mfext1 IS NULL ) ";

	// 파일삭제와 디비삭제의 일치를 위함.
		$mf_no_max = db_get_one("SELECT MAX(mf_no) FROM $mail_file_table WHERE m_no='" . $AUTH['auth_m_no'] . "' AND mb_id='". $_GET['mb_id'] . "' $q_where");
		if (!$mf_no_max)
			$mf_no_max = 0;

	// file del
		$UPLOAD_ROOT = get_mail_dir();
		$upload_dir = get_mb_dir($_GET['mb_id']);
		$errorMsg = '';

		$sql = "SELECT mf_filename FROM " . $mail_file_table . " WHERE m_no='" . $AUTH['auth_m_no'] . "' AND mb_id='" . $_GET['mb_id'] . "' AND mf_no <= '" . $mf_no_max . "' $q_where ORDER BY mf_no";
		$rows = db_get_rows($sql);
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$errorMsg .= file_del($rows[$i]['mf_filename'], '', $upload_dir, false);
		}	// for()

	// db del
		if (sizeof($rows) > 0)
		{
			// [2025-09-18] 중요 메일 삭제
			$sql = " DELETE FROM " . $TB['MAIL_STAR'] . " WHERE ms_m_no ='". $AUTH['auth_m_no'] . "' AND ms_mf_no IN (
						SELECT mf_no 
							FROM " . $mail_file_table . " 
						   WHERE m_no='" . $AUTH['auth_m_no'] . "' 
							 AND mb_id='" . $_GET['mb_id'] . "'
							 AND mf_no <= '" . $mf_no_max . "' 
						         $q_where
			)";
			db_query($sql);

			$sql = " DELETE FROM " . $mail_file_table . " WHERE m_no='". $AUTH['auth_m_no'] . "' AND mb_id='" . $_GET['mb_id'] . "' AND mf_no <= '" . $mf_no_max . "' $q_where";
			db_query($sql);

			// [2013-07-05] 메일 삭제 로그
			errorlog("[MAIL_BOX_MAILDEL]".get_mb_name($_GET['mb_id']).", ".sizeof($rows)."EA", "MAILDEL");
		}	// if()



// end
	if ($errorMsg)
		error($errorMsg, 'FILE', false);
	#msg("편지함을 비웠습니다.", 0);
	if (!$_GET['back_url'])
		$_GET['back_url'] = 'mail_box.php';
	movepage($_GET['back_url']);


require_once('include/global_end.php');
?>