<?php

/*
=====================================================================

  프로젝트 이름 : 피리 카페 심플

  만든사람 : 피리 PIREE ( tosports@naver.com )

  홈페이지 : https://piree.kr/nett_dev

  작성날짜 : 2024년 04월 29일 월요일 오전 06시 40분

  저 작 권 : Copyright ⓒ 2024 피리 piree.kr All right reserved
              그누보드 외에 추가된 소스는~
              만든사람의 허락없이 무단으로 사용할수 없습니다.
              사용하고자 할 경우 만든사람의 허락을 받아야 합니다.
              http://.piree.kr 에 문의해 주세요.

=====================================================================
 피리 카페 심플 > 프로그램 > 피리 카페 관리 > 내 카페 > 게시판 > 게시글 삭제
=====================================================================

*/

  include_once('./_common.php');

  include_once( piree__path($piree_prog_a[770099]['num'], '_CAFE.INFO.CHECK.inc.php') );

  $delete_token = get_session('ss_delete_token');

  set_session('ss_delete_token', '');

  IF ( !($token && $delete_token == $token) )
  {
    alert('토큰 에러로 삭제 불가합니다.');
  }

  include_once(G5_LIB_PATH.'/thumbnail.lib.php');

  $count_write = 0;

  $count_comment = 0;

//$wr = sql_fetch(" select * from $write_table where wr_id = '$wr_id' ");

  @include_once($board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete.head.skin.php');

  IF ( piree_cafe_auth_get())
  {

    ;
  }
  ELSE IF ( $member['mb_id'])
  {

    IF ( $member['mb_id'] !== $write['mb_id'] )
    {
      alert('자신의 게시글이 아니므로 삭제할 수 없습니다.');
    }

  }
  ELSE
  {

    alert('댓글을 삭제할 권한이 없습니다.');

  }

  $len = strlen($write['wr_reply']);

  IF ( $len < 0 )
  {
    $len = 0;
  }

  $reply = substr($write['wr_reply'], 0, $len);

  $sql_select  = "SELECT COUNT(*) FROM `". $piree['cafe_article'] ."` ";
  $sql_select .= "WHERE `wr_reply` like '". $reply ."%' AND `wr_id` <> '". $write['wr_id'] ."' AND `wr_num` = '". $write['wr_num'] ."' AND `wr_is_comment` = '0'";
  $exist_t = sql_EFV($sql);

  IF ( $exist_t > 0 && !piree_cafe_auth_get() )
  {
    alert('이 글과 관련된 답변글이 존재하므로 삭제 할 수 없습니다.\\n\\n우선 답변글부터 삭제하여 주십시오.');
  }

  @include_once($board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete.skin.php');

  # 나라오름님 수정 : 원글과 코멘트수가 정상적으로 업데이트 되지 않는 오류를 잡아 주셨습니다.

//$sql = " select wr_id, mb_id, wr_comment from $write_table where wr_parent = '$write[wr_id]' order by wr_id ";
  $sql_select  = "SELECT `wr_id`, `mb_id`, `wr_is_comment`, `wr_content` FROM `". $write_table ."` ";
  $sql_select .= "WHERE `wr_parent` = '". $write['wr_id'] ."' ORDER BY `wr_id`";
  $result = sql_query($sql_select);

// ( $row = sql_fetch_array($result) )
  FOR ( $loop_n=0; $row = sql_fetch_array($result); $loop_n++ )
  {

    IF ( !$row['wr_is_comment'] )
    {

        IF ( !delete_point($row['mb_id'], $piree['cafe_board_code'], $row['wr_id'], '쓰기') )
        {

          $point_geant_title_s = $board_cate_list_a[$board_cate_n]['bca_subject'] ." ". $row['wr_id'] ." 글삭제";

          insert_point($row['mb_id'], $board_cate_list_a[$board_cate_n]['bca_write_point'] * (-1), $point_geant_title_s);

        }

        $sql_select  = "SELECT * FROM `". $g5['board_file_table'] ."` ";
        $sql_select .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."' AND `wr_id` = '". $row['wr_id'] ."'";
        $result2 = sql_query($sql_select);

        FOR ( $loop_n=0; $row2 = sql_fetch_array($result2); $loop_n++ )
        {

          $delete_file = run_replace('delete_file_path', G5_DATA_PATH.'/file/'.$piree['cafe_board_code'].'/'.str_replace('../', '', $row2['bf_file']), $row2);

          IF( file_exists($delete_file) )
          {
              @unlink($delete_file);
          }

          IF( preg_match("/\.({$config['cf_image_extension']})$/i", $row2['bf_file']) )
          {
              delete_board_thumbnail($piree['cafe_board_code'], $row2['bf_file']);
          }

        }

        delete_editor_thumbnail($row['wr_content']);

        $sql_delete  = "DELETE FROM `". $g5['board_file_table'] ."` ";
        $sql_delete .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."' AND `wr_id` = '". $row['wr_id'] ."'";
        sql_query($sql_delete);

        $count_write++;

    }
    ELSE
    {

      IF ( !delete_point($row['mb_id'], $piree['cafe_board_code'], $row['wr_id'], '댓글') )
      {

        $point_geant_title_s = $board_cate_list_a[$board_cate_n]['bca_subject'] ." ". $write['wr_id'] ."-". $row['wr_id'] ."댓글삭제";

        insert_point($row['mb_id'], $board_cate_list_a[$board_cate_n]['bca_comment_point'] * (-1), $point_geant_title_s);

      }

      $count_comment++;

    }

  }

    $sql_delete  = "DELETE FROM `". $write_table ."` ";
    $sql_delete .= "WHERE `wr_parent` = '". $write['wr_id'] ."'";
    sql_query($sql_delete);

    $sql_delete  = "DELETE FROM `". $g5['board_new_table'] ."` ";
    $sql_delete .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."' AND `wr_parent` = '". $write['wr_id'] ."'";
    sql_query($sql_delete);

    $sql_delete  = "DELETE FROM `". $g5['scrap_table'] ."` ";
    $sql_delete .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."' AND `wr_id` = '". $write['wr_id'] ."'";
    sql_query($sql_delete);

/*

    $notice_array = explode("\n", trim($board_cate_list_a[$board_cate_n]['bca_notice']));
    $bo_notice = "";
    FOR ( $k=0; $k<count($notice_array); $k++ )
    {
      IF ( (int)$write[wr_id] != (int)$notice_array[$k] )
      {
        $bo_notice .= $notice_array[$k] . "\n";
      }
    }
    $bo_notice = trim($bo_notice);
*/

    $bo_notice = board_notice($board_cate_list_a[$board_cate_n]['bca_notice'], $write['wr_id']);

    $sql_update  = "UPDATE `". $g5['board_table'] ."` SET ";
    $sql_update .= "`bo_notice` = '". $bo_notice ."' ";
    $sql_update .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."'";
    sql_query($sql_update);

    IF ( $count_write > 0 || $count_comment > 0 )
    {

      $sql_update  = "UPDATE `". $g5['board_table'] ."` SET ";
      $sql_update .= "`bo_count_write` = `bo_count_write` - '". $count_write ."', ";
      $sql_update .= "`bo_count_comment` = `bo_count_comment` - '". $count_comment ."' ";
      $sql_update .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."'";
      sql_query($sql_update);

    }

  @include_once( $board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete.tail.skin.php' );

  delete_cache_latest($piree['cafe_board_code']);

  run_event('bbs_delete', $write, $board);

  $move_url_s = go_piree_cafe_board('', $piree_cafe_n, $board_cate_n) .'&amp;page='. $page . $qstr;

  goto_url($move_url_s);

