<?php
// m3 google sitemap ver 1.22 by mahler83 2009-07-30
// please give feedbacks to bomool.net
// Edit by happyjung.com  2017-01-17  https://www.happyjung.com/contents/item.php?it_id=1539566838
// Update 2020-11-30 17:38 ver.33

// 그누보드 common.php 까지의 경로를 설정하세요
include_once("./common.php");

//메모리 제한 늘리기
//ini_set('memory_limit','512M'); // 512MB
ini_set('memory_limit','-1'); // 무제한

// SITEMAP 갯수 ::: 임의로 추출하고자 하는 경우에는 아래에 숫자를 넣으세요
// 숫자를 지우면 환경설정의 게시글 노출 개수가 적용
$sitemap_rows = "";

// 영카트 노출개수 ::: 임의로 추출하고자 하는 경우에는 아래에 숫자를 넣으세요
// 숫자를 지우면 환경설정의 게시글 노출 개수가 적용
$youngcart_rows = "";

// 짧은주소를 사용할때는 Y , 사용 안할때는 N
// 그누보드 5.4 이상인 경우 그누보드 환경설정의 짧은주소 설정값이 우선 반영됩니다.
$sort_link = "N";

// 검색조건
$search  = " where bo_read_level='1'  "; // 읽기 Level=1 <<== 필수
$search .= " and bo_list_level='1' "; // 목록 Level=1
$search .= " and bo_use_search='1' "; // 검색 Level=1

//////////////////////////////////////////////////////////////////////////
///////////////         아래는 수정사항이 없습니다.        ///////////////
//////////////////////////////////////////////////////////////////////////

$result = sql_query(" select bo_table from `{$g5['board_table']}` {$search} order by bo_order asc ");
for ($i=0; $row=sql_fetch_array($result); $i++) {
	$bo[$i] = $row['bo_table'];
}

if (!$i) {
	echo "<meta charset='utf-8'>";
	echo '검색조건을 확인해주세요. sitemap.php 27~29줄';
	exit;
}

header("Content-type: text/xml;charset=utf-8");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
//echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
//echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\">";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";

if (G5_GNUBOARD_VER > "5.4") {
	if ($config['cf_bbs_rewrite']=='1') { // 짧은주소(숫자형)
		$sort_link = "Y";
		$sort_link2 = "1";
	} elseif ($config['cf_bbs_rewrite']=='2') { // 짧은주소(문자형)
		$sort_link = "Y";
		$sort_link2 = "2";
	}
}

if (!$sitemap_rows) {
	$sitemap_rows = $config['cf_page_rows']; // 환경설정의 게시글 기본 추출수
}

if (!$youngcart_rows) {
	$youngcart_rows = $config['cf_page_rows']; // 환경설정의 게시글 기본 추출수
}

// 아미나빌더인가요?
$field_query = "SHOW COLUMNS FROM {$g5['config_table']} WHERE `Field`='as_thema';";
$field_row = sql_fetch( $field_query );
if($field_row['Field']) { // 아미나빌더가 있으면
	$g5_builder = "amina";
}

$today = date("Y-m-d")."T".date("h:i:s")."+00:00";



