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

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

$img_arr = array();

$attach = get_file($bo_table, $wr_id);
$attach_cnt = count($attach);

$img_cnt = $amina['resize'] ? '1' : 'all';
$img_arr = amina_attach_img($attach, $wr_content, $img_cnt);
$chk_photo = $img_arr[0] ? 'photo' : 'text';
$chk_file = amina_attach_file($attach) ? 'file' : '';

//RSS
$post_rss = ($w != "u" && $amina['auto_rss']) ? 'rss' : $post_rss;
$post_rss = $secret ? '' : $post_rss;

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

//다운제한
$po_wr_table = $po_wr_type ? $wr_4 : '';
$po_wr_id = $po_wr_type ? $wr_8 : '';

//익명게시판용
$write_no_str = '';
if($amina['nameless_write'] && $wr_nameless) {
	$wr_rename = $amina['nameless_head'].amina_nameless_tail($amina['nameless_tail']);
	$write_no_str = "wr_name = '".addslashes($wr_rename)."', mb_id = '', wr_email = '', wr_homepage = '',";
}

//대표아이디로 등록
$member_id = $member['mb_id'];
$member_nick = $member['mb_nick'];
$rmb_wr_sql = $rmb_new_sql = '';
if($is_rid && $chk_rid && !$write_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 == "u") {
	$icon = amina_array_icon($wr['wr_5']);
	$icon['choice_point'] = ($is_admin && $amina['cmt_choice']) ? $choice_point : $icon['choice_point'];
	$wr_5 = $icon['level'].'|'.$post_mobile.'|'.$post_icon.'|'.$icon['choice'].'|'.$icon['choice_point'].'|'.$icon['choice_mb'].'|'.$post_cool.'|'.$post_icon_mb.'|'.$po_view_point.'|'.$po_down_point.'|'.$po_img_cnt.'|'.$po_wr_type.'|'.$po_wr_table.'|'.$po_wr_id.'|'.$icon['wr_copy'];
} else {
	if($amina['cmt_choice'] && $choice_point > 0) {
		$choice_act = '<no-choice>';

		//포인트차감
	    insert_point($member_id, (int)$choice_point * (-1), "{$board['bo_subject']} {$wr_id} {$amina['cmt_choice_txt']} 포인트 걸기", $bo_table, $wr_id, '@choice_point');
	} else {
		$choice_point = 0;
		$choice_act = '';
	}
	$mb_xp_level = $write_no_str ? '' : xp_level($member_id); //익명일 때 레벨을 날림
	$wr_5 = $mb_xp_level.'|'.$post_mobile.'|'.$post_icon.'|'.$choice_act.'|'.$choice_point.'|'.$icon['choice_mb'].'|'.$post_cool.'|'.$post_icon_mb.'|'.$po_view_point.'|'.$po_down_point.'|'.$po_img_cnt.'|'.$po_wr_type.'|'.$po_wr_table.'|'.$po_wr_id;
}

//동영상($wr_6)값
$wr_6_sql = '';
if($movie_url) {
	$movie_sound = '';
	if(preg_match('/api.soundcloud.com/i', $movie_url)) {
		$movie_url = str_replace('\"', '', $movie_url);
		$movie_sound = 'soundcloud';
	}

	$wr_6_val = $movie_url.'|'.$movie_w.'|'.$movie_h.'|'.$movie_auto.'|'.$movie_align.'|'.$movie_img.'|'.$movie_file.'|'.$movie_sound.'|'.$movie_caption;
	$wr_6_sql = ", wr_6='".$wr_6_val."'";
}

// 동영상 여부 체크하기
$chk_video = amina_video_all($wr_6_val, $wr_content) ? 'video' : '';

//체크가 되어 있어도 태그가 없으면 해제함.
if(!$tag) $chk_tag = '';

