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

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

// 원본 파일 디렉토리
$src_dir = G5_DATA_PATH.'/file/'.$bo_table;

$save = array();
$save_count_write = 0;
$save_count_comment = 0;
$cnt = 0;

// 등록자 설정
$is_cmt_memo = false;
if($chk_memo) {
	$is_cmt_memo = true;
	$admin_id = $member['mb_id'];
	$admin_name = $member['mb_nick'];
	$admin_password = $member['mb_password'];
	$admin_email = $member['mb_email'];
	$admin_homepage = $member['mb_homepage'];
	$admin_level = $member['mb_9'];
}

//복사시 댓글은 제외
$sql_cmt = '';
if($chk_cmt && $sw == "copy") $sql_cmt = "and wr_is_comment != '1'";

$sql = " select distinct wr_num from $write_table where wr_id in ({$wr_id_list}) order by wr_id ";
$result = sql_query($sql);
while ($row = sql_fetch_array($result)) {
    $wr_num = $row['wr_num'];
    for ($i=0; $i<count($chk_bo_table); $i++) {
        $move_bo_table = $chk_bo_table[$i];
        $move_write_table = $g5['write_prefix'] . $move_bo_table;

        $src_dir = G5_DATA_PATH.'/file/'.$bo_table; // 원본 디렉토리
        $dst_dir = G5_DATA_PATH.'/file/'.$move_bo_table; // 복사본 디렉토리

        $count_write = 0;
        $count_comment = 0;

        $next_wr_num = get_next_num($move_write_table);

        $sql2 = " select * from $write_table where wr_num = '$wr_num' $sql_cmt order by wr_parent, wr_is_comment, wr_comment desc, wr_id ";
        $result2 = sql_query($sql2);
        while ($row2 = sql_fetch_array($result2)) {

			$nick = cut_str($member['mb_nick'], $config['cf_cut_name']);
            if (!$auto_move && !$row2['wr_is_comment'] && $config['cf_use_copy_log'])
                $row2['wr_content'] .= "\n".'<div class="content_'.$sw.'">[이 게시물은 '.$nick.'님에 의해 '.G5_TIME_YMDHIS.' '.$board['bo_subject'].'에서 '.($sw == 'copy' ? '복사' : '이동').' 됨]</div>';

			if($ca_name) $row2['ca_name'] = $ca_name;

			//현재 시간으로 변경
			$wr_datetime = ($chk_date && !$row2['wr_is_comment']) ? G5_TIME_YMDHIS : $row2['wr_datetime'];

			$wr_file = AMINA ? "wr_file = '{$row2['wr_file']}'," : "";

			$comment_cnt = $sql_cmt ? 0 : $row2['wr_comment'];

            $sql = " insert into $move_write_table
                        set wr_num = '$next_wr_num',
                             wr_reply = '{$row2['wr_reply']}',
                             wr_is_comment = '{$row2['wr_is_comment']}',
                             wr_comment = '$comment_cnt',
                             wr_comment_reply = '{$row2['wr_comment_reply']}',
                             ca_name = '".addslashes($row2['ca_name'])."',
                             wr_option = '{$row2['wr_option']}',
                             wr_subject = '".addslashes($row2['wr_subject'])."',
                             wr_content = '".addslashes($row2['wr_content'])."',
                             wr_link1 = '".addslashes($row2['wr_link1'])."',
                             wr_link2 = '".addslashes($row2['wr_link2'])."',
                             wr_link1_hit = '{$row2['wr_link1_hit']}',
                             wr_link2_hit = '{$row2['wr_link2_hit']}',
                             wr_hit = '{$row2['wr_hit']}',
                             wr_good = '{$row2['wr_good']}',
                             wr_nogood = '{$row2['wr_nogood']}',
                             mb_id = '{$row2['mb_id']}',
                             wr_password = '{$row2['wr_password']}',
                             wr_name = '".addslashes($row2['wr_name'])."',
                             wr_email = '".addslashes($row2['wr_email'])."',
                             wr_homepage = '".addslashes($row2['wr_homepage'])."',
                             wr_datetime = '$wr_datetime',
							 ".$wr_file."	
							 wr_last = '{$row2['wr_last']}',
                             wr_ip = '{$row2['wr_ip']}',
                             wr_1 = '".addslashes($row2['wr_1'])."',
                             wr_2 = '".addslashes($row2['wr_2'])."',
                             wr_3 = '".addslashes($row2['wr_3'])."',
                             wr_4 = '".addslashes($row2['wr_4'])."',
                             wr_5 = '".addslashes($row2['wr_5'])."',
                             wr_6 = '".addslashes($row2['wr_6'])."',
                             wr_7 = '".addslashes($row2['wr_7'])."',
                             wr_8 = '".addslashes($row2['wr_8'])."',
                             wr_9 = '".addslashes($row2['wr_9'])."',
                             wr_10 = '".addslashes($row2['wr_10'])."' ";
            sql_query($sql);

            $insert_id = mysql_insert_id();

            // 코멘트가 아니라면
            if (!$row2['wr_is_comment']) {

				$save_parent = $insert_id;

                $sql3 = " select * from {$g5['board_file_table']} where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' order by bf_no ";
                $result3 = sql_query($sql3);
                for ($k=0; $row3 = sql_fetch_array($result3); $k++) {
                    if ($row3['bf_file']) {
                        // 원본파일을 복사하고 퍼미션을 변경
                        @copy($src_dir.'/'.$row3['bf_file'], $dst_dir.'/'.$row3['bf_file']);
                        @chmod($dst_dir/$row3['bf_file'], AMINA_FILE_PERMISSION);
                    }

                    $sql = " insert into {$g5['board_file_table']}
                                set bo_table = '$move_bo_table',
                                     wr_id = '$insert_id',
                                     bf_no = '{$row3['bf_no']}',
                                     bf_source = '".addslashes($row3['bf_source'])."',
                                     bf_file = '".addslashes($row3['bf_file'])."',
                                     bf_download = '{$row3['bf_download']}',
                                     bf_content = '".addslashes($row3['bf_content'])."',
                                     bf_filesize = '{$row3['bf_filesize']}',
                                     bf_width = '{$row3['bf_width']}',
                                     bf_height = '{$row3['bf_height']}',
                                     bf_type = '{$row3['bf_type']}',
                                     bf_datetime = '{$row3['bf_datetime']}' ";
                    sql_query($sql);

                    if ($sw == 'move' && $row3['bf_file'])
                        $save[$cnt]['bf_file'][$k] = $src_dir.'/'.$row3['bf_file'];
                }

				//태그 등록
				$tag = amina_get_tag($bo_table, $row2['wr_id']);
				if($tag) amina_add_tag($move_bo_table, $insert_id, $tag, $row2['wr_datetime']);

				//설문 등록
				$poll = sql_fetch(" select * from {$g5['amina_poll']} where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' limit 1 ");
				if($poll['poll_id']) {
                    $sql = " insert into {$g5['amina_poll']}
                                set bo_table = '$move_bo_table', 
                                    wr_id = '$insert_id', 
		                            ca_name = '".addslashes($row2['ca_name'])."',
		                            poll_subject = '".addslashes($poll['poll_subject'])."',
		                            poll_item1 = '".addslashes($poll['poll_item1'])."',
		                            poll_item2 = '".addslashes($poll['poll_item2'])."',
		                            poll_item3 = '".addslashes($poll['poll_item3'])."',
		                            poll_item4 = '".addslashes($poll['poll_item4'])."',
		                            poll_item5 = '".addslashes($poll['poll_item5'])."',
		                            poll_item6 = '".addslashes($poll['poll_item6'])."',
		                            poll_item7 = '".addslashes($poll['poll_item7'])."',
		                            poll_item8 = '".addslashes($poll['poll_item8'])."',
		                            poll_item9 = '".addslashes($poll['poll_item9'])."',
									poll_cnt = '{$poll['poll_cnt']}', 
									poll_cnt1 = '{$poll['poll_cnt1']}', 
									poll_cnt2 = '{$poll['poll_cnt2']}', 
									poll_cnt3 = '{$poll['poll_cnt3']}', 
									poll_cnt4 = '{$poll['poll_cnt4']}', 
									poll_cnt5 = '{$poll['poll_cnt5']}', 
									poll_cnt6 = '{$poll['poll_cnt6']}', 
									poll_cnt7 = '{$poll['poll_cnt7']}', 
									poll_cnt8 = '{$poll['poll_cnt8']}', 
									poll_cnt9 = '{$poll['poll_cnt9']}', 
									poll_level = '{$poll['poll_level']}', 
									poll_point = '{$poll['poll_point']}', 
									poll_regdate = '{$poll['poll_regdate']}', 
									poll_enddate = '{$poll['poll_enddate']}' "; 
                    sql_query($sql);
				}

				//안내 코멘트 등록
				if($is_cmt_memo) {
					$wr_content = "[알림] 이 게시물은 \'{$admin_name}\'님에 의해 ".date("Y년 m월 d일 H시 i분", G5_SERVER_TIME)."에 \'{$board['bo_subject']}\' 게시판에서 " . ($sw == 'copy' ? '복사' : '이동') ."되었습니다.";
					$sql = " select max(wr_comment) as max_comment from $write_table where wr_num = '$wr_num' and wr_is_comment = 1 ";
			        $row4 = sql_fetch($sql);
				    $row4['max_comment'] += 1;
				    $tmp_comment = $row4['max_comment'];

					$sql = " insert into $move_write_table
			                set ca_name = '".addslashes($row2['ca_name'])."',
						        wr_num = '$next_wr_num',
					            wr_parent = '$save_parent',
						        wr_is_comment = '1',
			                    wr_comment = '$tmp_comment',
						        wr_content = '$wr_content',
			                    mb_id = '$admin_id',
				                wr_password = '$admin_password',
					            wr_name = '$admin_name',
						        wr_email = '$admin_email',
			                    wr_homepage = '$admin_homepage',
				                wr_datetime = '".G5_TIME_YMDHIS."',
						        wr_ip = '{$_SERVER['REMOTE_ADDR']}',
				                wr_5 = '$admin_level' ";
				    sql_query($sql);

				    sql_query(" update $move_write_table set wr_comment = wr_comment + 1, wr_last = '".G5_TIME_YMDHIS."' where wr_id = '$save_parent' ");

					//코멘트 수 증가
					$count_comment++;
				}

                $count_write++;

                if ($sw == 'move' && $i == 0) {
                    // 스크랩 이동
                    sql_query(" update {$g5['scrap_table']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");

                    // 최신글 이동
                    sql_query(" update {$g5['board_new_table']} set bo_table = '$move_bo_table', wr_id = '$save_parent', wr_parent = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");

					//설문로그 이동
                    if($poll['poll_id']) sql_query(" update {$g5['amina_polllog']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");

					//별점로그 이동
                    sql_query(" update {$g5['amina_star']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");

					//추천로그 이동
                    sql_query(" update {$g5['board_good_table']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");		

					//열람 & 다운 이동
                    sql_query(" update {$g5['amina_point']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");		

					//AP 로그 이동
                    sql_query(" update {$g5['amina_mileage']} set po_rel_table = '$move_bo_table', po_rel_id = '$save_parent' where po_rel_table = '$bo_table' and po_rel_id = '{$row2['wr_id']}' ");		

					//글반응 이동
                    sql_query(" update {$g5['amina_response']} set bo_table = '$move_bo_table', wr_id = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");		
				}

			} else {

				$count_comment++;

                if ($sw == 'move') {
                    // 최신글 이동
                    sql_query(" update {$g5['board_new_table']} set bo_table = '$move_bo_table', wr_id = '$insert_id', wr_parent = '$save_parent' where bo_table = '$bo_table' and wr_id = '{$row2['wr_id']}' ");
				}
            }

            sql_query(" update $move_write_table set wr_parent = '$save_parent' where wr_id = '$insert_id' ");

            if ($sw == 'move')
                $save[$cnt]['wr_id'] = $row2['wr_parent'];

            $cnt++;
        }

        sql_query(" update {$g5['board_table']} set bo_count_write = bo_count_write + '$count_write' where bo_table = '$move_bo_table' ");
        sql_query(" update {$g5['board_table']} set bo_count_comment = bo_count_comment + '$count_comment' where bo_table = '$move_bo_table' ");

		if($sw == "copy" && $is_cmt_memo) { //원글에 코멘트 추가
			$board1 = sql_fetch(" select bo_subject from {$g5['board_table']} where bo_table = '$move_bo_table' ");

			$wr = sql_fetch(" select * from $write_table where wr_num = '$wr_num' and wr_is_comment = '0' ");

		    $wr_content = "[알림] 이 게시물은 \'{$admin_name}\'님에 의해 ".date("Y년 m월 d일 H시 i분", G5_SERVER_TIME)."에 \'{$board1['bo_subject']}\' 게시판으로 복사되었습니다.";
			$sql = " insert into $write_table
		                set ca_name = '{$wr['ca_name']}',
					        wr_num = '{$wr['wr_num']}',
				            wr_parent = '{$wr['wr_id']}',
					        wr_is_comment = '1',
				            wr_comment = '$tmp_comment',
					        wr_content = '$wr_content',
				            mb_id = '$admin_id',
			                wr_password = '$admin_password',
				            wr_name = '$admin_name',
					        wr_email = '$admin_email',
				            wr_homepage = '$admin_homepage',
			                wr_datetime = '".G5_TIME_YMDHIS."',
					        wr_ip = '{$_SERVER['REMOTE_ADDR']}',
				            wr_5 = '$admin_level' ";
		    sql_query($sql);

		    sql_query(" update $write_table set wr_comment = wr_comment + 1, wr_last = '".G5_TIME_YMDHIS."' where wr_id = '{$wr['wr_id']}' ");
		    sql_query(" update {$g5['board_table']} set bo_count_comment = bo_count_comment + 1 where bo_table = '$bo_table' ");
		}

		if(AMINA) delete_cache_latest($move_bo_table);
    }

    $save_count_write += $count_write;
    $save_count_comment += $count_comment;
}

if(AMINA) delete_cache_latest($bo_table);

if ($sw == 'move') {
    for ($i=0; $i<count($save); $i++){
        for ($k=0; $k<count($save[$i]['bf_file']); $k++)
            @unlink($save[$i]['bf_file'][$k]);

        sql_query(" delete from $write_table where wr_parent = '{$save[$i]['wr_id']}' ");
        sql_query(" delete from {$g5['board_new_table']} where bo_table = '$bo_table' and wr_id = '{$save[$i]['wr_id']}' ");
        sql_query(" delete from {$g5['board_file_table']} where bo_table = '$bo_table' and wr_id = '{$save[$i]['wr_id']}' ");

		//태그삭제
		amina_del_tag($bo_table, $save[$i]['wr_id']);

		//설문삭제
		sql_query(" delete from {$g5['amina_poll']} where bo_table = '$bo_table' and wr_id = '{$save[$i]['wr_id']}' ");
	}
    sql_query(" update {$g5['board_table']} set bo_count_write = bo_count_write - '$save_count_write', bo_count_comment = bo_count_comment - '$save_count_comment' where bo_table = '$bo_table' ");
}

// syndication ping
if(AMINA || (!AMINA && $amina['syndi'])) include G5_SYNDI_PATH.'/include/include.bbs.move_update.php';