<?php
require '../connForms/acdet.php';

$userName = $_POST['userName'];
$userPass = $_POST['userPass'];

$response = "";

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

$userName = $mysqli->real_escape_string($userName);
$userPass = $mysqli->real_escape_string($userPass);

$path = "../datBundle/".$userName."/dashCam/";

$iterations = 1000;
$salt = count($userName) * 23;
$hashPass = hash_pbkdf2("sha256", $userPass, $salt, $iterations, 25);


$sqlDel = "SELECT username = '$userName' and userpass = '$hashPass' FROM accounts";
$resultDel = $mysqli->query($sqlDel);

if ($resultDel -> num_rows > 0) {
    //get all files from dashCam dir to an array
    $files = scandir($path); 
    $arr = [];
    $j = 0;
    // clrear the first two positions . and ..
    for ($i = 2; $i < count($files); $i++) {
        $arr[$j] = $files[$i];
        $j++;
    }
    //delete all files from the dashCam directory
    foreach($arr as $file){ 
        $fileDelPath = $path.$file;
        if(file_exists($fileDelPath)){
            unlink($fileDelPath);
        }
    }
}

$mysqli->close();


?>