64 lines
2.5 KiB
PHP
Executable File
64 lines
2.5 KiB
PHP
Executable File
<?php
|
|
// File: callback.php
|
|
// هذا الملف يستقبل الرد من جوجل بعد تسجيل المستخدم دخوله.
|
|
// يقوم بتحديث حالة الجلسة بالبيانات الصحيحة.
|
|
|
|
// 1. الإعدادات
|
|
$clientID = '1086900987150-j8brn0i5s97315kh1ej9jr72grkfqgh5.apps.googleusercontent.com';
|
|
$clientSecret = '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';
|
|
$redirectUri = 'https://api.tripz-egypt.com/tripz/auth/google_auth/callback.php';
|
|
|
|
// 2. التحقق من وجود 'code' و 'state' من جوجل
|
|
if (!isset($_GET['code']) || !isset($_GET['state'])) {
|
|
die('Invalid callback request.');
|
|
}
|
|
$authCode = $_GET['code'];
|
|
$loginToken = basename($_GET['state']); // الحماية من Path Traversal
|
|
|
|
$pollDir = __DIR__ . '/polls';
|
|
$sessionFile = $pollDir . '/' . $loginToken . '.json';
|
|
|
|
// التحقق من أن ملف الجلسة موجود
|
|
if (!file_exists($sessionFile)) {
|
|
die('Invalid or expired session.');
|
|
}
|
|
|
|
// 3. تبديل الـ code بـ access token
|
|
$tokenUrl = 'https://oauth2.googleapis.com/token';
|
|
$postData = [
|
|
'code' => $authCode,
|
|
'client_id' => $clientID,
|
|
'client_secret' => $clientSecret,
|
|
'redirect_uri' => $redirectUri,
|
|
'grant_type' => 'authorization_code'
|
|
];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
$tokenData = json_decode($response, true);
|
|
|
|
if (isset($tokenData['access_token'])) {
|
|
// 4. جلب بيانات المستخدم
|
|
$userInfoUrl = 'https://www.googleapis.com/oauth2/v2/userinfo?access_token=' . $tokenData['access_token'];
|
|
$userInfoResponse = file_get_contents($userInfoUrl);
|
|
$userData = json_decode($userInfoResponse, true);
|
|
|
|
if (isset($userData['id'])) {
|
|
// 5. تحديث ملف الجلسة بالبيانات الجديدة
|
|
$finalData = [
|
|
'status' => 'success',
|
|
'userData' => $userData
|
|
];
|
|
file_put_contents($sessionFile, json_encode($finalData));
|
|
}
|
|
}
|
|
|
|
// 6. عرض صفحة نجاح للمستخدم في المتصفح
|
|
echo '<!DOCTYPE html><html><head><title>Success</title><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body style="font-family: sans-serif; text-align: center; padding-top: 50px;"><h1>Authentication Successful</h1><p>You can now return to the Tripz app.</p></body></html>';
|
|
exit();
|
|
?>
|