//글정보(wr_10) 값
$wr_10 = $chk_photo.'|'.$img_location.'|'.$img_space.'|'.$post_main.'|'.$post_good.'|'.$post_rss.'|'.$chk_video.'|'.$chk_file.'|'.$subj_b.'|'.$subj_i.'|'.$subj_s.'|'.$subj_color.'|'.$chk_tag.'|'.$chk_poll.'|'.$poll_location.'|'.$daum_view.'|'.$subj_icon.'|'.$post_ccl.'|'.$img_except;

//블라인드 처리
if($w == "u") $wr_9 = $wr['wr_9'];

//글쓴이 별점시
if($amina['star'] == "1") $wr_7 = $star_score.'|1';

//회원별점시
if($amina['star'] > 1 && $w == "u") { 
	$row = sql_fetch(" select SUM(bg_star) as score, COUNT(bg_star) as people from {$g5['amina_star']} where bo_table = '$bo_table' and wr_id = '$wr_id' and bg_flag = 'star' ");
	$wr_7 = $row['score'].'|'.$row['people'];
}

//관련태그 등록하기(wr_7에 같이 사용함)
if($related_tag) {
	if($wr_7) {
		$wr_7 = $wr_7.'|'.$tag_sop.'|'.$related_tag;
	} else {
		$wr_7 = '||'.$tag_sop.'|'.$related_tag;
	}
}

// 글정보 업데이트
sql_query(" update $write_table set $write_no_str wr_10 = '$wr_10', wr_9 = '$wr_9', wr_8 = '$wr_8', wr_7 = '$wr_7', wr_5 = '$wr_5', wr_4 = '$wr_4' $wr_6_sql $rmb_wr_sql where wr_id = '$wr_id' ");

// 새글 업데이트
$new = sql_fetch(" select * from {$g5['board_new_table']} where bo_table = '$bo_table' and wr_id = '$wr_id' and wr_parent = '$wr_id' limit 1 ");

if($new['wr_id']) { //글이 있으면 업데이트함.

	$new_keys = array();
	$new_keys = array_keys($new);

	if(!in_array('wr_daum', $new_keys)) {
	    sql_query(" alter table {$g5['board_new_table']} add wr_rss varchar(10) default '' NOT NULL ", false);
		sql_query(" alter table {$g5['board_new_table']} add wr_photo varchar(10) default '' NOT NULL AFTER wr_rss ", false);
	    sql_query(" alter table {$g5['board_new_table']} add wr_video varchar(10) default '' NOT NULL AFTER wr_photo ", false);
	    sql_query(" alter table {$g5['board_new_table']} add wr_main varchar(10) default '' NOT NULL AFTER wr_video ", false);
	    sql_query(" alter table {$g5['board_new_table']} add wr_good varchar(10) default '' NOT NULL AFTER wr_main ", false);
	    sql_query(" alter table {$g5['board_new_table']} add wr_daum varchar(10) default '' NOT NULL AFTER wr_good ", false);
	}

	$new_photo = ($chk_photo == 'photo') ? 'photo' : ''; //photo
	$new_video = ($chk_video == 'video') ? 'video' : ''; //video
	$new_main = ($post_main == 'main') ? 'main' : ''; //main
	$new_good = ($post_good == 'good') ? 'good' : ''; //good
	$new_daum = ($daum_view) ? 'daum' : ''; //daum

	$new_no_str = $write_no_str ? ", mb_id = ''" : $rmb_new_sql;

	sql_query(" update {$g5['board_new_table']} set wr_rss = '$post_rss', wr_photo = '$new_photo', wr_video = '$new_video', wr_main = '$new_main', wr_good = '$new_good', wr_daum = '$new_daum' $new_no_str where bo_table = '$bo_table' and wr_id = '$wr_id' and wr_parent = '$wr_id' ");
}

if (amina_tag_auth()) {
	include_once(AMINA_PATH.'/lib/tag.lib.php');
	$wr_post = amina_array_write($wr['wr_10']);

	if($wr_post['chk_tag']) amina_del_tag($bo_table, $wr_id);

	$tag =  $tag ? amina_trim($tag) : '';

	if($chk_tag && $tag) {
		$wr_datetime = ($w == 'u' && $write['wr_datetime']) ? $write['wr_datetime'] : G5_TIME_YMDHIS;
		amina_add_tag($bo_table, $wr_id, $tag, $wr_datetime);
	}
}

