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

	// debug only
		#if (!$_POST['q']) $_POST['q'] = $_GET['q'];		 echo '<xmp>';

	// set
		$limit = 10;

	// search
		$q_where = '';
		if ($_POST['q'] != '' && strlen($_POST['q']) <= 50)
		{
			if (strtoupper($G_SYS[CHARSET]) != 'UTF-8')
				$_POST['q'] = iconv_fix('UTF-8', $_POST['q'], $G_SYS[CHARSET]);
			set_q_search($_POST['q']);
			to_db_str(&$_POST['q'], false, false);
			$_POST['q'] = strtolower($_POST['q']);
			// [2008-06-12] 첫번째 문자열부터 검색하도록 변경함
			// [2008-07-04] 이름은 전체 문자열에서 검색하도록 변경
			$q_where = " AND ( ad_name LIKE '%" . $_POST['q'] . "%' OR ad_email LIKE '" . $_POST['q'] . "%' ) ";
			$q_where2 = " AND sa_email LIKE '" . $_POST['q'] . "%' ";
		} else
		{
			exit;
		}	// if()

	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();

		$get_emails = array();

		$rows = array();
		$rows_cnt = 0;
		$rows3 = array();
		$rows3_cnt = 0;


		// [2008-07-09] 조직도 사용여부
		if ($G['DINFO']['d_mbrgrp_isuse'] == 'Y' && !member_group_deny_check())
		{
			$q_m_id = '';
			if (strpos($_POST['q'], '@') !== false)
			{
				$_ex = explode('@', $_POST['q']);
				if ($_ex[1] == '')
				{
					$q_m_id = $_ex[0];
				} else if (strpos($G['DINFO']['d_name'], $_ex[1]) === 0)		// 다른 도메인일때는 회원테이블에서 검색할 필요없음.
				{
					$q_m_id = $_ex[0];
				}	// if()
				$q_m_id_like = " m.m_id = '" . $q_m_id . "' ";
			} else
			{
				$q_m_id = $_POST['q'];
				$q_m_id_like = " m.m_id LIKE '" . $q_m_id . "%' ";
			}	// if()

			if ($q_m_id != '')
			{
				if ($G_SYS[DB] == 'oracle')
				{
					$sql = "
						SELECT *
						FROM (
							SELECT
								m.m_name ad_name, m.m_id ad_email
								, ROW_NUMBER() OVER(PARTITION BY m.m_no ORDER BY m.m_no) rn
							FROM
								" . $TB['MEMBER'] . " m,
								".$TB['MEMBER_CLASS']." mc
							WHERE
								m.d_no='" . $G['DINFO']['d_no'] . "'
								AND (
									".$q_m_id_like."
									OR m.m_name LIKE '%" . $_POST['q'] . "%'
									)
								AND m.m_level > '".$G[LEVEL][wait]."'
								AND m.m_usertype IS NULL
								AND m.mg_no > 0
								AND m.mc_no=mc.mc_no(+)
							ORDER BY
								mc.d_no DESC, mc.mc_sort, m.m_name
						)
						 WHERE rn = 1
						";
				} else
				{
					// 직위, 이름 순서
					// [2008-08-01] 직위 생략 - CONCAT(CONCAT(m.m_name, ' '), mc.mc_name) ad_name, m.m_id ad_email
					// [2008-08-01] 조직이 설정된 회원만 검색
					$sql = "
						SELECT
							m.m_name ad_name, m.m_id ad_email
						FROM
							" . $TB['MEMBER'] . " m LEFT JOIN
							".$TB['MEMBER_CLASS']." mc ON (m.mc_no=mc.mc_no)
						WHERE
							m.d_no='" . $G['DINFO']['d_no'] . "'
							AND (
								".$q_m_id_like."
								OR m.m_name LIKE '%" . $_POST['q'] . "%'
								)
							AND m.m_level > '".$G[LEVEL][wait]."'
							AND m.m_usertype IS NULL
							AND m.mg_no > 0
						ORDER BY
							mc.d_no DESC, mc.mc_sort, m.m_name
						";
				}	// if()

				$rows3 = db_get_rows($sql, 0, $limit);
				$rows3_cnt = getarraycount($rows3);
				#vd($sql); vd($rows3);
			}	// if()
		}	// if()

		if ($rows3_cnt < $limit)
		{
			if ($rows3_cnt > 0)
			{
				for ($i=0; $i<sizeof($rows3); $i++)
				{
					// [2009-04-02] 주소록 자동완성시 이름에 특수문자(')가 들어간 경우의 오류 수정
					//$rows3[$i]['ad_email'] .= '@'.$G['DINFO']['d_name'];
					$rows3[$i]['ad_email'] = addslashes($rows3[$i]['ad_email'].'@'.$G['DINFO']['d_name']);
				}	// for()
				$_rows = db_rows_fieldtokey($rows3);
				$get_emails = $_rows['ad_email'];
				$q_where .= set_array2comma(" AND ad.ad_email NOT IN (", ") ", $get_emails);
			}	// if()

			// [2008-08-13] 공용주소록 사용여부
			$q_where_share = "";
			if ($G['DINFO']['d_shareaddr_isuse'] == 'Y')
				$q_where_share = " OR adg.d_no='" . $G['DINFO']['d_no'] . "' ";

			$sql = "
				SELECT
					ad.ad_name, ad.ad_email
				FROM
					" . $TB['ADDR_GROUP'] . " adg,
					" . $TB['ADDR'] . " ad
				WHERE
					(
						adg.m_no='" . $AUTH['auth_m_no'] . "'
						".$q_where_share."
					)
					AND adg.adg_no=ad.adg_no
					" . $q_where . "
				ORDER BY
					ad.ad_name, ad.ad_email
				";
			$rows = db_get_rows($sql, 0, $limit - $rows_cnt);
			$rows_cnt = getarraycount($rows);
			#vd($sql); vd($rows);
			$rows = array_merge($rows3, $rows);
			$rows_cnt += $rows3_cnt;
		}	// if()


		if ($rows_cnt < $limit)
		{
			if ($rows_cnt > 0)
			{
				$_rows = db_rows_fieldtokey($rows);

				for ($i=0; $i<sizeof($_rows['ad_email']); $i++)
				{
					if (array_search($_rows['ad_email'][$i], $get_emails) === false)
						$get_emails[] = addslashes($_rows['ad_email'][$i]);
						//$get_emails[] = $_rows['ad_email'][$i];
				}	// for()

				$q_where2 .= set_array2comma(" AND sa_email NOT IN (", ") ", $get_emails);
			}	// if()

			$sql = "
				SELECT
					sa_email ad_email
				FROM
					" . $TB['SPAM_ALLOW'] . "
				WHERE
					m_no='" . $AUTH['auth_m_no'] . "'
					$q_where2
				ORDER BY
					sa_email
				";
			$rows2 = db_get_rows($sql, 0, $limit - $rows_cnt);
			$rows2_cnt = getarraycount($rows2);
			#vd($sql); vd($rows2);
			$rows = array_merge($rows, $rows2);
			$rows_cnt += $rows2_cnt;
		}	// if()

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

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();
		for ($i=0; $i<$rows_cnt; $i++)
		{
			if (trim($rows[$i]['ad_name']) != '')
				echo '"' . str_replace('|!|', '', $rows[$i]['ad_name']) . '" <';
			echo str_replace('|!|', '', $rows[$i]['ad_email']);
			if (trim($rows[$i]['ad_name']) != '')
				echo '>';
			if ($i+1 != $rows_cnt)
				echo "|!|";
		}	// for()
		$DEBUG['db_fetch_all']['exec'] += getmicrotime() - $DEBUG['db_fetch_all']['start'];


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