<?
//*********************************** 접속로그 관련 ******************************************//
//N@LOG 프로그램에 있는 함수 차용 및 수정함. (os정보, browser정보 os+brower체크)
####################################################################################
//					os정보
####################################################################################
function getOSversion($os,$array){
	$os_version="";
	for($i=0;$i<sizeof($array);$i++){
		$j=$i+1;
		if(preg_match("/$os/i",$array[$i]) && preg_match("/^[0-9]{1,2}([\.]{1}[0-9]{1,2})*[a-z]{0,1}$/i",$array[$j])){
			$os_version=$array[$j];
		}	
	}
	return $os_version;
}

####################################################################################
//					browser정보
####################################################################################
function getBRversion($br,$array){
	$br_version="";
	for($i=0;$i<sizeof($array);$i++){
		$j=$i+1;
		if(preg_match("/$br/i",$array[$i]) && preg_match("/^[0-9]{1,2}([\.]{1}[0-9]{1,2})*[a-z]{0,1}$/i",$array[$j])){
			$br_version=$array[$j];
		}	
	}
	return $br_version;
}
####################################################################################
//					os+browser체크
####################################################################################
function getUserAgent(){

	$strAgent=$_SERVER["HTTP_USER_AGENT"];
	$strAgent=trim(preg_replace("~-|_|=|\+|;~"," ",$strAgent));

	$arrAgent=explode(" ",$strAgent);

	if(preg_match("/([a-z])+/i",$arrAgent[0])){
		$br_version_temp=explode("/",$arrAgent[0]);
	}
	$br_version_temp=$br_version_temp[1];

	if(preg_match("/Win|Window/",$strAgent)){
	$os_name="Windows";

		if(preg_match("/s 3\.1|n3\.1/",$strAgent)){
			$os_version="3.1";
		}

		if(preg_match("/s 95|n95/",$strAgent)){
			$os_version="95";
		}

		if(preg_match("/s 98|n98/",$strAgent)){
			$os_version="98";
		}	

		if(preg_match("/s NT|nNT/",$strAgent)){
			$os_version="NT";
		}

		if(preg_match("/s NT|nNT/",$strAgent) && preg_match("/T 5\.0| 2000/i",$strAgent)){
			$os_version="2000";
		}

		if(preg_match("/s NT|nNT/",$strAgent) && preg_match("/T 5\.1| XP/i",$strAgent)){
			$os_version="XP";
		}

		if(preg_match("/s CE|nCE/",$strAgent)){
			$os_version="CE";
		}

		if(preg_match("/s 9x|n 9x/",$strAgent) || preg_match("/me/i",$strAgent)){
			$os_version="Me";
		}
	}

	elseif(preg_match("/Mac PowerPC|PPC/i",$temp)){
		$os_name="Mac PowerPC";
		$os_version = getOSversion("Mac powerPC",$arrAgent);
	}

	elseif(preg_match("/Mac/",$temp)){
		$os_name="Macintosh";
		$os_version = getOSversion("Mac",$arrAgent);
	}

	elseif(preg_match("/Linux/",$temp)){
		$os_name="Linux";
		$os_version = getOSversion("Linux",$arrAgent);
	} 

	elseif(preg_match("/IRIX/",$temp)){
		$os_name="IRIX";
		$os_version = getOSversion("IRIX",$arrAgent);
	}

	elseif(preg_match("/sunOS/",$temp)){
		$os_name="sunOS";
		$os_version = getOSversion("sunOS",$arrAgent);
	}

	elseif(preg_match("/phone/",$temp)){
		$os_name="CellPhone";
		$os_version = getOSversion("phone",$arrAgent);
	}

	else{
		$os_name="Unknown";
		$os_version="";
	}


	if(preg_match("/MSN/i",$strAgent)){
		$br_name="MSN";
		$br_version=getBRversion("MSN",$arrAgent);
	}

	elseif(preg_match("/MSIE/i",$strAgent)){
		$br_name="MSIE";
		$br_version=getBRversion("MSIE",$arrAgent);
	}

	elseif(preg_match("/(\[){1}[a-z]{1,3}(\]){1}/i",$strAgent) && preg_match("/\]/i",$strAgent)){
		$br_name="Netscape";
		$br_version=$br_version_temp;
	}

	elseif(preg_match("/opera/i",$strAgent)){
		$br_name="Opera";
		$br_version=getBRversion("Opera",$arrAgent);
		if(!$br_version){
			$br_version=$br_version_temp;
		}
	}

	elseif(preg_match("/gec|gecko/i",$strAgent)){
		$br_name="Gecko";
		$br_version=getBRversion("Gecko",$arrAgent);
		if(!$br_version){
			$br_version=$br_version_temp;
		}
	}

	elseif(preg_match("/MSMB/i",$strAgent)){
		$br_name="MSMB";
	}

	else{
		$br_name="Unknown";
	}

	return array($os_name . $os_version, $br_name . $br_version);
}

