<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가 

//Action
$action = 'view';

include_once(AMINA_PATH.'/lib/view.lib.php');

//내용스킨 설정값
$board['wr_id'] = '-view'.$wr_id;

//내용스킨 설정값
if(file_exists($view_skin_path.'/set/set.load.php')) {
	include_once($view_skin_path.'/set/set.load.php');
	$set = amina_set_view($amina['view_set']);
}

// Star & Tag
$rtag = amina_array_tag($view['wr_7']);

// Auto Move
if ($amina['move'] && $amina['move_bo'] && !$view['is_notice']) {

	$is_auto_move = false;

	if (!$amina['move_term'] || $amina['move_term'] > (G5_SERVER_TIME - strtotime($write['wr_datetime']))/(60*60*24)) {

		if ($amina['move_hit'] && $amina['move_hit'] <= $write['wr_hit']) { //hit
			$is_auto_move = true;
		} else if ($amina['move_cmt'] && $amina['move_cmt'] <= $write['wr_comment']) { //comment
			$is_auto_move = true;
		} else if (!$amina['move_good'] && $amina['move_cnt'] && $amina['move_cnt'] <= $write['wr_good']) { //good
			$is_auto_move = true;
		} else if ($amina['move_good'] == "1" && $amina['move_cnt'] && $amina['move_cnt'] <= $write['wr_nogood']) { //nogood
			$is_auto_move = true;
		} else if ($amina['move_good'] == "2" && $amina['move_cnt'] && $amina['move_cnt'] <= ($write['wr_good'] - $write['wr_nogood'])) { //good - nogood
			$is_auto_move = true;
		} else if ($amina['move_good'] == "3" && $amina['move_cnt'] && $amina['move_cnt'] <= ($write['wr_nogood'] - $write['wr_good'])) { //nogood - good
			$is_auto_move = true;
		} else if ($amina['move_star'] && $amina['move_score']) { //star
			if(!$rtag['people']) $rtag['people'] = 0;
			$move_score = 0;
			if($rtag['people'] > 0) $move_score = $rtag['score'] / $rtag['people'];
			$move_score = round($move_score,2);
			if($rtag['people'] >= $amina['move_star'] && $move_score >= $amina['move_score']) $is_auto_move = true;
		}

		if($is_auto_move) {
			amina_move($board, $wr_id, $amina['move_bo'], $amina['move_ca'], "move");
		}
    }
}

//팬시박스로 글내용 볼 때
if($frame == "2") $board['bo_use_list_view'] = '';

//Deco View
$is_deco_view = ($amina['deco_view'] && $amina['deco_view'] != 'none') ? true : false;

//Address
$lurl = G5_BBS_URL.'/board.php?bo_table='.$bo_table.'&wr_id='.$wr_id; //긴 주소
$surl = amina_short_url($lurl, $amina['use_surl'], $bo_table, $wr_id); //짧은 주소
$address_url = urlencode($surl);
$amina_href = AMINA_URL.'/admin.dashboard.php?bo_table='.$bo_table;

//Watermarker
if($amina['wm']) {
	$board['water_mark'][0]['filename'] = G5_PATH.'/'.$amina['wm_file'];
	$board['water_mark'][0]['location'] = $amina['wm_loc'];
	$board['water_mark'][0]['x'] = $amina['wm_x'];
	$board['water_mark'][0]['y'] = $amina['wm_y'];
}

//Resize
$board['no_view_resize'] = ($amina['resize'] || $board['bo_image_width'] == "1") ? true : false;

//Fancybox
$board['view_fancybox'] = ($amina['view_fancybox'] == "1") ? 'none' : '';

if(!G5_IS_MOBILE && $board['view_fancybox'] != "none") {
	switch($amina['view_fancybox']) {
		case '2'	: $board['view_fancybox'] = 'fbox'; break;
		case '3'	: $board['view_fancybox'] = 'fbox-buttons'; break;
		case '4'	: $board['view_fancybox'] = 'fbox-thumbs'; break;
		case '5'	: $board['view_fancybox'] = 'lightbox'; break;
	}
}

