Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,42 @@
<?php
session_start();
require_once '../config/db.php';
require_once '../includes/functions.php';
// Include Composer autoloader
require_once '../vendor/autoload.php';
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("location: ../index.php");
exit;
}
$client_id = get_setting($pdo_sys, 'google_client_id');
$client_secret = get_setting($pdo_sys, 'google_client_secret');
if (empty($client_id) || empty($client_secret)) {
$_SESSION['settings_error'] = "กรุณาตั้งค่า Client ID และ Client Secret ก่อนทำการเชื่อมต่อ";
header("location: ../settings.php");
exit;
}
$client = new Google\Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
// Construct redirect URI dynamically
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$base_dir = dirname(dirname($_SERVER['PHP_SELF']));
$redirect_uri = $protocol . "://" . $host . $base_dir . "/auth/google_callback.php";
$redirect_uri = str_replace('//auth', '/auth', $redirect_uri);
$client->setRedirectUri($redirect_uri);
$client->addScope(Google\Service\Calendar::CALENDAR_EVENTS);
$client->setAccessType('offline');
$client->setPrompt('consent');
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
exit;
?>
@@ -0,0 +1,53 @@
<?php
session_start();
require_once '../config/db.php';
require_once '../includes/functions.php';
require_once '../vendor/autoload.php';
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("location: ../index.php");
exit;
}
if (isset($_GET['code'])) {
$client_id = get_setting($pdo_sys, 'google_client_id');
$client_secret = get_setting($pdo_sys, 'google_client_secret');
$client = new Google\Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$base_dir = dirname(dirname($_SERVER['PHP_SELF']));
$redirect_uri = $protocol . "://" . $host . $base_dir . "/auth/google_callback.php";
$redirect_uri = str_replace('//auth', '/auth', $redirect_uri);
$client->setRedirectUri($redirect_uri);
try {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
if (!isset($token['error'])) {
if (isset($token['refresh_token'])) {
save_setting($pdo_sys, 'google_refresh_token', $token['refresh_token'], $_SESSION['fullname']);
add_log($pdo_sys, $_SESSION['cid'], $_SESSION['fullname'], 'Google Auth', 'Successfully connected and received refresh token');
$_SESSION['settings_msg'] = "เชื่อมต่อบัญชี Google สำเร็จแล้ว!";
} else {
$_SESSION['settings_error'] = "ไม่ได้รับ Refresh Token กรณีเคยอนุญาตสิทธิ์ไปแล้ว กรุณาไปที่ Google Account > Security > Third-party apps เพื่อลบสิทธิ์ออกก่อน แล้วลองเชื่อมต่อใหม่อีกครั้ง";
}
} else {
$_SESSION['settings_error'] = "เกิดข้อผิดพลาดในการรับ Token: " . $token['error'];
}
} catch (Exception $e) {
$_SESSION['settings_error'] = "เกิดข้อผิดพลาด: " . $e->getMessage();
}
} else if (isset($_GET['error'])) {
$_SESSION['settings_error'] = "ถูกปฏิเสธการเข้าถึง: " . $_GET['error'];
}
header("location: ../settings.php");
exit;
?>
@@ -0,0 +1,93 @@
<?php
// auth/login.php
session_start();
require_once '../config/db.php';
require_once '../includes/functions.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = trim($_POST['username'] ?? '');
$pass = trim($_POST['password'] ?? '');
if (empty($user) || empty($pass)) {
$_SESSION['login_error'] = "กรุณากรอก Username และ Password";
header("location: ../index.php");
exit;
}
$pass_md5 = MD5($pass);
$sql = "SELECT HR_CID, HR_PASSWORD, HR_STATUS_ID, HR_PREFIX_NAME, HR_FNAME, HR_LNAME
FROM hr_person a
LEFT OUTER JOIN hr_prefix e ON a.HR_PREFIX_ID=e.HR_PREFIX_ID
WHERE a.HR_USERNAME = :user LIMIT 1";
try {
$stmt = $pdo_hos->prepare($sql);
$stmt->execute(['user' => $user]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
$_SESSION['login_error'] = "ไม่พบบัญชีผู้ใช้นี้";
add_log($pdo_sys, $user, 'Unknown', 'Login Failed', 'User not found');
header("location: ../index.php");
exit;
}
if (strtolower($row['HR_PASSWORD']) !== strtolower($pass_md5)) {
$_SESSION['login_error'] = "รหัสผ่านไม่ถูกต้อง";
add_log($pdo_sys, $row['HR_CID'], $row['HR_FNAME'], 'Login Failed', 'Incorrect password');
header("location: ../index.php");
exit;
}
if ($row['HR_STATUS_ID'] !== '01') {
$_SESSION['login_error'] = "บัญชีผู้ใช้นี้ถูกปิดการใช้งานไปแล้ว";
add_log($pdo_sys, $row['HR_CID'], $row['HR_FNAME'], 'Login Failed', 'Account disabled');
header("location: ../index.php");
exit;
}
unset($_SESSION['login_error']);
// Check Role (RBAC)
$role = null;
global $master_super_admin_cid;
if ($row['HR_CID'] === $master_super_admin_cid) {
$role = 'superadmin';
} else {
// Check in system_users table
$stmt_sys = $pdo_sys->prepare("SELECT role FROM system_users WHERE cid = :cid");
$stmt_sys->execute(['cid' => $row['HR_CID']]);
$sys_user = $stmt_sys->fetch();
if ($sys_user) {
$role = $sys_user['role'];
} else {
$role = 'user'; // Default role for authenticated hospital staff
}
}
$_SESSION['loggedin'] = true;
$_SESSION['role'] = $role;
$_SESSION['fullname'] = $row['HR_PREFIX_NAME'] . $row['HR_FNAME'] . " " . $row['HR_LNAME'];
$_SESSION['cid'] = $row['HR_CID'];
//$_SESSION['dep_id'] = $row['HR_DEPARTMENT_SUB_SUB_ID'];
//$_SESSION['department'] = $row['HR_DEPARTMENT_SUB_SUB_NAME'];
// Add Log
add_log($pdo_sys, $_SESSION['cid'], $_SESSION['fullname'], 'Login Success', "User logged in successfully (Role: $role)");
header("location: ../dashboard.php");
exit;
} catch (PDOException $e) {
$_SESSION['login_error'] = "เกิดข้อผิดพลาดของระบบ: " . $e->getMessage();
header("location: ../index.php");
exit;
}
} else {
header("location: ../index.php");
exit;
}
?>
@@ -0,0 +1,20 @@
<?php
// auth/logout.php
session_start();
require_once '../config/db.php';
require_once '../includes/functions.php';
if (isset($_SESSION['cid'])) {
add_log($pdo_sys, $_SESSION['cid'], $_SESSION['fullname'], 'Logout', 'User logged out');
}
// Unset all of the session variables
$_SESSION = array();
// Destroy the session.
session_destroy();
// Redirect to login page
header("location: ../index.php");
exit;
?>