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

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

$img_arr = array();

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

$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 = '';
if(amina_attach_file($attach)) $chk_file = 'file';

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

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

// 익명게시판용
$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 = '$wr_rename', mb_id = '', wr_email = '', wr_homepage = '', ";
}

//아이콘($wr_5) 값
if ($w == "u") {
	$icon = amina_array_icon($wr['wr_5']);
	if($is_admin) $icon['choice_point'] = $choice_point;
	$icon['choice'] = '';
	$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;
} else {
	if(!$choice_point) $choice_point = 0;
	$mb_xp_level = $write_no_str ? '' : xp_level($member['mb_id']); //익명일 때 레벨을 날림
	$wr_5 = $mb_xp_level.'|'.$post_mobile.'|'.$post_icon.'|'.$icon['choice'].'|'.$choice_point.'|'.$icon['choice_mb'].'|'.$post_cool.'|'.$post_icon_mb.'|'.$po_view_point.'|'.$po_down_point.'|'.$po_img_cnt;

	//포인트차감
	if($amina['cmt_choice'] && $choice_point > 0) {
        insert_point($member['mb_id'], (int)$choice_point * (-1), "{$board['bo_subject']} {$wr_id} {$amina['cmt_choice_txt']} 포인트 걸기", $bo_table, $wr_id, '@choice_point');
	}
}

//동영상($wr_6)값
$wr_6 = '';
if($amina['video'] && $movie_url) $wr_6 = $movie_url.'|'.$movie_w.'|'.$movie_h.'|'.$movie_auto.'|'.$movie_align.'|'.$movie_img.'|'.$movie_file;

// 동영상 여부 체크하기
$chk_video = '';
if(amina_video_all($wr_6, $wr_content)) $chk_video = '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;

//$wr_2 값 정리 - 구글지도
$wr_2 = '';
if($amina['google_map']) {
	if($map_delete) {
		;
	} else {
		//첨부사진 GPS정보 파악
		if(amina_photo_GPS() && $chk_mapExif) {
			for ($i=0; $i<count($attach); $i++) {
				if ($attach[$i]['view']) {
					$attach_img[$i] = G4_DATA_PATH.'/file/'.$bo_table.'/'.$attach[$i]['file'];
					$exif = @exif_read_data($attach_img[$i], 0, true);
					if ($exif['GPS'] && $exif['GPS']['GPSLatitude'] && $exif['GPS']['GPSLongitude']) { //위경도 좌표가 있다면
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLatitude'][0], "%d/%d"); //문자->숫자로 계산
						$gps_lat_d = $temp_d1/$temp_d2;
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLatitude'][1], "%d/%d");
						$gps_lat_m = $temp_d1/$temp_d2;
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLatitude'][2], "%d/%d");
						$gps_lat_s = $temp_d1/$temp_d2;
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLongitude'][0], "%d/%d"); //문자->숫자로 계산
						$gps_lon_d = $temp_d1/$temp_d2;
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLongitude'][1], "%d/%d");
						$gps_lon_m = $temp_d1/$temp_d2;
						list($temp_d1, $temp_d2) = sscanf($exif['GPS']['GPSLongitude'][2], "%d/%d");
						$gps_lon_s = $temp_d1/$temp_d2;

						$map_lat = $gps_lat_d+$gps_lat_m/60+$gps_lat_s/3600; //도분초를 도로 변환
						$map_lng = $gps_lon_d+$gps_lon_m/60+$gps_lon_s/3600;

						break;
					}
				}
			}
		}

		if($map_lat && $map_lng) {
			$map_zoom = ($map_zoom > 0) ? $map_zoom : '14';
			$wr_2 = $map_width.'|'.$map_height.'|'.$map_lat.'|'.$map_lng.'|'.$map_zoom;
		}
	}
}

//블라인드 처리
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 {$g4['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;
	}
}

//가변변수처리
$wr_10 =  $wr_10 ? amina_trim($wr_10) : '';
$wr_9 =  $wr_9 ? amina_trim($wr_9) : '';
$wr_8 =  $wr_8 ? amina_trim($wr_8) : '';
$wr_7 =  $wr_7 ? amina_trim($wr_7) : '';
$wr_6 =  $wr_6 ? amina_trim($wr_6) : '';
$wr_5 =  $wr_5 ? amina_trim($wr_5) : '';
$wr_2 =  $wr_2 ? amina_trim($wr_2) : '';

// 글정보 업데이트
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_6 = '$wr_6', wr_5 = '$wr_5', wr_2 = '$wr_2' where wr_id = '$wr_id' ");

// 새글 업데이트
$new = sql_fetch(" select * from {$g4['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 {$g4['board_new_table']} add wr_rss varchar(10) default '' NOT NULL ", false);
		sql_query(" alter table {$g4['board_new_table']} add wr_photo varchar(10) default '' NOT NULL AFTER wr_rss ", false);
	    sql_query(" alter table {$g4['board_new_table']} add wr_video varchar(10) default '' NOT NULL AFTER wr_photo ", false);
	    sql_query(" alter table {$g4['board_new_table']} add wr_main varchar(10) default '' NOT NULL AFTER wr_video ", false);
	    sql_query(" alter table {$g4['board_new_table']} add wr_good varchar(10) default '' NOT NULL AFTER wr_main ", false);
	    sql_query(" alter table {$g4['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 = ''" : "";

	sql_query(" update {$g4['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'] : G4_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 {$g4['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 {$g4['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 {$g4['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 = '".G4_TIME_YMDHIS."', poll_enddate = '$poll_enddate' ");
		}
	} else {
		if($poll['poll_id']) sql_query(" delete from {$g4['amina_poll']} where bo_table = '$bo_table' and wr_id = '$wr_id' ");
	}
}

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

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

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

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

		if($amina['upimg_size'] >= $attach[$i]['image_width']) continue;
	
		$org_img = G4_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 {$g4['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 = '$k' ");

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

	//Edit Attach Image Resize
	if(preg_match_all("/\<img[^\<\>]*\>/i", $wr_content, $match)) {
		for ($i=0; $i<count($match[0]); $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, G4_FILE_PERMISSION);
					@unlink($thumb); //Thumb Delete
				}
			}
		}
	}
}

// View Resize
if(!$amina['resize']) { 
	//워터마크
	$wm = array();
	if($amina['wm']) {
		$wm[0]['filename'] =  G4_PATH.'/'.$amina['wm_file'];
		$wm[0]['location'] = $amina['wm_loc'];
		$wm[0]['x'] = $amina['wm_x'];
		$wm[0]['y'] = $amina['wm_y'];
	}
	$width = $board['bo_image_width'] ? (int)$board['bo_image_width'] : 640;
	for ($i=0; $i<count($img_arr); $i++) {
		if(preg_match("/\/data\/(file|cheditor4|editor)\//i", $img_arr[$i])) {
			amina_make_thumb($img_arr[$i], $width, 0, 90, 'write', $wm);
		}
	}
}

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

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

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);
	}
}

?>