//레퍼러에서 도메인 정보 가져오기
function getRefererDomain(){
	$arr = explode("/",$_SERVER["HTTP_REFERER"]);
	return strtolower($arr[2]);
}

//쿼리스트링에서 검색엔진과 검색엔진 키워드 알아내기
function getSearchKeyword($str_domain){
	//$str="http://search.cyworld.com/search/all.html?qn=&s=&f=&bd=&bw=&tq=&z=A&q=%B3%B2%B1%D4%B8%AE&premiumText=&s=";
	//$str_domain = "search.cyworld.com";

	//검색엔진별 쿼리 키워드
	$query="";
	$engin="";
	$keyword="";
	if(preg_match("/.naver.com$/i",$str_domain)){
			$engin = "naver.com";
			$query = "query";
	}else if(preg_match("/.daum.net$/i",$str_domain)){
			$engin = "daum.net";
			$query = "q";
	}else if(preg_match("/.cyworld.com$/i",$str_domain)){
			$engin = "cyworld.com";
			$query = "q";
	}else if(preg_match("/.yahoo.com$/i",$str_domain)){
			$engin = "yahoo.com";
			$query = "p";
	}else if(preg_match("/.nate.com$/i",$str_domain)){
			$engin = "nate.com";
			$query = "q";
	}else if(preg_match("/.paran.com$/i",$str_domain)){
			$engin = "paran.com";
			$query = "Query";
	}

	//검색엔진 추가시 위의 주석과 함께 테스트 해본다~
	//if($str){
	//	$arr = explode("&",$str);
	if($_SERVER["QUERY_STRING"]){
		$arr = explode("&",$_SERVER["QUERY_STRING"]);
		for($i=0;$i<count($arr);$i++){
			$arr2 = explode("=",$arr[$i]);
			if($arr2[0]==$query){
				$keyword = $arr2[1];
			}
		}
	}

	return array($engin, $keyword);
}

