Files
Siro/backend/auth/syria/uploadImage.php

97 lines
3.3 KiB
PHP

<?php
// ============================================================
// auth/syria/uploadImage.php
// رفع صور وثائق السائق (هوية، رخصة، صورة شخصية، ...)
// يخزّنها في مجلدات حسب الدولة: auth/uploads/{country}/
// ============================================================
// ملاحظة: نستخدم bootstrap.php مباشرة (بدون connect.php) لأن JWT
// registration (token_type: registration) لا يملك صلاحية connect.php
require_once __DIR__ . '/../../core/bootstrap.php';
$con = Database::get('main');
uploadLog("🚀 [uploadImage.php] Document upload started.");
// --------- قراءة الحقول ---------
$driverID = filterRequest('driverID');
$imageType = filterRequest('imageType');
$country = filterRequest('country');
if (empty($imageType)) {
jsonError('imageType is required.');
}
// --------- تحديد الدولة ---------
$country = strtolower(trim($country ?: ''));
if (empty($country)) {
if (!empty($driverID)) {
try {
$stmt = $con->prepare("SELECT country FROM drivers WHERE id = ?");
$stmt->execute([$driverID]);
$country = strtolower(trim((string)$stmt->fetchColumn()));
} catch (Exception $e) {
$country = '';
}
}
if (empty($country)) {
$country = 'jordan';
}
}
if (!in_array($country, ['syria', 'jordan', 'egypt'])) {
$country = 'jordan';
}
// --------- بادئة اسم الملف ---------
$prefix = !empty($driverID) ? $driverID : 'unregistered';
uploadLog("📥 Params: driverID=" . ($driverID ?: 'null') . ", imageType=$imageType, country=$country");
// --------- رفع الملف ---------
$targetDir = __DIR__ . "/../../auth/uploads/{$country}/";
$result = uploadImageSecure('image', $targetDir, $prefix . '_' . $imageType);
if (!$result['success']) {
uploadLog("❌ Upload failed: {$result['error']}", 'ERROR', [
'driverID' => $driverID,
'imageType' => $imageType,
'country' => $country,
]);
jsonError($result['error']);
}
// --------- إزالة العشوائية من اسم الملف ---------
// الاسم يكون فقط: {driverID}_{imageType}.jpg (بدون random hex)
$ext = pathinfo($result['filename'], PATHINFO_EXTENSION);
$simpleName = $prefix . '_' . $imageType . '.' . $ext;
$simplePath = rtrim($targetDir, '/') . '/' . $simpleName;
if (file_exists($simplePath)) {
unlink($simplePath); // overwrite
}
rename($result['path'], $simplePath);
$result['filename'] = $simpleName;
$result['path'] = $simplePath;
// --------- بناء الرابط العام ---------
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = getenv('APP_DOMAIN') ?: ($_SERVER['HTTP_HOST'] ?? 'api.siromove.com');
$basePath = rtrim(dirname(dirname(dirname($_SERVER['SCRIPT_NAME']))), '/');
$url = "$protocol://$host{$basePath}/auth/uploads/{$country}/{$result['filename']}";
uploadLog("✅ Uploaded: {$result['path']} -> $url", 'INFO', [
'driverID' => ($driverID ?: 'null'),
'imageType' => $imageType,
'country' => $country,
]);
jsonSuccess([
'url' => $url,
'file_link' => $url,
'filename' => $result['filename'],
'driverID' => ($driverID ?: ''),
'imageType' => $imageType,
'country' => $country,
]);