Initial commit with updated Auth and media ignored
This commit is contained in:
129
auth/syria/uploadSyrianDocs.php
Executable file
129
auth/syria/uploadSyrianDocs.php
Executable file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
// File: upload_serial_document.php
|
||||
// يرفع صورة وثيقة إلى مسار خاص (خارج الويب العام) ويُرجع Signed URL مؤقّت.
|
||||
|
||||
// بيئتك
|
||||
require_once __DIR__ . '/../../connect.php'; // يجب أن يوفّر: $con (اختياري) + printSuccess/printFailure + filterRequest
|
||||
|
||||
// --------- إعدادات ---------
|
||||
const MAX_FILE_MB = 5;
|
||||
const ALLOWED_MIMES = ['image/jpeg','image/png','image/webp']; // فقط صور
|
||||
const UPLOAD_ROOT = __DIR__ . "/../../private_uploads"; // مجلد خاص (غير عام)
|
||||
const SIGN_SECRET = getenv('SECRET_KEY_HMAC'); // غيّرها واقرأها من .env
|
||||
const PUBLIC_BASE = 'https://syria.intaleq.xyz/intaleq'; // الدومين العلني
|
||||
const SIGNED_TTL_SEC = 172800; // 2 days = 60*60*24
|
||||
|
||||
// أنشئ مجلد الرفع إن لم يكن موجودًا
|
||||
if (!is_dir(UPLOAD_ROOT)) { @mkdir(UPLOAD_ROOT, 0700, true); }
|
||||
|
||||
// (اختياري) هيدرز أمان
|
||||
$authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
||||
$hmacHeader = $_SERVER['HTTP_X_HMAC_AUTH'] ?? '';
|
||||
// TODO: تحقّق حسب منطقك إن أردت فرض المصادقة هنا.
|
||||
|
||||
// --------- حقول مطلوبة من Flutter عبر filterRequest ---------
|
||||
$driverId = filterRequest('driver_id');
|
||||
$docType = filterRequest('doc_type');
|
||||
$purpose = filterRequest('purpose'); // اختياري
|
||||
|
||||
if (empty($driverId) || empty($docType)) {
|
||||
jsonError("driver_id and doc_type are required.");
|
||||
exit;
|
||||
}
|
||||
|
||||
// اسمح فقط بقيم محددة للوثائق
|
||||
$allowedDocTypes = [
|
||||
'driver_license_front',
|
||||
'driver_license_back',
|
||||
'car_license_front',
|
||||
'car_license_back',
|
||||
];
|
||||
if (!in_array($docType, $allowedDocTypes, true)) {
|
||||
jsonError("Invalid doc_type.");
|
||||
exit;
|
||||
}
|
||||
|
||||
// --------- التحقق من الملف ---------
|
||||
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
||||
jsonError("No file uploaded or upload error.");
|
||||
exit;
|
||||
}
|
||||
$tmpPath = $_FILES['file']['tmp_name'];
|
||||
$origName = $_FILES['file']['name'] ?? 'upload.bin';
|
||||
$size = filesize($tmpPath);
|
||||
if ($size === false || $size <= 0) {
|
||||
jsonError("Invalid file size."); exit;
|
||||
}
|
||||
if ($size > MAX_FILE_MB * 1024 * 1024) {
|
||||
jsonError("File too large. Max " . MAX_FILE_MB . " MB."); exit;
|
||||
}
|
||||
|
||||
// MIME دقيق
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$mime = $finfo->file($tmpPath) ?: 'application/octet-stream';
|
||||
if (!in_array($mime, ALLOWED_MIMES, true)) {
|
||||
jsonError("Unsupported file type: $mime"); exit;
|
||||
}
|
||||
|
||||
// لاحقة الامتداد
|
||||
$extMap = [
|
||||
'image/jpeg' => '.jpg',
|
||||
'image/png' => '.png',
|
||||
'image/webp' => '.webp',
|
||||
];
|
||||
$ext = $extMap[$mime];
|
||||
|
||||
// --------- توليد مسار حتمي بدون تاريخ ---------
|
||||
// تنظيف driver_id لاسم ملف آمن
|
||||
$driverIdSafe = preg_replace('/[^A-Za-z0-9_\-]/', '_', $driverId);
|
||||
// شجرة مجلدات ثابتة من hash(driver_id) لتوزيع الملفات
|
||||
$h = hash('sha1', $driverIdSafe);
|
||||
$subdir = substr($h, 0, 2) . '/' . substr($h, 2, 2);
|
||||
$destDir = UPLOAD_ROOT . '/' . $subdir;
|
||||
if (!is_dir($destDir)) { @mkdir($destDir, 0700, true); }
|
||||
|
||||
// الاسم النهائي بدون تاريخ
|
||||
$serverName = "{$driverIdSafe}__{$docType}{$ext}";
|
||||
$destPath = $destDir . '/' . $serverName;
|
||||
|
||||
// استبدال أي نسخة قديمة عن قصد (overwrite)
|
||||
if (is_file($destPath)) { @unlink($destPath); }
|
||||
|
||||
// نقل الملف
|
||||
if (!move_uploaded_file($tmpPath, $destPath)) {
|
||||
jsonError("Failed to save the uploaded file.");
|
||||
exit;
|
||||
}
|
||||
@chmod($destPath, 0600);
|
||||
|
||||
// --------- Signed URL ---------
|
||||
// سنضمّن driver_id و doc_type و ext في الرابط والتوقيع.
|
||||
// ext بدون النقطة
|
||||
$extShort = ltrim($ext, '.');
|
||||
$expires = time() + SIGNED_TTL_SEC;
|
||||
|
||||
// الرسالة الموقّعة: driver_id:doc_type:ext:expires
|
||||
$message = $driverIdSafe . ':' . $docType . ':' . $extShort . ':' . $expires;
|
||||
$signature = hash_hmac('sha256', $message, SIGN_SECRET);
|
||||
|
||||
// رابط القراءة عبر البوابة الآمنة فقط
|
||||
// ملاحظة: لا نُرجع المسار الحقيقي، فقط معطيات موقّعة
|
||||
$fileUrl = PUBLIC_BASE . "/secure_image.php"
|
||||
. "?driver_id={$driverIdSafe}"
|
||||
. "&doc_type={$docType}"
|
||||
. "&ext={$extShort}"
|
||||
. "&expires={$expires}"
|
||||
. "&signature={$signature}";
|
||||
|
||||
// --------- استجابة ---------
|
||||
printSuccess([
|
||||
"status" => "success",
|
||||
"success_file" => true,
|
||||
"file_url" => $fileUrl,
|
||||
"file_name" => $serverName, // الاسم الفعلي المحفوظ
|
||||
"driver_id" => $driverIdSafe,
|
||||
"doc_type" => $docType,
|
||||
"mime_type" => $mime,
|
||||
"size_bytes" => $size,
|
||||
"expires_at" => date('c', $expires)
|
||||
]);
|
||||
Reference in New Issue
Block a user