Update: 2026-07-12 18:53:59

This commit is contained in:
Hamza-Ayed
2026-07-12 18:53:59 +03:00
parent 8cd4a4b57e
commit 653d73422f
54 changed files with 2078 additions and 436 deletions
+46
View File
@@ -58,6 +58,30 @@ function getInternalSocketKey(): string {
$INTERNAL_KEY = getInternalSocketKey();
// ── Redis location server (للتحقق من عضوية الراكب في المسار) ──
// نفس خادم Redis الذي يكتب عليه driver_socket وtransit/functions.php
$_locationRedis = null;
function getLocationRedis(): ?\Redis {
global $_locationRedis;
if ($_locationRedis !== null) {
try { $_locationRedis->ping(); return $_locationRedis; }
catch (\Exception $_) { $_locationRedis = null; }
}
try {
$passFile = '/home/location/.reds_pass_key';
$redisPass = file_exists($passFile) ? trim((string)@file_get_contents($passFile)) : '';
$r = new \Redis();
$r->connect('127.0.0.1', 6379, 1.5);
if ($redisPass) $r->auth($redisPass);
$_locationRedis = $r;
return $r;
} catch (\Exception $e) {
socket_log("[REDIS_ERROR] Location Redis unavailable: " . $e->getMessage());
return null;
}
}
function getJwtSecret(): string {
$keyPath = getenv('JWT_SECRET_KEY_PATH');
if ($keyPath && file_exists($keyPath)) {
@@ -207,11 +231,33 @@ $io->on('connection', function ($socket) {
// 🚌 اشتراك الراكب في بثّ موقع باص خط (مواصلاتي)
// يُستدعى عند فتح الراكب لخط في قائمة مواصلاتي
// يُرفض إن لم يكن الراكب عضواً نشطاً في المؤسسة المالكة للخط
$socket->on('subscribe_transit_route', function ($data) use ($socket, $passengerId) {
$data = (array) $data;
$routeId = (int)($data['route_id'] ?? 0);
if ($routeId <= 0) return;
// ── فحص العضوية عبر Redis ──────────────────────────────
// transit:route_org:{routeId} → org_id (كُتب في route/approve.php)
// transit:org_members:{orgId} → SET من passenger_ids (كُتب في transitCacheEnrollment)
$redis = getLocationRedis();
if ($redis) {
$orgId = $redis->get("transit:route_org:{$routeId}");
if ($orgId === false || $orgId === null) {
socket_log("[TRANSIT_BLOCKED] Route #{$routeId} not in Redis — passenger #{$passengerId}");
$socket->emit('transit_error', ['code' => 'ROUTE_NOT_FOUND', 'route_id' => $routeId]);
return;
}
$isMember = $redis->sIsMember("transit:org_members:{$orgId}", (string)$passengerId);
if (!$isMember) {
socket_log("[TRANSIT_BLOCKED] Passenger #{$passengerId} not enrolled in org #{$orgId} (route #{$routeId})");
$socket->emit('transit_error', ['code' => 'NOT_ENROLLED', 'route_id' => $routeId]);
return;
}
} else {
socket_log("[TRANSIT_WARN] Redis unavailable — enrollment check skipped (passenger #{$passengerId} route #{$routeId})");
}
$socket->join('transit_route_' . $routeId);
socket_log("[TRANSIT] Passenger #$passengerId subscribed to route #$routeId");
});