diff --git a/driver_socket.php b/driver_socket.php index 9d9becc..4b5ebfc 100644 --- a/driver_socket.php +++ b/driver_socket.php @@ -143,7 +143,7 @@ function forwardLocationToPassengerSocket( $http = new AsyncHttp(); $http->request( - 'http://rides.intaleq.xyz:3031', + 'http://127.0.0.1:3031', [ 'method' => 'POST', 'data' => http_build_query([ @@ -456,8 +456,8 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) { forwardLocationToPassengerSocket( $driverId, $passengerId, [ - 'lat' => $lat, - 'lng' => $lng, + 'latitude' => $lat, + 'longitude' => $lng, 'heading' => $heading, 'speed' => $speed, 'ride_id' => $rideId, diff --git a/env_test.php b/env_test.php new file mode 100644 index 0000000..afe0cc0 --- /dev/null +++ b/env_test.php @@ -0,0 +1,57 @@ + $action, @@ -43,8 +43,8 @@ function sendToLocationServer($action, $data) { function findBestDrivers($con, $lat, $lng, $carType) { // 1. الاتصال بـ Redis لجلب الأقرب - $locationServerUrl = getenv('LOCATION_API_URL'); - $INTERNAL_KEY = trim(file_get_contents(getenv('INTERNAL_SOCKET_KEY_PATH'))); + $locationServerUrl = "https://location.intaleq.xyz/api_get_nearby.php"; + $INTERNAL_KEY = trim((string)@file_get_contents('/home/intaleq-api/.internal_socket_key')); $postData = ['lat' => $lat, 'lng' => $lng, 'radius' => 5, 'limit' => 100]; @@ -242,9 +242,12 @@ function notifyPassengerOnRideServer($passenger_id, $payload) { ]); $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_errno($ch)) { - error_log("Curl Error (Passenger Socket): " . curl_error($ch)); + error_log("[SOCKET_DEBUG] Curl Error (Passenger Socket) to $url: " . curl_error($ch)); + } else { + error_log("[SOCKET_DEBUG] Sent to Passenger Socket $url | HTTP: $httpCode | Response: $response | Passenger: $passenger_id"); } curl_close($ch); @@ -261,11 +264,9 @@ function dispatchRideToDrivers($driversData, $rideId, $payloadTemplate, $startNa $countDrivers = count($driversData); error_log("🚀 [DISPATCH_START] RideID: $rideId | Drivers Count: $countDrivers"); - $socketUrl = getenv('LOCATION_SOCKET_URL'); - if (!$socketUrl) throw new RuntimeException('LOCATION_SOCKET_URL not configured'); - - $internalKeyPath = getenv('INTERNAL_SOCKET_KEY_PATH'); - $internalKey = file_exists($internalKeyPath) ? trim(file_get_contents($internalKeyPath)) : ''; + $socketUrl = 'http://188.68.36.205:2021'; + $internalKeyPath = '/home/intaleq-api/.internal_socket_key'; + $internalKey = file_exists($internalKeyPath) ? trim((string)@file_get_contents($internalKeyPath)) : ''; foreach ($driversData as $driver) { $driverId = $driver['driver_id']; diff --git a/loginWallet.php b/loginWallet.php index c880946..978545f 100755 --- a/loginWallet.php +++ b/loginWallet.php @@ -76,8 +76,7 @@ try { 'jti' => bin2hex(random_bytes(16)), ]; - $keyPathPay = getenv('SECRET_KEY_PAY_PATH'); - $secretKey = trim(file_get_contents($keyPathPay)); + $secretKey = trim((string)@file_get_contents('/home/intaleq-api/.secret_key_pay')); $jwt = Firebase\JWT\JWT::encode($payload, $secretKey, 'HS256'); $hmac = hash_hmac('sha256', $id, getenv('SECRET_KEY_HMAC')); diff --git a/passenger_socket.php b/passenger_socket.php index 97663d8..fb80b2a 100644 --- a/passenger_socket.php +++ b/passenger_socket.php @@ -4,13 +4,6 @@ * ===================== * WebSocket Server للركاب — بورت 3030 * Internal HTTP Server — بورت 3031 - * - * الإصلاحات عن النسخة السابقة: - * 1. إصلاح بنية الـ closures: - * كان update_driver_location خارج الـ onMessage callback → لا يُنفَّذ أبداً - * 2. تحليل الـ payload بشكل صحيح (json_decode للـ nested objects) - * 3. Heartbeat يُعاد تشغيله تلقائياً عند إعادة الاتصال - * 4. تنظيف البنية العامة */ use Workerman\Worker; @@ -18,10 +11,32 @@ use PHPSocketIO\SocketIO; require_once __DIR__ . '/vendor/autoload.php'; +// --------------------------------------------------------- +// نظام تسجيل الأحداث (Logging System) +// --------------------------------------------------------- +$LOG_FILE = __DIR__ . '/socket_debug.log'; + +function socket_log($message, $data = null) { + global $LOG_FILE; + $date = date('Y-m-d H:i:s'); + $logMsg = "[$date] $message"; + if ($data !== null) { + $logMsg .= " | DATA: " . (is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE)); + } + $logMsg .= PHP_EOL; + + echo $logMsg; // للطباعة في الكونسول إذا كان يعمل في الـ Foreground + @file_put_contents($LOG_FILE, $logMsg, FILE_APPEND); // الكتابة في الملف +} + +// --------------------------------------------------------- + +socket_log("=== STARTING PASSENGER SOCKET SERVER ==="); + $INTERNAL_KEY = trim((string) @file_get_contents('/home/intaleq-rides/.internal_socket_key')); if (empty($INTERNAL_KEY)) { - echo '[CRITICAL] Internal key missing! Exiting.' . PHP_EOL; + socket_log("[CRITICAL_ERROR] Internal key missing! Exiting."); exit(1); } @@ -37,7 +52,10 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) { $innerHttp->onMessage = function ($connection, $request) use ($io, $INTERNAL_KEY) { $headers = $request->header(); + $clientIp = $connection->getRemoteIp(); + if (($headers['x-internal-key'] ?? '') !== $INTERNAL_KEY) { + socket_log("[HTTP_ERROR] Unauthorized internal request from IP: $clientIp"); $connection->send('Unauthorized'); return; } @@ -51,6 +69,7 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) { $rawPayload = $post['payload'] ?? null; if (!$passengerId || !$rawPayload) { + socket_log("[HTTP_ERROR] Missing passenger_id or payload for action: update_ride_status", $post); $connection->send('Error: Missing passenger_id or payload'); return; } @@ -59,10 +78,10 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) { ? (json_decode($rawPayload, true) ?? $rawPayload) : $rawPayload; + socket_log("[HTTP_SUCCESS] Emitting 'ride_status_change' to Passenger #$passengerId", $payload); $io->to('passenger_' . $passengerId)->emit('ride_status_change', $payload); $connection->send('OK'); - echo '[' . date('H:i:s') . '] Status update sent to Passenger #' . $passengerId . PHP_EOL; } elseif ($action === 'update_driver_location') { @@ -70,6 +89,7 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) { $rawPayload = $post['payload'] ?? null; if (!$passengerId || !$rawPayload) { + socket_log("[HTTP_ERROR] Missing passenger_id or payload for action: update_driver_location", $post); $connection->send('Error: Missing passenger_id or payload'); return; } @@ -78,38 +98,43 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) { ? (json_decode($rawPayload, true) ?? $rawPayload) : $rawPayload; + socket_log("[HTTP_SUCCESS] Emitting 'driver_location_update' to Passenger #$passengerId", $payload); $io->to('passenger_' . $passengerId)->emit('driver_location_update', $payload); $connection->send('OK'); } else { + socket_log("[HTTP_WARNING] Unknown action received: $action", $post); $connection->send('Unknown action: ' . $action); } }; $innerHttp->listen(); - echo '[' . date('H:i:s') . "] Internal HTTP started on port $INTERNAL_PORT" . PHP_EOL; + socket_log("[INFO] Internal HTTP started on port $INTERNAL_PORT"); }); $io->on('connection', function ($socket) { $query = $socket->handshake['query'] ?? []; $passengerId = $query['id'] ?? null; + $clientIp = $socket->conn->remoteAddress ?? 'Unknown'; if (!$passengerId) { + socket_log("[SOCKET_REJECTED] Connection rejected (No passenger ID) from IP: $clientIp"); $socket->disconnect(); return; } $socket->join('passenger_' . $passengerId); - echo '[' . date('H:i:s') . "] Passenger Connected: #$passengerId" . PHP_EOL; + socket_log("[SOCKET_CONNECTED] Passenger Connected: #$passengerId (IP: $clientIp)"); $socket->on('heartbeat', function ($data) { - // استقبال النبضة فقط — لا يحتاج رد + // يمكن تفعيل السطر التالي للتأكد من النبضات إذا أردت دقة شديدة، لكنه قد يملأ ملف الـ log + // socket_log("[SOCKET_HEARTBEAT] Received from Passenger #$passengerId"); }); - $socket->on('disconnect', function () use ($passengerId) { - echo '[' . date('H:i:s') . "] Passenger Disconnected: #$passengerId" . PHP_EOL; + $socket->on('disconnect', function () use ($passengerId, $clientIp) { + socket_log("[SOCKET_DISCONNECTED] Passenger Disconnected: #$passengerId (IP: $clientIp)"); }); }); diff --git a/ride/rides/acceptRide.php b/ride/rides/acceptRide.php index 4d51bcf..85c7832 100755 --- a/ride/rides/acceptRide.php +++ b/ride/rides/acceptRide.php @@ -160,13 +160,13 @@ try { ]); } - // FCM — push notification + // FCM — push notification صامت if (!empty($passengerToken)) { sendFCM_Internal( $passengerToken, - "تم قبول رحلتك", - "الكابتن " . ($driverInfo['driverName'] ?? '') . " في طريقه إليك", - ['ride_id' => (string) $rideId, 'driver_info' => $driverInfo], + "", // تفريغ العنوان للإرسال الصامت + "", // تفريغ المحتوى للإرسال الصامت + ['ride_id' => (string) $rideId, 'driver_info' => $driverInfo, 'status' => 'accepted'], "Accepted Ride", false ); diff --git a/ride/rides/arrive_ride.php b/ride/rides/arrive_ride.php index a8b0e5f..37cdf18 100755 --- a/ride/rides/arrive_ride.php +++ b/ride/rides/arrive_ride.php @@ -54,11 +54,11 @@ try { 'ride_id' => (string)$rideId ]; - // 🔥 استخدام sendFCM_Internal + // 🔥 استخدام sendFCM_Internal كرسالة صامتة sendFCM_Internal( $passengerToken, // الهدف - "السائق وصل 📍", // العنوان - "الكابتن ينتظرك في الموقع المحدد.", // النص + "", // تفريغ العنوان + "", // تفريغ النص $fcmData, // البيانات "Arrive Ride", // التصنيف false // ليس Topic diff --git a/ride/rides/start_ride.php b/ride/rides/start_ride.php index 74febe7..e987895 100755 --- a/ride/rides/start_ride.php +++ b/ride/rides/start_ride.php @@ -78,13 +78,13 @@ try { 'ride_id' => (string)$ride_id ]; - // 🔥 استخدام sendFCM_Internal + // 🔥 استخدام sendFCM_Internal كرسالة صامتة sendFCM_Internal( $passengerToken, // الهدف - "بدأت الرحلة 🏁", // العنوان - "نتمنى لك رحلة آمنة ومريحة.", // النص + "", // تفريغ العنوان + "", // تفريغ النص $fcmData, // البيانات - "Trip is Begin", // التصنيف (حافظنا عليه كما هو في التطبيق) + "Trip is Begin", // التصنيف false // ليس Topic ); }