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

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

$point_log = AMINA ? '댓글' : '코멘트';

//자신의 글과 특정일 이후에 등록된 코멘트에 대한 포인트 등록 불가 설정
if($w == "c" && $board['bo_comment_point'] && $member['mb_id']) {
	$chk_comment = ($amina['limit_date'] > 0) ? date("Y-m-d H:i:s", G5_SERVER_TIME - ( $amina['limit_date'] * 24 * 3600 )) : '';

	if(($wr['mb_id'] == $member['mb_id'] && $amina['limit_cmt']) || ($chk_comment && $wr['wr_datetime'] < $chk_comment)) { 
		if (!delete_point($member['mb_id'], $bo_table, $comment_id, $point_log)) 
		    insert_point($member['mb_id'], $board['bo_comment_point'] * (-1), "{$board['bo_subject']} {$wr_id}-{$comment_id} {$point_log}삭제");
	} else { 
		if($wr['mb_id'] != $member['mb_id'] && $amina['lucky_rate'] > 0 && $amina['lucky_point'] > 0) {
			//당첨여부 확인
			$lucky = sql_fetch(" select count(*) as cnt from {$g5['point_table']} where mb_id = '{$member['mb_id']}' and po_rel_table = '$bo_table' and po_rel_id = '$wr_id' and po_rel_action = '@lucky' ");

			if(!$lucky['cnt']) { //미당첨자일 경우에만 돌림
				$lucky1 = rand(1,$amina['lucky_rate']);
				$lucky2 = rand(1,$amina['lucky_rate']);
				if($lucky1 == $lucky2) {
					$lucky_point = $board['bo_comment_point'] * rand(1,$amina['lucky_point']);

					//포인트 등록
					insert_point($member['mb_id'], (int)$lucky_point, "{$board['bo_subject']} {$wr_id} Lucky MP!", $bo_table, $wr_id, '@lucky');
	
					//글정보 업데이트
					$lucky_str = "wr_10 = '$lucky_point', ";
				}
			}
		}
	}
} else {
	$lucky_str = ($w == "cu" && $amina['lucky_rate'] > 0 && $amina['lucky_point'] > 0) ? "wr_10 = '{$wr_cmt['wr_10']}', " : "";
}

//Mobile
$post_mobile = G5_IS_MOBILE ? '1' : '';

//익명댓글
$cmt_no_str = $cmt_new_str = '';
if($amina['nameless_cmt'] && $wr_nameless) {
	$wr_rename = $amina['nameless_head'].amina_nameless_tail($amina['nameless_tail']);
	$cmt_no_str = "wr_name = '".addslashes($wr_rename)."', mb_id = '', wr_email = '', wr_homepage = '',";
	$cmt_new_str = "mb_id = ''";
}

//대표아이디로 등록
$member_id = $member['mb_id'];
$member_nick = $member['mb_nick'];
$rmb_wr_sql = $rmb_new_sql = '';
if($is_rid && $chk_rid && !$cmt_no_str) {
	$rmb = array();
	$rmb = ($chk_rid == "1") ? get_admin('super') : $member;
	$member_id = $rmb['mb_id'];
	$member_nick = $rmb['mb_nick'];
	$rmb_name = $board['bo_use_name'] ? $rmb['mb_name'] : $rmb['mb_nick'];
    $rmb_password = $rmb['mb_password'];
	$rmb_email = $rmb['mb_email'];
    $rmb_homepage = $rmb['mb_homepage'];
	$rmb_wr_sql = ", mb_id = '$member_id', wr_name = '$rmb_name', wr_password = '$rmb_password', wr_email = '$rmb_email', wr_homepage = '$rmb_homepage'";
	$rmb_new_sql = "mb_id = '$member_id'";
}

//아이콘($wr_5) 값 정리하기..
if ($w == "cu") {
	$icon = amina_array_icon($wr_cmt['wr_5']);
	$wr_5 = $icon['level'].'|'.$post_mobile.'|'.$post_icon.'|'.$icon['choice'].'|'.$icon['choice_point'].'|'.$icon['choice_mb'].'|'.$post_cool.'|'.$post_icon_mb;
	$wr_9 = $wr_cmt['wr_9'];
} else {
	$mb_xp_level = $cmt_no_str ? '' : xp_level($member_id); //익명일 때 레벨을 날림
	$wr_5 = $mb_xp_level.'|'.$post_mobile.'|'.$post_icon.'|'.$icon['choice'].'|'.$icon['choice_point'].'|'.$icon['choice_mb'].'|'.$post_cool.'|'.$post_icon_mb;
	$wr_9 = 0;
}

sql_query(" update $write_table set $cmt_no_str $lucky_str wr_9 = '$wr_9', wr_5 = '$wr_5' $rmb_wr_sql where wr_id = '$comment_id' ");

$rmb_new_sql = $cmt_new_str ? $cmt_new_str : $rmb_new_sql;
if($rmb_new_sql) sql_query(" update {$g5['board_new_table']} set $rmb_new_sql where bo_table = '$bo_table' and wr_id = '$comment_id' ", false);

//댓글반응 남기기
if($w == 'c') {
	$re_name = $member_id ? $member_nick : $wr_name;

	//글쓴이에게
	if($wr['mb_id'] && $wr['mb_id'] != $member['mb_id'] && $aminaRe['cmt']) amina_response($bo_table, $wr_id, $wr['wr_subject'], $wr['mb_id'], $member_id, $re_name, 'cmt', 0, $comment_id);

	//원댓글쓴이에게
	if($tmp_comment_reply && $aminaRe['cmt_reply']) {
		$pre_cmt_char = substr($tmp_comment_reply,0,-1);
		$pre_cmt = sql_fetch(" select mb_id from {$write_table} where wr_parent = '$wr_id' and wr_is_comment = 1 and wr_comment = '$tmp_comment' and wr_comment_reply = '$pre_cmt_char' "); 
		if($pre_cmt['mb_id'] && $pre_cmt['mb_id'] != $wr['mb_id'] && $pre_cmt['mb_id'] != $member['mb_id']) amina_response($bo_table, $wr_id, $wr['wr_subject'], $pre_cmt['mb_id'], $member_id, $re_name, 'cmt_reply', 1, $comment_id);
	}
}

// xp 정보 업데이트
if($w != "cu" && $member['mb_id']) check_xp($member['mb_id']);

if(file_exists($cmt_skin_path.'/write_comment_update.tail.skin.php')) include_once ($cmt_skin_path.'/write_comment_update.tail.skin.php');

//이동 별도 지정시
if($go_url) goto_url($go_url.'&go_url='.urlencode($go_url));

?>