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


// set
	$CFG = array();
	#$CFG['TITLE'] = "<b>초기화면</b>";
	if ($G_SYS[DB] == 'oracle') 
	{
		$db_substr = 'SUBSTR';
	} else 
	{
		$db_substr = 'SUBSTRING';
	}	// if()


// data
	// [2006-07-25] 기본 설정
		$dfcon = get_default_config($AUTH['auth_m_no']);


	// 받은편지함 읽은/읽지않은 메일수
		get_mail_box();
		$mail_unread_cnt = 0;
		for ($i=0; $i<sizeof($G_SYS['USER_MAIL_BOX']); $i++)
		{
			if ($G_SYS['USER_MAIL_BOX'][$i]['mb_id'] == 'inbox')
			{
				$mail_unread_cnt = number_format($G_SYS['USER_MAIL_BOX'][$i]['mf_cnt']);
				break;			
			}	// if()
		}	// for()

		$mail_all_cnt = db_get_one("SELECT COUNT(*) FROM " . $AUTH['auth_m_mf_table'] . " WHERE m_no='". $AUTH['auth_m_no'] . "' AND mb_id='inbox'");
		$mail_all_cnt = number_format($mail_all_cnt);

	// 사용용량
		$mbr = db_get_row("SELECT m_quota, m_forwarding_type, m_autoreply_isuse, m_bigfile_quota, m_webhard_quota FROM " . $TB['MEMBER'] . " WHERE m_no='". $AUTH['auth_m_no'] . "'");
		$m_quota_view = filesize_human($mbr['m_quota']*1024, 1);
		$DEBUG['db_mf_size']['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) */" : "";
		$mf_filesize_sum = db_get_one("SELECT ".$q_hint." SUM(mf_filesize) FROM " . $AUTH['auth_m_mf_table'] . " WHERE m_no='". $AUTH['auth_m_no'] . "'");
		$DEBUG['db_mf_size']['exec'] += getmicrotime() - $DEBUG['db_mf_size']['start'];
		$mf_filesize_view = filesize_human($mf_filesize_sum);
		$m_quota_rate = ceil($mf_filesize_sum / ($mbr['m_quota']*1024) * 100);

	// [2008-03-24] 대용량첨부 용량
		$q = "SELECT ms_bigfile, ms_webhard FROM ".$TB['MEMBER_SIZE']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$mbr_size = db_get_row($q);

		$m_bigfile_quota = $mbr['m_bigfile_quota'];
		$m_bigfile_quota = $m_bigfile_quota*1024;
		$m_bigfile_quota_view = filesize_human($m_bigfile_quota, 1);

		$ms_bigfile = $mbr_size['ms_bigfile'];
		$ms_bigfile_view = filesize_human($ms_bigfile);

		$bigfile_free_size = $m_bigfile_quota - $ms_bigfile;
		if ($bigfile_free_size < 0)
			$bigfile_free_size = 0;

		$m_bigfile_quota_rate = @ceil($ms_bigfile / $m_bigfile_quota * 100);

	// [2008-03-24] 웹하드 용량
		$m_webhard_quota = $mbr['m_webhard_quota'];
		$m_webhard_quota = $m_webhard_quota*1024;
		$m_webhard_quota_view = filesize_human($m_webhard_quota, 1);

		$ms_webhard = $mbr_size['ms_webhard'];
		$ms_webhard_view = filesize_human($ms_webhard);

		$webhard_free_size = $m_webhard_quota - $ms_webhard;
		if ($webhard_free_size < 0)
			$webhard_free_size = 0;

		$m_webhard_quota_rate = @ceil($ms_webhard / $m_webhard_quota * 100);


	// [2008-03-25] 환경설정
		$q = "SELECT COUNT(*) FROM ".$TB['SPAM_ALLOW']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$m_spam_allow_cnt = number_format(db_get_one($q));

		$q = "SELECT COUNT(*) FROM ".$TB['SPAM_REJECT']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$m_spam_reject_cnt = number_format(db_get_one($q));

		$q = "SELECT COUNT(*) FROM ".$TB['AUTO_FILTER']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$m_auto_filter_cnt = number_format(db_get_one($q));

		$q = "SELECT COUNT(*) FROM ".$TB['POP3']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$m_pop3_cnt = number_format(db_get_one($q));



	// 공지사항
		$new_notice_limit = 5;		// 가져올 공지사항 수.
		$sql = "
			SELECT
				nt_no, nt_subject, nt_regdate
			FROM
				" . $TB['NOTICE'] . "
			WHERE
				d_no='" . $AUTH['auth_d_no'] . "' 
			ORDER BY
				nt_no DESC
			";
		$nt = db_get_rows($sql, 0, $new_notice_limit);
		$nt_view = array();
		for ($i=0; $i<sizeof($nt); $i++)
		{
			$nt_view[$i]['nt_subject'] = han_substr_view($nt[$i]['nt_subject'], 60);		
			$nt_view[$i]['nt_regdate'] = substr($nt[$i]['nt_regdate'], 5, 6);		
		}	// for()


	// 새로운 메일 - 최근 받은 날짜순으로 읽지 않은 메일만 가져옴.
		$new_mail_limit = $dfcon['m_cfg_new_cnt'];		// 가져올 메일 갯수.
		$DEBUG['get_new_mail_list']['start'] = getmicrotime();
		#$new_mail_box = array('inbox', 'spam');
		$new_mail_box = explode('|', $dfcon['m_cfg_new_mail_box']);

		// [2007-01-31] 스팸 필터 / 스팸 편지함을 생략하는 기능.  (기본 false)
		if ($G_SYS['SPAM_CHECK_SKIP'])
		{
			$new_mail_box_tmp = array();
			for ($i=0; $i<sizeof($new_mail_box); $i++)
			{
				if ($new_mail_box[$i] != 'spam')
				{
					$new_mail_box_tmp[] = $new_mail_box[$i];
				}	// if()
			}	// for()
			$new_mail_box = $new_mail_box_tmp;
		}	// if()

		$q_hint = '';
		$q_stopkey = '';
		if ($G_SYS[DB] == 'oracle')
		{
			$q_stopkey = " AND ROWNUM <= '".$new_mail_limit."' ";
			// [2006-09-14] 날짜 정렬이 제대로 안되던 문제 수정.
			$q_hint = "/*+ ORDERED index_asc (".$AUTH['auth_m_mf_table']." MF_".$AUTH['auth_d_no']."_".$AUTH['auth_m_mail_file_group']."_MF_ARRIVAL_DATE) */";
		}	// if()		
		$nm = array();
		$nm_cnt = array();
		for ($mb=0; $mb<sizeof($new_mail_box); $mb++)
		{
			// get mb_name
				for ($i=0; $i<sizeof($G_SYS['USER_MAIL_BOX']); $i++)
				{
					if ($G_SYS['USER_MAIL_BOX'][$i]['mb_id'] == $new_mail_box[$mb])
					{
						$new_mail_box_name[] = $G_SYS['USER_MAIL_BOX'][$i]['mb_name'];
						break;			
					}	// if()
				}	// for()

			// mysql 3 에서 역인덱스를 사용하지 못하는 성능상의 문제를 보완하기 위함.
				if ($G_SYS[DB] != 'oracle')
				{
					//$_mf_cnt_prev = 0;
					for ($i=1; $i<=4; $i++)
					{
						$_time = time();
						$_date = date('Y-m-d H:i:s', ($_time-(86400*round(pow(2.5,$i)))));
						$sql = "
							SELECT 
								COUNT(*) 
							FROM 
								".$AUTH['auth_m_mf_table']."
							WHERE
								m_no='". $AUTH['auth_m_no'] . "'
								AND mb_id='". $new_mail_box[$mb]. "'
								AND mf_arrival_date >= '".$_date."'
								AND mf_is_read='N'
							";
						$_mf_cnt = db_get_one($sql);
						// [2007-01-04] 읽지 않은 메일이 있는데도 일정 기간이전의 데이타를 못 가져오는 오류 수정.
						/* if ($_mf_cnt_prev > 0 && $_mf_cnt_prev == $_mf_cnt)
						{
							break;
						} else */
						if ($_mf_cnt >= $new_mail_limit)
						{
							$q_stopkey = " AND mf_arrival_date >= '".$_date."' ";
							break;
						} else 
						{
							// [2008-06-25] 읽지 않은 메일이 있는데도 일정 기간이전의 데이타를 못 가져오는 오류 수정. - 2
							$q_stopkey = '';							
						}	// if()
						//$_mf_cnt_prev = $_mf_cnt;
					}	// for()
				}	// if()

			$sql = "SELECT $q_hint mf_no, mf_from, mf_subject, mf_arrival_date FROM ".$AUTH['auth_m_mf_table']." $q_index WHERE m_no='". $AUTH['auth_m_no'] . "' AND mb_id='".$new_mail_box[$mb]."' " . $q_stopkey . " AND mf_is_read='N' ORDER BY mf_arrival_date DESC";
			$nm[$mb] = db_get_rows($sql, 0, $new_mail_limit);
			$nm_cnt[$mb] = sizeof($nm[$mb]);
			for ($i=0; $i<$nm_cnt[$mb]; $i++)
			{
				$nm[$mb][$i]['mf_subject'] = (trim($nm[$mb][$i]['mf_subject']) == NULL) ? '__제목없음__' : htmlspecialchars_fix(han_substr_view($nm[$mb][$i]['mf_subject'],70));
				$_addr = mime_address_parse($nm[$mb][$i]['mf_from']);
				$nm[$mb][$i]['mf_from'] = htmlspecialchars_fix(han_substr_view($_addr['display'], 20));
				$nm[$mb][$i]['mf_arrival_date'] = get_short_date($nm[$mb][$i]['mf_arrival_date']);
			}	// for()
		}	// for()
		$DEBUG['get_new_mail_list']['exec'] += getmicrotime() - $DEBUG['get_new_mail_list']['start'];


// [2008-08-19] 2.5 이전 버젼의 스킨에서 호환성 보장
	include('index_compat.inc.php');

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


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