//Options & Icon Array
$wr_post = amina_array_write($view['wr_10']);
$v_icon = amina_array_icon($view['wr_5']);

$board['img_location'] = $wr_opt['img_location'];

//1:1 BBS Check
if($amina['bo_type'] == "2" && !$is_admin) include_once(AMINA_PATH.'/view.check.1vs1.php');

//View & Down Point
$is_po_view = $is_po_down = '';
if($amina['po_view'] || $amina['po_down']) include_once(AMINA_PATH.'/view.check.point.php');

$view['use_po_view'] = $is_po_view;
$view['use_po_down'] = $is_po_down;
$view['msg_po_view'] = $po_view_txt;
$view['msg_po_down'] = $po_down_txt;

//Attach Sort
list($view['attach']['file'], $view['attach']['image'], $view['attach']['video'], $view['attach']['audio']) = amina_attach_sort($view['file']);

//Subject
$view['blind'] = '';
$view['fa_icon'] = '';
if($view['wr_9'] == "lock" || $view['wr_9'] == "shingo") {
	$view['subject'] = '<i class="fa fa-eye-slash fa-lg"></i> 블라인드 처리된 글입니다.';
	if($is_admin) $view['subject'] .= '[ '.cut_hangul_last(get_text($view['wr_subject'])).' ]';
	$view['blind'] = '<p><i class="fa fa-eye-slash fa-lg"></i> 블라인드 처리된 글입니다.</p>';
} else {
	$view['fa_icon'] = $wr_post['subj_icon'] ? '<i class="fa fa-'.$wr_post['subj_icon'].'"></i> ' : '';
	$view['subject'] = cut_hangul_last(get_text($view['wr_subject']));
	if($wr_post['subj_s']) $view['subject'] = '<del>'.$view['subject'].'</del>';
	if($wr_post['subj_b']) $view['subject'] = '<b>'.$view['subject'].'</b>';
	if($wr_post['subj_i']) $view['subject'] = '<i>'.$view['subject'].'</i>';
	if($wr_post['subj_color']) $view['subject'] = '<span style="color:'.$wr_post['subj_color'].';">'.$view['subject'].'</span>';
}

//Member Photo
$view['mb_photo'] = '';
if($amina['view_photo']) { 
	switch($amina['view_photo']) {
		case '1'	:	$view['mb_photo'] = mb_photo($member['mb_id'], $view['mb_id']); break;
		case '2'	:	$view['mb_photo'] = mb_photo($member['mb_id'], $view['mb_id'], '', '', 'is'); break;
		case '3'	: 	if($v_icon['post_icon_mb']) {
							$view['mb_photo'] = mb_photo($member['mb_id'], $view['mb_id'], '', '', 'is');
							if($view['mb_photo']) break;
						}
						if(!$view['mb_photo'] && !file_exists($emo_skin_path.'/'.$v_icon['post_icon'].'.gif')) $v_icon['post_icon'] = rand(1, $amina['emo_cnt']);
						$view['mb_photo'] = '<img src="'.$emo_skin_url.'/'.$v_icon['post_icon'].'.gif" alt="" />'."\n";
						break;
		default		:	$view['mb_photo'] = ''; break;
	}		
}	

//Mobile Icon
$view['icon_mobile'] = $v_icon['mobile'] ? ' <i class="fa fa-tablet fa-lg"></i> ' : '';

//XP Icon
$view['icon_level'] = $xp['xp_use'] ? xp_icon($view['mb_id'], $v_icon['level']) : '';

//Name
$view['name'] = $is_sideview ? amina_sideview($view['mb_id'], $view['wr_name'], $view['wr_email'], $view['wr_homepage']) : $view['name'];

//Choice
if($amina['cmt_choice'] && $v_icon['choice_point'] > 0) {
	$is_choice = 'none';
	if($v_icon['choice'] == "<choice>") $is_choice = 'done';
	if($is_choice != "done" && ($member['mb_id'] == $view['mb_id'])) $is_choice = 'select';
}
$view['choice'] = $is_choice;

//Attach
$view['attach_skin'] = ($amina['attach_skin'] && $amina['attach_skin'] != 'none') ? amina_attach_skin($amina['attach_skin'], $bo_table, $wr_id, $view) : '';