function insertLog(){
	// 테이블 지정
	$tbl_log = $GLOBALS["_conf_tbl"]["log"]["log"];
	$tbl_os = $GLOBALS["_conf_tbl"]["log"]["os"];
	$tbl_browser = $GLOBALS["_conf_tbl"]["log"]["browser"];
	$tbl_referer = $GLOBALS["_conf_tbl"]["log"]["referer"];
	$tbl_domain = $GLOBALS["_conf_tbl"]["log"]["domain"];
	$tbl_ip = $GLOBALS["_conf_tbl"]["log"]["ip"];
	$tbl_page = $GLOBALS["_conf_tbl"]["log"]["page"];
	$tbl_searchengin = $GLOBALS["_conf_tbl"]["log"]["searchengin"];
	$tbl_keyword = $GLOBALS["_conf_tbl"]["log"]["keyword"];
	$tbl_counter = $GLOBALS["_conf_tbl"]["log"]["counter"];

	//데이터 할당
	$arrAgent = getUserAgent();
	$str_os = $arrAgent[0];
	$str_browser = $arrAgent[1];
	$str_referer = mysql_real_escape_string(urldecode($_SERVER["HTTP_REFERER"]));
	if($str_referer==""){
		$str_referer = "직접입력";
	}

	$str_domain = getRefererDomain();
	if($str_domain==""){
		$str_domain = "직접입력";
	}

	$str_ip = $_SERVER["REMOTE_ADDR"];
	//$str_page = $_SERVER["SCRIPT_NAME"];
	$str_page = $_SERVER["REQUEST_URI"];
	$arrQuery = getSearchKeyword($str_domain);
	$str_searchengin = $arrQuery[0];
	$str_keyword = mysql_real_escape_string(urldecode($arrQuery[1]));

	//os 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_os WHERE os='$str_os'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$os_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_os set hit=hit+1 WHERE idx='$os_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_os SET os='$str_os', hit='1'", $GLOBALS["dblink"]);
		$os_idx = mysql_insert_id($GLOBALS["dblink"]);
	}

	//browser 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_browser WHERE browser='$str_browser'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$browser_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_browser set hit=hit+1 WHERE idx='$browser_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_browser SET browser='$str_browser', hit='1'", $GLOBALS["dblink"]);
		$browser_idx = mysql_insert_id($GLOBALS["dblink"]);
	}


	//referer 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_referer WHERE referer='$str_referer'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$referer_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_referer set hit=hit+1 WHERE idx='$referer_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_referer SET referer='$str_referer', hit='1'", $GLOBALS["dblink"]);
		$referer_idx = mysql_insert_id($GLOBALS["dblink"]);
	}


	//domain 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_domain WHERE domain='$str_domain'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$domain_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_domain set hit=hit+1 WHERE idx='$domain_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_domain SET domain='$str_domain', hit='1'", $GLOBALS["dblink"]);
		$domain_idx = mysql_insert_id($GLOBALS["dblink"]);
	}

	//ip 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_ip WHERE ip='$str_ip'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$ip_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_ip set hit=hit+1 WHERE idx='$ip_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_ip SET ip='$str_ip', hit='1'", $GLOBALS["dblink"]);
		$ip_idx = mysql_insert_id($GLOBALS["dblink"]);
	}

	//page 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_page WHERE page='$str_page'", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$page_idx = mysql_result($rs,0);
		mysql_query("UPDATE $tbl_page set hit=hit+1 WHERE idx='$page_idx'", $GLOBALS["dblink"]);
	}else{
		mysql_query("INSERT INTO $tbl_page SET page='$str_page', hit='1'", $GLOBALS["dblink"]);
		$page_idx = mysql_insert_id($GLOBALS["dblink"]);
	}

	//searchengin 정보 입력
	$searchengin_idx="";
	if($str_searchengin){
		$rs = mysql_query("SELECT idx FROM $tbl_searchengin WHERE searchengin='$str_searchengin'", $GLOBALS["dblink"]);
		$total = mysql_num_rows($rs);
		if($total > 0){
			$searchengin_idx = mysql_result($rs,0);
			mysql_query("UPDATE $tbl_searchengin set hit=hit+1 WHERE idx='$searchengin_idx'", $GLOBALS["dblink"]);
		}else{
			mysql_query("INSERT INTO $tbl_searchengin SET searchengin='$str_searchengin', hit='1'", $GLOBALS["dblink"]);
			$searchengin_idx = mysql_insert_id($GLOBALS["dblink"]);
		}
	}


	$keyword_idx = "";
	//keyword 정보 입력
	if($str_keyword){
		$rs = mysql_query("SELECT idx FROM $tbl_keyword WHERE keyword='$str_keyword'", $GLOBALS["dblink"]);
		$total = mysql_num_rows($rs);
		if($total > 0){
			$keyword_idx = mysql_result($rs,0);
			mysql_query("UPDATE $tbl_keyword set hit=hit+1 WHERE idx='$keyword_idx'", $GLOBALS["dblink"]);
		}else{
			mysql_query("INSERT INTO $tbl_keyword SET keyword='$str_keyword', hit='1'", $GLOBALS["dblink"]);
			$keyword_idx = mysql_insert_id($GLOBALS["dblink"]);
		}
	}

	//log 정보 입력
	$sql = "INSERT INTO $tbl_log SET
		browser='$browser_idx',
		domain='$domain_idx',
		referer='$referer_idx',
		ip='$ip_idx',
		searchengin='$searchengin_idx',
		keyword='$keyword_idx',
		os='$os_idx',
		page='$page_idx'
	";
	mysql_query($sql, $GLOBALS["dblink"]);

	//날짜설정
	$yyyy = date('Y');
	$mm = date('m');
	$dd = date('d');
	$week = date('w');
	$hh = date('G');
	//count 정보 입력
	$rs = mysql_query("SELECT idx FROM $tbl_counter WHERE yyyy='$yyyy' AND mm='$mm' AND dd='$dd' ", $GLOBALS["dblink"]);
	$total = mysql_num_rows($rs);
	if($total > 0){
		$counter_idx = mysql_result($rs,0);
		$hh_sql = "h".$hh."=h".$hh."+1, ";
		$sql = "UPDATE $tbl_counter SET 
			$hh_sql
			hit=hit+1
			WHERE idx='$counter_idx'
		";
		mysql_query($sql, $GLOBALS["dblink"]);
	}else{
		$hh_sql = "h".$hh."=h".$hh."+1, ";
		$sql = "INSERT INTO $tbl_counter SET 
			yyyy='$yyyy', 
			mm='$mm', 
			dd='$dd',
			week='$week',
			$hh_sql
			hit='1'
		";
		mysql_query($sql, $GLOBALS["dblink"]);
	}
}

