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

$captcha_html = "";
if ($is_guest && $board['bo_comment_level'] < 2) {
    $captcha_html = captcha_html('_comment');
}

$is_comment_write = false;
if ($member['mb_level'] >= $board['bo_comment_level']) {
    $is_comment_write = true;
}

$c_id = isset($_GET['c_id']) ? clean_xss_tags($_GET['c_id'], 1, 1) : '';
$c_wr_content = '';
$list = array();

// 코멘트 출력
$sql = "select * from {$write_table} where wr_parent = '$wr_id' and wr_is_comment = 1 order by wr_num, wr_cmt_order";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
    $list[$i] = $row;

    $tmp_name = get_text(cut_str($row['wr_name'], $config['cf_cut_name'])); // 설정된 자리수 만큼만 이름 출력
    if ($board['bo_use_sideview']) {
        $list[$i]['name'] = get_sideview($row['mb_id'], $tmp_name, $row['wr_email'], $row['wr_homepage']);
    } else {
        $list[$i]['name'] = '<span class="'.($row['mb_id']?'member':'guest').'">'.$tmp_name.'</span>';
    }
    
    $list[$i]['content'] = '<p>비밀글 입니다.</p>';
    if (!strstr($row['wr_option'], 'secret') || $is_admin || ($write['mb_id']===$member['mb_id'] && $member['mb_id']) || ($row['mb_id']===$member['mb_id'] && $member['mb_id'])) {
        $list[$i]['content1'] = $row['wr_content'];
        $list[$i]['content'] = conv_content($row['wr_content'], 1, 'wr_content');
        $list[$i]['content'] = search_font($stx, $list[$i]['content']);
    } else {
        $ss_name = 'ss_secret_comment_'.$bo_table.'_'.$list[$i]['wr_id'];
        if(!get_session($ss_name))
            $list[$i]['content'] = '<a href="'.G5_BBS_URL.'/password.php?w=sc&amp;bo_table='.$bo_table.'&amp;wr_id='.$list[$i]['wr_id'].$qstr.'" class="s_cmt"><i class="fa fa-lock"></i> 댓글내용 확인</a>';
        else {
            $list[$i]['content'] = conv_content($row['wr_content'], 0, 'wr_content');
            $list[$i]['content'] = search_font($stx, $list[$i]['content']);
        }
    }
    if($row['wr_cmt_del'] == 1) {
        $list[$i]['content'] = '<p class="cmt_del">삭제된 댓글입니다.</p>';
    }
    $list[$i]['datetime'] = substr($row['wr_datetime'],2,14);
    // 관리자가 아니라면 중간 IP 주소를 감춘후 보여줍니다.
    $list[$i]['ip'] = $row['wr_ip'];
    if (!$is_admin) {
        $list[$i]['ip'] = preg_replace("/([0-9]+).([0-9]+).([0-9]+).([0-9]+)/", G5_IP_DISPLAY, $row['wr_ip']);
    }
    
    /* 수정,삭제,답글 버튼 노출 여부 */
    $list[$i]['is_reply'] = true;
    $list[$i]['is_edit']  = false;
    $list[$i]['is_del']   = false;
    
    if ($is_comment_write || $is_admin) {
        $token = '';
        if($member['mb_id']) {
            if ($row['mb_id'] === $member['mb_id'] || $is_admin) {
                set_session('ss_delete_comment_'.$row['wr_id'].'_token', $token = uniqid(time()));
                $list[$i]['is_edit']   = true;
                $list[$i]['is_del']    = true;
            }
        } else {
            //비회원의 작성댓글은 수정 삭제가 불가
        }
    }
    
    //동영상
    $list[$i]['movie'] = '';
    if($row['wr_movie_url']) {
        $movie_id = get_movie_id($row['wr_movie_url']);
        if($movie_id) {
            $list[$i]['movie'] = $movie_id;
        }
    }
    
    //첨부파일 확인
    $list[$i]['file'] = get_file($board['bo_table'], $row['wr_id']);
    if($list[$i]['file']['count'] > 0) {
        set_session('ss_view_'.$board['bo_table'].'_'.$row['wr_id'], true);
    }
}

//  코멘트수 제한 설정값
if ($is_admin) {
    $comment_min = $comment_max = 0;
} else {
    $comment_min = (int)$board['bo_comment_min'];
    $comment_max = (int)$board['bo_comment_max'];
}

include_once($board_skin_path.'/view_comment.skin.php');

