<?
include_once("./_common.php");
extract($_GET);

//-- 공유할 기본 정보 불러오기
$tablename	= $g4[write_prefix] . $bo_table;
$sql	= "select * from $tablename where wr_id='$wr_id'";
$rd	= sql_fetch($sql);

$se_thumb	= "";							//-- 스마트 에디터 파일
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $rd[wr_content], $match);

for($j = 0; $j < count($match); $j++) {
	for($k = 0; $k < count($match[$j]); $k++) {
		if(strlen($match[$j][$k])) {
			$tmp_img = explode("/data/zm.exp/", strip_tags($match[$j][$k]));
			$tmp_org_img	= $g4[path] . "/data/zm.exp/" . $tmp_img[1];
			if($tmp_img[1]) {
				$tmp	= explode("/", $tmp_img[1]);
				$tmp_thm_img	= $g4[path] . "/data/zm.exp";

				for($m = 0; $m < count($tmp)-1; $m++)
					$tmp_thm_img .= "/" . $tmp[$m];

				$tmp_thm_img	.= "/thumb/" . $tmp[count($tmp)-1];

				if(file_exists($tmp_org_img)) {
					if(!file_exists($tmp_thm_img)) {
						//-- 그래도 파일이 없으면 너무 작아서 안 만든 것임. 원본을 사용
						$tmp_thm_img	= str_replace("/thumb/", "/", $tmp_thm_img);
					}
				}
				$se_thumb	= str_replace("$g4[path]", "$g4[url]", $tmp_thm_img);
			}
		}
	}
}

//-- 썸네일 정보
$tableboard	= $g4[board_file_table];
$sql		= "select * from $tableboard where bo_table='$bo_table' and wr_id='$wr_id' order by bf_no asc limit 1";
$rdimg	= sql_fetch($sql);

//-- SE 업로드
if($se_thumb) { 	$thumb	= $se_thumb; }

//-- 파일 첨부
if($rdimg[bf_type] > 0) { $thumb	= $g4[url]."/data/file/".$bo_table."/thumb/".$rdimg[bf_file]; }

$title		= str_replace(array("\r", "\n"), "", $rd[wr_subject]);
//-- 태그 제거
$rd[wr_content]	= strip_tags($rd[wr_content]);
$desc	= cut_str(str_replace(array("\r", "\n"), "", $rd[wr_content]), 200);

$title	= addslashes($title);
$desc	= addslashes($desc);
?>
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1,minimum-scale=1, maximum-scale=1">
<? //-- Facebook 공유를 위한 메타 태그 ?>
<meta id="fb_image" property='og:image' content='<?=$thumb?>'>
<meta id="fb_title" property="og:title" content="<?=$title?>">
<meta id="fb_desc" property="og:description" content="<?=$desc?>">
<meta property="og:site_name" content="<?=$config[cf_title]?>">

<script src="<?=$g4[path]?>/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="./kakao.link.js"></script>
</head>
<body>
<?
$long_url	= $g4[url] . "/bbs/board.php?bo_table=".$bo_table."&wr_id=".$wr_id;
$sns_url	= $g4[url] . $_SERVER[PHP_SELF] . "?bo_table=".$bo_table."&wr_id=".$wr_id;
?>

<script>
function executeKakaoStoryLink()
{
    kakao.link("story").send({
        post : "<?=$sns_url?>",
        appid : "<?=$_SERVER[SERVER_NAME]?>",
        appver : "1.0",
        appname : "원주스토리",
        urlinfo : JSON.stringify({title:"<?=$title?>", desc:"<?=$desc?>", imageurl:["<?=$thumb?>"], type:"article"})
    });
}
</script>

<?
if($sns == "fb") {
	$api_url		= "https://www.facebook.com/sharer/sharer.php?u=". urlencode($sns_url);
	zm_gourl($api_url);
	exit;
}

if($sns == "tw") {
	//$surl	= naver_short_url(urlencode($long_url));
	$txt	= urlencode($title);
	$api_url	= "https://twitter.com/intent/tweet?text=".urlencode($sns_url) . " " . $txt;
	zm_gourl($api_url);
}
?>

<? if($sns == "ks") : ?>
	<script> $(function(){ 	executeKakaoStoryLink();  window.close(); });</script>
<? endif; ?>

<? if($sns == "bd") {
	$txt	= urlencode($sns_url) . " " . urlencode($desc);
	$api_url	= "bandapp://create/post?text=".$txt;
	zm_gourl($api_url);
	echo "<script>window.close();</script>";
} ?>

<?
//-- $sns 값이 없으면 SNS에서 연결 된 것으로 간주하고 해당 페이지로 이동
if($sns == "") {
	zm_gourl("$long_url");
}
?>
</body>
</html>