<?php
require_once('config.php');
$error_message;
$valid = true;


$login_id = $_REQUEST['login_id'];
//$new_passwod = $_REQUEST['new_passwod'];


if(isset($_REQUEST['new_passwod'])){
    //$login_password = parse_url($_REQUEST['new_passwod']); 
    $new_passwod = myUrlEncode($_REQUEST['new_passwod']);
    //echo $login_password;
}

function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
    $encodedString = urlencode($string);
    // Replace reserved characters with their respective replacements
    return str_replace($entities, $replacements, $encodedString);
}


/* $old_passwod='admin1';
$new_passwod='admin123';
$login_id='admin'; */

/* $sql = "EXEC sp_password @old = ?, @new = ?, @loginame = ?";

$procedure_params=array($old_passwod,$new_passwod,$login_id);

$stmt = sqlsrv_prepare($conn, $sql, $procedure_params);
if (!sqlsrv_execute($stmt)) {
    echo "Your code is fail!";
    die;
}
while($row = sqlsrv_fetch_array($stmt)){
    //Stuff
} */



$sql = "ALTER LOGIN \"".$login_id."\" WITH PASSWORD = '".$new_passwod."'";


$stmt = sqlsrv_prepare($conn, $sql);
if (!sqlsrv_execute($stmt)) {
    echo "Your code is fail!";
    die;
}
while($row = sqlsrv_fetch_array($stmt)){
    //Stuff
}

    


	if( $stmt ) {
		
		 sqlsrv_close( $conn);	
		 returnData();	
		} else {
		 
		 $error_message = "Transaction rolled back.<br />";
		 returnError($error_message);
	} 
	


function returnData(){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Password changed successfully');
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'Alert',
	'message' => $error_message,
	'data' => $json);	
	
	echo json_encode($returnData);
		
	exit();
}	


?>