<?php 
if (session_status() === PHP_SESSION_NONE){session_start();}

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$plaintext_password = "";
$str_data = file_get_contents("api/vpn.json");
$data = json_decode($str_data, true);
 $json = $data['cred'];
 if(isset($_POST["password"])){
$plaintext_password = $_POST["password"];  
};
  // The hash of the password that
  // can be stored in the database
  $hash = password_hash($plaintext_password, 
          PASSWORD_DEFAULT);
  



$username = $data["cred"]["username"];
$password = $data["cred"]["password"];
if(isset($_POST['submit'])){
    $jsonData = file_get_contents("./api/vpn.json");
$arrayData = json_decode($jsonData, true);
$replacementData = array('cred' =>  array(
	"username"             => $_POST["username"],
    "password"               => $hash
   
    ))
    ;
    $newArrayData = array_replace($arrayData, $replacementData);
$newJsonData = json_encode($newArrayData, JSON_PRETTY_PRINT);
file_put_contents("./api/vpn.json", $newJsonData);

header("Location: user_update.php");
}
//include ('includes/header.php');

?>

<!-- Begin Page Content -->
        <div class="container-fluid">

          <!-- Page Heading -->
          <h1 class="h3 mb-1 text-gray-800"> Update Username and Password</h1>

              <!-- Custom codes -->
                <div class="card border-left-primary alert-primary shadow h-100 card shadow mb-4">
                <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary"><i class="fa fa-android"></i> Update Credentials</h6>
                </div>                
                <div class="card-body">
                    <?
echo "                  <form  method=\"post\">\n";
echo "                      <div>\n";
echo "                        <div class=\"form-group\">\n";
echo "                          <label class=\"bmd-label-floating\">username</label>\n";
echo "						   <input type=\"hidden\" name=\"id\" value=\"\$res\">\n";
?>                      <input type="text" class="form-control" name="username" value="<?echo $json['username']?>"><?
echo "                        </div>\n";
echo "                      </div>\n";
echo "                      <div>\n";
echo "                        <div class=\"form-group\">\n";
echo "                          <label class=\"bmd-label-floating\">password</label>\n";
          ?>                <input type="text" class="form-control" name="password" value="<?echo $json["password"]?>"><?
echo "                        </div>\n";
echo "                      </div>\n";
echo "                      <div>\n";
echo "                        
                        					<br><button type=\"submit\" name=\"submit\" class=\"btn btn-primary pull-right\">Submit</button>\n";
echo "				</form>\n";
echo "				</div>\n";
echo "              </div>\n";
echo "            </div>\n";
echo "          </div>\n";
echo "    <br><br><br>\n";
include ('includes/footer.php');
echo "</body>\n";
echo "\n";
echo "</html>\n";