//시간대별 접속자수 표시
function getAccessCounterHourly($s_date, $e_date){
	$tbl = $GLOBALS["_conf_tbl"]["log"]["counter"];

	$sql  = "SELECT ";
	for($i=0;$i<24;$i++){
		$sql .= "SUM(h".$i.") AS h".$i.", ";
	}
    $sql .= "SUM(hit) AS hit ";
    $sql .= "FROM $tbl ";
	$sql .=" WHERE concat(yyyy,'-',mm,'-',dd) >= '$s_date' AND concat(yyyy,'-',mm,'-',dd) <= '$e_date'";


    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_rs = mysql_num_rows($rs);
    
    if($total_rs > 0){
        $list['total'] = $total_rs;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    return $list;
}

//일별 접속자수 표시
function getAccessCounterDaily($s_date, $e_date){
	$tbl = $GLOBALS["_conf_tbl"]["log"]["counter"];


	//전체합계
	$sql  = "SELECT sum(hit) ";
    $sql .= "FROM $tbl ";
	$sql .=" WHERE concat(yyyy,'-',mm,'-',dd) >= '$s_date' AND concat(yyyy,'-',mm,'-',dd) <= '$e_date'";

    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $sum = mysql_result($rs,0,0);

	$sql  = "SELECT yyyy, mm, dd, week, hit ";
    $sql .= "FROM $tbl ";
	$sql .=" WHERE concat(yyyy,'-',mm,'-',dd) >= '$s_date' AND concat(yyyy,'-',mm,'-',dd) <= '$e_date'";

	//echo $sql;
    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_rs = mysql_num_rows($rs);
    
    if($total_rs > 0){
        $list['total'] = $total_rs;
        $list['sum'] = $sum;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
        $list['sum'] = 0;
    }
    return $list;
}

//월별 접속자수 표시
function getAccessCounterMonthly($s_date, $e_date){
	$tbl = $GLOBALS["_conf_tbl"]["log"]["counter"];

	$s_date = substr($s_date,0,7);
	$e_date = substr($e_date,0,7);

	//전체합계
	$sql  = "SELECT sum(hit) ";
    $sql .= "FROM $tbl ";
	$sql .=" WHERE concat(yyyy,'-',mm) >= '$s_date' AND concat(yyyy,'-',mm) <= '$e_date'";

    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $sum = mysql_result($rs,0,0);
	//echo $sql;


	$sql  = "SELECT yyyy, mm, sum(hit) as sum_hit ";
    $sql .= "FROM $tbl ";
	$sql .=" WHERE concat(yyyy,'-',mm) >= '$s_date' AND concat(yyyy,'-',mm) <= '$e_date' GROUP BY mm";

	//echo $sql;
    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_rs = mysql_num_rows($rs);
    
    if($total_rs > 0){
        $list['total'] = $total_rs;
        $list['sum'] = $sum;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
        $list['sum'] = 0;
    }
    return $list;
}

//접속통계 일반 쿼리
function getAccessCounterTable($tbl_type, $s_date, $e_date, $scale, $offset=0){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["log"]["log"];
	$tbl_browser = $GLOBALS["_conf_tbl"]["log"]["browser"];
	$tbl_domain = $GLOBALS["_conf_tbl"]["log"]["domain"];
	$tbl_referer = $GLOBALS["_conf_tbl"]["log"]["referer"];
	$tbl_ip = $GLOBALS["_conf_tbl"]["log"]["ip"];
	$tbl_searchengin = $GLOBALS["_conf_tbl"]["log"]["searchengin"];
	$tbl_keyword = $GLOBALS["_conf_tbl"]["log"]["keyword"];
	$tbl_os = $GLOBALS["_conf_tbl"]["log"]["os"];
	$tbl_page = $GLOBALS["_conf_tbl"]["log"]["page"];

	$sql  = "SELECT count(idx) ";
    $sql .= "FROM $tbl  ";
	$sql .=" WHERE wdate >= '$s_date 00:00:00' AND wdate <= '$e_date 23:59:59'";

    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_sum = mysql_result($rs,0,0);


	if($tbl_type=="os"){
		$sql = " SELECT B.os, count(A.idx) AS hit FROM $tbl A, $tbl_os B ";
		$sql .=" WHERE A.os=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.os ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="browser"){
		$sql = " SELECT B.browser, count(A.idx) AS hit FROM $tbl A, $tbl_browser B ";
		$sql .=" WHERE A.browser=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.browser ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="ip"){
		$sql = " SELECT B.ip, count(A.idx) AS hit FROM $tbl A, $tbl_ip B ";
		$sql .=" WHERE A.ip=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.ip ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="domain"){
		$sql = " SELECT B.domain, count(A.idx) AS hit FROM $tbl A, $tbl_domain B ";
		$sql .=" WHERE A.domain=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.domain ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="referer"){
		$sql = " SELECT B.referer, count(A.idx) AS hit FROM $tbl A, $tbl_referer B ";
		$sql .=" WHERE A.referer=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.referer ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="page"){
		$sql = " SELECT B.page, count(A.idx) AS hit FROM $tbl A, $tbl_page B ";
		$sql .=" WHERE A.page=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.page ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="searchengin"){
		$sql = " SELECT B.searchengin, count(A.idx) AS hit FROM $tbl A, $tbl_searchengin B ";
		$sql .=" WHERE A.searchengin=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.searchengin ";
		$sql .=" order by hit desc ";
	}else if($tbl_type=="keyword"){
		$sql = " SELECT B.keyword, count(A.idx) AS hit FROM $tbl A, $tbl_keyword B ";
		$sql .=" WHERE A.keyword=B.idx ";
		$sql .=" AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'";
		$sql .=" group by A.keyword ";
		$sql .=" order by hit desc ";
	}

    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_rs = mysql_num_rows($rs);

	//echo $sql;

    if($total_rs > 0){
        $list['total'] = $total_rs;
		$list['total_sum'] = $total_sum;
        // 페이지 네비게이션 오프셋 지정.
		if(!$offset){
			$offset=0;
		}else{
			$offset=$offset;
		}

		// offset 이 전체 게시물수보다 작을때 offset 을 전체게시물 - 페이지당 보여줄 글 수로 offset 설정
		if($total_rs<=$offset){
			$offset = $total_rs - $scale;
		}

		//scale 0 으로 지정시에는 전체 가져옴
		if($scale > 0){
			$sql .= " limit $offset,$scale ";
		}
		$rs = mysql_query($sql,$GLOBALS["dblink"]);
	
		// offset 을 이용한 limit 가 적용된 갯수
		$total = mysql_num_rows($rs);
		$list['list']['total'] = $total;
		// 페이지 네비게이션 오프셋 지정.
		    
        for($i=0; $i < $total; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
		$list['total_sum'] = 0;
    }
    
    return $list;
}


//접속로그 보기
function getAccessCounterLog($s_date, $e_date, $scale, $offset=0){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["log"]["log"];
	$tbl_browser = $GLOBALS["_conf_tbl"]["log"]["browser"];
	$tbl_domain = $GLOBALS["_conf_tbl"]["log"]["domain"];
	$tbl_referer = $GLOBALS["_conf_tbl"]["log"]["referer"];
	$tbl_ip = $GLOBALS["_conf_tbl"]["log"]["ip"];
	$tbl_searchengin = $GLOBALS["_conf_tbl"]["log"]["searchengin"];
	$tbl_keyword = $GLOBALS["_conf_tbl"]["log"]["keyword"];
	$tbl_os = $GLOBALS["_conf_tbl"]["log"]["os"];
	$tbl_page = $GLOBALS["_conf_tbl"]["log"]["page"];


	$sql = "SELECT A.idx, A.wdate, B.browser, C.domain, D.referer, E.ip, F.searchengin, G.keyword, H.os, I.page FROM $tbl A
	LEFT JOIN $tbl_browser B ON A.browser=B.idx
	LEFT JOIN $tbl_domain C ON A.domain=C.idx
	LEFT JOIN $tbl_referer D ON A.referer=D.idx
	LEFT JOIN $tbl_ip E ON A.ip=E.idx
	LEFT JOIN $tbl_searchengin F ON A.searchengin=F.idx
	LEFT JOIN $tbl_keyword G ON A.keyword=G.idx
	LEFT JOIN $tbl_os H ON A.os=H.idx
	LEFT JOIN $tbl_page I ON A.page=I.idx
	WHERE 1=1 
	AND A.wdate >= '$s_date 00:00:00' AND A.wdate <= '$e_date 23:59:59'
	";
	$sql .= " order by idx desc ";

    $rs = mysql_query($sql, $GLOBALS["dblink"]);
    $total_rs = mysql_num_rows($rs);

	//echo $sql;

    if($total_rs > 0){
        $list['total'] = $total_rs;
        // 페이지 네비게이션 오프셋 지정.
		if(!$offset){
			$offset=0;
		}else{
			$offset=$offset;
		}

		// offset 이 전체 게시물수보다 작을때 offset 을 전체게시물 - 페이지당 보여줄 글 수로 offset 설정
		if($total_rs<=$offset){
			$offset = $total_rs - $scale;
		}

		//scale 0 으로 지정시에는 전체 가져옴
		if($scale > 0){
			$sql .= " limit $offset,$scale ";
		}
		$rs = mysql_query($sql,$GLOBALS["dblink"]);
	
		// offset 을 이용한 limit 가 적용된 갯수
		$total = mysql_num_rows($rs);
		$list['list']['total'] = $total;
		// 페이지 네비게이션 오프셋 지정.
		    
        for($i=0; $i < $total; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    
    return $list;
}
?>
