<?php

include_once("database.php");
$postData = file_get_contents("php://input");

if (isset($postData) && !empty($postData)) {

  $request = json_decode($postData, true);
  $isFile = trim($request['isFile']);
  if($isFile){
    $username = trim($request['file']['username']);
    $filename = trim($request['file']['filename']);
    $lang = trim($request['file']['language']);
    $path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $filename . $lang;

    if (unlink($path)) {
      $sql = "UPDATE users SET n_files=n_files-1 WHERE username = '$username'";
      mysqli_query($mysqli,$sql);
      $msg = "file successfully deleted";
      echo json_encode($msg);
    }
    else{
      http_response_code(404);
    }
    exit;
  }
  $username = trim($request['username']);
  $dirname = trim($request['file']['name']);
  $path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $dirname;

  $ret_stat = 0;
  system('rm -r ' . $path, $ret_stat);
  if ($ret_stat === 0) {
    $msg = "directory successfully deleted";
    echo json_encode($msg);
  }
  else {
    http_response_code(404);
  }
}
?>
