Files
Siro/loction_server/siro/ride/location/add.php
2026-06-29 23:09:43 +03:00

80 lines
3.2 KiB
PHP
Executable File

<?php
include "../../connect.php";
header('Content-Type: application/json; charset=utf-8');
//https://location.intaleq.xyz/intaleq/ride/location/add.php
try {
$driver_id = filterRequest("driver_id");
$latitude = filterRequest("latitude");
$longitude = filterRequest("longitude");
$status = filterRequest("status");
$heading = filterRequest("heading");
$speed = filterRequest("speed");
// ملاحظة: قمنا بتجاهل device_timestamp هنا لضمان دقة الترتيب الزمني في السيرفر
// إذا كنت تحتاج وقت الهاتف لأغراض الترتيب في حالة انقطاع النت، يفضل إضافته في عمود منفصل مستقبلاً
// distance قد تأتي باسم totalDistance من التطبيق
$distanceRaw = filterRequest("distance");
if ($distanceRaw === null || $distanceRaw === '') {
$distanceRaw = filterRequest("totalDistance");
}
// تطبيع القيم الرقمية (كما هي في كودك الأصلي)
$norm = function($v, $default = 0.0) {
if ($v === null) return $default;
$v = str_replace(',', '.', trim((string)$v));
return is_numeric($v) ? (float)$v : $default;
};
$lat = $norm($latitude, 0.0);
$lng = $norm($longitude, 0.0);
$head = $norm($heading, 0.0);
$spd = $norm($speed, 0.0);
$dist = round($norm($distanceRaw, 0.0), 2);
if ($dist > 99999999.99) { $dist = 99999999.99; }
if ($dist < -99999999.99){ $dist = -99999999.99; }
if (empty($driver_id) || ($lat == 0.0 && $lng == 0.0)) {
printFailure("Invalid payload");
exit;
}
// ---------------------------------------------------------
// التعديل الجوهري هنا:
// ---------------------------------------------------------
// تم حذف منطق حساب الوقت بواسطة PHP أو الهاتف.
// سنستخدم NOW() داخل جملة SQL مباشرة لضمان توقيت UTC موحد.
$sql = "INSERT INTO `car_tracks`
(`driver_id`,`latitude`,`longitude`,`heading`,`speed`,`distance`,`status`,`created_at`)
VALUES
(:driver_id, :latitude, :longitude, :heading, :speed, :distance, :status, NOW())";
// 👆 استخدمنا NOW() بدلاً من المتغير
$stmt = $con->prepare($sql);
$ok = $stmt->execute([
':driver_id' => $driver_id,
':latitude' => $lat,
':longitude' => $lng,
':heading' => $head,
':speed' => $spd,
':distance' => $dist,
':status' => (string)($status ?? 'on'),
// ':created_at' => $created_at_to_use, // 👈 تم حذف هذا السطر لأنه لم يعد مطلوباً
]);
if ($ok) {
printSuccess("car_tracks saved successfully");
} else {
printFailure("Failed to save car track");
}
} catch (PDOException $e) {
// يفضل عدم طباعة تفاصيل الخطأ للمستخدم النهائي في الإنتاج، لكن لا بأس للـ Debug
error_log("car_tracks insert error: " . $e->getMessage());
printFailure("Database error");
} catch (Throwable $e) {
error_log("car_tracks insert fatal: " . $e->getMessage());
printFailure("Server error");
}
?>