Files
2026-09-16 23:20:08 +07:00

119 lines
4.7 KiB
PHP

<?php
require_once 'config.php';
// =========================================
// 1. ฟังก์ชันบันทึก Logs (ประวัติการทำงาน)
// =========================================
function logAction($action_type, $module, $description)
{
global $pdo;
$user_id = $_SESSION['user_id'] ?? null;
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$stmt = $pdo->prepare("INSERT INTO system_logs (user_id, action_type, module, description, ip_address) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $action_type, $module, $description, $ip]);
}
// =========================================
// 2. ฟังก์ชันจัดการ Google Drive API
// =========================================
/*
ข้อกำหนด: ต้องใช้ Service Account (JSON key) ในการเชื่อมต่อ
และแชร์โฟลเดอร์เป้าหมายให้กับอีเมลของ Service Account ด้วยสิทธิ์ Editor
*/
function getGoogleClient()
{
// ต้องติดตั้ง: composer require google/apiclient
if (!class_exists('Google_Client')) {
return null; // จำลองการทำงานกรณีไม่ได้ติดตั้งไลบรารี
}
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/credentials/google-service-account.json');
$client->addScope(Google_Service_Drive::DRIVE_FILE);
return $client;
}
function uploadToGoogleDrive($fileTmpPath, $fileName, $mimeType)
{
$folderId = getSetting('drive_folder_id'); // ดึง ID จาก DB (ที่ตั้งค่าใน CMS)
$client = getGoogleClient();
if (!$client) {
// หากไม่มี Library จำลองการคืนค่า ID เพื่อใช้ทดสอบ
return ['id' => 'mock_drive_id_' . time(), 'url' => 'https://drive.google.com/file/d/mock/view'];
}
try {
$driveService = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile([
'name' => $fileName,
'parents' => [$folderId]
]);
$content = file_get_contents($fileTmpPath);
$file = $driveService->files->create($fileMetadata, [
'data' => $content,
'mimeType' => $mimeType,
'uploadType' => 'multipart',
'fields' => 'id, webViewLink'
]);
// แก้ไขสิทธิ์ให้ทุกคนที่มีลิงก์สามารถดูได้
$permission = new Google_Service_Drive_Permission(['type' => 'anyone', 'role' => 'reader']);
$driveService->permissions->create($file->id, $permission);
return [
'id' => $file->id,
'url' => 'https://drive.google.com/thumbnail?sz=s1024&id=' . $file->id
];
} catch (Exception $e) {
// หากอัปโหลดไม่สำเร็จ หรือ Folder ID ผิดพลาด ให้บันทึก error หรือคืนค่าเป็น null
error_log("Google Drive Upload Error: " . $e->getMessage());
return [
'id' => null,
'url' => null
];
}
}
function deleteFromGoogleDrive($fileId)
{
if (!$fileId || strpos($fileId, 'mock_') === 0) return true; // สำหรับการทดสอบ
$client = getGoogleClient();
if ($client) {
$driveService = new Google_Service_Drive($client);
try {
$driveService->files->delete($fileId);
return true;
} catch (Exception $e) {
return false;
}
}
return false;
}
// =========================================
// 3. ฟังก์ชันแจ้งเตือน (Flash Messages)
// =========================================
function setFlash($type, $message)
{
$_SESSION['flash'] = ['type' => $type, 'message' => $message];
}
function displayFlash()
{
if (isset($_SESSION['flash'])) {
$type = $_SESSION['flash']['type'];
$msg = $_SESSION['flash']['message'];
$color = $type === 'success' ? 'emerald' : ($type === 'error' ? 'red' : 'blue');
echo "<div id='flash-msg' class='fixed bottom-4 right-4 bg-{$color}-50 border-l-4 border-{$color}-500 p-4 rounded shadow-xl z-50 animate-bounce'>
<p class='text-gray-800 font-bold'>{$msg}</p>
</div>";
echo "<script>setTimeout(() => document.getElementById('flash-msg').style.display='none', 4000);</script>";
unset($_SESSION['flash']);
}
}