160 lines
5.3 KiB
PHP
160 lines
5.3 KiB
PHP
<?php
|
|
// descope_qa_write.php
|
|
|
|
// 1) 에러 출력 (개발용)
|
|
// ini_set('display_errors', 1);
|
|
// error_reporting(E_ALL);
|
|
|
|
// 2) 세션/사용자 복원 (리스트와 동일하게)
|
|
require_once __DIR__ . '/../skin/member/basic/descope_session.php';
|
|
// descope_session.php 내부에서 session_start(), JWT → $_SESSION['user'] 세팅까지 처리된다는 가정
|
|
|
|
// (필요 시 로그인 강제)
|
|
// if (empty($_SESSION['user']['userId'])) {
|
|
// header('Location: /egbim/index.php?popup=login');
|
|
// exit;
|
|
// }
|
|
|
|
// $loginId = $_SESSION['user']['loginIds'][0] ?? null;
|
|
// $userId = $_SESSION['user']['userId'] ?? null;
|
|
|
|
// if (!$loginId || !$userId) {
|
|
// $errors[] = '로그인이 필요합니다.';
|
|
// }
|
|
|
|
// 3) DB 연결 (PDO)
|
|
$host = 'localhost';
|
|
$dbname = 'egbim';
|
|
$user = 'egbim';
|
|
$pass = 'baron3840!!';
|
|
$charset = 'utf8mb4';
|
|
$dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
];
|
|
try {
|
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
|
} catch (PDOException $e) {
|
|
exit('DB 연결 에러: ' . $e->getMessage());
|
|
}
|
|
|
|
// 4) 수정 모드 판단
|
|
$postId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|
$isEdit = $postId > 0;
|
|
|
|
// 5) 수정 모드이면 기존 글 불러오기
|
|
if ($isEdit) {
|
|
$stmt = $pdo->prepare("SELECT * FROM qa_posts WHERE post_id = ?");
|
|
$stmt->execute([$postId]);
|
|
$post = $stmt->fetch() ?: exit('존재하지 않는 글입니다.');
|
|
// 권한 체크 (원하시면 활성화)
|
|
// if ($post['login_id'] !== ($_SESSION['user']['loginIds'][0] ?? '')) {
|
|
// exit('수정 권한이 없습니다.');
|
|
// }
|
|
} else {
|
|
// 등록 모드 기본값
|
|
$post = [
|
|
'category' => '',
|
|
'title' => '',
|
|
'content' => '',
|
|
'is_secret' => 0,
|
|
'status' => 'await',
|
|
];
|
|
}
|
|
|
|
// 6) 폼 제출 처리
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// 6-1) 로그인 정보 (실제론 세션에서)
|
|
$loginId = $_SESSION['user']['loginIds'][0];
|
|
$userId = $_SESSION['user']['userId'];
|
|
// $loginId = 'sdi1108@naver.com';
|
|
// $userId = 'sdi1108@naver.com';
|
|
|
|
// 6-2) POST 데이터 수집
|
|
$postLabel = trim($_POST['category'] ?? '');
|
|
$title = trim($_POST['title'] ?? '');
|
|
$content = trim($_POST['content'] ?? '');
|
|
$secret = isset($_POST['secret']) ? 1 : 0;
|
|
$status = $_POST['status'] ?? 'await';
|
|
|
|
// 6-3) 한글 카테고리 → 코드 매핑
|
|
$categoryMap = [
|
|
'오류문의' => 'error',
|
|
'개선문의' => 'improvement',
|
|
'일반문의' => 'general',
|
|
'공지사항' => 'notice',
|
|
];
|
|
$category = $categoryMap[$postLabel] ?? '';
|
|
|
|
// 6-4) 유효성 검사
|
|
if (!in_array($category, $categoryMap, true)) {
|
|
$errors[] = '유효한 구분을 선택하세요.';
|
|
}
|
|
if ($title === '' || mb_strlen($title) > 100) {
|
|
$errors[] = '제목은 1~100자 사이여야 합니다.';
|
|
}
|
|
if ($content === '') {
|
|
$errors[] = '내용을 입력해주세요.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$pdo->beginTransaction();
|
|
try {
|
|
if ($isEdit) {
|
|
// 6-5a) UPDATE
|
|
$sql = "UPDATE qa_posts
|
|
SET category = :cat,
|
|
title = :tit,
|
|
content = :cont,
|
|
is_secret = :sec,
|
|
status = :st,
|
|
updated_at = NOW()
|
|
WHERE post_id = :pid";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':cat' => $category,
|
|
':tit' => $title,
|
|
':cont' => $content,
|
|
':sec' => $secret,
|
|
':st' => $status,
|
|
':pid' => $postId,
|
|
]);
|
|
} else {
|
|
// 6-5b) INSERT
|
|
$sql = "INSERT INTO qa_posts
|
|
(login_id,user_id,category,title,content,is_secret,status,created_at)
|
|
VALUES
|
|
(:lid,:uid,:cat,:tit,:cont,:sec,:st,NOW())";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':lid' => $loginId,
|
|
':uid' => $userId,
|
|
':cat' => $category,
|
|
':tit' => $title,
|
|
':cont'=> $content,
|
|
':sec' => $secret,
|
|
':st' => $status,
|
|
]);
|
|
$postId = $pdo->lastInsertId();
|
|
}
|
|
|
|
// 6-6) 첨부파일 처리 (생략… 동일 로직)
|
|
// foreach($_FILES['attach']['tmp_name'] …) { … }
|
|
|
|
$pdo->commit();
|
|
|
|
// 6-7) 상세보기로 리다이렉트
|
|
header("Location: /egbim/bbs/descope_qa_detail.php?id={$postId}");
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
$errors[] = 'DB 오류: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 7) 뷰 렌더링 (기존 스킨 재활용)
|
|
include __DIR__ . '/../skin/qa/basic/descope_qa_write.skin.php';
|