Sindbad~EG File Manager

Current Path : /home/beeson/public_html/WebMathXpert/ThreeBodyWeb/
Upload File :
Current File : //home/beeson/public_html/WebMathXpert/ThreeBodyWeb/PrivateGallery.php

<?php
// PrivateGallery.php
session_start();
$dbFile = 'ThreeBodyDatabase.db';
$db = new SQLite3($dbFile);

$password = $_GET['password'] ?? '';
if (empty($password)) {
    echo "No password provided.";
    exit;
}

// Fetch simulation documents with matching password.
$stmt = $db->prepare("SELECT * FROM documents WHERE password = :pwd ORDER BY date_created DESC");
$stmt->bindValue(':pwd', $password, SQLITE3_TEXT);
$result = $stmt->execute();

$documents = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    $documents[] = $row;
}

// Fetch body data for each document.
$documentsData = [];
foreach ($documents as $doc) {
    $docId = $doc['DocumentNumber'];
    $stmt = $db->prepare("SELECT mass, color, r, x0, y0, p0, q0 FROM bodies WHERE DocumentNumber = :doc");
    $stmt->bindValue(':doc', $docId, SQLITE3_INTEGER);
    $resultBodies = $stmt->execute();
    $bodiesArray = [];
    while ($bodyRow = $resultBodies->fetchArray(SQLITE3_ASSOC)) {
        $bodiesArray[] = $bodyRow;
    }
    if (!isset($doc['caption'])) {
        $doc['caption'] = "";
    }
    $doc['bodies'] = $bodiesArray;
    $documentsData[$docId] = $doc;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Private Gallery</title>
  <style>
    body {
      background-color: #e6f2ff;
      font-family: Arial, sans-serif;
      margin: 10px;
    }
    .gallery {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }

    .thumb {
      width: 150px;
      height: 100px;
      object-fit: cover;
      border: 2px solid transparent;
    }
	 .thumb-container {
	   position: relative;
	   width: 150px;
	   cursor: pointer;
	   background-color: cornsilk;
	   display: flex;
	   flex-direction: column;
	   align-items: center;
	 }

	 .thumb-container:hover .delete-icon {
	   display: block;
	 }

	 .delete-icon {
	   display: none;             /* Hidden by default */
	   position: absolute;
	   top: 4px;
	   left: 4px;
	   font-size: 18px;
	   cursor: pointer;
	   z-index: 5;
	   background-color: rgba(255, 255, 255, 0.8); /* optional: background for contrast */
	   border-radius: 4px;
	   padding: 2px;
	 }
    .caption {
      font-size: 0.9em;
      color: #333;
      text-align: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      width: 150px;
      background-color: #e6f2ff;
      padding: 2px 4px;
      margin-top: 1px;
      box-sizing: border-box;
    }
	.delete-icon {
	   position: absolute;
	   bottom: 4px;
	   left: 6px;
	   font-size: 20px;
	   cursor: pointer; 
	   height:8px;
	   background-color: cornsilk;
	   z-index: 5; 
	}
	.gallery-button {
		margin-left: 20px;
		padding: 6px 12px;
		background-color: #00008B;
		color: white;
		text-decoration: none;
		font-size: 16px;
		border-radius: 4px;
		vertical-align: middle;
	}
	
	.gallery-button {
	  display: inline-block;
	  padding: 10px 20px;
	  margin-left: 20px;
	  font-size: 16px;
	  cursor: pointer;
	  background-color: #00008B;
	  color: white;
	  text-decoration: none;
	  border: none;
	  border-radius: 4px;
	}

	.gallery-button:hover {
		background-color: #0000a0;
	}
  </style>
  <script>
    const documentsData = <?php echo json_encode($documentsData); ?>;
    const galleryPassword = <?php echo json_encode($password); ?>;

    function confirmAndDelete(docId) {
      const confirmDelete = confirm("Are you sure you want to delete this entry?");
      if (!confirmDelete) return;

      // Send deletion request
		fetch("DeleteFromGalleryHelper.php", {
		  method: "POST",
		  mode: "cors", // explicitly enable CORS
		  credentials: "same-origin", // or "include" if your server sets cookies
		  headers: {
		    "Content-Type": "application/x-www-form-urlencoded"
		  },
		  body: `DocumentNumber=${encodeURIComponent(docId)}&password=${encodeURIComponent(galleryPassword)}`
		})
      .then(response => response.text())
      .then(result => {
        if (result.trim() === "OK") {
          alert("Entry deleted.");
          location.reload();
        } else {
          alert("Deletion failed: " + result);
        }
      })
      .catch(error => {
        alert("Error: " + error);
      });
    }

    function selectDocument(docId) {
      const docData = documentsData[docId];
      const form = document.createElement("form");
      form.method = "post";
      form.action = "ThreeBody.php?indatabase=true";

      const fields = ["nbodies", "caption", "image_filename", "tmax"];
      fields.forEach(field => {
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = field;
        input.value = docData[field] || "";
        form.appendChild(input);
      });

      const bodyFields = ["mass", "color", "r", "x0", "y0", "p0", "q0"];
      docData.bodies.forEach(body => {
        bodyFields.forEach(field => {
          const input = document.createElement("input");
          input.type = "hidden";
          input.name = field + "[]";
          input.value = body[field] || "";
          form.appendChild(input);
        });
      });

      const solveInput = document.createElement("input");
      solveInput.type = "hidden";
      solveInput.name = "solveNow";
      solveInput.value = "1";
      form.appendChild(solveInput);

      document.body.appendChild(form);
      form.submit();
    }

    document.addEventListener("DOMContentLoaded", () => {
      document.querySelector(".gallery").addEventListener("click", (e) => {
        if (e.target.classList.contains("delete-icon")) {
          const container = e.target.closest(".thumb-container");
          const docId = container.dataset.docid;
          confirmAndDelete(docId);
        } else {
          const container = e.target.closest(".thumb-container");
          if (!container) return;
          const docId = container.dataset.docid;
          selectDocument(docId);
        }
      });
    });
  </script>
</head>
<body>
<h1>
  Private Gallery
  <a href="BrowseThreeBody.php" class="gallery-button">Public Gallery</a>
</h1>
  <div class="gallery">
    <?php foreach ($documents as $doc): 
        $imgSrc = !empty($doc['image_filename']) ? "./images/" . htmlspecialchars($doc['image_filename']) : "placeholder.svg";
        $caption = !empty($doc['caption']) ? htmlspecialchars($doc['caption']) : "";
    ?>
    <div class="thumb-container" data-docid="<?php echo $doc['DocumentNumber']; ?>">
      <span class="delete-icon" title="Delete">🗑️</span>
      <img src="<?php echo $imgSrc; ?>" alt="Problem <?php echo $doc['DocumentNumber']; ?>" class="thumb">
      <?php if (!empty($caption)): ?>
        <div class='caption' title="<?php echo $caption; ?>"><?php echo $caption; ?></div>
      <?php endif; ?>
    </div>
    <?php endforeach; ?>
  </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists