first commit

This commit is contained in:
Hamza-Ayed
2026-06-09 08:40:31 +03:00
commit d8901e1a87
3161 changed files with 536187 additions and 0 deletions

89
backend/ride/invitor/add.php Executable file
View File

@@ -0,0 +1,89 @@
<?php
require_once __DIR__ . '/../../connect.php';
function generateUniqueCode($con) {
while (true) {
$letters = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVWXYZ"), 0, 2); // Excluded I, O for clarity
$numbers = substr(str_shuffle("23456789"), 0, 3); // Excluded 0, 1 for clarity
$code = $letters . $numbers;
$stmt = $con->prepare("SELECT COUNT(*) FROM invites WHERE inviteCode = ?");
$stmt->execute([$code]);
if ($stmt->fetchColumn() == 0) {
return $code;
}
}
}
$driverId = filterRequest("driverId");
$inviterDriverPhone = filterRequest("inviterDriverPhone");
// 🔐 تشفير رقم الهاتف
$inviterDriverPhoneEncrypted = $encryptionHelper->encryptData($inviterDriverPhone);
// تحقق من وجود رقم الهاتف مسبقًا
$checkSql = "SELECT `id`, `inviteCode`, `isInstall` FROM `invites` WHERE `inviterDriverPhone` = :inviterDriverPhone";
$checkStmt = $con->prepare($checkSql);
$checkStmt->bindParam(':inviterDriverPhone', $inviterDriverPhoneEncrypted, PDO::PARAM_STR);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
$existingInvite = $checkStmt->fetch(PDO::FETCH_ASSOC);
if ($existingInvite['isInstall'] == 1) {
jsonError($existingInvite['inviteCode']);
} else {
// تحديث الدعوة الحالية
$updateSql = "UPDATE `invites` SET `driverId` = :driverId, `expirationTime` = :expirationTime, `createdAt` = NOW() WHERE `id` = :id";
$updateStmt = $con->prepare($updateSql);
$expirationTime = date('Y-m-d H:i:s', strtotime('+24 hours'));
$updateStmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$updateStmt->bindParam(':expirationTime', $expirationTime);
$updateStmt->bindParam(':id', $existingInvite['id'], PDO::PARAM_INT);
try {
$updateStmt->execute();
printSuccess([
"message" => "Invite updated successfully",
"inviteId" => $existingInvite['id'],
"inviteCode" => $existingInvite['inviteCode'],
"expirationTime" => $expirationTime
]);
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
}
} else {
// إنشاء دعوة جديدة
$inviteCode = generateUniqueCode($con);
$expirationTime = date('Y-m-d H:i:s', strtotime('+24 hours'));
$sql = "INSERT INTO `invites` (`driverId`, `inviterDriverPhone`, `inviteCode`, `expirationTime`, `createdAt`, `isInstall`)
VALUES (:driverId, :inviterDriverPhone, :inviteCode, :expirationTime, NOW(), 0)";
$stmt = $con->prepare($sql);
$stmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$stmt->bindParam(':inviterDriverPhone', $inviterDriverPhoneEncrypted, PDO::PARAM_STR);
$stmt->bindParam(':inviteCode', $inviteCode);
$stmt->bindParam(':expirationTime', $expirationTime);
try {
$stmt->execute();
if ($stmt->rowCount() > 0) {
$insertedID = $con->lastInsertId();
printSuccess([
"message" => "Invite created successfully",
"inviteId" => $insertedID,
"inviteCode" => $inviteCode,
"expirationTime" => $expirationTime
]);
} else {
jsonError("Failed to save invite data");
}
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
}
?>

View File

@@ -0,0 +1,97 @@
<?php
require_once __DIR__ . '/../../connect.php';
function generateUniqueCode($con, $length = 7) {
while (true) {
$letters = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$numbers = substr(str_shuffle("0123456789"), 0, 3);
$code = $letters . $numbers;
$stmt = $con->prepare("SELECT COUNT(*) FROM invitesToPassengers WHERE inviteCode = ?");
$stmt->execute([$code]);
if ($stmt->fetchColumn() == 0) {
return $code;
}
}
}
$driverId = filterRequest("driverId");
$inviterPassengerPhone = filterRequest("inviterPassengerPhone");
if (!$driverId || !$inviterPassengerPhone) {
jsonError("Missing required parameters: driverId or inviterPassengerPhone");
}
// 🔐 تشفير رقم الهاتف
$inviterPassengerPhoneEncrypted = $encryptionHelper->encryptData($inviterPassengerPhone);
// التحقق من وجود الرقم مسبقًا
$checkSql = "SELECT `id`, `inviteCode`, `isInstall`, `isGiftToken` FROM `invitesToPassengers` WHERE `inviterPassengerPhone` = :inviterPassengerPhone";
$checkStmt = $con->prepare($checkSql);
$checkStmt->bindParam(':inviterPassengerPhone', $inviterPassengerPhoneEncrypted, PDO::PARAM_STR);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
$existingInvite = $checkStmt->fetch(PDO::FETCH_ASSOC);
if ($existingInvite['isInstall'] == 1 || $existingInvite['isGiftToken'] == 1) {
printFailure([
"message" => "Invite code already used or gift token already applied",
"inviteCode" => $existingInvite['inviteCode']
]);
} else {
// تحديث الدعوة
$updateSql = "UPDATE `invitesToPassengers` SET `driverId` = :driverId, `expirationTime` = :expirationTime, `createdAt` = NOW() WHERE `id` = :id";
$updateStmt = $con->prepare($updateSql);
$expirationTime = date('Y-m-d H:i:s', strtotime('+1 hour'));
$updateStmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$updateStmt->bindParam(':expirationTime', $expirationTime);
$updateStmt->bindParam(':id', $existingInvite['id'], PDO::PARAM_INT);
try {
$updateStmt->execute();
printSuccess([
"message" => "Invite updated successfully",
"inviteId" => $existingInvite['id'],
"inviteCode" => $existingInvite['inviteCode'],
"expirationTime" => $expirationTime
]);
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
}
} else {
// إنشاء دعوة جديدة
$inviteCode = generateUniqueCode($con);
$expirationTime = date('Y-m-d H:i:s', strtotime('+4 hour'));
$sql = "INSERT INTO `invitesToPassengers`
(`driverId`, `inviterPassengerPhone`, `inviteCode`, `expirationTime`, `createdAt`, `isInstall`, `isGiftToken`)
VALUES
(:driverId, :inviterPassengerPhone, :inviteCode, :expirationTime, NOW(), 0, 0)";
$stmt = $con->prepare($sql);
$stmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$stmt->bindParam(':inviterPassengerPhone', $inviterPassengerPhoneEncrypted, PDO::PARAM_STR);
$stmt->bindParam(':inviteCode', $inviteCode);
$stmt->bindParam(':expirationTime', $expirationTime);
try {
$stmt->execute();
if ($stmt->rowCount() > 0) {
$insertedID = $con->lastInsertId();
printSuccess([
"message" => "Invite created successfully",
"inviteId" => $insertedID,
"inviteCode" => $inviteCode,
"expirationTime" => $expirationTime
]);
} else {
jsonError("Failed to save invite data");
}
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
}
?>

View File

View File

@@ -0,0 +1,52 @@
<?php
require_once __DIR__ . '/../../connect.php';
$driverId = filterRequest("driverId");
$sql = "SELECT
i.`id`,
i.`driverId`,
i.`inviterDriverPhone`,
i.`createdAt`,
i.`isInstall`,
d.`id` AS driverInviterId,
d.`phone` AS invitorPhone,
d.`nameArabic` AS invitorName,
COALESCE(r.finishedTrips, 0) AS countOfInvitDriver
FROM
`invites` i
LEFT JOIN `driver` d ON d.phone = i.inviterDriverPhone
LEFT JOIN (
SELECT
driver_id,
COUNT(*) AS finishedTrips
FROM
ride
WHERE
status = 'Finished'
GROUP BY
driver_id
) r ON r.driver_id = d.id
WHERE
i.driverId = :driverId
AND i.isInstall = 1";
$stmt = $con->prepare($sql);
$stmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 🔓 فك التشفير للحقول المطلوبة
foreach ($rows as &$row) {
$row['inviterDriverPhone'] = $encryptionHelper->decryptData($row['inviterDriverPhone']);
$row['invitorPhone'] = $encryptionHelper->decryptData($row['invitorPhone']);
$row['invitorName'] = $encryptionHelper->decryptData($row['invitorName']);
}
jsonSuccess($rows);
} else {
jsonError("No records found.");
}
?>

View File

@@ -0,0 +1,48 @@
<?php
require_once __DIR__ . '/../../connect.php';
$driverId = filterRequest("driverId");
$sql = "SELECT
i.`id`,
i.`driverId`,
i.inviterPassengerPhone,
i.`createdAt`,
i.`isInstall`,
p.`id` AS passengerId,
p.first_name AS passengerName,
COALESCE(r.finishedTrips, 0) AS countOfInvitDriver
FROM
invitesToPassengers i
LEFT JOIN `driver` d ON
d.id = i.driverId
LEFT JOIN passengers p ON
p.phone = i.inviterPassengerPhone
LEFT JOIN (
SELECT passenger_id,
COUNT(*) AS finishedTrips
FROM ride
WHERE `status` = 'Finished'
GROUP BY passenger_id
) r ON r.passenger_id = i.passengerID
WHERE
i.driverId = :driverId AND i.isInstall = 1 AND p.id != ''";
$stmt = $con->prepare($sql);
$stmt->bindParam(':driverId', $driverId, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 🔓 فك التشفير للحقول المطلوبة
foreach ($rows as &$row) {
$row['inviterPassengerPhone'] = $encryptionHelper->decryptData($row['inviterPassengerPhone']);
$row['passengerName'] = $encryptionHelper->decryptData($row['passengerName']);
}
jsonSuccess($rows);
} else {
jsonError("No records found.");
}
?>

View File

@@ -0,0 +1,16 @@
<?php
require_once __DIR__ . '/../../connect.php';
$id = filterRequest("id");
$sql = "UPDATE `invites` SET `isGiftToken` = 1 WHERE `id` = :id";
$stmt = $con->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
jsonSuccess(null, "Record updated successfully.");
} else {
jsonError("No records were updated");
}
?>

