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


if (!$G_SYS['NM_SMTP_SEND_LIMIT'])
	msg("발송수 제한 모듈이 탑재된 상태에서만 접근하실 수 있습니다.");


// set
	$CFG['TITLE'] = '<b>메일서버관리</b> >> <b>메일 발송 상태</b>';

//	$list_limit = 50;

    if ($_GET['excel_down'])
    {
        set_time_limit(3600);
        ini_set('memory_limit', '2047M');
        $list_limit = 999999;
    } else {
        $list_limit = 50;
    }

// data
	// [2025-08-22] 개별 검색 추가
	$CFG['SEARCH_LIST_FIELD'] = array(
		's_send_user' => '아이디',
		'm_name' => '이름',
		'm_no' => '회원번호',
	);

	$select_search_field = select_list_value($_GET['s_field'], array_values($CFG['SEARCH_LIST_FIELD']), array_keys($CFG['SEARCH_LIST_FIELD']), '전체', 0, 1);

	// search
		$q_where = "";

		if ($_GET['d_no'])
			$q_where .= " AND d.d_no='".$_GET['d_no']."' ";

		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);

			if ($_GET['s_field']) {
				switch ($_GET['s_field']) {
					case 's_send_user':
						$q_where .= " AND (s.s_send_user like '%" . $_GET['s_word'] . "%' OR m.m_id like '%" . $_GET['s_word'] . "%') ";
						break;

					case 'm_name':
						$q_where .= " AND m.m_name like '%" . $_GET['s_word'] . "%' ";
						break;

					case 'm_no':
						$q_where .= " AND s.m_no='" . $_GET['s_word'] . "' ";
						break;
				}
			} else {
				$q_where .= " AND (
							s.m_no ='" . $_GET['s_word'] . "' or s.s_send_user like '%" . $_GET['s_word'] . "%' OR m.m_id like '%" . $_GET['s_word'] . "%' 
							or m.m_name like '%" . $_GET['s_word'] . "%' ";
				$q_where .= " ) ";
			}

			$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
		}	// if()

		// 검색조건이 없을 때 기본적으로 오늘 로그만 보여주도록 함.
		if ($_GET['sdp'] != 'A' && !$_GET['sd1'] && !$_GET['sd2'])
		{
			$_GET['sdp'] = '0';
			$_GET['sd1'] = date('Y-m-d');
			$_GET['sd2'] = $_GET['sd1'];
		}	// if()

		if ($_GET['sd1'] && strtotime($_GET['sd1']))
		{
			$_ex = explode('-', $_GET['sd1']);
			$_GET['sd1'] = sprintf('%04d', $_ex[0]).'-'.sprintf('%02d', $_ex[1]).'-'.sprintf('%02d', $_ex[2]);
			if ($_GET['sd2'] && strtotime($_GET['sd2']))
			{
				$_ex = explode('-', $_GET['sd2']);
				$_GET['sd2'] = sprintf('%04d', $_ex[0]).'-'.sprintf('%02d', $_ex[1]).'-'.sprintf('%02d', $_ex[2]);
				$q_where .= " AND s_date >= '".$_GET['sd1']."' AND s_date <= '".$_GET['sd2']."' ";
			}	// if()
		}	// if()

		if ($_GET['l_status'] != '')
			$q_where .= " AND l_status='".$_GET['l_status']."' ";

	// sort
		$CFG['ADMIN_MEMBER_FIELD'] = array(
				'send_count' => '메일 발송수',
				'd_no' => '발송자 도메인',
				'm_id' => '발송자 아이디',
				'm_name' => '발송자 이름',
				'send_last' => '마지막 발송',
				'send_ip' => '발송 IP',
				);

		$order_set = get_order(array_keys($CFG['ADMIN_MEMBER_FIELD']), 'send_count ', $_GET['s_key'], $_GET['s_key2']);
		$order_img = get_order_img(array_keys($CFG['ADMIN_MEMBER_FIELD']), $_GET['s_key'], $_GET['s_key2'], false, 'common_skin');		// array

	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();
		$sql = "
			SELECT
				s.m_no, SUM(s.s_rcpt_count) as send_count, s.s_send_user, m.m_id, m.m_name, 
				d.d_no, s.s_send_dom, d.d_name
			FROM
				" . $TB['SMTP_SEND'] . " s LEFT JOIN
				".$TB['MEMBER']." m ON (m.m_no=s.m_no) LEFT JOIN
				".$TB['DOMAIN']." d ON (m.d_no=d.d_no)
			WHERE
				1=1
				" . $q_where . "
			GROUP BY
				 s.m_no, s.s_send_user, m.m_id, m.m_name, d.d_no, s.s_send_dom, d.d_name
			ORDER BY
				" . $order_set['query'] . "
			";
		$rows = db_get_rows($sql, 0, $list_limit);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// 도메인
		$q = "SELECT * FROM ".$TB['DOMAIN']." WHERE d_is_use='Y' ORDER BY d_name";
		$mbr_domain = db_get_rows($q, true);
		$mbr_domain_select = select_list_value($_GET['d_no'], $mbr_domain['d_name'], $mbr_domain['d_no'], '전체 도메인', 0, 1);

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();
		$today = date('Y-m-d');
		for ($i=0; $i<sizeof($rows); $i++)
		{
//			$q = "SELECT d_no, m_id, m_name FROM ".$TB['MEMBER']." WHERE m_no='".$rows[$i]['m_no']."'";
//			$mbr = db_get_row($q);

			if ($rows[$i]['s_send_user'] !== '' && $rows[$i]['s_send_user'] !== null) {
				$rows[$i]['m_id'] = $rows[$i]['s_send_user'];
			}

			if ($rows[$i]['s_send_dom'] !== '' && $rows[$i]['s_send_dom'] !== null) {
				$rows[$i]['d_name'] = $rows[$i]['s_send_dom'];
			}
//			$rows[$i]['d_no'] = $mbr['d_no'];
//			$rows[$i]['m_id'] = $mbr['m_id'];
//			$rows[$i]['m_name'] = $mbr['m_name'];

//			$q = "SELECT d_name FROM ".$TB['DOMAIN']." WHERE d_no='".$mbr['d_no']."'";
//			$rows[$i]['d_name'] = db_get_one($q);

			if ($_GET['sd1'] == $today && $_GET['sd2'] == $today)
			{
				$q = "SELECT MAX(s_time) FROM ".$TB['SMTP_SEND']." WHERE s_date='".$_GET['sd2']."' AND m_no='".$rows[$i]['m_no']."'";
				$rows[$i]['send_last'] = db_get_one($q);
				$rows[$i]['send_last_text'] = '마지막 발송 시간';

				$q = "SELECT s_ip, SUM(s_rcpt_count) send_count FROM ".$TB['SMTP_SEND']." WHERE s_date='".$_GET['sd2']."' AND m_no='".$rows[$i]['m_no']."' GROUP BY s_ip ORDER BY s_ip";
				$rows[$i]['send_ip'] = db_get_rows($q);
			} else 
			{
				$q = "SELECT MAX(s_date) FROM ".$TB['SMTP_SEND']." WHERE s_date >= '".$_GET['sd1']."' AND s_date <= '".$_GET['sd2']."' AND m_no='".$rows[$i]['m_no']."'";
				$rows[$i]['send_last'] = db_get_one($q);
				$rows[$i]['send_last_text'] = '마지막 발송 날짜';

				$q = "SELECT s_ip, SUM(s_rcpt_count) send_count FROM ".$TB['SMTP_SEND']." WHERE s_date >= '".$_GET['sd1']."' AND s_date <= '".$_GET['sd2']."' AND m_no='".$rows[$i]['m_no']."' GROUP BY s_ip ORDER BY s_ip";
				$rows[$i]['send_ip'] = db_get_rows($q);
			}	// if()
		}	// for()
		$rows = db_rows_fieldtokey($rows);
		$rows_cnt = getarraycount($rows['m_no']);

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

	// 메일 로그 뷰어수
		$admin_mailing_all_cnt = number_format($rows_cnt);

// skin view
if ($_GET['excel_down'])
{
    $excel_down_name = 'NmailAdminMailSentStat_' . $_GET['sd1'] . '_' . $_GET['sd2'] . '.xls';
    header("Content-type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=$excel_down_name");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public");

    include('sadmin/admin_mail_send_stat_excel.html');

    exit;
}

	nskinset('sadmin', 'admin_mail_send_stat.html');		// 컨텐츠
	include($NSKIN['view_layout']);


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