Use shared auth and server-side HA calls
This commit is contained in:
@@ -1,88 +1,94 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
const HA_BASE = 'https://ha.chaegeon.com';
|
||||
const HA_LONG_LIVED_TOKEN = 'REDACTED_HA_TOKEN';
|
||||
const POKE_ENTITY_ID = 'counter.kog_jjireugi';
|
||||
|
||||
function ha_get_entity_state($entity_id) {
|
||||
$url = rtrim(HA_BASE, '/') . '/api/states/' . urlencode($entity_id);
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: Bearer ' . HA_LONG_LIVED_TOKEN,
|
||||
'Content-Type: application/json'
|
||||
],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 5,
|
||||
]);
|
||||
$res = curl_exec($ch);
|
||||
$errno = curl_errno($ch);
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($errno !== 0 || $status < 200 || $status >= 300) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$json = json_decode($res, true);
|
||||
// HA는 상태값을 문자열로 줍니다. 숫자면 (string)"12" 형태.
|
||||
if (!is_array($json) || !isset($json['state'])) return null;
|
||||
|
||||
// 숫자로 캐스팅 시도 (실패해도 문자열 반환 가능)
|
||||
$stateRaw = $json['state'];
|
||||
if (is_numeric($stateRaw)) {
|
||||
return (int)$stateRaw;
|
||||
}
|
||||
return $stateRaw; // 숫자가 아니면 원본 문자열
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if ($password === '0010') {
|
||||
echo json_encode(['result' => 'ok']);
|
||||
} else {
|
||||
echo json_encode(['result' => 'fail']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
if (isset($_GET['failMessages'])) {
|
||||
echo json_encode([
|
||||
'failMessages' => [
|
||||
'비밀번호가 틀립니다.',
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (isset($_GET['pokeCount'])) {
|
||||
// HA 엔티티 상태 읽기
|
||||
$count = ha_get_entity_state(POKE_ENTITY_ID);
|
||||
|
||||
if ($count === null) {
|
||||
http_response_code(502);
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'error' => 'failed_to_fetch_from_ha'
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'count' => $count
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'not_found']);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'method_not_allowed']);
|
||||
?>
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../common/auth.php';
|
||||
require_once __DIR__ . '/../common/ha.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
|
||||
$config = custom_config();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$data = custom_read_request_data();
|
||||
$action = $data['action'] ?? 'login';
|
||||
|
||||
if ($action === 'login') {
|
||||
$password = (string)($data['password'] ?? '');
|
||||
if (custom_verify_password($password)) {
|
||||
custom_issue_auth_cookie();
|
||||
custom_json(['result' => 'ok']);
|
||||
}
|
||||
|
||||
custom_json(['result' => 'fail'], 401);
|
||||
}
|
||||
|
||||
if ($action === 'logout') {
|
||||
custom_clear_auth_cookie();
|
||||
custom_json(['result' => 'ok']);
|
||||
}
|
||||
|
||||
if ($action === 'changePassword') {
|
||||
$current = (string)($data['currentPassword'] ?? '');
|
||||
$next = (string)($data['newPassword'] ?? '');
|
||||
|
||||
if (!custom_verify_password($current)) {
|
||||
custom_json(['result' => 'fail', 'message' => 'current_password_invalid'], 401);
|
||||
}
|
||||
|
||||
if (mb_strlen($next) < 4) {
|
||||
custom_json(['result' => 'fail', 'message' => 'password_too_short'], 422);
|
||||
}
|
||||
|
||||
if (!custom_save_password_hash(password_hash($next, PASSWORD_DEFAULT))) {
|
||||
custom_json(['result' => 'fail', 'message' => 'password_save_failed'], 500);
|
||||
}
|
||||
|
||||
custom_issue_auth_cookie();
|
||||
custom_json(['result' => 'ok']);
|
||||
}
|
||||
|
||||
if ($action === 'poke') {
|
||||
$sender = trim((string)($data['sender'] ?? ''));
|
||||
$message = trim((string)($data['message'] ?? ''));
|
||||
$ok = custom_ha_webhook('poke', [
|
||||
'sender' => $sender !== '' ? $sender : '익명',
|
||||
'message' => $message,
|
||||
]);
|
||||
|
||||
custom_json(['result' => $ok ? 'ok' : 'fail'], $ok ? 200 : 502);
|
||||
}
|
||||
|
||||
custom_json(['error' => 'not_found'], 404);
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
if (isset($_GET['session'])) {
|
||||
custom_json(['authenticated' => custom_has_auth_cookie()]);
|
||||
}
|
||||
|
||||
if (isset($_GET['failMessages'])) {
|
||||
custom_json([
|
||||
'failMessages' => [
|
||||
'비밀번호가 틀립니다.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if (isset($_GET['pokeCount'])) {
|
||||
$state = custom_ha_state('main', $config['entities']['poke_count']);
|
||||
$count = $state['state'] ?? null;
|
||||
|
||||
if ($count === null) {
|
||||
custom_json(['ok' => false, 'error' => 'failed_to_fetch_from_ha'], 502);
|
||||
}
|
||||
|
||||
custom_json([
|
||||
'ok' => true,
|
||||
'count' => is_numeric($count) ? (int)$count : $count,
|
||||
]);
|
||||
}
|
||||
|
||||
custom_json(['error' => 'not_found'], 404);
|
||||
}
|
||||
|
||||
custom_json(['error' => 'method_not_allowed'], 405);
|
||||
|
||||
Reference in New Issue
Block a user