View File

@@ -0,0 +1,59 @@
<?php
require_once __DIR__ . '/../../connect.php';
$inviterDriverPhone = filterRequest("inviterDriverPhone");
if (empty($inviterDriverPhone)) {
jsonError("Invalid or missing inviter phone.");
exit;
}
try {
// تشفير الرقم
$inviterDriverPhoneEncrypted = $encryptionHelper->encryptData($inviterDriverPhone);
// ✅ الآن الاستعلام نظيف وطبيعي جداً لأن قاعدة البيانات تم إصلاحها
$fetchSql = "SELECT
i.`id`,
i.`driverId`,
i.`inviterDriverPhone`,
i.`createdAt`,
i.`inviteCode`,
i.`isInstall`,
i.`isGiftToken`,
i.`expirationTime`,
dt.token
FROM `invites` i
LEFT JOIN `driverToken` dt ON dt.captain_id = i.driverId
WHERE i.`inviterDriverPhone` = :inviterDriverPhone
AND i.`expirationTime` > NOW()";
$fetchStmt = $con->prepare($fetchSql);
$fetchStmt->bindParam(':inviterDriverPhone', $inviterDriverPhoneEncrypted);
$fetchStmt->execute();
if ($fetchStmt->rowCount() > 0) {
$invite = $fetchStmt->fetch(PDO::FETCH_ASSOC);
// فك التشفير
$invite['inviterDriverPhone'] = $encryptionHelper->decryptData($invite['inviterDriverPhone']);
if (!empty($invite['token'])) {
$invite['token'] = $encryptionHelper->decryptData($invite['token']);
}
// التحديث
$updateSql = "UPDATE `invites` SET `isInstall` = 1 WHERE `id` = :id";
$updateStmt = $con->prepare($updateSql);
$updateStmt->bindParam(':id', $invite['id'], PDO::PARAM_INT);
$updateStmt->execute();
printSuccess("Record found and updated successfully.", $invite);
} else {
jsonError("No records found.");
}
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
jsonError("Database error: " . $e->getMessage());
}
?>

