Path : /var/www/html/smart_kpp2_v2/
File Upload :
Current File : /var/www/html/smart_kpp2_v2/forgotpassword.php

<?php
/** ensure this file is being included by a parent file */
//defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

$p = (get_magic_quotes_gpc()) ? $_GET["p"]  : addslashes($_GET["p"]);
$p2 = (get_magic_quotes_gpc()) ? $_GET["p2"]  : addslashes($_GET["p2"]);
require_once "amssplus_connect.php";

$user_agent = $_SERVER['HTTP_USER_AGENT'];
function getOS() { 
    global $user_agent;
    $os_platform    =   "desktop";
    $os_array       =   array(
							'/windows nt 10.0/i'     =>  'Windows 10',
                            '/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',
                            '/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;
				if(($os_platform=='Android') or ($os_platform=='BlackBerry') or ($os_platform=='iPhone') or ($os_platform=='iPad')){
				$os_platform="mobile";
				}
				else{
				$os_platform="desktop";
				}
        }
    }   
    return $os_platform;
}
$user_os = getOS();
?>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
		    <link rel='stylesheet prefetch' href='css/animate.min.css'>
        <link rel="stylesheet" href="css/style1.css">
<head>
<title>Reset Password</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name = "viewport" content = "width = device-width">
<link rel="stylesheet" href="css/style-gg.css">
<script language="javascript" type="text/javascript">

	function setFocus() {
		document.loginForm.username.select();
		document.loginForm.username.focus();
	}
