최초 커밋
This commit is contained in:
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
// // /egbim/bbs/descope_qa_comment.php
|
||||
// header('Content-Type: application/json; charset=utf-8');
|
||||
// ini_set('display_errors', 1);
|
||||
// error_reporting(E_ALL);
|
||||
|
||||
// /* 공통 응답 헬퍼 */
|
||||
// function out($arr, $code=200){
|
||||
// http_response_code($code);
|
||||
// echo json_encode($arr, JSON_UNESCAPED_UNICODE);
|
||||
// exit;
|
||||
// }
|
||||
|
||||
// /* 세션 복원 */
|
||||
// require_once __DIR__ . '/../skin/member/basic/descope_session.php';
|
||||
// if (session_status() !== PHP_SESSION_ACTIVE) session_start();
|
||||
|
||||
// /* 로그인 정보 */
|
||||
// $loginId = $_SESSION['user']['loginIds'][0] ?? '';
|
||||
// $userName = $_SESSION['user']['name'] ?? ($_SESSION['user']['userName'] ?? '');
|
||||
|
||||
// /* 관리자 가드 (하드코딩 3인) - 외부 파일 있으면 사용, 없으면 폴백 */
|
||||
// $ADMIN_IDS = [
|
||||
// 'kjy0426@hanmaceng.co.kr',
|
||||
// 'b24014@hanmaceng.co.kr',
|
||||
// 'b23065@hanmaceng.co.kr',
|
||||
// 'b23008@baroncs.co.kr',
|
||||
// 'cjy627@hanmaceng.co.kr',
|
||||
// 'b23072@hanmaceng.co.kr',
|
||||
// ];
|
||||
|
||||
// // ✅ 관리자 가드
|
||||
// @include_once $_SERVER['DOCUMENT_ROOT'].'/egbim/bbs/admin_guard.php';
|
||||
// $isAdmin = function_exists('is_qna_admin') ? is_qna_admin($loginId) : false;
|
||||
|
||||
// /* 입력(JSON 또는 form) */
|
||||
// $raw = file_get_contents('php://input');
|
||||
// $data = json_decode($raw, true);
|
||||
// if (!is_array($data) || !isset($data['postId'])) $data = $_POST;
|
||||
|
||||
// $postId = isset($data['postId']) ? (int)$data['postId'] : 0;
|
||||
// $comment = isset($data['comment']) ? trim((string)$data['comment']) : '';
|
||||
|
||||
// if ($postId < 1 || $comment === '') {
|
||||
// out(['status'=>'fail','message'=>'잘못된 요청입니다.'], 400);
|
||||
// }
|
||||
// if (mb_strlen($comment,'UTF-8') > 2000) {
|
||||
// out(['status'=>'fail','message'=>'댓글은 2,000자 이내로 입력하세요.'], 400);
|
||||
// }
|
||||
|
||||
// /* DB 연결 */
|
||||
// try{
|
||||
// $pdo = new PDO(
|
||||
// "mysql:host=localhost;dbname=egbim;charset=utf8mb4",
|
||||
// 'egbim','baron3840!!',
|
||||
// [
|
||||
// PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
|
||||
// PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
|
||||
// ]
|
||||
// );
|
||||
// }catch(Exception $e){
|
||||
// out(['status'=>'fail','message'=>'DB 연결 실패: '.$e->getMessage()],500);
|
||||
// }
|
||||
|
||||
// /* 글 존재 및 권한 체크
|
||||
// - 관리자: 모든 글/비밀글에 댓글 OK
|
||||
// - 일반(비관리자): "비밀글이 아니고(category = general)"인 글에만 댓글 허용
|
||||
// */
|
||||
// $st = $pdo->prepare("SELECT post_id, login_id AS author_id, is_secret FROM qa_posts WHERE post_id=?");
|
||||
// $st->execute([$postId]);
|
||||
// $post = $st->fetch();
|
||||
// if (!$post) out(['status'=>'fail','message'=>'존재하지 않는 글입니다.'],404);
|
||||
|
||||
// if (!$isAdmin && (int)$post['is_secret'] === 1 && $post['author_id'] !== $loginId) {
|
||||
// out(['status'=>'fail','message'=>'비밀글에는 작성자 본인과 관리자만 댓글을 작성할 수 있습니다.'],403);
|
||||
// }
|
||||
|
||||
// /* 로그인 이름 폴백 (비로그인/게스트 허용 유지) */
|
||||
// if ($loginId === '') $loginId = 'guest';
|
||||
// if ($userName === '') $userName = $loginId;
|
||||
|
||||
// /* qa_comments.user_name 컬럼 존재 여부 확인 (있으면 이름도 저장) */
|
||||
// $hasUserName = false;
|
||||
// $q = $pdo->prepare("
|
||||
// SELECT COUNT(*)
|
||||
// FROM INFORMATION_SCHEMA.COLUMNS
|
||||
// WHERE TABLE_SCHEMA = DATABASE()
|
||||
// AND TABLE_NAME = 'qa_comments'
|
||||
// AND COLUMN_NAME = 'user_name'
|
||||
// ");
|
||||
// $q->execute();
|
||||
// $hasUserName = ((int)$q->fetchColumn() > 0);
|
||||
|
||||
// /* 저장 */
|
||||
// try{
|
||||
// if ($hasUserName){
|
||||
// $ins = $pdo->prepare("
|
||||
// INSERT INTO qa_comments (post_id, commenter, user_name, content, created_at)
|
||||
// VALUES (:pid, :login_id, :user_name, :content, NOW())
|
||||
// ");
|
||||
// $ins->execute([
|
||||
// ':pid' => $postId,
|
||||
// ':login_id' => $loginId,
|
||||
// ':user_name' => $userName,
|
||||
// ':content' => $comment,
|
||||
// ]);
|
||||
// } else {
|
||||
// $ins = $pdo->prepare("
|
||||
// INSERT INTO qa_comments (post_id, commenter, content, created_at)
|
||||
// VALUES (:pid, :login_id, :content, NOW())
|
||||
// ");
|
||||
// $ins->execute([
|
||||
// ':pid' => $postId,
|
||||
// ':login_id' => $loginId,
|
||||
// ':content' => $comment,
|
||||
// ]);
|
||||
// }
|
||||
|
||||
// $cid = (int)$pdo->lastInsertId();
|
||||
|
||||
// // 다시 조회
|
||||
// if ($hasUserName){
|
||||
// $sel = $pdo->prepare("
|
||||
// SELECT comment_id, commenter AS login_id, user_name,
|
||||
// content AS comment_text, created_at
|
||||
// FROM qa_comments
|
||||
// WHERE comment_id = :cid
|
||||
// ");
|
||||
// } else {
|
||||
// $sel = $pdo->prepare("
|
||||
// SELECT comment_id, commenter AS login_id,
|
||||
// NULL AS user_name, content AS comment_text, created_at
|
||||
// FROM qa_comments
|
||||
// WHERE comment_id = :cid
|
||||
// ");
|
||||
// }
|
||||
// $sel->execute([':cid' => $cid]);
|
||||
// $row = $sel->fetch();
|
||||
// if (!$row) out(['status'=>'fail','message'=>'저장 후 조회 실패'],500);
|
||||
|
||||
// $displayName = $row['user_name'] ?: $row['login_id'];
|
||||
|
||||
// // out([
|
||||
// // 'status' => 'ok',
|
||||
// // 'comment_id' => (int)$row['comment_id'],
|
||||
// // 'login_id' => htmlspecialchars($row['login_id'], ENT_QUOTES,'UTF-8'),
|
||||
// // 'user_name' => $row['user_name'] ? htmlspecialchars($row['user_name'], ENT_QUOTES,'UTF-8') : null,
|
||||
// // 'display_name' => htmlspecialchars($displayName, ENT_QUOTES,'UTF-8'),
|
||||
// // 'comment_text' => nl2br(htmlspecialchars($row['comment_text'], ENT_QUOTES,'UTF-8')),
|
||||
// // 'created_at' => $row['created_at'],
|
||||
// // ]);
|
||||
// out([
|
||||
// 'status' => 'ok',
|
||||
// 'comment_id' => (int)$row['comment_id'],
|
||||
// 'login_id' => $row['login_id'],
|
||||
// 'user_name' => $row['user_name'],
|
||||
// 'display_name' => $displayName,
|
||||
// 'comment_text' => $row['comment_text'],
|
||||
// 'created_at' => $row['created_at'],
|
||||
// ]);
|
||||
|
||||
// }catch(Exception $e){
|
||||
// out(['status'=>'fail','message'=>'댓글 저장 실패: '.$e->getMessage()],500);
|
||||
// }
|
||||
?>
|
||||
<?php
|
||||
// /egbim/bbs/descope_qa_comment.php
|
||||
date_default_timezone_set('Asia/Seoul');
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
/* 공통 응답 헬퍼 */
|
||||
function out($arr, $code=200){
|
||||
http_response_code($code);
|
||||
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* 세션 복원 */
|
||||
require_once __DIR__ . '/../skin/member/basic/descope_session.php';
|
||||
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
|
||||
|
||||
/* 로그인 정보 */
|
||||
$loginId = $_SESSION['user']['loginIds'][0] ?? '';
|
||||
$userName = $_SESSION['user']['name'] ?? ($_SESSION['user']['userName'] ?? '');
|
||||
|
||||
/* 관리자 가드 */
|
||||
@include_once $_SERVER['DOCUMENT_ROOT'].'/egbim/bbs/admin_guard.php';
|
||||
$isAdmin = function_exists('is_qna_admin') ? is_qna_admin($loginId) : false;
|
||||
if ($isAdmin && function_exists('get_admin_display_name')) {
|
||||
// ✅ 관리자라면 관리자용 표시명으로 교체
|
||||
$userName = get_admin_display_name($loginId, $userName);
|
||||
}
|
||||
|
||||
/* 입력(JSON 또는 form) */
|
||||
$raw = file_get_contents('php://input');
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data) || !isset($data['postId'])) $data = $_POST;
|
||||
|
||||
$postId = isset($data['postId']) ? (int)$data['postId'] : 0;
|
||||
$comment = isset($data['comment']) ? trim((string)$data['comment']) : '';
|
||||
|
||||
if ($postId < 1 || $comment === '') {
|
||||
out(['status'=>'fail','message'=>'잘못된 요청입니다.'], 400);
|
||||
}
|
||||
if (mb_strlen($comment,'UTF-8') > 2000) {
|
||||
out(['status'=>'fail','message'=>'댓글은 2,000자 이내로 입력하세요.'], 400);
|
||||
}
|
||||
|
||||
/* DB 연결 */
|
||||
try{
|
||||
$pdo = new PDO(
|
||||
"mysql:host=db;dbname=egbim;charset=utf8mb4",
|
||||
'egbim','baron3840!!',
|
||||
[
|
||||
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
|
||||
]
|
||||
);
|
||||
$pdo->exec("SET time_zone = '+09:00'");
|
||||
}catch(Exception $e){
|
||||
out(['status'=>'fail','message'=>'DB 연결 실패: '.$e->getMessage()],500);
|
||||
}
|
||||
|
||||
/* 글 존재 및 권한 체크 */
|
||||
$st = $pdo->prepare("SELECT post_id, login_id AS author_id, is_secret FROM qa_posts WHERE post_id=?");
|
||||
$st->execute([$postId]);
|
||||
$post = $st->fetch();
|
||||
if (!$post) out(['status'=>'fail','message'=>'존재하지 않는 글입니다.'],404);
|
||||
|
||||
if (!$isAdmin && (int)$post['is_secret'] === 1 && $post['author_id'] !== $loginId) {
|
||||
out(['status'=>'fail','message'=>'비밀글에는 작성자 본인과 관리자만 댓글을 작성할 수 있습니다.'],403);
|
||||
}
|
||||
|
||||
/* 로그인 이름 폴백 */
|
||||
if ($loginId === '') $loginId = 'guest';
|
||||
if ($userName === '') $userName = $loginId;
|
||||
|
||||
/* qa_comments.user_name 컬럼 여부 */
|
||||
$hasUserName = false;
|
||||
$q = $pdo->prepare("
|
||||
SELECT COUNT(*)
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'qa_comments'
|
||||
AND COLUMN_NAME = 'user_name'
|
||||
");
|
||||
$q->execute();
|
||||
$hasUserName = ((int)$q->fetchColumn() > 0);
|
||||
|
||||
/* 저장 */
|
||||
try{
|
||||
if ($hasUserName){
|
||||
$ins = $pdo->prepare("
|
||||
INSERT INTO qa_comments (post_id, commenter, user_name, content, created_at)
|
||||
VALUES (:pid, :login_id, :user_name, :content, NOW())
|
||||
");
|
||||
$ins->execute([
|
||||
':pid' => $postId,
|
||||
':login_id' => $loginId,
|
||||
':user_name' => $userName, // ✅ 관리자면 관리자명으로 들어감
|
||||
':content' => $comment,
|
||||
]);
|
||||
} else {
|
||||
$ins = $pdo->prepare("
|
||||
INSERT INTO qa_comments (post_id, commenter, content, created_at)
|
||||
VALUES (:pid, :login_id, :content, NOW())
|
||||
");
|
||||
$ins->execute([
|
||||
':pid' => $postId,
|
||||
':login_id' => $loginId,
|
||||
':content' => $comment,
|
||||
]);
|
||||
}
|
||||
|
||||
$cid = (int)$pdo->lastInsertId();
|
||||
|
||||
// 다시 조회
|
||||
if ($hasUserName){
|
||||
$sel = $pdo->prepare("
|
||||
SELECT comment_id, commenter AS login_id, user_name,
|
||||
content AS comment_text, created_at
|
||||
FROM qa_comments
|
||||
WHERE comment_id = :cid
|
||||
");
|
||||
} else {
|
||||
$sel = $pdo->prepare("
|
||||
SELECT comment_id, commenter AS login_id,
|
||||
NULL AS user_name, content AS comment_text, created_at
|
||||
FROM qa_comments
|
||||
WHERE comment_id = :cid
|
||||
");
|
||||
}
|
||||
$sel->execute([':cid' => $cid]);
|
||||
$row = $sel->fetch();
|
||||
if (!$row) out(['status'=>'fail','message'=>'저장 후 조회 실패'],500);
|
||||
|
||||
$displayName = $row['user_name'] ?: $row['login_id'];
|
||||
|
||||
out([
|
||||
'status' => 'ok',
|
||||
'comment_id' => (int)$row['comment_id'],
|
||||
'login_id' => $row['login_id'],
|
||||
'user_name' => $row['user_name'],
|
||||
'display_name' => $displayName,
|
||||
'comment_text' => $row['comment_text'],
|
||||
'created_at' => $row['created_at'],
|
||||
'isAdmin' => $isAdmin,
|
||||
]);
|
||||
|
||||
}catch(Exception $e){
|
||||
out(['status'=>'fail','message'=>'댓글 저장 실패: '.$e->getMessage()],500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user