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


// set
	$CFG = array();
	$CFG['TITLE'] = '<b>편지쓰기</b> >> <b>미리보기</b>';

// data
	// process data
		$view = array();

		$view['mf_subject'] = htmlspecialchars_fix(stripslashes($_POST['mf_subject']));

		$view['mf_importance'] = htmlspecialchars_fix(stripslashes($_POST['mf_importance']));

		// [2005-07-12] 보낸날짜가 없을때는 도착날짜를 사용함.
		$view['mf_send_date'] = date('Y-m-d H:i:s');

		// [2013-01-28] 보낸사람 선택기능 (앨리어스 도메인/주소 연동)
		if ($_POST['mf_from_select'] != '' && $_POST['mf_from_select'] != $AUTH['auth_m_email'])
		{
			$_ex = explode('@', $_POST['mf_from_select']);

			// [2026-01-05] 도메인이 다를 경우 알리아스 도메인이므로 기존 계정의 m_cfg_from_name 가져오도록 추가
			if ($_ex[1] != $G_SYS['MAIL_DOMAIN']) {
				$q = "SELECT m_cfg_from_name name FROM " . $TB['MEMBER'] . " WHERE d_no='" . $G['DINFO']['d_no'] . "' AND m_id='" . $_ex[0] . "'";
				$mf_from_name = db_get_one($q);
			} else {
				$q = "SELECT m_cfg_from_name name FROM " . $TB['MEMBER'] . " WHERE d_no='" . $G['DINFO']['d_no'] . "' AND m_usertype='A' AND m_name='" . $AUTH['auth_m_id'] . "' AND m_id='" . $_ex[0] . "'";
				$mf_from_name = db_get_one($q);
			}

			// [2025-12-01] 대표 계정 사용 여부
			if ($G_SYS['NM_TEAM_MAIL']) {
				// m_name 가져오기
				$q = "SELECT
									tm.* , m.m_name
								FROM
									" . $TB['TEAM_MAIL'] . " tm
									JOIN " . $TB['MEMBER'] . " m on tm.d_no = m.d_no AND tm.team_m_id = m.m_id
								WHERE
									1=1
									AND tm.d_no = '" . $G['DINFO']['d_no'] . "' 
									and tm.m_id = '" . $AUTH['auth_m_id'] . "'
									AND tm.team_m_id = '" . $_ex[0] . "' 
									AND tm.tm_is_from_use = 'Y'
								";

				$team_mail = db_get_row($q);

				if ($team_mail) {
					$mf_from_name = $team_mail['m_name'];
				}
			}

			$view['mf_from'] = htmlspecialchars_fix('"' . $mf_from_name . '" <' . $_POST['mf_from_select'] . '>');
		} else
		{
			// [2025-11-10] 보낸사람 이름 설정 시 반영
			$q = "SELECT m_cfg_from_name name FROM " . $TB['MEMBER'] . " WHERE m_no='" . $AUTH['auth_m_no']  . "'";
			$m_cfg_from_name = db_get_one($q);

			if ($m_cfg_from_name != '') {
				$view['mf_from'] = htmlspecialchars_fix('"' . $m_cfg_from_name . '" <' . $AUTH['auth_m_email'] . '>');
			} else {
				$view['mf_from'] = htmlspecialchars_fix('"' . $AUTH['auth_m_name'] . '" <' . $AUTH['auth_m_email'] . '>');
			}
		}	// if()

		// 주소에서 마지막 콤마 삭제
		$view['mf_to'] = htmlspecialchars_fix(stripslashes(rtrim($_POST['mf_to'], ',')));
		$view['mf_cc'] = htmlspecialchars_fix(stripslashes(rtrim($_POST['mf_cc'], ',')));
		//$view['mf_bcc'] = htmlspecialchars_fix(stripslashes($_POST['mf_bcc']));

		// 서명 붙여넣기
		$sg_content = '';
		if ($_POST['sg_no'])
		{
			$sg_content = db_get_one("SELECT sg_content FROM " . $TB['SIGN'] . " WHERE sg_no='" . $_POST['sg_no'] . "' AND m_no='" . $AUTH['auth_m_no'] . "'");
		}	// if()

		if ($_POST['mf_content_type'] == 'H')
		{
			$view['mf_body'] = '<style><!-- P, UL, LI { margin-top: 0px; margin-bottom: 0px; } --></style>'."\n".stripslashes($_POST['mf_body_html']);
			if ($sg_content)
				$view['mf_body'] .= '<br>' . get_sg_content($sg_content, $_POST['mf_content_type']);

			// [2009-02-09] 편지지
			if ($_POST['mf_body_letter'] != '' && is_numeric($_POST['mf_body_letter']))
			{
				$q = "SELECT * FROM ".$TB['LETTER']." WHERE l_no='".$_POST['mf_body_letter']."' AND l_isuse='Y'";
				$letter = db_get_row($q);
				if ($letter)
				{
					$letter_layout = 'letter/layout.inc.html';
					$letter['body'] = $view['mf_body'];
					ob_start();
					include($letter_layout);
					$view['mf_body'] = ob_get_contents();
					ob_end_clean();
				}	// if()
			}	// if()
		} else
		{
			$view['mf_body'] = stripslashes($_POST['mf_body_text']);
			if ($sg_content)
				$view['mf_body'] .= "\n" . get_sg_content($sg_content, $_POST['mf_content_type']);
//			$view['mf_body'] = nl2br(htmlspecialchars_fix($view['mf_body']));
		}	// if()


		// 첨부파일
		// //||gitlab.png|1762409088.8586|0
		$view['is_attachment'] = $_POST['mf_attach_names'] != NULL || $_POST['mf_add_attach_names'] != NULL;

		// 원문 첨부파일 - 일반 첨부임
		$view['mf_attach_names'] = array();
		// mf_add_attach_names' => string '||gitlab.png|1762409008.8775.0|4667||gitlab.png|1762409008.8775.0|4667'

		$_mf_add_attach_names = explode('||', $_POST['mf_add_attach_names']);
		// 중복 표시 방지를 위해 파일명 저장
		$fileNames = array();
		for ($i = 0; $i < count($_mf_add_attach_names); $i++) {
			if ($_mf_add_attach_names[$i] != '') {
				$files = explode('|', $_mf_add_attach_names[$i]);
				if (sizeof($files) > 1) {
					$file = $UPLOAD_ROOT . '/' . $UPLOAD_DIR[BIGFILE_TMP] . '/' . $files[1];
					$fileNames[] = $files[1];
					if (file_exists($file)) {
						$filesize                                = filesize($file);
						$view['mf_attach_names']['name'][]       = $files[0];
						$view['mf_attach_names']['name_url'][]   = $files[0];
						$view['mf_attach_names']['size'][]       = $filesize;
						$view['mf_attach_names']['size_human'][] = filesize_human($filesize);
						$view['mf_attach_names']['tmp_file'][]   = $files[1];
					}
				}
			}
		}

		// 첨부파일
		$wfs = array();
		$wfs_cnt = 0;
		$wfs_size = 0;
		$bf_tmp_list = array();
		$bf_key_list = array();
		$bf_size_list = array();

		$_mf_attach_names = explode('||', trim($_POST['mf_attach_names']));
		for ($i=0; $i<sizeof($_mf_attach_names); $i++) {
			if ($_mf_attach_names[$i] != '') {
				$files = explode('|', $_mf_attach_names[$i]);
				if (sizeof($files) > 1) {
					$file = $UPLOAD_ROOT . '/' . $UPLOAD_DIR[BIGFILE_TMP] . '/' . $files[1];
					if ($files[2] !== '1') {
						// 일반 첨부 중 mf_add_attach_names에서 처리한 파일은 처리하지 않기 위해 조건 추가
						if (! in_array($files[1], $fileNames)) {
							if (file_exists($file)) {
								$filesize                                = filesize($file);
								$view['mf_attach_names']['name'][]       = $files[0];
								$view['mf_attach_names']['name_url'][]   = $files[0];
								$view['mf_attach_names']['size'][]       = $filesize;
								$view['mf_attach_names']['size_human'][] = filesize_human($filesize);
								$view['mf_attach_names']['tmp_file'][]   = $files[1];
							}
						}
					} else { // [2025-11-10] 대용량첨부 별도 표시
						$wf                = array();
						$wf['wf_name_tmp'] = $files[1];
						$wf['wf_name']     = basename_fix($files[0]);
						$wf['wf_key']      = 0;
						$wf['wfd_key']     = 0;
						$wf['wf_size']     = filesize($file);
						$wf['bf_type']     = 'B';
						$wfs[]             = $wf;
						$wfs_size          += $wf['wf_size'];

						$bf_tmp_list[]  = basename_fix($file);
						$bf_size_list[] = $wf['wf_size'];
					}
				}
			}
		}

		// 웹하드 첨부
		if (isset($_POST['webhard_wf_key']))
		{
			require_once("include/webhard_lib.php");

			for ($i=0; $i<sizeof($_POST['webhard_wfd_key']); $i++)
			{
				// file list
				$q = "
						SELECT
							*
						FROM
							".$TB['WEBHARD_FILE']."
						WHERE
							wfd_key='".$_POST['webhard_wfd_key'][$i]."' 
						  AND wf_key='".$_POST['webhard_wf_key'][$i]."'
						ORDER BY
							wf_name ASC
						";
				$wf = db_get_row($q);

				// 웹하드 정보확인 및 권한제한
				$ws = webhard_get_share($_POST['webhard_wfd_key'][$i], $_POST['webhard_ws_key'][$i], 'R');

				$wf['bf_type'] = 'W';
				$wfs[] = $wf;
				$wfs_size += $wf['wf_size'];
			}	// for()
		}	// if()

		$wfs_cnt = sizeof($wfs);
		// 예약발송
		$view['is_reserve'] = $_POST['is_reserve'] == 1 ? true : false;

		if ($view['is_reserve']) {

			$mf_reserve_date = htmlspecialchars_fix(stripslashes($_POST['mf_reserve_date']));
			$mf_reserve_time_h = htmlspecialchars_fix(stripslashes($_POST['mf_reserve_time_h']));
			$mf_reserve_time_m = htmlspecialchars_fix(stripslashes($_POST['mf_reserve_time_m']));
			$view['mf_send_date'] = $mf_reserve_date . ' ' . $mf_reserve_time_h . ':' . $mf_reserve_time_m . ':00';
		}

		// [2025-11-06] 발송 전 미리보기
		$q = "SELECT m_send_preview FROM " . $TB['MEMBER'] . " WHERE m_no='" . $AUTH['auth_m_no'] . "'";
		$send_preview = db_get_one($q);
		$view['m_send_preview'] = radio_value('m_send_preview', $send_preview, array_values($G[ISUSE]), array_keys($G[ISUSE]), NULL, 0, 1);

		// [2025-11-10] 대용량 첨부 별도 표시
		if ($wfs_cnt) {
			ob_start();
			if ($_POST['mf_content_type_now'] == 'H') {
				include(nskindir('webmail') . '/mail_write_preview_attach_html.inc.html');
			} else {
				include(nskindir('webmail'). '/mail_write_preview_attach_text.inc.html');
			}
			// [2018-11-12] 대용량첨부 링크 조정변수 추가
			if($G_SYS['BIGFILE_DOWNLOAD_LINK_POSITION']==="TOP"){
				$view['mf_body'] = ob_get_contents().$view['mf_body'];
			}else{
				$view['mf_body'] .= "<br style=\"line-height:10px\">".ob_get_contents();
			}
			ob_end_clean();
		}

		// [2025-11-10] text 모드 줄바꿈 처리
		if ($_POST['mf_content_type_now'] != 'H') {
			$view['mf_body'] = nl2br(htmlspecialchars_fix($view['mf_body']));
		}


// skin view
	$layout = ($_GET['modal']) ? 'modal' : 'window_open';
	nskinset('webmail', 'mail_write_preview.html', 'modal');
	include($NSKIN['view_layout']);

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