//설문등록
$use_poll = false;
if($is_admin || $amina['poll_level'] == 1 || ($amina['poll_level'] > 1 && $member['mb_id'] >= $amina['poll_level'])) $use_poll = true;

if($use_poll) {
	$poll = array();
	$poll = sql_fetch(" select poll_id from {$g5['amina_poll']} where bo_table = '$bo_table' and wr_id = '$wr_id' ");

	if($is_admin) {
		if($poll_point == "" || $poll_point < 0) $poll_point = $amina['poll_point'];
	} else {
		$poll_point = $amina['poll_point'];
	}

	if($chk_poll) {

		$poll_enddate = $poll_y.'-'.$poll_m.'-'.$poll_d.' '.$poll_h.':'.$poll_i.':00';
		$poll_enddate =  amina_trim($poll_enddate);

		$poll_subject =  $poll_subject ? amina_trim($poll_subject) : '';
		$poll_item1 =  $poll_item1 ? amina_trim($poll_item1) : '';
		$poll_item2 =  $poll_item2 ? amina_trim($poll_item2) : '';
		$poll_item3 =  $poll_item3 ? amina_trim($poll_item3) : '';
		$poll_item4 =  $poll_item4 ? amina_trim($poll_item4) : '';
		$poll_item5 =  $poll_item5 ? amina_trim($poll_item5) : '';
		$poll_item6 =  $poll_item6 ? amina_trim($poll_item6) : '';
		$poll_item7 =  $poll_item7 ? amina_trim($poll_item7) : '';
		$poll_item8 =  $poll_item8 ? amina_trim($poll_item8) : '';
		$poll_item9 =  $poll_item9 ? amina_trim($poll_item9) : '';
		$poll_level =  $poll_level ? amina_trim($poll_level) : '';
		$poll_point =  $poll_point ? amina_trim($poll_point) : '';

		if($poll['poll_id']) {
			sql_query(" update {$g5['amina_poll']} set ca_name = '$ca_name', poll_subject = '$poll_subject', poll_item1 = '$poll_item1', poll_item2 = '$poll_item2', poll_item3 = '$poll_item3', poll_item4 = '$poll_item4', poll_item5 = '$poll_item5', poll_item6 = '$poll_item6', poll_item7 = '$poll_item7', poll_item8 = '$poll_item8', poll_item9 = '$poll_item9', poll_level = '$poll_level', poll_point = '$poll_point', poll_enddate = '$poll_enddate' where bo_table = '$bo_table' and wr_id = '$wr_id' ");
		} else {
			//신규 생성
			sql_query(" insert {$g5['amina_poll']} set bo_table = '$bo_table', wr_id = '$wr_id', ca_name = '$ca_name', poll_subject = '$poll_subject', poll_item1 = '$poll_item1', poll_item2 = '$poll_item2', poll_item3 = '$poll_item3', poll_item4 = '$poll_item4', poll_item5 = '$poll_item5', poll_item6 = '$poll_item6', poll_item7 = '$poll_item7', poll_item8 = '$poll_item8', poll_item9 = '$poll_item9', poll_level = '$poll_level', poll_point = '$poll_point', poll_regdate = '".G5_TIME_YMDHIS."', poll_enddate = '$poll_enddate' ");
		}
	} else {
		if($poll['poll_id']) sql_query(" delete from {$g5['amina_poll']} where bo_table = '$bo_table' and wr_id = '$wr_id' ");
	}
}

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

// 이미지 회전
if(function_exists('exif_read_data')) {
	for ($i=0; $i < $attach_cnt; $i++) {
		if(!$attach[$i]['view']) continue;
		$source_img = G5_DATA_PATH.'/file/'.$bo_table.'/'.$attach[$i]['file'];
		amina_photo_rotate($bo_table, $wr_id, $i, $source_img, $attach[$i]['image_type']);
	}
}