// 최신글 추출하기
$qry_new = sql_query(" select * from `{$g5['board_new_table']}` order by bn_id desc limit 0, {$sitemap_rows} ", false);
for ($i=0; $row_new=sql_fetch_array($qry_new); $i++) {

	$tmp_write_table = $g5['write_prefix'].$post['bo_table']; 

	// 댓글을 제외하기
	if ($row_new['wr_id'] == $row_new['wr_parent']) {
		$sql_board = "select bo_table from `{$g5['board_table']}` {$search} and bo_table='{$row_new['bo_table']}' ";
		$tot_board = sql_fetch_array(sql_query($sql_board));
		
		if ($tot_board['bo_table']) {
			$row = sql_fetch_array(sql_query("select * from `{$tmp_write_table}` where wr_id='{$row_new['wr_id']}' AND wr_option NOT LIKE '%secret%' "));
			
			if ($row['wr_datetime']=="" || $row['wr_datetime']=="0000-00-00" || $row['wr_datetime']=="0000-00-00 00:00:00" || $row['wr_datetime']=="0000-00-00 00:00:00.000") {
				$date = $today;
			//} elseif ($row['wr_last'] && !($row['wr_last']=="0000-00-00" || $row['wr_last']=="0000-00-00 00:00:00" || $row['wr_last']=="0000-00-00 00:00:00.000")) {
			//	$date = substr($row['wr_last'], 0, 10)."T".substr($row['wr_last'], 11, 20)."+00:00";
			} else {
				$date = substr($row['wr_datetime'], 0, 10)."T".substr($row['wr_datetime'], 11, 20)."+00:00";
			}
			
			if ($sort_link == "Y") {
				if ($sort_link2 == "2") {
					$board_url = G5_URL."/{$row_new['bo_table']}/{$row['wr_seo_title']}/"; // 짧은주소 (문자형)
				} else {
					$board_url = G5_URL."/{$row_new['bo_table']}/{$row_new['wr_id']}"; // 짧은주소 (숫자형)
				}
			} else {
				$board_url = G5_BBS_URL."/board.php?bo_table={$row_new['bo_table']}&amp;wr_id={$row_new['wr_id']}";
			}

			echo "<url>\n" ;
			echo "<loc>{$board_url}</loc>\n";
			echo "<lastmod>{$date}</lastmod>\n";
			echo "<changefreq>monthly</changefreq>\n";
			echo "<priority>1.0</priority>\n";
			echo "</url>\n";
		}
	}
}


// 게시판
$sql_board = "select bo_table from `{$g5['board_table']}` {$search} order by bo_order asc";
$tot = sql_num_rows(sql_query($sql_board));
if ($tot > 1) {

	$qry_board = sql_query($sql_board);
	while($row_board = sql_fetch_array($qry_board)) {
		$bo_arr[] = $row_board['bo_table'];
	}

	$i = 1;

	foreach($bo_arr as $bo) {

		// 게시판 타이틀
		$date = $today;

		if ($sort_link=="Y") {
			$board_url = G5_URL ."/{$bo}"; // 짧은주소
		} else {
			$board_url = G5_BBS_URL."/board.php?bo_table={$bo}";
		}
		
		echo "<url>\n";
		echo "<loc>{$board_url}</loc>\n";
		echo "<lastmod>{$date}</lastmod>\n";
		echo "<changefreq>monthly</changefreq>\n";
		echo "<priority>1.0</priority>\n";
		echo "</url>\n";


		// 게시글 추출
		//$qry = sql_query("select wr_id, wr_last from `{$g5['write_prefix']}{$bo}` where wr_is_comment='0' AND wr_option NOT LIKE '%secret%' order by wr_id DESC limit 100 ");
		$qry = sql_query("select * from `{$g5['write_prefix']}{$bo}` where wr_is_comment='0' AND wr_option NOT LIKE '%secret%' order by wr_id DESC limit 0, {$sitemap_rows} ", false);
		while($row = sql_fetch_array($qry)) {
			
			$row_date = sql_fetch("select wr_datetime,wr_last from `{$g5['write_prefix']}{$bo}` where wr_parent='{$row['wr_id']}' order by wr_id DESC");
			
			if ($row_date['wr_datetime']=="" || $row_date['wr_datetime']=="0000-00-00" || $row_date['wr_datetime']=="0000-00-00 00:00:00" || $row_date['wr_datetime']=="0000-00-00.000") {
				$date = $today;
			//} elseif ($row_date['wr_last'] || !($row_date['wr_last']=="0000-00-00" || $row_date['wr_last']=="0000-00-00 00:00:00" || $row_date['wr_last']=="0000-00-00.000")) {
			//	$date = substr($row_date['wr_last'], 0, 10)."T".substr($row_date['wr_last'], 11, 20)."+00:00";
			} else {
				$date = substr($row_date['wr_datetime'], 0, 10)."T".substr($row_date['wr_datetime'], 11, 20)."+00:00";
			}

			if ($sort_link=="Y") {
				if ($sort_link2 == "2") {
					$board_url = G5_URL."/{$bo}/{$row['wr_seo_title']}/"; // 짧은주소 (문자형)
				} else {
					$board_url = G5_URL."/{$bo}/{$row['wr_id']}"; // 짧은주소 (숫자형)
				}
			} else {
				$board_url = G5_BBS_URL."/board.php?bo_table={$bo}&amp;wr_id={$row['wr_id']}";
			}
			
			echo "<url>\n";
			echo "<loc>{$board_url}</loc>\n";
			echo "<lastmod>{$date}</lastmod>\n";
			echo "<changefreq>daily</changefreq>\n";
			echo "<priority>1.0</priority>\n";
			echo "</url>\n";
		}
		$i++;
	}
}


