<?
require_once('include/global.php');
require_once('include/admin_lib.php');
include_once("include/file.php");
require_once('sadmin_conf.inc.php');

mail_server_check_limit();		// 메일서버가 동작중인지 확인

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

	// 회원이 존재할때는 삭제불가.
		// [2008-02-21] 메일링 리스트 회원은 제외
		//$sql = "SELECT COUNT(*) FROM ".$TB['MEMBER']." WHERE d_no='". $_GET['d_no'] . "'";
		$sql = "SELECT COUNT(*) FROM ".$TB['MEMBER']." WHERE d_no='". $_GET['d_no'] . "' AND m_usertype IS NULL";
		$mbr_cnt = db_get_one($sql);
		if ($mbr_cnt > 0)
			msg("도메인내에 [".$mbr_cnt."]명의 회원이 존재하므로 삭제할 수 없습니다.\\n\\n회원을 모두 삭제후 삭제하시기 바랍니다.");

// data
	// 도메인명 가져오기
		$sql = "SELECT d_name FROM ".$TB['DOMAIN']." WHERE d_no='". $_GET['d_no'] . "'";
		$d_name = db_get_one($sql);

        // 도메인명 못가져올 경우 중단
        if ($d_name === null) {
            msg('해당 도메인을 가져오지 못해서 삭제할 수 없습니다.');
        }

	// 게시판
		$q = "SELECT ba_no FROM ".$TB['BOARD_ADMIN']." WHERE d_no='" . $_GET['d_no'] ."' ORDER BY ba_no";
		//echo $q;
		$ba = db_get_rows($q);
		for ($i=0; $i<sizeof($ba); $i++)
		{
			$ba_no = $ba[$i]['ba_no'];

			// file delete
				$upload_dir = $UPLOAD_DIR[BOARD] . "/" . $ba_no;		// ex) BOARD/1 => /xxxx/upload/BOARD/1/
				dir_del_all($upload_dir, false);

			// delete
				$q = "DELETE FROM ".$TB['BOARD_COMMENT']." WHERE ba_no='$ba_no'";
				//echo "<HR>" . $q;
				db_query($q);

				$q = "DELETE FROM ".$TB['BOARD']." WHERE ba_no='$ba_no'";
				//echo "<HR>" . $q;
				db_query($q);

				$q = "DELETE FROM ".$TB['BOARD_ADMIN']." WHERE ba_no='$ba_no' AND d_no='" . $_GET['d_no'] . "'";
				//echo "<HR>" . $q;
				db_query($q);
		}	// for()

		// [2011-01-05] 메뉴관리
		$q = "DELETE FROM ".$TB['BOARD_MENU']." WHERE d_no='" . $_GET['d_no'] . "'";
		//echo "<HR>" . $q;
		db_query($q);


	// 공지사항
		$sql = "DELETE FROM ".$TB['NOTICE']." WHERE d_no='".$_GET['d_no']."'";
		db_query($sql);


	// [2012-11-28] 공용일정
		$sql = "DELETE FROM ".$TB['DIARY']." WHERE d_no='".$_GET['d_no']."'";
		db_query($sql);


	// [2012-11-28] 공용주소록
		$adg = db_get_rows("SELECT adg_no FROM " . $TB['ADDR_GROUP'] . " WHERE d_no='".$_GET['d_no']."'");
		for ($i=0; $i<sizeof($adg); $i++)
		{
			$q = "DELETE FROM " . $TB['ADDR'] . " WHERE adg_no='" . $adg[$i]['adg_no'] . "'";
			db_query($q);
		}	// for()


	// [2008-08-13] 회원직위
		$sql = "DELETE FROM ".$TB['MEMBER_CLASS']." WHERE d_no='".$_GET['d_no']."'";
		db_query($sql);


	// [2008-08-13] 회원조직관계 - 메일계정 삭제시 같이 삭제됨.


	// 회원조직
		$sql = "DELETE FROM ".$TB['MEMBER_GROUP']." WHERE d_no='".$_GET['d_no']."'";
		db_query($sql);


	// 메일링리스트
		$sql = "SELECT m_no FROM ".$TB['MEMBER']." WHERE d_no='". $_GET['d_no'] . "' AND m_usertype = 'M'";
		$ml_m_no = db_get_rows($sql, true);
		if (sizeof($ml_m_no) > 0)
		{
			$sql = "DELETE FROM " . $TB['MAILING_LIST'] . " WHERE m_no IN (".implode_keys($ml_m_no['m_no']).")";
			db_query($sql);

			$sql = "DELETE FROM ".$TB['MEMBER']." WHERE d_no='". $_GET['d_no'] . "' AND m_usertype = 'M'";
			db_query($sql);

			// 메일서버의 리스트는 메일서버에서 도메인삭제시 일괄삭제됨.
		}	// if()


	// [2008-09-11] 웹하드 Guest 계정
		$sql = "DELETE FROM ".$TB['MEMBER']." WHERE d_no='". $_GET['d_no'] . "' AND m_usertype = 'W'";
		db_query($sql);


	// 공용웹하드
		require_once("include/webhard_lib.php");
		$sql = "SELECT ws_key FROM " . $TB['WEBHARD_SHARE'] . " WHERE d_no='". $_GET['d_no'] . "'";
		$ws = db_get_rows($sql);
		for ($i=0; $i<sizeof($ws); $i++)
		{
			$upload_dir = 'webhard/'.$d_name.'/share/'.$ws[$i]['ws_key'];
			webhard_folder_del($ws[$i]['ws_key'], $wfd_key=0, $upload_dir, $is_root_del=true, $m_no=0);
		}	// for()
		$sql = "DELETE FROM " . $TB['WEBHARD_SHARE'] . " WHERE d_no='". $_GET['d_no'] . "'";
		db_query($sql);
		dir_del_all('webhard/'.$d_name, false);


	// 대용량첨부 폴더
		dir_del_all('bigfile/'.$d_name, false);


	// 로고 폴더
		dir_del_all('logo/'.$d_name, false);

	// 파비콘 폴더
		dir_del_all('favicon/'.$d_name, false);


	// 마지막에 삭제해야할 부분
	// 메일폴더
		for ($i=0; $i<=9999; $i++)
		{
			// [2012-11-28]
			//$m_mail_file_group = '0000';
			$m_mail_file_group = sprintf("%04d", $i);
			$mail_file_table = $G_SYS[PRE_FIX] . 'mail_file_' . $_GET['d_no'] . '_' . $m_mail_file_group;
			if ($G_SYS[DB] == 'oracle')
			{
				$sql = "SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_NAME = '" . strtoupper($mail_file_table) . "' AND OBJECT_TYPE = 'TABLE'";
				//db_query("DROP SEQUENCE sqc_".$mail_file_table);
			} else
			{
				$sql = "SHOW TABLES LIKE '".$mail_file_table."'";
			}	// if()
			$st = db_get_one($sql);
			if ($st)
			{
				// [2011-02-08] Oracle 환경에서 도메인 삭제시 에러 보완
				if ($G_SYS[DB] == 'oracle')
				{
					db_query("DROP SEQUENCE sqc_".$mail_file_table);
				}	// if()
				$sql = "DROP TABLE ".$mail_file_table;
				db_query($sql);
			} else
			{
				if ($m_mail_file_group != '0000')
					break;
			}	// if()
		}	// for()

	// 회원필드
		$sql = "DELETE FROM ".$TB['MEMBER_FIELD']." WHERE d_no='".$_GET['d_no']."'";
		db_query($sql);


	// 웹메일용 처리.
		require_once('include/admin_lib.php');
		domain_del($d_name);


	// 도메인정보
		$sql = "
			DELETE
			FROM
				" . $TB['DOMAIN'] . "
			WHERE
				d_no='" . $_GET['d_no'] . "'
			";
		db_query($sql);

// end
	msg("삭제되었습니다.", 0);
	movepage('sdomain_list.php?' . get_qs('d_no'));


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