View File

@@ -0,0 +1,44 @@
<?php
require_once __DIR__ . '/../../connect.php';
$inviteCode = filterRequest("inviteCode");
if (empty($inviteCode)) {
jsonError("Invalid or missing invite code.");
exit;
}
try {
$checkSql = "SELECT `id`, `expirationTime`, `driverId` FROM `invites`
WHERE `inviteCode` = :inviteCode
AND `isInstall` = 0
AND `expirationTime` > NOW()";
$checkStmt = $con->prepare($checkSql);
$checkStmt->bindParam(':inviteCode', $inviteCode);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
$invite = $checkStmt->fetch(PDO::FETCH_ASSOC);
$updateSql = "UPDATE `invites` SET `isInstall` = 1 WHERE `id` = :id";
$updateStmt = $con->prepare($updateSql);
$updateStmt->bindParam(':id', $invite['id'], PDO::PARAM_INT);
$updateStmt->execute();
if ($updateStmt->rowCount() > 0) {
printSuccess([
"message" => "Invite code successfully used and marked as installed.",
"driverId" => $invite['driverId'],
"expirationTime" => $invite['expirationTime']
]);
} else {
jsonError("Failed to update the invite record.");
}
} else {
jsonError("Invalid invite code, already installed, or expired.");
}
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
?>