// 영카트
if (defined('G5_YOUNGCART_VER')) {
	// 분류 추출하기
	$qry = sql_query("select ca_id from `{$g5['g5_shop_category_table']}` where ca_use='1' order by ca_id desc limit 0, {$youngcart_rows} ", false);
	while($row = sql_fetch_array($qry)) {
		
		$date=$today;
		
		if ($sort_link=="Y") {
			if ($sort_link2 == "2") {
				$board_url = G5_SHOP_URL."/list-{$row['ca_id']}"; // 짧은주소 (문자형)
			} else {
				$board_url = G5_SHOP_URL."/list-{$row['ca_id']}"; // 짧은주소 (숫자형)
			}
		} else {
			$board_url = G5_SHOP_URL."/list.php?ca_id={$row['ca_id']}";
		}
		
		echo "<url>\n";
		echo "<loc>{$board_url}</loc>\n";
		echo "<lastmod>{$date}</lastmod>\n";
		echo "<changefreq>daily</changefreq>\n";
		echo "<priority>1.0</priority>\n";
		echo "</url>\n";
	}

	// 상품 추출하기
	$qry = sql_query("select it_id, it_time from `{$g5['g5_shop_item_table']}` where it_use='1' order by it_time desc limit 0, {$youngcart_rows} ", false);
	while($row = sql_fetch_array($qry)) {
		
		if ($row['it_time']=="" || $row['it_time']=="0000-00-00") {
			$date = $today;
		} else {
			$date = substr($row['it_time'], 0, 10)."T".substr($row['it_time'], 11, 20)."+00:00";
		}
		
		if ($sort_link=="Y") {
			if ($sort_link2 == "2") {
				$board_url =  G5_SHOP_URL."/{$row['it_id']}"; // 짧은주소 (문자형)
			} else {
				$board_url =  G5_SHOP_URL."/{$row['it_id']}"; // 짧은주소 (숫자형)
			}
		} else {
			$board_url = G5_SHOP_URL ."/item.php?it_id={$row['it_id']}";
		}

		// 특정상품만 노출 안시키고 싶을때
		if ($row['it_id']=="특정상품아이디") {
		} else {
			echo "<url>\n";
			echo "<loc>{$board_url}</loc>\n";
			echo "<lastmod>{$date}</lastmod>\n";
			echo "<changefreq>daily</changefreq>\n";
			echo "<priority>1.0</priority>\n";
			echo "</url>\n";
		}
	}

	// 영카트 사용후기
	$is_file_exist = file_exists(G5_SHOP_PATH.'/itemuseview.php');
	if ($is_file_exist) {
		//echo 'Found it';
		$qry = sql_query("select b.it_id, b.it_name, b.it_basic, a.is_subject, a.is_id, a.is_name, a.is_content, a.is_time from `{$g5['g5_shop_item_use_table']}` a
				 left join `{$g5['g5_shop_item_table']}` b on (a.it_id = b.it_id)
				 where b.it_use='1' and a.is_confirm='1' order by a.is_time desc limit 0, {$sitemap_rows} ", false);
		while($row = sql_fetch_array($qry)) {
			
			if ($row['is_time']=="" || $row['is_time']=="0000-00-00") {
				$date = $today;
			} else {
				$date = substr($row['is_time'], 0, 10)."T".substr($row['is_time'], 11, 20)."+00:00";
			}
			
			$board_url = G5_SHOP_URL."/itemuseview.php?is_id={$row['is_id']}";
			
			echo "<url>\n";
			echo "<loc>{$board_url}</loc>\n";
			echo "<lastmod>{$date}</lastmod>\n";
			echo "<changefreq>daily</changefreq>\n";
			echo "<priority>1.0</priority>\n";
			echo "</url>\n";

		}
	}


	// 영카트 사용문의
	$qry = sql_query("select b.it_id, b.it_name, b.it_basic, a.iq_subject, a.iq_id, a.iq_question, a.iq_time from `{$g5['g5_shop_item_qa_table']}` a
			 left join `{$g5['g5_shop_item_table']}` b on (a.it_id = b.it_id)
			 where a.iq_secret='0' order by a.iq_time desc limit 0, {$sitemap_rows} ", false);
	while($row = sql_fetch_array($qry)) {
		
		if ($row['iq_time']=="" || $row['iq_time']=="0000-00-00") {
			$date = $today;
		} else {
			$date = substr($row['iq_time'], 0, 10)."T".substr($row['iq_time'], 11, 20)."+00:00";
		}

		$board_url = G5_SHOP_URL."/itemqaview.php?iq_id={$row['iq_id']}";

		echo "<url>\n";
		echo "<loc>{$board_url}</loc>\n";
		echo "<lastmod>{$date}</lastmod>\n";
		echo "<changefreq>daily</changefreq>\n";
		echo "<priority>1.0</priority>\n";
		echo "</url>\n";
	}
}


// 컨텐츠
$qry = sql_query("select co_id,co_seo_title from `{$g5['content_table']}` " );
while($row = sql_fetch_array($qry)) {
	// content 는 날짜 필드가 없어서 오늘 날짜를 표시
	$date=$today;
	
	if ((G5_GNUBOARD_VER > "5.4") && $sort_link=="Y") {
		if ($sort_link2 == "2") {
			$board_url = G5_URL."/".G5_CONTENT_DIR."/{$row['co_seo_title']}/"; // 짧은주소 (문자형)
		} else {
			$board_url = G5_URL."/".G5_CONTENT_DIR."/{$row['co_id']}/"; // 짧은주소 (숫자형)
		}
	} else {
		$board_url = G5_BBS_URL."/content.php?co_id={$row['co_id']}";
	}

	echo "<url>\n";
	echo "<loc>{$board_url}</loc>\n";
	echo "<lastmod>{$date}</lastmod>\n";
	echo "<changefreq>monthly</changefreq>\n";
	echo "<priority>1.0</priority>\n";
	echo "</url>\n";

}


// 아미나빌더 컨텐츠
if ($g5_builder == "amina") {
	$row_tot = sql_num_rows(sql_query("select as_thema from `{$g5['config_table']}` "));
	if ($row_tot > 0) {
		$qry = sql_query("select html_id from `{$g5['apms_page']}` where as_show='1' order by id DESC ");
		while($row = sql_fetch_array($qry)) {
			
			// page 는 날짜 필드가 없어서 오늘 날짜를 표시
			$date = $today;
			
			$board_url = G5_BBS_URL."/page.php?hid={$row['html_id']}";
			
			echo "<url>\n";
			echo "<loc>{$board_url}</loc>\n";
			echo "<lastmod>{$date}</lastmod>\n";
			echo "<changefreq>monthly</changefreq>\n";
			echo "<priority>1.0</priority>\n";
			echo "</url>\n";
		}
		$i++;
	}
}


// 그누컨텐츠
if (defined('G5_GNUCONTENTS_VER')) {
	//$query = sql_query("select it_id, it_time from `{$g5['g5_shop_item_table']}` where it_use='1' order by it_time desc");
	$qry = sql_query("select it_id, it_time from `{$g5['g5_contents_item_table']}` where it_use='1' order by it_time desc limit 0, {$sitemap_rows} ", false);
	while($row = sql_fetch_array($qry)) {
		
		if ($row['it_time']=="" || $row['it_time']=="0000-00-00") {
			$date = $today;
		} else {
			$date = substr($row['it_time'], 0, 10)."T".substr($row['it_time'], 11, 20)."+00:00";
		}
		
		$board_url = G5_CONTENTS_URL."/item.php?it_id={$row['it_id']}";
		
		echo "<url>\n";
		echo "<loc>{$board_url}</loc>\n";
		echo "<lastmod>{$date}</lastmod>\n";
		echo "<changefreq>daily</changefreq>\n";
		echo "<priority>1.0</priority>\n";
		echo "</url>\n";
		
	}
}

echo "</urlset>";
?>