Files
intaleq/backend/ride/firebase/fcm_fun.php
T
Hamza-AyedandClaude Opus 5 92dc6b3641 chore: استيراد أولي من سيرو (ecfe7568) — بلا أي تعديل
نسخة كاملة من مستودع سيرو عند ecfe7568 لتكون أساس تطبيق «انطلق».
نُسخ المتعقَّب في git فقط (12,509 ملفاً / 302 م.ب) بـ git archive، لا
`cp -r` — فاستُثنيت تلقائياً مخلفات البناء (build · node_modules ·
.dart_tool · .gradle · Pods ≈ 10.7 غ.ب) وكل ما يستثنيه .gitignore.

هذا الكوميت **بلا أي تعديل عمداً** حتى يكون كل ما يليه فرقاً مقروءاً
مقابل سيرو الأصلي. سيرو نفسه لم يُمسّ.

⚠️ لا يبني بعد: `.env` و`lib/env/env.g.dart` غير متعقَّبين في سيرو (وهذا
صحيح — أسرار لكل مستأجر). كل تطبيق فلاتر هنا يحتاج .env خاصاً بانطلق ثم
توليد env.g.dart عبر build_runner. لا تُنسخ أسرار سيرو.

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

296 lines
10 KiB
PHP

<?php
// fcm_functions.php
// مكتبة مركزية لإرسال إشعارات FCM (استدعاء داخلي - بدون HTTP)
// ============================================================================
// دالة Base64 URL-Safe Encoding
// ============================================================================
function base64UrlEncode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
// ============================================================================
// دالة الحصول على Access Token من Google OAuth2
// ============================================================================
function resolveFCMServiceAccountPath($serviceAccountPath = null) {
if (!empty($serviceAccountPath) && file_exists($serviceAccountPath)) {
return $serviceAccountPath;
}
$envPath = getenv('FIREBASE_SERVICE_ACCOUNT_PATH') ?: '';
if (!empty($envPath) && file_exists($envPath)) {
return $envPath;
}
if (file_exists('/keys/firebase_service_account.json')) {
return '/keys/firebase_service_account.json';
}
if (file_exists('/keys/service-account.json')) {
return '/keys/service-account.json';
}
if (file_exists(__DIR__ . '/../../keys/firebase_service_account.json')) {
return __DIR__ . '/../../keys/firebase_service_account.json';
}
return __DIR__ . '/service-account.json';
}
function getFCMAccessToken($serviceAccountPath = null) {
$serviceAccountPath = resolveFCMServiceAccountPath($serviceAccountPath);
if (!file_exists($serviceAccountPath)) {
error_log("❌ FCM: service-account.json not found at: $serviceAccountPath");
return null;
}
$credentials = json_decode(file_get_contents($serviceAccountPath), true);
$clientEmail = $credentials['client_email'];
$privateKey = $credentials['private_key'];
$now = time();
$header = base64UrlEncode(json_encode(['alg' => 'RS256', 'typ' => 'JWT']));
$claim = base64UrlEncode(json_encode([
'iss' => $clientEmail,
'scope' => 'https://www.googleapis.com/auth/firebase.messaging',
'aud' => 'https://oauth2.googleapis.com/token',
'exp' => $now + 3600,
'iat' => $now
]));
$signature = '';
openssl_sign("$header.$claim", $signature, $privateKey, 'SHA256');
$jwt = "$header.$claim." . base64UrlEncode($signature);
$ch = curl_init("https://oauth2.googleapis.com/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode != 200) {
error_log("❌ FCM OAuth Error ($httpCode): $res");
return null;
}
return json_decode($res, true)['access_token'] ?? null;
}
// ============================================================================
// 🔥 الدالة الرئيسية: إرسال إشعار FCM (داخلي - بدون HTTP)
// ============================================================================
function sendFCMNotification($params) {
// استخراج البارامترات
$token = $params['token'] ?? null;
$title = $params['title'] ?? '';
$body = $params['body'] ?? '';
$category = $params['category'] ?? '';
$data = $params['data'] ?? [];
$tone = $params['tone'] ?? 'default';
$isTopic = $params['isTopic'] ?? false;
$serviceAccountPath = resolveFCMServiceAccountPath($params['service_account_path'] ?? null);
// التحقق من البيانات الأساسية
if (empty($token) || empty($title) || empty($body)) {
error_log("❌ FCM: Missing required fields (token, title, or body)");
return [
'success' => false,
'error' => 'Missing required parameters',
'http_code' => 400
];
}
// الحصول على Access Token
$accessToken = getFCMAccessToken($serviceAccountPath);
if (!$accessToken) {
return [
'success' => false,
'error' => 'Failed to get Access Token',
'http_code' => 500
];
}
// جلب Project ID
$creds = json_decode(file_get_contents($serviceAccountPath), true);
$projectId = $creds['project_id'];
$fcmUrl = "https://fcm.googleapis.com/v1/projects/$projectId/messages:send";
// بناء الـ Payload
$messagePayload = [
'message' => [
'notification' => [
'title' => $title,
'body' => $body
],
'android' => [
'priority' => 'HIGH',
'notification' => [
'sound' => $tone,
'channel_id' => 'high_importance_channel'
]
],
'apns' => [
'headers' => ['apns-priority' => '10'],
'payload' => [
'aps' => [
'sound' => ($tone === 'ding' || $tone === 'default') ? 'default' : (str_ends_with($tone, '.caf') ? $tone : $tone . '.caf'),
'content-available' => 1
]
]
]
]
];
// تحديد الهدف
if ($isTopic) {
$messagePayload['message']['topic'] = $token;
} else {
$messagePayload['message']['token'] = $token;
}
// إضافة الـ Data Payload
$customData = ['category' => (string)$category];
if (is_array($data) && !empty($data)) {
$customData = array_merge($customData, $data);
}
// تحويل كل القيم إلى String (FCM requirement)
$processedData = [];
foreach ($customData as $key => $val) {
if (is_array($val) || is_object($val)) {
$processedData[$key] = json_encode($val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} else {
$processedData[$key] = (string)$val;
}
}
$messagePayload['message']['data'] = $processedData;
// الإرسال إلى FCM
$ch = curl_init($fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json; charset=UTF-8'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messagePayload, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
// معالجة النتيجة
if ($httpCode == 200) {
error_log("✅ FCM Sent: Category=$category, Token=" . substr($token, 0, 15) . "...");
return [
'success' => true,
'http_code' => $httpCode,
'response' => json_decode($result, true)
];
} else {
error_log("❌ FCM Error ($httpCode): $result | CURL: $curlError");
return [
'success' => false,
'http_code' => $httpCode,
'error' => json_decode($result, true),
'curl_error' => $curlError
];
}
}
// ============================================================================
// 🎯 دوال مُساعدة جاهزة للاستخدام المباشر
// ============================================================================
/**
* إرسال إشعار "وصول السائق"
*/
function notifyDriverArrival($passengerToken, $driverName, $rideId) {
return sendFCMNotification([
'token' => $passengerToken,
'title' => "السائق وصل إليك 📍",
'body' => "$driverName في انتظارك الآن.",
'category' => 'Arrive Ride',
'tone' => 'tone1',
'data' => [
'ride_id' => (string)$rideId,
'timestamp' => date('Y-m-d H:i:s')
]
]);
}
/**
* إرسال إشعار "بدأت الرحلة"
*/
function notifyTripBegin($passengerToken, $driverName, $rideId) {
return sendFCMNotification([
'token' => $passengerToken,
'title' => "بدأت الرحلة 🚗",
'body' => "السائق $driverName بدأ رحلتك الآن.",
'category' => 'Trip is Begin',
'tone' => 'start',
'data' => [
'ride_id' => (string)$rideId,
'start_time' => date('Y-m-d H:i:s')
]
]);
}
/**
* إرسال إشعار "قبول الطلب"
*/
function notifyRideAccepted($passengerToken, $driverInfo, $rideId) {
return sendFCMNotification([
'token' => $passengerToken,
'title' => "تم قبول الطلب 🚖",
'body' => "الكابتن {$driverInfo['driverName']} قادم إليك.",
'category' => 'Accepted Ride',
'tone' => 'start',
'data' => [
'ride_id' => (string)$rideId,
'driver_id' => (string)$driverInfo['driverId'],
'driver_info' => $driverInfo // سيتم تحويلها لـ JSON تلقائياً
]
]);
}
/**
* إرسال إشعار "إلغاء الرحلة من السائق"
*/
function notifyRideCancelled($passengerToken, $rideId, $reason = '') {
return sendFCMNotification([
'token' => $passengerToken,
'title' => "تم إلغاء الرحلة ❌",
'body' => "السائق اعتذر عن إكمال الرحلة.",
'category' => 'Cancel Trip from driver',
'tone' => 'cancel',
'data' => [
'ride_id' => (string)$rideId,
'reason' => $reason,
'cancelled_at' => date('Y-m-d H:i:s')
]
]);
}
/**
* إرسال إشعار "انتهاء الرحلة"
*/
function notifyTripFinished($passengerToken, $tripData) {
return sendFCMNotification([
'token' => $passengerToken,
'title' => "انتهت الرحلة 🏁",
'body' => "شكرًا لاستخدامك تطبيق Tripz",
'category' => 'Driver Finish Trip',
'tone' => 'default',
'data' => [
'DriverList' => $tripData // Array سيتم تحويلها لـ JSON
]
]);
}
?>