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


// set
	if ($_GET['isadmin'])
	{
		auth_limit('admin');
		$CFG[TITLE] = '<b>관리자메뉴</b> >> <b>회원관리</b> >> <b>메일 포워딩 관리</b>';
		$_GET['d_no'] = $G['DINFO']['d_no'];
	} else
	{
		auth_limit('serveradmin');
		$CFG[TITLE] = '<b>메일서버관리</b> >> <b>전체회원관리</b> >> <b>메일 포워딩 관리</b>';
	}	// if()

	$CFG['m_forwarding_type'] = array(
		'N' => '사용안함',
		'Y' => '사용함 (메일 남기지 않음)',
		'C' => '사용함 (메일 남김)',
	);
// data

	// 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']);
		$q_where .= " AND  (m.m_id LIKE '%" . $_GET['s_word'] . "%' OR m.m_name LIKE '%" . $_GET['s_word']
				. "%' OR d.d_name LIKE '%" . $_GET['s_word'] . "%' OR m.m_forwarding LIKE '%" . $_GET['s_word'] . "%') ";
		$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
	}	// if()

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

	// sort
	$CFG['ADMIN_MEMBER_FIELD'] = array(
		'd.d_name' => '도메인',
		'm.m_id' => '아이디',
		'm.m_name' => '이름',
		'm.m_forwarding' => '포워딩 주소',
		'm.m_forwarding_type' => '메일 수신 상태',
	);

	if ($_GET['isadmin'])
	{
		unset($CFG['ADMIN_MEMBER_FIELD']['d_name']);
	}	// if()

	$order_set = get_order(array_keys($CFG['ADMIN_MEMBER_FIELD']), 'm.m_no ', $_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 - pager
		$DEBUG['db_cnt']['start'] = getmicrotime();
		$pager_params['totalItems'] = db_get_one("
			SELECT
				COUNT(*)
			FROM
			" . $TB['MEMBER'] . " m,
			" . $TB['DOMAIN'] . " d
			WHERE 
				m.d_no = d.d_no
				AND m_usertype IS NULL
			".$q_where);

        $pager_params['perPage'] = 20;

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

		// [2016-01-11] PHP 5버전대에서 Ampersand가 DEPRECATED처리되어 &삭제
		#$pager = &new Pager_Sliding($pager_params);
		// [2016-01-11] Ampersand가 DEPRECATED관련하여 PEAR Pager라이브러리 업그레이드 소스적용
		#$pager = new Pager_Sliding($pager_params);
		$pager = Pager::factory($pager_params);
		$pager_links = $pager->getLinks();
		$pager_limit = get_pager_limit($pager->getCurrentPageID(), $pager_params['perPage']);

	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();
		$sql = "
			SELECT 
			m.m_id, m.m_no, d.d_no, d.d_name, m.m_name, m.m_forwarding_type, m.m_forwarding
		FROM 
			" . $TB['MEMBER'] . " m,
			" . $TB['DOMAIN'] . " d
		WHERE 
			m.d_no = d.d_no
			AND m_usertype IS NULL
			".$q_where . "
			ORDER BY
				" . $order_set['query'] . "
		";
		$rows = db_get_rows($sql, $pager_limit['from'], $pager_limit['count']);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// 도메인
		if ($_GET['isadmin'])
		{
			$mbr_domain_select = NULL;
		} else
		{
			$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);
		}	// if()

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$rows[$i]['l_date'] = $rows[$i]['last_logindate'];

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

			if (strpos($rows[$i]['m_forwarding'], ',') !== false) {
				$_ex = explode(',', $rows[$i]['m_forwarding']);

				for ($j=0; $j<sizeof($_ex); $j++) {
					$rows[$i]['forwarding'][$j] .= trim($_ex[$j]);
					list($rows[$i]['m_usertype'][$j], $rows[$i]['status'][$j], $rows[$i]['forward_m_name'][$j]) = get_email_status($_ex[$j]);
				}
			} else {
				// ','가 없으면 그대로 문자열 유지
				$rows[$i]['forwarding'][0] = $rows[$i]['m_forwarding'];

				// 공백일 경우에는 빈값으로 보여주기 위해서 추가
				if ($rows[$i]['forwarding'][0] !== '' && $rows[$i]['forwarding'][0] !== null) {
					list($rows[$i]['m_usertype'][0], $rows[$i]['status'][0], , $rows[$i]['forward_m_name'][0]) = get_email_status(
						$rows[$i]['m_forwarding']
					);
				} else {
					list($rows[$i]['m_usertype'][0], $rows[$i]['status'][0]) = array(null, null);
				}
			}

		}	// for()
		$rows = db_rows_fieldtokey($rows);
		$rows_cnt = getarraycount($rows['l_date']);

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

	// 메일 포워딩 리스트 뷰어수
		$admin_mailing_all_cnt = number_format($pager_params['totalItems']);

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


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