Initial commit: 교육 프로젝트 배포
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
/**
|
||||
* bbs 디렉토리 API 공통 함수 모듈
|
||||
* 모든 API 파일에서 공통으로 사용하는 기능 제공
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* API 응답 헤더 설정
|
||||
*/
|
||||
function api_header_json() {
|
||||
if (!headers_sent()) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 세션 시작 및 로그인 확인
|
||||
*
|
||||
* @return array|null 로그인된 경우 ['member_id' => '...', 'sys_comp_code' => '...'] 반환, 아니면 null
|
||||
*/
|
||||
function api_get_session_user() {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
$memberId = (string)($_SESSION['member_id'] ?? '');
|
||||
if ($memberId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sysCompCode = (string)($_SESSION['sys_comp_code'] ?? '');
|
||||
if ($sysCompCode === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'member_id' => $memberId,
|
||||
'sys_comp_code' => $sysCompCode,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 로그인 체크 후 사용자 정보 반환
|
||||
* 미로그인시 401 에러 응답 후 종료
|
||||
*
|
||||
* @return array ['member_id' => '...', 'sys_comp_code' => '...']
|
||||
*/
|
||||
function api_require_login() {
|
||||
$user = api_get_session_user();
|
||||
if ($user === null) {
|
||||
http_response_code(401);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Unauthorized',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* sys_comp_code 조회 (없으면 DB에서 조회)
|
||||
*
|
||||
* @param PDO $pdo 데이터베이스 연결
|
||||
* @param string $memberId 회원 ID
|
||||
* @param string $preferredCode 우선할 코드 (있으면 사용)
|
||||
* @return string sys_comp_code 또는 빈 문자열
|
||||
*/
|
||||
function api_get_sys_comp_code(PDO $pdo, string $memberId, string $preferredCode = ''): string {
|
||||
if ($preferredCode !== '') {
|
||||
return $preferredCode;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare('SELECT sys_comp_code FROM edu_users WHERE member_id = ? ORDER BY sys_comp_code LIMIT 1');
|
||||
$stmt->execute([$memberId]);
|
||||
return (string)($stmt->fetchColumn() ?: '');
|
||||
}
|
||||
|
||||
/**
|
||||
* POST/JSON 입력 데이터 파싱
|
||||
*
|
||||
* @return array $_POST 또는 JSON decoded 배열
|
||||
*/
|
||||
function api_get_input(): array {
|
||||
$input = json_decode((string)file_get_contents('php://input'), true);
|
||||
if (is_array($input)) {
|
||||
return $input;
|
||||
}
|
||||
return $_GET + $_POST;
|
||||
}
|
||||
|
||||
/**
|
||||
* content_id 유효성 확인
|
||||
*
|
||||
* @param PDO $pdo 데이터베이스 연결
|
||||
* @param string $contentId 확인할 content_id
|
||||
* @return string|null 존재하면 content_id 반환, 없으면 null
|
||||
*/
|
||||
function api_verify_content_id(PDO $pdo, string $contentId): ?string {
|
||||
if ($contentId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare('SELECT content_id FROM edu_contents WHERE content_id = ? LIMIT 1');
|
||||
$stmt->execute([$contentId]);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
return $row ? (string)($row['content_id']) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* API 에러 응답
|
||||
*
|
||||
* @param int $httpCode HTTP 응답 코드
|
||||
* @param string $message 에러 메시지
|
||||
* @param array $extra 추가 정보
|
||||
*/
|
||||
function api_error(int $httpCode, string $message, array $extra = []) {
|
||||
http_response_code($httpCode);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $message,
|
||||
...$extra
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* API 성공 응답
|
||||
*
|
||||
* @param mixed $data 응답 데이터
|
||||
* @param string|null $message 성공 메시지
|
||||
*/
|
||||
function api_success($data = null, ?string $message = null) {
|
||||
$response = ['success' => true];
|
||||
if ($message !== null) {
|
||||
$response['message'] = $message;
|
||||
}
|
||||
if ($data !== null) {
|
||||
$response['data'] = $data;
|
||||
}
|
||||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
Reference in New Issue
Block a user