diff --git a/loction_server/api_get_nearby.php b/loction_server/api_get_nearby.php index df5b18f8..43a80b60 100755 --- a/loction_server/api_get_nearby.php +++ b/loction_server/api_get_nearby.php @@ -5,13 +5,31 @@ use Predis\Client; header('Content-Type: application/json'); -$INTERNAL_KEY = trim(file_get_contents('/home/location/.internal_socket_key')); -$PASS_REDIS = trim(file_get_contents('/home/location/.reds_pass_key')); +function getInternalKey(): string { + $envKey = getenv('INTERNAL_SOCKET_KEY'); + if ($envKey) return trim($envKey); + $path = getenv('INTERNAL_SOCKET_KEY_PATH'); + if ($path && file_exists($path)) return trim((string)@file_get_contents($path)); + if (file_exists('/keys/.internal_socket_key')) return trim((string)@file_get_contents('/keys/.internal_socket_key')); + if (file_exists('/home/location/.internal_socket_key')) return trim((string)@file_get_contents('/home/location/.internal_socket_key')); + return ''; +} + +function getRedisPass(): ?string { + $path = getenv('REDIS_PASS_KEY_PATH'); + if ($path && file_exists($path)) return trim((string)@file_get_contents($path)); + if (file_exists('/keys/.reds_pass_key')) return trim((string)@file_get_contents('/keys/.reds_pass_key')); + if (file_exists('/home/location/.reds_pass_key')) return trim((string)@file_get_contents('/home/location/.reds_pass_key')); + return null; +} + +$INTERNAL_KEY = getInternalKey(); +$PASS_REDIS = getRedisPass(); $headers = getallheaders(); $receivedKey = $headers['x-internal-key'] ?? $_SERVER['HTTP_X_INTERNAL_KEY'] ?? ''; -if ($receivedKey !== $INTERNAL_KEY) { +if (!empty($INTERNAL_KEY) && $receivedKey !== $INTERNAL_KEY) { http_response_code(403); echo json_encode(['status' => false, 'msg' => 'Unauthorized']); exit; @@ -26,14 +44,18 @@ if (!$lat || !$lng) { echo json_encode(['status' => false, 'msg' => 'Invalid Coordinates']); exit; } +try { $redisHost = getenv('REDIS_HOST') ?: ($_ENV['REDIS_HOST'] ?? (file_exists('/.dockerenv') ? 'redis' : '127.0.0.1')); - $redis = new Client([ + $redisConfig = [ 'scheme' => 'tcp', 'host' => $redisHost, - 'password' => $PASS_REDIS, 'port' => 6379, 'database' => 0 - ]); + ]; + if (!empty($PASS_REDIS)) { + $redisConfig['password'] = $PASS_REDIS; + } + $redis = new Client($redisConfig); $redis->connect(); // 🔥 التعديل هنا: إضافة WITHCOORD لجلب الإحداثيات من الريدز مباشرة