//Content
if(!$is_admin && $view['blind']) { //블라인드 일 때..
	$view['content'] = $view['wr_content'] = $view['blind'];
} else {
	if($view['use_po_view'] == 'new') { //Preview
		$view['content'] = $view['blind'].amina_preview_skin($amina['preview_skin'], $bo_table, $wr_id, $view, $view['msg_po_view']);
	} else {
		if($wr_post['img_location'] == "insert") $view['content'] = $view['rich_content'];
		$view['content'] = $view['blind'].amina_view_contents($view['content'], $view['attach'], $view['wr_6'], $wr_post, (int)$board['bo_image_width'], '', $view['wr_2'], $amina['view_code'], $wr_post['img_except']);
	}
}

//Poll
$view['poll_up'] = $view['poll_up'] = '';
if($wr_post['chk_poll']) {
	if($wr_post['poll_location']) {
		$view['poll_up'] = amina_poll_skin($amina['poll_skin'], $bo_table, $wr_id, $member['mb_id']);
	} else {
		$view['poll_down'] = amina_poll_skin($amina['poll_skin'], $bo_table, $wr_id, $member['mb_id']);
	}
}

//Related
$view['related_skin'] = ($amina['related_skin'] && $amina['related_skin'] != 'none' && $view['wr_8']) ? amina_related_skin($amina['related_skin'], $view['wr_4'], $view['wr_8']) : '';

//Star
$view['star_skin'] = ($amina['star'] > 1) ? amina_star_skin($amina['star_skin'], $bo_table, $wr_id, $view['wr_7']) : '';

//Good
$view['good_skin'] = ($board['bo_use_good'] || $board['bo_use_nogood']) ? amina_good_skin($amina['view_good'], $bo_table, $wr_id, $view['wr_good'], $view['wr_nogood']) : '';

//Sign
$view['sign_skin'] = ($amina['sign_skin'] && $amina['sign_skin'] != "none" && $is_signature) ? amina_sign_skin($amina['sign_skin'], $bo_table, $wr_id, $view['mb_id'], $view['name'], $signature) : '';

//Like
$view['like_skin'] = ($set['view_like_skin'] && $set['view_like_skin'] != "none") ? amina_like($set['view_like_skin'], $bo_table, $wr_id, $view['subject'].' '.$view['wr_content']) : '';

//SNS
$view['icon_sns'] = '';
if(!$view['blind'] && $amina['view_sns'] != "none") { 
	list($sns_img) = amina_thumb_img($view['file'], $view['wr_6'], $view['wr_content'], 1, $view['wr_4'], $view['wr_8'], $view['wr_8']);
	$view['icon_sns'] = amina_sns('view_sns'.$view['wr_id'], $amina['view_sns'], $view['wr_subject'], $view['wr_content'], $lurl, $surl, $sns_img);
}

//Tag
$view['tag_skin'] = $view['tag'] = '';
if($wr_post['chk_tag'] || $rtag['related_tag']) {
	include_once(AMINA_PATH.'/lib/tag.lib.php');
	if($wr_post['chk_tag']) $view['tag'] = amina_get_tag($bo_table, $wr_id, true);
	if($rtag['related_tag'] && $amina['tag_skin'] && $amina['tag_skin'] != "none") $view['tag_skin'] = amina_tag_skin($amina['tag_skin'], $bo_table, $wr_id, $rtag['tag_sop'], $rtag['related_tag']);
}

//CSS Load
if(!defined('AMINA_CSS')) include_once(AMINA_PATH.'/css.php');

//Syntaxhighlighter
if($amina['view_code'] && !defined('AMINA_SYNTAXHIGLIGHTER')) include_once(AMINA_PATH.'/app/syntaxhighlighter.php');

//fancybox
if($amina['view_fancybox'] > 2) {
	if($board['view_fancybox'] == 'lightbox') {
		if(!defined('AMINA_LIGHTBOX2')) include_once(AMINA_PATH.'/app/lightbox2.php'); //lightbox
	} else {
		if(!defined('AMINA_VIEW_FANCYBOX2')) include_once(AMINA_PATH.'/view.skin.fancybox.php');
	}
}

