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


// 아웃룩에서 메일을 가져간후 삭제했을 경우 웹메일 디비에서도 자동삭제함.
sync_mailfile_db();


// set
	$CFG['TITLE'] = '<b>환경설정</b> >> <b>편지함관리</b>';


// data
	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();
		$q_hint = ($G_SYS[DB] == 'oracle') ? "/*+ ORDERED index_asc (".$AUTH['auth_m_mf_table']." MF_".$AUTH['auth_d_no']."_".$AUTH['auth_m_mail_file_group']."_MNO_MBID) */" : "";
		$sql = "
			SELECT
				".$q_hint."
				mf.mb_id, mf.mf_is_read, SUM(mf.mf_filesize) mf_filesize_sum, COUNT(*) mf_cnt_all
			FROM
				" . $AUTH['auth_m_mf_table'] . " mf
			WHERE
				mf.m_no='" . $AUTH['auth_m_no'] . "'
			GROUP BY
				mf.mb_id, mf.mf_is_read
			ORDER BY
				mf.mb_id, mf.mf_is_read
			";
		$rows = db_get_rows($sql);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// process data
		$DEBUG['proc_data']['start'] = getmicrotime();

		// 그룹결과 가져오기
		$mb_grp = array();
		$mb_grp_cnt = 0;
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$mb_grp[$mb_grp_cnt]['mb_id'] = $rows[$i]['mb_id'];
			$mb_grp[$mb_grp_cnt]['mf_cnt_unread'] += ($rows[$i]['mf_is_read'] == 'N') ? $rows[$i]['mf_cnt_all'] : 0;
			$mb_grp[$mb_grp_cnt]['mf_filesize_sum'] += $rows[$i]['mf_filesize_sum'];
			$mb_grp[$mb_grp_cnt]['mf_cnt_all'] += $rows[$i]['mf_cnt_all'];

			// 동일한 메일박스는 합산처리
			if ($mb_grp[$mb_grp_cnt]['mb_id'] != $rows[$i+1]['mb_id'])
				$mb_grp_cnt++;

		}	// for()

		// total
		$mb_total = array();
		for ($i=0; $i<sizeof($mb_grp); $i++)
		{
			$mb_total['mf_filesize_sum'] += $mb_grp[$i]['mf_filesize_sum'];
			$mb_total['mf_cnt_unread'] += $mb_grp[$i]['mf_cnt_unread'];
			$mb_total['mf_cnt_all'] += $mb_grp[$i]['mf_cnt_all'];
		}	// for()

		// 메일박스별로 삽입하기.
		$mb = get_mail_box();
		for ($i=0; $i<sizeof($mb); $i++)
		{
			for ($j=0; $j<sizeof($mb_grp); $j++)
			{
				if ($mb[$i]['mb_id'] == $mb_grp[$j]['mb_id'])
				{
					$mb[$i]['mf_cnt_unread'] = $mb_grp[$j]['mf_cnt_unread'];
					$mb[$i]['mf_filesize_sum'] = $mb_grp[$j]['mf_filesize_sum'];
					$mb[$i]['mf_cnt_all'] = $mb_grp[$j]['mf_cnt_all'];
					break;
				}	// if()
			}	// for()
		}	// for()

		// [2006-07-05] 회원 메일용량 할당량
		$m_quota = db_get_one("SELECT m_quota FROM " . $TB['MEMBER'] . " WHERE m_no='". $AUTH['auth_m_no'] . "'");
		$m_quota_view = filesize_human($m_quota*1024, 1);
		$mb_total['mf_filesize_sum_rate'] = ($mb_total['mf_filesize_sum'] > 0) ? ceil($mb_total['mf_filesize_sum'] / 1024 / $m_quota * 100) : 0;

		// view
		$mb_total_view = array();
		$mb_total_view['mf_filesize_sum'] = filesize_human($mb_total['mf_filesize_sum']);
		$mb_total_view['mf_cnt_unread'] = number_format($mb_total['mf_cnt_unread']);
		$mb_total_view['mf_cnt_all'] = number_format($mb_total['mf_cnt_all']);
		for ($i=0; $i<sizeof($mb); $i++)
		{
			// [2012-11-08] 사용량 합계에서 차지하는 비율 -> 사용한 용량 비율
			//$mb[$i]['mf_filesize_sum_rate'] = ($mb[$i]['mf_filesize_sum'] > 0) ? ceil($mb[$i]['mf_filesize_sum'] / $mb_total['mf_filesize_sum'] * 100) : 0;
			$mb[$i]['mf_filesize_sum_rate'] = ($mb[$i]['mf_filesize_sum'] > 0) ? ceil($mb[$i]['mf_filesize_sum'] / 1024 / $m_quota * 100) : 0;
			$mb[$i]['mf_filesize_sum'] = filesize_human($mb[$i]['mf_filesize_sum']);
			$mb[$i]['mf_cnt_unread'] = number_format($mb[$i]['mf_cnt_unread']);
			$mb[$i]['mf_cnt_all'] = number_format($mb[$i]['mf_cnt_all']);
		}	// for()

		$DEBUG['proc_data']['exec'] += getmicrotime() - $DEBUG['proc_data']['start'];


// skin view
	nskinset('webmail', 'mail_box.html');
	include($NSKIN['view_layout']);


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