remove-canvas.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /* Removes the canvas passed by parameter, prior check that it
  3. belongs to the user currently logged in */
  4. session_start();
  5. if(!isset($_POST['remove_canvas_ID'], $_SESSION['userlogin'])) {
  6. echo 400; // Missing parameters
  7. }
  8. else {
  9. // Retrieve user credentials
  10. $canvas_id = $_POST['remove_canvas_ID'];
  11. $email = $_SESSION['userlogin'];
  12. require_once('../../php/db_utils.php');
  13. $conn = db_connect(); // Connect to the database
  14. // Check if the canvas exists and belongs to the current user
  15. if(!($result = mysqli_query($conn, 'SELECT canvas_id FROM canvas WHERE canvas_id = "' . $canvas_id . '" AND user_id = "' . $email . '"'))) {
  16. echo 400; // Wrong query
  17. }
  18. else if(mysqli_num_rows($result) != 1) { // User not registered or duplicated
  19. echo 401;
  20. }
  21. else { // Canvas exists: delete canvas
  22. if(!mysqli_query($conn, "DELETE FROM canvas WHERE canvas_id = '$canvas_id'")) {
  23. echo 400; // Wrong query
  24. }
  25. else { // Canvas successfully deleted: remove json file
  26. unlink("../json/$canvas_id.json");
  27. echo 200;
  28. }
  29. }
  30. mysqli_free_result($result);
  31. db_close($conn); // Close the database
  32. }
  33. ?>