Initial commit with updated Auth and media ignored

This commit is contained in:
Hamza-Ayed
2026-04-28 13:04:27 +03:00
commit 67af97474c
477 changed files with 66444 additions and 0 deletions

64
auth/google_auth/callback.php Executable file
View File

@@ -0,0 +1,64 @@
<?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();
?>

View File

@@ -0,0 +1,38 @@
<?php
// File: check_status.php
// هذا الملف الذي سيقوم التطبيق بالاتصال به بشكل دوري.
// يتحقق من حالة الجلسة ويرجع البيانات عند نجاحها.
header('Content-Type: application/json');
// 1. استقبال الـ loginToken من التطبيق
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['loginToken'])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Login token is missing.']);
exit();
}
$loginToken = basename($input['loginToken']); // حماية
// 2. التحقق من ملف الجلسة
$pollDir = __DIR__ . '/polls';
$sessionFile = $pollDir . '/' . $loginToken . '.json';
if (file_exists($sessionFile)) {
$sessionData = json_decode(file_get_contents($sessionFile), true);
// إذا نجحت العملية، أرجع البيانات واحذف الملف
if ($sessionData['status'] === 'success') {
echo json_encode($sessionData);
unlink($sessionFile); // حذف الملف بعد النجاح
} else {
// إذا كانت لا تزال معلقة
echo json_encode(['status' => 'pending']);
}
} else {
// إذا انتهت المهلة أو حدث خطأ
http_response_code(404);
echo json_encode(['status' => 'expired', 'message' => 'Session not found or expired.']);
}
exit();
?>

View File

@@ -0,0 +1,55 @@
<?php
$redirectUri = 'https://api.tripz-egypt.com/tripz/auth/google_auth/callback.php';
// google_auth.php
require_once __DIR__ . '/../vendor/autoload.php';
use Google\Client;
header('Content-Type: application/json');
$response = [
'success' => false,
'error' => null,
'data' => null,
];
try {
if (!isset($_POST['code'])) {
throw new Exception("Missing authorization code.");
}
$code = $_POST['code'];
$client = new Client();
$client->setClientId('1086900987150-j8brn0i5s97315kh1ej9jr72grkfqgh5.apps.googleusercontent.com');
$client->setClientSecret('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');
$client->setRedirectUri('postmessage');
$client->addScope('email');
$client->addScope('profile');
$token = $client->fetchAccessTokenWithAuthCode($code);
if (isset($token['error'])) {
throw new Exception("Access token error: " . $token['error']);
}
$client->setAccessToken($token['access_token']);
$oauth2 = new Google_Service_Oauth2($client);
$userinfo = $oauth2->userinfo->get();
$response['success'] = true;
$response['data'] = [
'id' => $userinfo->id,
'email' => $userinfo->email,
'name' => $userinfo->name,
'picture' => $userinfo->picture,
];
} catch (Exception $e) {
$response['error'] = $e->getMessage();
}
echo json_encode($response);

41
auth/google_auth/login.php Executable file
View File

@@ -0,0 +1,41 @@
<?php
// File: start_login.php
// هذا الملف يبدأ عملية تسجيل الدخول.
// يقوم بإنشاء معرف فريد ورابط تسجيل الدخول وإرسالهم للتطبيق.
// 1. الإعدادات
$clientID = '1086900987150-j8brn0i5s97315kh1ej9jr72grkfqgh5.apps.googleusercontent.com';
$redirectUri = 'https://api.tripz-egypt.com/tripz/auth/google_auth/callback.php';
$scopes = 'email profile';
// 2. إنشاء معرف فريد للجلسة (login token)
$loginToken = bin2hex(random_bytes(24));
// 3. إنشاء مجلد لتخزين الجلسات المؤقتة إذا لم يكن موجوداً
$pollDir = __DIR__ . '/polls';
if (!is_dir($pollDir)) {
mkdir($pollDir, 0775, true);
}
// 4. إنشاء ملف مؤقت لهذه الجلسة
$sessionFile = $pollDir . '/' . $loginToken . '.json';
file_put_contents($sessionFile, json_encode(['status' => 'pending']));
// 5. بناء رابط جوجل مع تمرير المعرف الفريد في متغير 'state'
$authUrl = 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query([
'client_id' => $clientID,
'redirect_uri' => $redirectUri,
'response_type' => 'code',
'scope' => $scopes,
'access_type' => 'offline',
'state' => $loginToken // مهم جداً
]);
// 6. إرجاع الرابط والمعرف للتطبيق
header('Content-Type: application/json');
echo json_encode([
'authUrl' => $authUrl,
'loginToken' => $loginToken
]);
exit();
?>