</script>
</head><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<body onLoad="setFocus();">
<!-- Div ของ login -->
		    <div class='form animated flipInX'>

  <div align="center"><img src="images/login/obec_logo_300.png" width="100" height="130"  border="0" alt="">
  <h3><strong><font color="#0000CC">Reset Password</font></strong><br>
  </h3></div>
 
  <?php if($p == "chk"){?>
        	<form action="?p=reset" method="post" name="loginForm" id="loginForm"><center>        	  
<p>
  <input name="person_id" type="text"  class="form-control" placeholder="เลขประจำตัวประชาชน" size="15" maxlength="13" id="person_id" /></p>
<table width="100%" border="0" align="center">
  <tr>
    <td width="500" height="10" align="center" valign="top"><select name="chk_type" id="chk_type"   class="form-control" >
      <option value="">เลือกประเภทบุคลากร</option>
      <option value="parea">-บุคลากร สพท.</option>
      <option value="psch">-บุคลากรโรงเรียน</option>
        </select></td>
    </tr>
</table>
  
  <br>

<button type="submit" name="login_submit" class="btn btn-primary"> ตรวจสอบข้อมูล</button><br>
    <input name="user_os" type="hidden" value="<?php echo $user_os ?>">
        	</center>
        	</form>
            <?php }?>
            
            <center>    
            <?php if($p == "reset"){?>         
            <?php
				
			$person_id = (get_magic_quotes_gpc()) ? $_POST["person_id"]  : addslashes($_POST["person_id"]);			
			$chk_type = (get_magic_quotes_gpc()) ? $_POST["chk_type"]  : addslashes($_POST["chk_type"]);
			
			if ($person_id =="") {
echo "<center><h2><br><br>ไม่กรอกข้อมูล เลข ปชช.</h2> <br><input type=\"button\" value=\"กลับไปแก้ไข\" class=\"btn btn-danger\" onClick=\"javascript:history.go(-1)\" ></center>" ;
exit () ;
} 
			if ($chk_type =="") {
echo "<center><h2><br><br>ไม่เลือกประเภทบุคลากร</h2> <br><input type=\"button\" value=\"กลับไปแก้ไข\" class=\"btn btn-danger\" onClick=\"javascript:history.go(-1)\" ></center>" ;
exit () ;
} 
			if($chk_type == "parea"){ //parea
			$sql_user="SELECT
			person_main.person_id,
			person_main.prename,
			person_main.`name`,
			person_main.surname,
			system_user.username,
			system_user.userpass
			FROM
			person_main
			Inner Join system_user ON person_main.person_id = system_user.person_id
			WHERE
			person_main.person_id = $person_id AND
			person_main.`status` = 0";
			
			}else{ //psch
			
			$sql_user="SELECT
			person_sch_main.person_id,
			person_sch_main.prename,
			person_sch_main.`name`,
			person_sch_main.surname,
			system_user.username,
			system_user.userpass
			FROM
			person_sch_main
			Inner Join system_user ON person_sch_main.person_id = system_user.person_id
			WHERE
			person_sch_main.person_id = $person_id AND
			person_sch_main.`status` = 0";
			}//end
			
			$dbquery_user = mysqli_query($connect,$sql_user);
			$ref_user = mysqli_fetch_array($dbquery_user);
			$total_user = mysqli_num_rows($dbquery_user);
			?>
            <?php }?>
            <?php if($total_user =='1' and $p == "reset"){?>
           <br> สวัสดี คุณ <font color="#FF0000"><?php echo $ref_user['name'];?>  <?php echo $ref_user['surname'];?></font>
            <form action="?p2=save" method="post" name="loginForm" id="loginForm"><center>        	  
<p>กรอกรหัสผ่านใหม่ที่ต้องการ ให้ตรงกันทั้ง 2 ช่อง
  <input name="userpass" type="password"  class="form-control" placeholder="กรอกรหัสผ่านใหม่" size="15" id="userpass" /></p>
<p>
  <input name="userpass2" type="password"  class="form-control" placeholder="กรอกรหัสผ่านใหม่ อีกครั้ง" size="15" id="userpass2" /><input name="person_id" type="hidden"  class="form-control" value="<?php echo $ref_user['person_id'];?>" size="15" id="person_id" /></p>  
<button type="submit" name="login_submit" class="btn btn-success" >บันทึกข้อมูล</button><br>    
        	</center>
        	</form>             
            <?php }?>
            <?php if($p2 == "save"){
            
			require_once "amssplus_connect.php";
			
			// ฟังก์ชันแสดงข้อผิดพลาด
			function showError($message) {
				echo "<center><h2><br><br>$message</h2><br>
					  <input type=\"button\" value=\"กลับไปแก้ไข\" class=\"btn btn-danger\" onClick=\"history.go(-1)\"></center>";
				exit();
			}
			
			// รับค่าจากฟอร์ม
			$person_id = $_POST["person_id"] ?? '';
			$userpass = $_POST["userpass"] ?? '';
			$userpass2 = $_POST["userpass2"] ?? '';
			
			// ตรวจสอบข้อมูลที่จำเป็น
			if (empty($person_id)) {
				showError("ไม่กรอกข้อมูลเลข ปชช.");
			}
			
			if (empty($userpass) || empty($userpass2)) {
				showError("ไม่กรอกรหัสผ่านหรือยืนยันรหัสผ่าน");
			}
			
			if ($userpass !== $userpass2) {
				showError("กรอกรหัสผ่าน 2 ช่องไม่ตรงกัน");
			}
			
			// ตรวจสอบความแข็งแรงของรหัสผ่าน (อย่างน้อย 8 ตัวอักษร)
			if (strlen($userpass) < 8) {
				showError("รหัสผ่านต้องมีความยาวอย่างน้อย 8 ตัวอักษร");
			}
			
			// ใช้ PDO สำหรับการเชื่อมต่อฐานข้อมูล
			try {
				$dsn = 'mysql:host=' . $hostname . ';dbname=' . $dbname . ';charset=utf8mb4';
				$options = [
					PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
					PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
					PDO::ATTR_EMULATE_PREPARES => false,
					PDO::ATTR_PERSISTENT => false // ไม่ใช้ persistent connection
				];
				
				$pdo = new PDO($dsn, $user, $password, $options);
				
				// เข้ารหัสรหัสผ่านด้วยวิธีที่ปลอดภัย
				$hashedPassword = md5($userpass);
				
				// อัปเดตข้อมูลด้วย prepared statement
				$stmt = $pdo->prepare("UPDATE system_user SET userpass = ? WHERE person_id = ?");
				$stmt->execute([$hashedPassword, $person_id]);
				
				// แสดงผลสำเร็จ
				echo "<center><h2><br><br>บันทึกข้อมูลเรียบร้อย</h2><br>
					  <input type=\"button\" value=\"กลับไปหน้าหลัก\" class=\"btn btn-success\" onClick=\"window.location.href='index.php'\"></center>";
				
			} catch (PDOException $e) {
				showError("เกิดข้อผิดพลาดในการอัปเดตรหัสผ่าน: " . $e->getMessage());
			}
			



            }?>            
            <?php if($total_user =='' and $p == "reset"){?>
            <div class="alert alert-danger" role="alert">
            <font color="#FF0000">
            <?php
			echo "ไม่พบชื่อผู้ใช้ (Username) ของท่าน <br>หรือท่านอาจจะไม่ได้เป็นบุคลากรปัจจุบัน";
			echo "<br>";
			echo "โปรดติดต่อผู้ดูแลระบบ";	
			?>		
            </font></div>
             <?php }?>
            
            <?php if($p != "reset"){?><a class="btn btn-primary" href="index.php" role="button">กลับหน้าหลัก</a><?php }?><?php if($p == "reset"){?><a class="btn btn-primary" href="forgotpassword.php?p=chk" role="button">กลับหน้าหลัก</a><?php }?>
           
            </center>
  <hr>
    <script src='js/jquery.js'></script>

        <script src="js/index.js"></script>	
		
		  
		<font size="2"><center>กลุ่มส่งเสริมการศึกษาทางไกล เทคโนโลยีสารสนเทศและการสื่อสาร<br>
		สำนักงานเขตพื้นที่การศึกษาประถมศึกษายโสธร เขต 1 <a href="https://office.yst1.go.th" traget "_blank">https://office.yst1.go.th</a></font></center></div>
	</div>
</div>



<!-- จบ Login -->
</body>
</html>