//Btn Href
$print_href = AMINA_URL.'/print.php?bo_table='.$bo_table.'&wr_id='.$wr_id;
$shingo_href = ($amina['shingo'] > 0) ? 'javascript:amina_shingo(\''.AMINA_URL.'/blind.php?bo_table='.$bo_table.'&wr_id='.$wr_id.'\',\'shingo\');' : '';
$write_href = $write_href ? $write_href.$frame_opt.'&sca='.urlencode($sca) : '';
$search_href = $search_href ? $search_href.$frame_opt : '';
$list_href = $list_href.$frame_opt;
if(AMINA) {
	if ($move_href ) {
		$move_href = str_replace("./move", AMINA_URL."/move", $move_href);
		$move_href = 'href="'.$move_href.'" onclick="board_move(this.href); return false;"';
	}
	if ($copy_href) {
		$copy_href = str_replace("./move", AMINA_URL."/move", $copy_href);
		$copy_href = 'href="'.$copy_href.'" onclick="board_move(this.href); return false;"';
	}
	if ($delete_href) $delete_href = 'href="'.$delete_href.'" onclick="del(this.href); return false;"';
} else {
	if ($move_href) {
		$move_href = str_replace("./move", AMINA_URL."/move", $move_href);
		$move_href = 'href="'.$move_href.'"';
	}
	if ($copy_href) {
		$copy_href = str_replace("./move", AMINA_URL."/move", $copy_href);
		$copy_href = 'href="'.$copy_href.'"';
	}
	if ($delete_href) $delete_href = 'href="'.$delete_href.'"';
}

//Time
$view['date'] = date("Y.m.d H:i", strtotime($view['wr_datetime']));
$view['subj_icon'] = $wr_post['subj_icon'] ? '<i class="icon-'.$wr_post['subj_icon'].'"></i> ' : '';

//CCL
if($wr_post['post_ccl']) {
	$ccl_href = 'http://creativecommons.org/licenses';
	switch($wr_post['post_ccl']) {
		case '1' : $view['ccl_icon'] = 'lic_by_g.gif'; $view['ccl_deed'] = $ccl_href.'/by/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by/2.0/kr/legalcode'; $view['ccl'] = '저작자표시'; break;
		case '2' : $view['ccl_icon'] = 'lic_by_nc_g.gif'; $view['ccl_deed'] = $ccl_href.'/by-nc/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by-nc/2.0/kr/legalcode'; $view['ccl'] = '저작자표시-비영리'; break;
		case '3' : $view['ccl_icon'] = 'lic_by_nd_g.gif'; $view['ccl_deed'] = $ccl_href.'/by-nd/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by-nd/2.0/kr/legalcode'; $view['ccl'] = '저작자표시-변경금지'; break;
		case '4' : $view['ccl_icon'] = 'lic_by_sa_g.gif'; $view['ccl_deed'] = $ccl_href.'/by-sa/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by-sa/2.0/kr/legalcode'; $view['ccl'] = '저작자표시-동일조건변경허락'; break;
		case '5' : $view['ccl_icon'] = 'lic_by_nc_sa_g.gif'; $view['ccl_deed'] = $ccl_href.'/by-nc-sa/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by-nc-sa/2.0/kr/legalcode'; $view['ccl'] = '저작자표시-비영리-동일조건변경허락'; break;
		case '6' : $view['ccl_icon'] = 'lic_by_nc_nd_g.gif'; $view['ccl_deed'] = $ccl_href.'/by-nc-nd/2.0/kr/'; $view['ccl_code'] = $ccl_href.'/by-nc-nd/2.0/kr/legalcode'; $view['ccl'] = '저작자표시-비영리-변경금지'; break;
	}
}

?>

<link rel="stylesheet" href="<?php echo $view_skin_url.'/'.$amina['view_css']; ?>.css">

<div id="as-view-wrap">
<?php
	if($is_deco_view) {
		echo '<link rel="stylesheet" href="'.AMINA_URL.'/skin/deco/view/'.$amina['deco_view'].'.css">'."\n";
		echo '<div class="deco-view">'."\n"; //데코 전체 시작
		echo '<div class="deco-view-head-back"><div class="deco-view-head-front"><div class="deco-view-head"></div></div></div>'."\n"; //데코 상단
		echo '<div class="deco-view-body-back"><div class="deco-view-body-front">'."\n"; //데코 중단 시작
	}
