<?php
include_once "./_common.php";
include_once(G5_CAPTCHA_PATH . '/captcha.lib.php');

if ($is_guest)
  alert('회원만 이용하실 수 있습니다.');

if (!chk_captcha()) {
  alert('자동등록방지 숫자가 틀렸습니다.');
}

$recv_list = explode(',', trim($_POST['me_recv_mb_id']));
$str_nick_list = '';
$msg = '';
$error_list = [];
$member_list = [
  'id' => [],
  'nick' => []
];
$me_memo = isset($_POST['me_memo']) ? preg_replace("#[\\\]+$#", "", substr(trim($_POST['me_memo']), 0, 65536)) : '';

EventHandler::triggerEvent("gnuboard.memo.form_update_before", $recv_list);

for ($i = 0; $i < count($recv_list); $i++) {
  $recv_list_id = substr(preg_replace("/[^a-zA-Z0-9_]*/", "", $recv_list[$i]), 0, 20);
  $row = sql_fetch("SELECT mb_id, mb_nick, mb_open, mb_leave_date, mb_intercept_date FROM {$g5['member_table']} where mb_id = '{$recv_list_id}' ");
  if ($row) {
    if ($is_admin || ($row['mb_open'] && (!$row['mb_leave_date'] || !$row['mb_intercept_date']))) {
      $member_list['id'][] = $row['mb_id'];
      $member_list['nick'][] = $row['mb_nick'];
    } else {
      $error_list[] = $recv_list_id;
    }
  }
}

$error_msg = implode(",", $error_list);

if ($error_msg && !$is_admin)
  alert("회원아이디 '{$error_msg}' 은(는) 존재(또는 정보공개)하지 않는 회원아이디 이거나 탈퇴, 접근차단된 회원아이디 입니다.\\n쪽지를 발송하지 않았습니다.");

if (!count($member_list['id'])) {
  alert('해당 회원이 존재하지 않습니다.');
}

if (!$is_admin) {
  if (count($member_list['id'])) {
    $point = (int) $config['cf_memo_send_point'] * count($member_list['id']);
    if ($point) {
      if ($member['mb_point'] - $point < 0) {
        alert('보유하신 포인트(' . number_format($member['mb_point']) . '점)가 모자라서 쪽지를 보낼 수 없습니다.');
      }
    }
  }
}

for ($i = 0; $i < count($member_list['id']); $i++) {
  $tmp_row = sql_fetch("SELECT max(me_id) as max_me_id FROM {$g5['memo_table']} ");
  $me_id = $tmp_row['max_me_id'] + 1;

  $recv_mb_id = $member_list['id'][$i];
  $recv_mb_nick = get_text($member_list['nick'][$i]);

  // 받는 회원 쪽지 INSERT
  $sql = "INSERT INTO {$g5['memo_table']} ( me_recv_mb_id, me_send_mb_id, me_send_datetime, me_memo, me_read_datetime, me_type, me_send_ip ) values ( '$recv_mb_id', '{$member['mb_id']}', '" . G5_TIME_YMDHIS . "', '{$me_memo}', '0000-00-00 00:00:00' , 'recv', '{$_SERVER['REMOTE_ADDR']}' ) ";

  sql_query($sql);

  if ($me_id = sql_insert_id()) {

    // 보내는 회원 쪽지 INSERT
    $sql = "INSERT INTO {$g5['memo_table']} ( me_recv_mb_id, me_send_mb_id, me_send_datetime, me_memo, me_read_datetime, me_send_id, me_type , me_send_ip ) values ( '$recv_mb_id', '{$member['mb_id']}', '" . G5_TIME_YMDHIS . "', '{$me_memo}', '0000-00-00 00:00:00', '$me_id', 'send', '{$_SERVER['REMOTE_ADDR']}' ) ";
    sql_query($sql);

    $member_list['me_id'][$i] = $me_id;
  }

  // 실시간 쪽지 알림 기능
  $sql = "UPDATE {$g5['member_table']} SET mb_memo_call = '{$member['mb_id']}', mb_memo_cnt = '" . get_memo_not_read($recv_mb_id) . "' where mb_id = '$recv_mb_id' ";
  sql_query($sql);

  if (!$is_admin) {
    insert_point($member['mb_id'], (int) $config['cf_memo_send_point'] * (-1), $recv_mb_nick . '(' . $recv_mb_id . ')님께 쪽지 발송', '@memo', $recv_mb_id, $me_id);
  }
}

if ($member_list) {

  $redirect_url = G5_HTTP_BBS_URL . "/memo.php?kind=send";
  $str_nick_list = implode(',', $member_list['nick']);

  EventHandler::triggerEvent("gnuboard.memo.form_update_after", $member_list, $str_nick_list, $redirect_url, $_POST['me_memo']);

  alert($str_nick_list . " 님께 쪽지를 전달하였습니다.", $redirect_url, false);
} else {

  $redirect_url = G5_HTTP_BBS_URL . "/memo_form.php";

  EventHandler::triggerEvent("gnuboard.memo.form_update_failed", $member_list, $redirect_url, $_POST['me_memo']);

  alert("회원아이디 오류 같습니다.", $redirect_url, false);
}