// Up Resize
if($amina['upimg_size'] > 0 && $amina['upimg_quality'] > 0) {

	//Direct Attach Image Resize
	for ($i=0; $i < $attach_cnt; $i++) {

		if(!$attach[$i]['view']) continue;

		if($amina['upimg_size'] >= $attach[$i]['image_width']) continue;
	
		$org_img = G5_DATA_PATH.'/file/'.$bo_table.'/'.$attach[$i]['file'];

		$thumb = bthumbnail($org_img, $amina['upimg_size'], 0, false, 1 , $amina['upimg_quality']);
		if($thumb) {
			if(!file_exists($thumb)) $thumb = amina_file_path($thumb);
			$img_size = @getimagesize($thumb);
			$img_filesize = @filesize($thumb);
			
			sql_query(" update {$g5['board_file_table']} set bf_filesize = '{$img_filesize}', bf_width = '{$img_size[0]}', bf_height = '{$img_size[1]}', bf_type = '{$img_size[2]}' where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$i' ");

			@unlink($org_img); //Org Delete
			@copy($thumb, $org_img); //Thumb Copy
			@chmod($org_img, AMINA_FILE_PERMISSION);
			@unlink($thumb); //Thumb Delete
		}
	}

	//Edit Attach Image Resize
	if(preg_match_all("/\<img[^\<\>]*\>/i", $wr_content, $match)) {
		$match_cnt = count($match[0]);
		for ($i=0; $i < $match_cnt; $i++) {
			$chk_img = amina_value($match[0][$i]);
			if($chk_img['src'] && preg_match("/\/data\/(cheditor4|editor)\//i", $chk_img['src'])) {
				$thumb = amina_make_thumb($chk_img['src'], $amina['upimg_size'], 0, $amina['upimg_quality'], 'write');
				if($thumb) {
					if(!file_exists($thumb)) $thumb = amina_file_path($thumb);
					$org_img = preg_replace("/\/_thumb\/[^\/]*\//i", "/", $thumb);
					@unlink($org_img); //Org Delete
					@copy($thumb, $org_img); //Thumb Copy
					@chmod($org_img, AMINA_FILE_PERMISSION);
					@unlink($thumb); //Thumb Delete
				}
			}
		}
	}
}

if(!AMINA) {
	sql_query(" delete from {$g5['autosave_table']} where as_uid = '{$uid}' "); // 자동저장된 레코드를 삭제한다.

	if($amina['syndi']) include G5_SYNDI_PATH.'/include/include.bbs.write_update.php'; // syndication ping
}

//원글쓴이에게 답글반응 남기기
if($w == 'r' && $wr['mb_id'] && $wr['mb_id'] != $member['mb_id'] && $aminaRe['reply']) {
	$re_name = $member_id ? $member_nick : $wr_name;
	amina_response($bo_table, $wr_id, $wr_subject, $wr['mb_id'], $member_id, $re_name, 'reply');
}

//연동글일 때 원글작성자에세 글반응 남기기
if($w == '' && $wr_4 && $wr_8) {
	$bo = sql_fetch(" select * from {$g5['board_table']} where bo_table = '$wr_4' ");
	if($bo['bo_table']) {
		$tmp_write_table = $g5['write_prefix'] . $wr_4;
		$linked = sql_fetch(" select mb_id from $tmp_write_table where wr_id = '$wr_8' limit 1");
		if($linked['mb_id'] && $linked['mb_id'] != $member_id) {
			$linked_name = $member_id ? $member_nick : $wr_name;
			amina_response($wr_4, $wr_8, $wr_subject, $linked['mb_id'], $member_id, $linked_name, 'linked');
		}
	}
}

if(file_exists($write_skin_path.'/write_update.tail.skin.php')) include_once ($write_skin_path.'/write_update.tail.skin.php');

//이동 별도 지정시
if($go_url) {
	if($file_upload_msg) {
		alert($file_upload_msg, $go_url);
	} else {
		goto_url($go_url);
	}
}

?>