?>

<div id="as-view">
<?php 
	if($board['is_print'] && file_exists($view_skin_path.'/view.print.php')) {
		if(!$is_admin && $view['blind']) {
			amina_alert('블라인드글은 인쇄할 수 없습니다.', $frame);
		}
		include($view_skin_path.'/view.print.php'); 
	} else {
		include($view_skin_path.'/view.skin.php'); 
	}
?>
</div>

<?php if(!G5_IS_MOBILE && $amina['linked_tab'] && $amina['linked_skin'] && $amina['linked_skin'] != 'none' && $frame != "linked" && !$board['is_print']) { ?>
	<?php  // 연동탭 정리
		$linked = amina_array_linked($amina['linked_tab']);
		$linked_cnt = count($linked);
		$linked_skin_path = AMINA_PATH.'/skin/linked/'.$amina['linked_skin'];
		$linked_skin_url = AMINA_URL.'/skin/linked/'.$amina['linked_skin'];
		include($linked_skin_path.'/linked.skin.php'); 
	?>
	<div id="iframe_top"></div>
	<div id="LinkedContent1">
		<?php if($amina['cmt_skin'] != "none") include_once('./view_comment.php'); ?>
	</div>
	<div id="LinkedContent2" style="display:none;">
		<div id="LinkedWrap">
			<iframe name="LinkedFrame" id="LinkedFrame" width="100%" height="100%" scrolling="no"></iframe>
		</div>
	</div>
<?php } else { if($amina['cmt_skin'] != "none") include_once('./view_comment.php'); } ?>

<?php if($frame != "2" && !$board['is_print']) { ?>
	<div id="as-view-btn">
		<?php include ($btn_skin_path.'/btn.skin.php'); //Btn Skin ?>
	</div>
<?php } ?>

<?php
	if($is_deco_view) {
		echo '</div></div>'."\n"; //데코 중단 끝
		echo '<div class="deco-view-tail-back"><div class="deco-view-tail-front"><div class="deco-view-tail"></div></div></div>'."\n"; //데코 하단
		echo '</div>'."\n"; //데코 전체 끝
	}
?>
</div>

<?php
//열람 & 다운 메시지
$msg_po_down = '';
if($is_po_down == 'new') {
	if($board['bo_download_point'] < 0) {
		$msg_po_down = number_format($v_icon['po_down_point'])."점의 다운 MP와 별도로 ".number_format($board['bo_download_point']*(-1))."점의 다운로드 기본 MP가 차감됩니다.\\n\\n다운로드 기본 MP는 다운 MP와 상관없이 게시물당 한번만 차감됩니다.";
	} else {
		$msg_po_down = number_format($v_icon['po_down_point'])."점의 다운 MP가 차감됩니다.";
	}
} else {
	if ($member['mb_id'] && $board['bo_download_point'] < 0) {
		$msg_po_down = number_format($board['bo_download_point']*(-1))."점의 다운로드 MP가 차감됩니다.\\n\\n다운로드 MP는 게시물당 한번만 차감되며 다음에 다시 다운로드 하셔도 중복하여 차감하지 않습니다.";
	}
}

?>
<script>
	function clipboard_address(str) {
		prompt("이 글의 고유주소입니다. Ctrl+C를 눌러 복사하거나 확인을 눌러 주세요.", str);
		window.clipboardData.setData("Text",str);
	}

	function file_download(link, file) {
		<?php if ($msg_po_down) { ?>
			if (confirm("'"+file+"' 파일을 다운로드 하시면 <?php echo $msg_po_down; ?>\n\n그래도 다운로드 하시겠습니까?"))
		<?php } ?>
	    document.location.href=link;
	}
	<?php if(AMINA) { ?>
		function board_move(href) {
		    window.open(href, "boardmove", "left=50, top=50, width=500, height=550, scrollbars=1");
		}
	<?php } ?>
</script>
