Files
confirms/worker/api_receiver.php
2026-03-11 17:25:45 +09:00

142 lines
4.5 KiB
PHP

<?php
/**
* [A 작업] 네이버 검증 요청 실시간 수신 리시버
* - 프레임워크를 로드하지 않아 매우 빠르고 안전함
* - 받은 데이터를 Redis 큐에 넣고 즉시 응답
*/
/**
* 날짜별 로그 기록 함수
* @param string $message 로그 내용
* @param string $level 로그 레벨 (INFO, ERROR, DEBUG 등)
*/
function writeLog($message, $level = 'ERROR') {
try {
// 1. 로그 저장 경로 설정 (프로젝트 루트의 logs 폴더)
$logBaseDir = __DIR__ . '/logs';
$Dir = $logBaseDir . '/';
// 2. 폴더가 없으면 생성
if (!is_dir($Dir)) {
if (!mkdir($Dir, 0777, true) && !is_dir($Dir)) {
error_log("Failed to create log directory: $Dir");
return;
}
chmod($Dir, 0777); // 권한 명시적 설정
}
// 3. 파일명 결정
$logFile = $Dir . date('Y-m-d') . '.log';
// 4. 로그 포맷팅 (시간 [레벨] 메시지)
$timestamp = date('Y-m-d H:i:s');
$singleLineMessage = str_replace(["\r", "\n", "\t"], " ", $message);
$formattedMessage = "[$timestamp] [$level] $singleLineMessage" . PHP_EOL;
// 5. 파일 기록
$result = file_put_contents($logFile, $formattedMessage, FILE_APPEND | LOCK_EX);
if ($result === false) {
error_log("Failed to write log file: $logFile");
} else {
chmod($logFile, 0666); // 로그 파일 권한 설정
}
} catch (Exception $e) {
error_log("writeLog Exception: " . $e->getMessage());
}
}
// 도우미 함수 정의
function apiResponse($error = null) {
$base = $error;
return json_encode($base);
}
// 1. 응답 헤더 설정 (JSON)
header('Content-Type: application/json; charset=utf-8');
// ===== 최우선: 모든 호출 정보를 로그에 저장 (보안 키 체크 전) =====
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$fullUrl = $protocol . "://" . ($_SERVER['HTTP_HOST'] ?? 'localhost') . ($_SERVER['REQUEST_URI'] ?? '');
$rawData = file_get_contents('php://input');
$requestInfo = [
'timestamp' => date('Y-m-d H:i:s'),
'method' => $_SERVER['REQUEST_METHOD'] ?? 'UNKNOWN',
'full_url' => $fullUrl,
'request_uri' => $_SERVER['REQUEST_URI'] ?? '',
'query_string' => $_SERVER['QUERY_STRING'] ?? '',
'get_params' => $_GET,
'post_data' => $rawData,
'client_ip' => $_SERVER['REMOTE_ADDR'] ?? '',
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'referer' => $_SERVER['HTTP_REFERER'] ?? '',
];
writeLog("REQUEST_INFO | " . json_encode($requestInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 'INFO');
// ================================================================
// 2. 보안 키 체크 (URL 파라미터 key=값)
$configKey = "7EE868F4B36D36B3D86736828F4729EAC4992083"; // 실제 사용할 키값으로 변경하세요
$receivedKey = $_GET['key'] ?? '';
$logDir = __DIR__ . '/logs/';
if ($receivedKey !== $configKey) {
writeLog("SECURITY_FAIL | Invalid key: $receivedKey", 'WARNING');
http_response_code(403);
echo apiResponse([
'code' => '-1',
'message' => 'Unregistered key'
]);
exit;
}
try {
// 3. 데이터 수신 (POST JSON 또는 GET 파라미터)
$data = json_decode($rawData, true);
if (empty($data)) {
throw new Exception("Empty data received");
}
// 4. Redis 연결
$redis = new Redis();
// Docker 서비스 이름인 'redis' 사용
$success = $redis->connect('redis', 6379);
if (!$success) {
throw new Exception("Could not connect to Redis");
}
$redis->select(9); // 10번 DB 사용
// 5. 큐에 넣을 데이터 포맷팅
$payload = [
'request_data' => $data,
'received_at' => date('Y-m-d H:i:s'),
'client_ip' => $_SERVER['REMOTE_ADDR']
];
// 'naver:raw_queue'라는 이름의 리스트에 저장
$redis->lPush('naver:raw_queue', json_encode($payload));
// 6. 네이버측에 성공 응답 (202 Accepted)
// 처리가 완료된 것은 아니지만, 접수는 완료되었음을 의미
http_response_code(200);
echo apiResponse([
'code' => 'success',
'message' => ''
]);
} catch (Exception $e) {
// 7. 장애 발생 시 로그 기록 (시스템 로그)
writeLog( 'Exception :' . apiResponse($data) , 'ERROR');
http_response_code(500);
echo apiResponse([
'code' => '-1',
'message' => $e->getMessage()
]);
}