View File

@@ -0,0 +1,16 @@
<?php
require_once __DIR__ . '/../../connect.php';
$id = filterRequest("id");
$sql = "UPDATE `invitesToPassengers` SET `isGiftToken` = 1 WHERE `id` = :id";
$stmt = $con->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
jsonSuccess(null, "Record updated successfully.");
} else {
jsonError("No records were updated");
}
?>

View File

@@ -0,0 +1,48 @@
<?php
require_once __DIR__ . '/../../connect.php';
$inviteCode = filterRequest("inviteCode");
$passengerID = filterRequest("passengerID");
if (empty($inviteCode) || empty($passengerID)) {
jsonError("Invalid or missing invite code or passenger ID.");
exit;
}
// 🔐 تشفير كود الدعوة قبل البحث
$inviteCodeEncrypted = $encryptionHelper->encryptData($inviteCode);
try {
$checkSql = "SELECT `id`, `expirationTime` FROM `invitesToPassengers`
WHERE `inviteCode` = :inviteCode
AND `isInstall` = 0
AND `isGiftToken` = 0";
$checkStmt = $con->prepare($checkSql);
$checkStmt->bindParam(':inviteCode', $inviteCodeEncrypted);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
$invite = $checkStmt->fetch(PDO::FETCH_ASSOC);
$updateSql = "UPDATE `invitesToPassengers`
SET `isInstall` = 1, `passengerID` = :passengerID
WHERE `id` = :id";
$updateStmt = $con->prepare($updateSql);
$updateStmt->bindParam(':id', $invite['id'], PDO::PARAM_INT);
$updateStmt->bindParam(':passengerID', $passengerID);
$updateStmt->execute();
if ($updateStmt->rowCount() > 0) {
jsonSuccess(null, "Invite code successfully used and marked as installed.");
} else {
jsonError("Invite found but update failed.");
}
} else {
jsonError("Invalid invite code, already used, or marked as gift.");
}
} catch (PDOException $e) {
jsonError("Database error: " . $e->getMessage());
}
?>