Files
tripz-llc/loction_server/api_get_nearby.php
T
Hamza-AyedandClaude Opus 5 4d8414c96b feat: استيراد كود سيرو إلى تريبز (سيرو @ecfe7568) — بلا تعديل
قرار المالك 2026-07-27: باك إند سيرو PHP هو المعتمد، وتطبيقاته المجرّبة
ميدانياً تحل محل إعادة البناء المؤرشفة. سيرو نفسه لم يُمسّ.

الخريطة:
  backend · payment_server · loction_server · ride_server ·
  passenger_server · docker · dashboard · stress_test  → الجذر
  siro_rider  → apps/rider          siro_driver  → apps/driver
  siro_admin  → dashboards/admin    siro_service → dashboards/service
  android_bot → apps/android_bot    socialBot    → apps/socialBot

نُسخ المتعقَّب في git سيرو فقط عبر `git archive` (3,198 ملفاً / ~169 م.ب)
لا `cp -r` — فاستُثنيت مخلفات البناء تلقائياً. بلا أي تعديل محتوى عمداً:
كل ما يلي يصير فرقاً مقروءاً مقابل المصدر.

لم يُستورد وسببه: siromove.com (الموقع التسويقي يبقى marketing/ في تريبز،
سيرو فيه 8 ملفات) · docs و planning (تريبز له docs/ الخاص) · deploy.sh
(ليس نشراً على سيرفر بل `git add . && git push origin --all` — فخّ في
مستودع آخر) · transit_dashboard (بانتظار قرار مصير backend-transit و
dashboards/transit-web).

⚠️ لا يبني بعد — ثلاثة نواقص متوقعة ومقصودة:
1. `.env` و `lib/env/env.g.dart` غير متعقَّبين في سيرو (أسرار لكل مستأجر):
   كل تطبيق فلاتر يحتاج .env خاصاً ثم توليد env.g.dart بـ build_runner.
2. إعدادات Firebase (9 ملفات google-services.json و GoogleService-Info.plist)
   يستبعدها .gitignore تريبز — ولكل مستأجر مشروع Firebase خاص أصلاً.
3. apps/driver في سيرو يشير إلى `../../Intaleq/packages/get` خارج المستودع →
   يجب ضمّ الحزم داخله أسوة بـ apps/rider.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 05:14:13 +03:00

120 lines
4.4 KiB
PHP
Executable File

<?php
// api_get_nearby.php (Updated)
require_once __DIR__ . '/vendor/autoload.php';
use Predis\Client;
header('Content-Type: application/json');
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(__DIR__ . '/../loction-keys/.internal_socket_key')) return trim((string)@file_get_contents(__DIR__ . '/../loction-keys/.internal_socket_key'));
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 (!empty($INTERNAL_KEY) && $receivedKey !== $INTERNAL_KEY) {
http_response_code(403);
echo json_encode(['status' => false, 'msg' => 'Unauthorized']);
exit;
}
$lat = $_REQUEST['lat'] ?? null;
$lng = $_REQUEST['lng'] ?? null;
$radius = $_REQUEST['radius'] ?? 5;
$limit = $_REQUEST['limit'] ?? 100;
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'));
$redisConfig = [
'scheme' => 'tcp',
'host' => $redisHost,
'port' => 6379,
'database' => 0
];
if (!empty($PASS_REDIS)) {
$redisConfig['password'] = $PASS_REDIS;
}
$redis = new Client($redisConfig);
$redis->connect();
// 🔥 التعديل هنا: إضافة WITHCOORD لجلب الإحداثيات من الريدز مباشرة
$geoResults = $redis->georadius(
'geo:drivers:available',
$lng, $lat, $radius, 'km',
['WITHDIST' => true, 'WITHCOORD' => true, 'COUNT' => $limit * 2, 'SORT' => 'ASC']
);
$validDrivers = [];
$currentTime = time();
$max_silence = 300;
foreach ($geoResults as $res) {
// هيكل النتيجة مع WITHCOORD يختلف قليلاً
$d_id = $res[0]; // ID
$d_dist = $res[1]; // Distance
$d_coord= $res[2]; // [0=>lng, 1=>lat] 🔥 الإحداثيات هنا
$profile = $redis->hgetall("driver:profile:$d_id");
$lastUpdate = isset($profile['updated_at']) ? (int)$profile['updated_at'] : 0;
if (empty($profile) || ($currentTime - $lastUpdate) > $max_silence) {
$redis->zrem('geo:drivers:available', $d_id);
$redis->zrem('geo:drivers:busy', $d_id);
continue;
}
// 🆕 فحص driver:public cache (بيانات السائق الثابتة)
$public = $redis->hgetall("driver:public:$d_id");
$hasCache = !empty($public) && !empty($public['first_name']);
$validDrivers[] = $hasCache ? array_merge($public, [
'id' => $d_id,
'driver_id' => $d_id,
'latitude' => $d_coord[1],
'longitude' => $d_coord[0],
'heading' => $profile['heading'] ?? $public['heading'] ?? 0,
'speed' => $profile['speed'] ?? $public['speed'] ?? 0,
'distance' => $d_dist,
'cached' => true
]) : [
'id' => $d_id,
'driver_id' => $d_id,
'distance' => $d_dist,
'heading' => $profile['heading'] ?? 0,
'speed' => $profile['speed'] ?? 0,
'latitude' => $d_coord[1],
'longitude' => $d_coord[0],
'cached' => false
];
if (count($validDrivers) >= $limit) break;
}
echo json_encode(['status' => true, 'data' => $validDrivers]);
} catch (Exception $e) {
echo json_encode(['status' => false, 'msg' => $e->getMessage()]);
}
?>