<?php

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

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

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

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

  작성날짜 : 2024년 04월 22일 월요일 오전 08시 23분 / 구름많음, 14.6, 쾌적, 20, 13, 28%, 30%, 1.7ms / 양호, 39, 17

  저 작 권 : 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') );

  include_once( get_cafe_skin_file('path', 'basic', '_head.sub.php') );

  IF ( !$is_member )
  {
    $href = './login.php?'.$qstr.'&amp;url='. urlencode(go_piree_cafe_board(null, $piree_cafe_n, $board_cate_n, $wr_id));
    echo '<script> alert(\'회원만 접근 가능합니다.\'); top.location.href = \''.str_replace('&amp;', '&', $href).'\'; </script>';
    exit;
  }

  IF ( ! (isset($write['wr_id']) && $write['wr_id']) )
  {
    alert_close('스크랩하시려는 게시글이 존재하지 않습니다.');
  }

  $sql_select  = "SELECT COUNT(*) FROM `". $g5['scrap_table'] ."` ";
  $sql_select .= "WHERE `mb_id` = '". $member['mb_id'] ."' AND `bo_table` = '". $bo_table ."' AND `wr_id` = '". $wr_id ."'";
  $exist_t = sql_efv($sql);

  IF ( $exist_t > 0 )
  {

    echo '
    <script>
    if (confirm(\'이미 스크랩하신 글 입니다.'."\n\n".'지금 스크랩을 확인하시겠습니까?\'))
        document.location.href = \'./'. piree__url($piree_prog_a[770099]['num'], 'piree.cafe.scrap.php') .'\';
    else
        window.close();
    </script>
    <noscript>
    <p>이미 스크랩하신 글 입니다.</p>
    <a href="'. piree__url($piree_prog_a[770099]['num'], 'piree.cafe.scrap.php') .'">스크랩 확인하기</a>
    <a href="'. go_piree_cafe_board(null, $piree_cafe_n, $board_cate_n, $wr_id) .'">돌아가기</a>
    </noscript>';
    exit;

  }

  $wr_content = isset($_POST['wr_content']) ? trim($_POST['wr_content']) : '';

  IF ( $wr_content && ($cafe_member_a['cafe_mem_level_n'] >= $board_cate_list_a[$board_cate_n]['bca_comment_level']) )
  {

      $wr = get_write($write_table, $wr_id);

      IF ( isset($wr['wr_id']) && $wr['wr_id'] )
      {

//.00.15 - 댓글_수정시__연속_게시물_등록_메시지로__인한_오류_수정
          IF ( $w == 'c' && $_SESSION['ss_datetime'] >= (G5_SERVER_TIME - $config['cf_delay_sec']) && !piree_cafe_auth_get() )
          {
              alert('너무 빠른 시간내에 게시물을 연속해서 올릴 수 없습니다.');
          }

          set_session('ss_datetime', G5_SERVER_TIME);

          $mb_id = $member['mb_id'];

          $wr_name = addslashes(clean_xss_tags($board_cate_list_a[$board_cate_n]['bca_use_name'] ? $member['mb_name'] : $member['mb_nick']));

          $wr_password = $member['mb_password'];

          $wr_email = addslashes($member['mb_email']);

          $wr_homepage = addslashes(clean_xss_tags($member['mb_homepage']));

          $sql_select  = "SELECT MAX(`wr_comment`) AS `max_comment` FROM `". $piree['cafe_article'] ."` ";
          $sql_select .= "WHERE `wr_parent` = '". $wr_id ."' AND `wr_is_comment` = '1'";
          $row = sql_fetch($sql_select);
          $row['max_comment'] += 1;

          $sql_insert  = "INSERT INTO `". $piree['cafe_article'] ."` SET ";
          $sql_insert .= "`ca_name`            = '". $wr['ca_name'] ."', ";
          $sql_insert .= "`wr_option`          = '', ";
          $sql_insert .= "`wr_num`             = '". $wr['wr_num'] ."', ";
          $sql_insert .= "`wr_reply`           = '', ";
          $sql_insert .= "`wr_parent`          = '". $wr_id ."', ";
          $sql_insert .= "`wr_is_comment`      = '1', ";
          $sql_insert .= "`wr_comment`         = '". $row['max_comment'] ."', ";
          $sql_insert .= "`wr_content`         = '". $wr_content ."', ";
          $sql_insert .= "`mb_id`              = '". $mb_id ."', ";
          $sql_insert .= "`wr_password`        = '". $wr_password ."', ";
          $sql_insert .= "`wr_name`            = '". $wr_name ."', ";
          $sql_insert .= "`wr_email`           = '". $wr_email ."', ";
          $sql_insert .= "`wr_homepage`        = '". $wr_homepage ."', ";
          $sql_insert .= "`wr_datetime`        = '". G5_TIME_YMDHIS ."', ";
          $sql_insert .= "`wr_ip`              = '". $_SERVER['REMOTE_ADDR'] ."', ";
          $sql_insert .= "`wr_pi_piree_cafe_n` = '". $wr['wr_pi_piree_cafe_n'] ."', ";
          $sql_insert .= "`wr_pi_board_cate_n` = '". $wr['wr_pi_board_cate_n'] ."', ";
          $sql_insert .= "`wr_pi_piree_cafe_s` = '". addslashes($wr['wr_pi_piree_cafe_s']) ."', ";
          $sql_insert .= "`wr_pi_board_cate_s` = '". addslashes($wr['wr_pi_board_cate_s']) ."'";

          IF ( !sql_query($sql_insert) )
          {
              alert("스크랩 [ 댓글 ]을 입력하지 못하였습니다.");
          }

          $comment_id = sql_insert_id();

          $sql_update  = "UPDATE `". $piree['cafe_article'] ."` SET ";
          $sql_update .= "`wr_comment` = `wr_comment` + 1 ";
          $sql_update .= "`WHERE `wr_id` = '". $wr_id ."'";
          sql_query($sql_update);

          $sql_insert  = "INSERT `". $g5['board_new_table'] ."` SET ";
          $sql_insert .= "`bo_table`           = '". $bo_table ."', ";
          $sql_insert .= "`wr_id`              = '". $comment_id ."', ";
          $sql_insert .= "`wr_parent`          = '". $wr_id ."', ";
          $sql_insert .= "`bn_datetime`        = '". G5_TIME_YMDHIS ."', ";
          $sql_insert .= "`mb_id`              = '". $member['mb_id'] ."', ";
          $sql_insert .= "`bn_pi_piree_cafe_n` = '". $wr['wr_pi_piree_cafe_n'] ."', ";
          $sql_insert .= "`bn_pi_board_cate_n` = '". $wr['wr_pi_board_cate_n'] ."', ";
          $sql_insert .= "`bn_pi_piree_cafe_s` = '". addslashes($wr['wr_pi_piree_cafe_s']) ."', ";
          $sql_insert .= "`bn_pi_board_cate_s` = '". addslashes($wr['wr_pi_board_cate_s']) ."'";
          sql_query($sql_insert);

          $sql_update  = "UPDATE `". $g5['board_table'] ."` SET ";
          $sql_update .= "`bo_count_comment` = `bo_count_comment` + 1 ";
          $sql_update .= "WHERE `bo_table` = '". $bo_table ."'";
          sql_query($sql_update);

          $point_trade_title_s = $board_cate_list_a[$board_cate_n]['bca_subject'] ." ".$wr_id ."-".$comment_id ." 댓글쓰기(스크랩)";

          insert_point($member['mb_id'], $board_cate_list_a[$board_cate_n]['bca_comment_point'], $point_trade_title_s, $bo_table, $comment_id, '댓글');

      }

  }

  $sql_insert  = "INSERT INTO `". $g5['scrap_table'] ."` SET ";
  $sql_insert .= "`mb_id`              = '". $member['mb_id'] ."', ";
  $sql_insert .= "`bo_table`           = '". $bo_table ."', ";
  $sql_insert .= "`wr_id`              = '". $wr_id ."', ";
  $sql_insert .= "`ms_datetime`        = '". G5_TIME_YMDHIS ."', ";
  $sql_insert .= "`sc_pi_piree_cafe_n` = '". $wr['wr_pi_piree_cafe_n'] ."', ";
  $sql_insert .= "`sc_pi_board_cate_n` = '". $wr['wr_pi_board_cate_n'] ."', ";
  $sql_insert .= "`sc_pi_piree_cafe_s` = '". addslashes($wr['wr_pi_piree_cafe_s']) ."', ";
  $sql_insert .= "`sc_pi_board_cate_s` = '". addslashes($wr['wr_pi_board_cate_s']) ."'";

  IF ( !sql_query($sql_insert) )
  {

    $sql_alter = "ALTER TABLE `". $g5['scrap_table'] ."` ADD `sc_pi_piree_cafe_n` INT UNSIGNED NOT NULL DEFAULT '0'";
    sql_query($sql_alter, false);

    $sql_alter = "ALTER TABLE `". $g5['scrap_table'] ."` ADD `sc_pi_board_cate_n` INT UNSIGNED NOT NULL DEFAULT '0'";
    sql_query($sql_alter, false);

    $sql_alter = "ALTER TABLE `". $g5['scrap_table'] ."` ADD `sc_pi_piree_cafe_s` VARCHAR(255) NOT NULL DEFAULT ''";
    sql_query($sql_alter, false);

    $sql_alter = "ALTER TABLE `". $g5['scrap_table'] ."` ADD `sc_pi_board_cate_s` VARCHAR(255) NOT NULL DEFAULT ''";
    sql_query($sql_alter, false);

    IF ( !sql_query($sql_update) )
    {

      alert("[ 스크랩 ]을 입력하지 못하였습니다.");
    }

  }

  $sql_update  = "UPDATE `". $g5['member_table'] ."` SET ";
  $sql_update .= "`mb_scrap_cnt` = '". get_scrap_totals($member['mb_id']) ."' ";
  $sql_update .= "WHERE `mb_id` = '". $member['mb_id'] ."'";

  IF ( !sql_query($sql_update) )
  {
      alert("회원님의 [ 스크랩 ] 수를 입력하지 못하였습니다. ");
  }

  delete_cache_latest($bo_table);

?>

<script>
    if (confirm("이 글을 스크랩 하였습니다.\n\n지금 스크랩을 확인하시겠습니까?"))
        document.location.href = "<?php echo piree__url($piree_prog_a[770099]['num'], 'piree.cafe.scrap.php'); ?>";
    else
        window.close();
</script>
<noscript>
<p>이 글을 스크랩 하였습니다.</p>
<a href="<?php echo piree__url($piree_prog_a[770099]['num'], 'piree.cafe.scrap.php'); ?>">스크랩 확인하기</a>
</noscript>

<?php 

?>