779 lines
25 KiB
PHP
779 lines
25 KiB
PHP
<?php
|
|
|
|
$user_agent = $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
function getOS()
|
|
{
|
|
|
|
global $user_agent;
|
|
|
|
$os_platform = "Unknown OS Platform";
|
|
|
|
$os_array = array(
|
|
'/windows nt 10.0/i' => 'Windows 10/11',
|
|
'/windows nt 6.3/i' => 'Windows 8.1',
|
|
'/windows nt 6.2/i' => 'Windows 8',
|
|
'/windows nt 6.1/i' => 'Windows 7',
|
|
'/windows nt 6.0/i' => 'Windows Vista',
|
|
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
|
|
'/windows nt 5.1/i' => 'Windows XP',
|
|
'/windows xp/i' => 'Windows XP',
|
|
'/windows nt 5.0/i' => 'Windows 2000',
|
|
'/windows me/i' => 'Windows ME',
|
|
'/win98/i' => 'Windows 98',
|
|
'/win95/i' => 'Windows 95',
|
|
'/win16/i' => 'Windows 3.11',
|
|
'/macintosh|mac os x/i' => 'Mac OS X',
|
|
'/mac_powerpc/i' => 'Mac OS 9',
|
|
'/linux/i' => 'Linux',
|
|
'/ubuntu/i' => 'Ubuntu',
|
|
'/iphone/i' => 'iPhone',
|
|
'/ipod/i' => 'iPod',
|
|
'/ipad/i' => 'iPad',
|
|
'/android/i' => 'Android',
|
|
'/blackberry/i' => 'BlackBerry',
|
|
'/webos/i' => 'Mobile'
|
|
);
|
|
|
|
foreach ($os_array as $regex => $value) {
|
|
|
|
if (preg_match($regex, $user_agent)) {
|
|
$os_platform = $value;
|
|
}
|
|
}
|
|
|
|
return $os_platform;
|
|
}
|
|
|
|
function getBrowser()
|
|
{
|
|
|
|
global $user_agent;
|
|
|
|
$browser = "Unknown Browser";
|
|
|
|
$browser_array = array(
|
|
'/edg/i' => 'Microsoft Edge',
|
|
'/msie/i' => 'Internet Explorer',
|
|
'/firefox/i' => 'Firefox',
|
|
'/safari/i' => 'Safari',
|
|
'/chrome/i' => 'Chrome',
|
|
'/opera/i' => 'Opera',
|
|
'/netscape/i' => 'Netscape',
|
|
'/maxthon/i' => 'Maxthon',
|
|
'/konqueror/i' => 'Konqueror',
|
|
'/mobile/i' => 'Handheld Browser'
|
|
);
|
|
|
|
foreach ($browser_array as $regex => $value) {
|
|
|
|
if (preg_match($regex, $user_agent)) {
|
|
$browser = $value;
|
|
}
|
|
}
|
|
|
|
return $browser;
|
|
}
|
|
|
|
$user_os = getOS();
|
|
$user_browser = getBrowser();
|
|
$show_ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
//$device_details = "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";
|
|
|
|
//print_r($device_details);
|
|
|
|
//echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเป็นภาษาไทยแบบเต็ม------------------------------------------------//
|
|
function thai_date($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year += 543;
|
|
|
|
return $day . " " . $thMonth[$month - 1] . " " . $year;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเป็นภาษาไทยแบบเต็ม-----------------------------------------------------//
|
|
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงเดือนเป็นภาษาไทยแบบเต็ม------------------------------------------------//
|
|
function thai_month($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year += 543;
|
|
|
|
return $thMonth[$month - 1] . " " . $year;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงเดือนเป็นภาษาไทยแบบเต็ม-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเป็นภาษาไทย2------------------------------------------------//
|
|
function thai_date2($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
$year += 543;
|
|
|
|
return $day . "-" . $month . "-" . $year;
|
|
}
|
|
|
|
function thai_date3($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
return $day . "-" . $month . "-" . $year;
|
|
}
|
|
|
|
function thai_date_full($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year = $year + 543;
|
|
|
|
return $day . " " . $thMonth[$month - 1] . " พ.ศ. " . $year;
|
|
}
|
|
|
|
function thai_date5($date)
|
|
{
|
|
list($day, $month, $year) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
return $day . " " . $thMonth[$month - 1] . " พ.ศ. " . $year;
|
|
}
|
|
|
|
function thai_date6($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year = $year + 543;
|
|
|
|
return $day . " " . $thMonth[$month - 1] . " " . $year;
|
|
}
|
|
|
|
function thai_date7($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
$year = $year + 543;
|
|
|
|
return "วันที่ " . $day . " " . $thMonth[$month - 1] . " พ.ศ. " . $year;
|
|
}
|
|
|
|
function thai_date8($date)
|
|
{
|
|
list($day, $month, $year) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
$thMonth = array("ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
return $day . " " . $thMonth[$month - 1] . " " . $year;
|
|
}
|
|
|
|
function novel_date1($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
return $day . "/" . $month . "/" . $year;
|
|
}
|
|
|
|
function thai_date9($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
$year += 543;
|
|
|
|
return $day . "/" . $month . "/" . $year;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเป็นภาษาไทย2-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเป็นรูปแบบของ mysql------------------------------------------------//
|
|
function mysql_date1($date)
|
|
{
|
|
list($day, $month, $year) = explode('-', $date);
|
|
|
|
switch ($month) {
|
|
case "ม.ค.":
|
|
$thMonth = "01";
|
|
break;
|
|
case "ก.พ.":
|
|
$thMonth = "02";
|
|
break;
|
|
case "มี.ค.":
|
|
$thMonth = "03";
|
|
break;
|
|
case "เม.ย.":
|
|
$thMonth = "04";
|
|
break;
|
|
case "พ.ค.":
|
|
$thMonth = "05";
|
|
break;
|
|
case "มิ.ย.":
|
|
$thMonth = "06";
|
|
break;
|
|
case "ก.ค.":
|
|
$thMonth = "07";
|
|
break;
|
|
case "ส.ค.":
|
|
$thMonth = "08";
|
|
break;
|
|
case "ก.ย.":
|
|
$thMonth = "09";
|
|
break;
|
|
case "ต.ค.":
|
|
$thMonth = "10";
|
|
break;
|
|
case "พ.ย.":
|
|
$thMonth = "11";
|
|
break;
|
|
case "ธ.ค.":
|
|
$thMonth = "12";
|
|
break;
|
|
}
|
|
|
|
$year = $year - 543;
|
|
|
|
return $year . "-" . $thMonth . "-" . $day;
|
|
}
|
|
|
|
function mysql_date2($date)
|
|
{
|
|
list($null, $day, $month, $year) = explode('-', $date);
|
|
|
|
switch ($month) {
|
|
case "ม.ค.":
|
|
$thMonth = "01";
|
|
break;
|
|
case "ก.พ.":
|
|
$thMonth = "02";
|
|
break;
|
|
case "มี.ค.":
|
|
$thMonth = "03";
|
|
break;
|
|
case "เม.ย.":
|
|
$thMonth = "04";
|
|
break;
|
|
case "พ.ค.":
|
|
$thMonth = "05";
|
|
break;
|
|
case "มิ.ย.":
|
|
$thMonth = "06";
|
|
break;
|
|
case "ก.ค.":
|
|
$thMonth = "07";
|
|
break;
|
|
case "ส.ค.":
|
|
$thMonth = "08";
|
|
break;
|
|
case "ก.ย.":
|
|
$thMonth = "09";
|
|
break;
|
|
case "ต.ค.":
|
|
$thMonth = "10";
|
|
break;
|
|
case "พ.ย.":
|
|
$thMonth = "11";
|
|
break;
|
|
case "ธ.ค.":
|
|
$thMonth = "12";
|
|
break;
|
|
}
|
|
|
|
$year = $year - 543;
|
|
|
|
return $year . "-" . $thMonth . "-" . $day;
|
|
}
|
|
|
|
function mysql_date3($date)
|
|
{
|
|
@list($day, $month, $year) = @explode('-', $date);
|
|
|
|
return $year . "-" . $month . "-" . $day;
|
|
}
|
|
//--------------------------------ฟังก์ชันแปลงวันเป็นรูปแบบของ mysql------------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแบ่งช่วงของวัน------------------------------------------------//
|
|
function explode_date1($date)
|
|
{
|
|
list($date1, $date2) = explode('-', $date);
|
|
return mysql_date1($date1);
|
|
}
|
|
function explode_date2($date)
|
|
{
|
|
list($date1, $date2) = explode('-', $date);
|
|
$new_date2 = mysql_date2($date2);
|
|
if ($new_date2 == "-543--") {
|
|
echo "";
|
|
} else {
|
|
return $new_date2;
|
|
}
|
|
}
|
|
//---------------------------------ฟังก์ชันแบ่งช่วงของวัน-----------------------------------------------------//
|
|
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเป็นรูปแบบเลขที่ใบสมัคร------------------------------------------------//
|
|
function code_date($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
$Month = $month;
|
|
|
|
$year += 543;
|
|
|
|
$sYear = substr($year, 2, 2);
|
|
|
|
return $sYear . $day . $Month;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเป็นรูปแบบเลขที่ใบสมัคร-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแยกชื่อ-นามสกุลไฟล์------------------------------------------------//
|
|
function explode_filename($filename)
|
|
{
|
|
list($name, $ext) = explode('.', $filename);
|
|
|
|
return $name;
|
|
}
|
|
|
|
function explode_extname($filename)
|
|
{
|
|
list($name, $ext) = explode('.', $filename);
|
|
|
|
return $ext;
|
|
}
|
|
//---------------------------------ฟังก์ชันแยกชื่อ-นามสกุลไฟล์-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแบ่งชื่อ------------------------------------------------//
|
|
function split_fname($name)
|
|
{
|
|
list($fname, $lname) = explode(' ', $name);
|
|
|
|
return $fname;
|
|
}
|
|
function split_lname($name)
|
|
{
|
|
list($fname, $lname) = explode(' ', $name);
|
|
|
|
return $lname;
|
|
}
|
|
//---------------------------------ฟังก์ชันแบ่งชื่อ-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเวลาเป็นวัน------------------------------------------------//
|
|
function datetime_to_date($date)
|
|
{
|
|
list($date1, $time) = explode(' ', $date);
|
|
list($year, $month, $day) = explode('-', $date1);
|
|
|
|
$year += 543;
|
|
|
|
return $day . "-" . $month . "-" . $year;
|
|
}
|
|
function datetime_to_date2($date)
|
|
{
|
|
list($date1, $time) = explode('-', $date);
|
|
|
|
return $date1;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเวลาเป็นวัน-----------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแปลงวันเวลาเป็นเวลา------------------------------------------------//
|
|
function datetime_to_time($date)
|
|
{
|
|
list($date1, $time) = explode(' ', $date);
|
|
|
|
list($hh, $mm, $ss) = explode(':', $time);
|
|
|
|
return $hh . ":" . $mm;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเวลาเป็นเวลา-----------------------------------------------------//
|
|
|
|
//------------------------ตัดคำ----------------------------------------------------//
|
|
|
|
//------------------------------ฟังก์ชั่นตั้งชื่อไฟล์จากวันเวลา------------------------------------------------//
|
|
function gen_filename($date)
|
|
{
|
|
list($date1, $time) = explode(' ', $date);
|
|
list($year, $month, $day) = explode('-', $date1);
|
|
list($hh, $mm, $ss) = explode(':', $time);
|
|
|
|
$year += 543;
|
|
$year2 = substr($year, 2, 2);
|
|
|
|
return $year2 . "" . $month . "" . $day . "" . $hh . "" . $mm . "" . $ss;
|
|
}
|
|
//---------------------------------ฟังก์ชันแปลงวันเวลาเป็นเวลา-----------------------------------------------------//
|
|
|
|
//------------------------ตัดคำ----------------------------------------------------//
|
|
function cutStr($str, $maxChars = '', $holder = '')
|
|
{
|
|
|
|
if (strlen($str) > $maxChars) {
|
|
$str = iconv_substr($str, 0, $maxChars, "UTF-8") . $holder;
|
|
}
|
|
return $str;
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันแยกเดือน-ปี------------------------------------------------//
|
|
function showDate($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $date;
|
|
}
|
|
|
|
function showDate2($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $date;
|
|
}
|
|
|
|
function showMonth($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $month;
|
|
}
|
|
|
|
function showMonth2($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $month;
|
|
}
|
|
|
|
function showYear($today, $date)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $year;
|
|
}
|
|
|
|
function showYear2($today, $date)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
return $year;
|
|
}
|
|
|
|
function showAll($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year += 543;
|
|
return $thMonth[$month - 1] . " พ.ศ. " . $year;
|
|
}
|
|
|
|
function showAll2($today)
|
|
{
|
|
list($year, $month, $date) = explode('-', $today);
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
$year += 543;
|
|
return $date . " " . $thMonth[$month - 1] . " พ.ศ. " . $year;
|
|
}
|
|
|
|
function typeFile($name)
|
|
{
|
|
list($f, $l) = explode('.', $name);
|
|
|
|
return $l;
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันแยกเดือน-ปี------------------------------------------------//
|
|
|
|
//--------------------------------------ฟังก์ชันแยกชื่อ-นามสกุล------------------------------------------------//
|
|
function explode_fname($data)
|
|
{
|
|
list($fname, $lname) = explode(' ', $data);
|
|
|
|
return $fname;
|
|
}
|
|
|
|
function explode_lname($data)
|
|
{
|
|
list($fname, $lname) = explode(' ', $data);
|
|
|
|
return $lname;
|
|
}
|
|
//---------------------------------ฟังก์ชันแยกชื่อ-นามสกุล-----------------------------------------------------//
|
|
|
|
//------------------ วันนี้ แยก วัน เดือน ปี -------------------------------------
|
|
|
|
function today_date($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
if ($day < 10) {
|
|
$day = substr($day, 1, 1);
|
|
}
|
|
|
|
return $day;
|
|
}
|
|
|
|
function today_month($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
$thMonth = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
|
|
if ($month < 10) {
|
|
$month = substr($month, 1, 1);
|
|
}
|
|
|
|
|
|
return $thMonth[$month - 1];
|
|
}
|
|
|
|
function today_year($date)
|
|
{
|
|
list($year, $month, $day) = explode('-', $date);
|
|
|
|
$year = $year + 543;
|
|
|
|
return $year;
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันเข้ารหัสและถอดรหัส (URL Security)------------------------------------------------//
|
|
function encrypt_param($data) {
|
|
if (empty($data)) return '';
|
|
$method = 'AES-256-CBC';
|
|
$key = defined('APP_KEY') ? APP_KEY : 'default_fallback_key_2026';
|
|
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
|
|
$encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
|
|
// Combine IV (base64) and Encrypted payload (base64) to avoid binary concatenation issues
|
|
$payload = base64_encode($iv) . '::' . $encrypted;
|
|
// Use Base64URL to prevent +, /, and = from breaking in GET/POST requests
|
|
return rtrim(strtr(base64_encode($payload), '+/', '-_'), '=');
|
|
}
|
|
|
|
function decrypt_param($data) {
|
|
if (empty($data)) return '';
|
|
$method = 'AES-256-CBC';
|
|
$key = defined('APP_KEY') ? APP_KEY : 'default_fallback_key_2026';
|
|
|
|
// Decode Base64URL back to standard Base64
|
|
$b64 = str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT);
|
|
$decoded = base64_decode($b64);
|
|
|
|
if (strpos($decoded, '::') === false) {
|
|
// Fallback for legacy base64-only data to prevent breaking existing bookmarked links during transition
|
|
return base64_decode($data); // Fallback to raw base64 decode if it was an old link
|
|
}
|
|
|
|
list($iv_b64, $encrypted) = explode('::', $decoded, 2);
|
|
$iv = base64_decode($iv_b64);
|
|
return openssl_decrypt($encrypted, $method, $key, 0, $iv);
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันตรวจสอบสิทธิ์แอดมิน------------------------------------------------//
|
|
function is_admin($username) {
|
|
global $conn2;
|
|
if (!$conn2 || empty($username)) return false;
|
|
|
|
// Auto-create sys_admins table
|
|
$create_sql = "CREATE TABLE IF NOT EXISTS sys_admins (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(255) NOT NULL UNIQUE,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
granted_by VARCHAR(255)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
$conn2->query($create_sql);
|
|
|
|
// Check total admins (Bootstrap logic)
|
|
$res_count = $conn2->query("SELECT COUNT(*) AS total FROM sys_admins");
|
|
if ($res_count) {
|
|
$row = $res_count->fetch_assoc();
|
|
if ($row['total'] == 0) {
|
|
// No admins exist yet, allow everyone so they can set themselves as admin
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Check if user is admin
|
|
$sql = "SELECT id FROM sys_admins WHERE username = ?";
|
|
$stmt = $conn2->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("s", $username);
|
|
$stmt->execute();
|
|
$res = $stmt->get_result();
|
|
$is_admin = ($res->num_rows > 0);
|
|
$stmt->close();
|
|
$stmt->close();
|
|
return $is_admin;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันตรวจสอบสิทธิ์การค้นหาด้วยชื่อ------------------------------------------------//
|
|
function can_search_name($username) {
|
|
global $conn2;
|
|
if (!$conn2 || empty($username)) return false;
|
|
|
|
// Auto-create sys_user_permissions table
|
|
$create_sql = "CREATE TABLE IF NOT EXISTS sys_user_permissions (
|
|
username VARCHAR(255) PRIMARY KEY,
|
|
can_search_name TINYINT(1) DEFAULT 0,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
updated_by VARCHAR(255)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
$conn2->query($create_sql);
|
|
|
|
// If user is admin, they can do anything
|
|
if (is_admin($username)) return true;
|
|
|
|
// Check specific permission
|
|
$sql = "SELECT can_search_name FROM sys_user_permissions WHERE username = ? AND can_search_name = 1";
|
|
$stmt = $conn2->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("s", $username);
|
|
$stmt->execute();
|
|
$res = $stmt->get_result();
|
|
$has_permission = ($res->num_rows > 0);
|
|
$stmt->close();
|
|
return $has_permission;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//--------------------------------------ฟังก์ชันเก็บประวัติการใช้งานระบบ (System Audit Logs)------------------------------------------------//
|
|
function system_log($conn, $username, $action, $details = null) {
|
|
if (!$conn) return false;
|
|
|
|
try {
|
|
// Auto-create table if not exists (safe to run, minimal overhead if table exists)
|
|
$create_sql = "CREATE TABLE IF NOT EXISTS sys_audit_logs (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
username VARCHAR(255) NOT NULL,
|
|
action VARCHAR(50) NOT NULL,
|
|
details TEXT,
|
|
ip_address VARCHAR(45),
|
|
user_agent TEXT,
|
|
INDEX idx_action (action),
|
|
INDEX idx_username (username),
|
|
INDEX idx_created_at (created_at)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
$conn->query($create_sql);
|
|
|
|
$ip_address = $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';
|
|
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'UNKNOWN';
|
|
$details_json = is_array($details) ? json_encode($details, JSON_UNESCAPED_UNICODE) : $details;
|
|
|
|
$sql = "INSERT INTO sys_audit_logs (username, action, details, ip_address, user_agent) VALUES (?, ?, ?, ?, ?)";
|
|
$stmt = $conn->prepare($sql);
|
|
if ($stmt) {
|
|
$stmt->bind_param("sssss", $username, $action, $details_json, $ip_address, $user_agent);
|
|
$result = $stmt->execute();
|
|
$stmt->close();
|
|
return $result;
|
|
}
|
|
} catch (Throwable $e) {
|
|
error_log("system_log Error: " . $e->getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Helper function to loosely determine abnormal labs
|
|
function isAbnormalLab($result, $normal) {
|
|
if (empty($result) || empty($normal) || $normal == '-') return false;
|
|
|
|
$resStr = strtolower(trim($result));
|
|
$normStr = strtolower(trim($normal));
|
|
|
|
// Keyword check
|
|
if (in_array($resStr, ['positive', 'reactive', 'detected'])) {
|
|
if (strpos($normStr, 'negative') !== false || strpos($normStr, 'non-reactive') !== false || strpos($normStr, 'undetected') !== false) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Numeric check
|
|
preg_match('/^[-+]?[0-9]*\.?[0-9]+/', $result, $resMatch);
|
|
if (!empty($resMatch)) {
|
|
$val = (float)$resMatch[0];
|
|
// Fix: Replace dashes between numbers with " to " so they aren't parsed as negative signs
|
|
$normal_parsed = preg_replace('/(\d)\s*-\s*(\d)/', '$1 to $2', $normal);
|
|
|
|
preg_match_all('/[-+]?[0-9]*\.?[0-9]+/', $normal_parsed, $normMatches);
|
|
if (!empty($normMatches[0])) {
|
|
$numbers = $normMatches[0];
|
|
// Since we replaced '-', check for "to" or '-' in original
|
|
if (count($numbers) == 2 && (strpos($normal, '-') !== false || strpos($normal_parsed, 'to') !== false) && !preg_match('/[ชญ]/u', $normal)) {
|
|
if ($val < (float)$numbers[0] || $val > (float)$numbers[1]) return true;
|
|
} else if (count($numbers) >= 4 && preg_match('/[ชญ]/u', $normal)) {
|
|
$min1 = min((float)$numbers[0], (float)$numbers[2]);
|
|
$max1 = max((float)$numbers[1], (float)$numbers[3]);
|
|
if ($val < $min1 || $val > $max1) return true;
|
|
} else if (strpos($normal, '<') !== false && count($numbers) >= 1) {
|
|
preg_match('/<\s*([0-9.]+)/', $normal, $ltMatch);
|
|
if (!empty($ltMatch) && $val >= (float)$ltMatch[1]) return true; // changed to >= if normal is < X, then X is abnormal
|
|
} else if (strpos($normal, '>') !== false && count($numbers) >= 1) {
|
|
preg_match('/>\s*([0-9.]+)/', $normal, $gtMatch);
|
|
if (!empty($gtMatch) && $val <= (float)$gtMatch[1]) return true; // changed to <= if normal is > X, then X is abnormal
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
?>
|