367 lines
17 KiB
PHP
367 lines
17 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$message = '';
|
|
$message_type = 'danger';
|
|
|
|
if (isset($_GET['message'])) {
|
|
$message = htmlspecialchars($_GET['message']);
|
|
if (isset($_GET['type'])) {
|
|
$message_type = htmlspecialchars($_GET['type']);
|
|
}
|
|
}
|
|
|
|
$uploadDir = 'uploads/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$conn = db();
|
|
if (isset($_FILES['fileToUpload'])) {
|
|
$targetFile = $uploadDir . basename($_FILES["fileToUpload"]["name"]);
|
|
|
|
$stmt = $conn->prepare("SELECT 1 FROM files WHERE filename = ?");
|
|
$stmt->execute([basename($_FILES["fileToUpload"]["name"])]);
|
|
|
|
if ($stmt->fetch()) {
|
|
$message = "Sorry, file already exists.";
|
|
$message_type = 'danger';
|
|
} else {
|
|
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
|
|
$stmt = $conn->prepare("INSERT INTO files (filename, filesize, filetype) VALUES (?, ?, ?)");
|
|
$stmt->execute([
|
|
basename($_FILES["fileToUpload"]["name"]),
|
|
$_FILES["fileToUpload"]["size"],
|
|
$_FILES["fileToUpload"]["type"]
|
|
]);
|
|
$message = "The file ". htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
|
|
$message_type = 'success';
|
|
} else {
|
|
$message = "Sorry, there was an error uploading your file.";
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
} elseif (isset($_POST['remote_url'])) {
|
|
$url = filter_var($_POST['remote_url'], FILTER_VALIDATE_URL);
|
|
if ($url) {
|
|
$fileName = basename(parse_url($url, PHP_URL_PATH));
|
|
if (empty($fileName)) {
|
|
$fileName = 'remote_file_' . time();
|
|
}
|
|
$targetFile = $uploadDir . $fileName;
|
|
|
|
$stmt = $conn->prepare("SELECT 1 FROM files WHERE filename = ?");
|
|
$stmt->execute([$fileName]);
|
|
|
|
if ($stmt->fetch()) {
|
|
$message = "Sorry, a file with the name '{$fileName}' already exists.";
|
|
$message_type = 'danger';
|
|
} else {
|
|
$content = @file_get_contents($url);
|
|
if ($content !== false) {
|
|
if (file_put_contents($targetFile, $content)) {
|
|
$stmt = $conn->prepare("INSERT INTO files (filename, filesize, filetype) VALUES (?, ?, ?)");
|
|
$stmt->execute([
|
|
$fileName,
|
|
strlen($content),
|
|
mime_content_type($targetFile) // Guess mime type
|
|
]);
|
|
$message = "The file from URL has been uploaded as '{$fileName}'.";
|
|
$message_type = 'success';
|
|
} else {
|
|
$message = "Sorry, there was an error saving the remote file.";
|
|
$message_type = 'danger';
|
|
}
|
|
} else {
|
|
$message = "Sorry, could not retrieve the file from the specified URL.";
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
} else {
|
|
$message = "Invalid URL provided.";
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>My Files Cloud</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<header class="hero">
|
|
<div class="container">
|
|
<h1 class="display-4">My Files Cloud</h1>
|
|
<p class="lead">Your personal cloud storage solution.</p>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container my-5">
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
|
|
<?php echo $message; ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<section class="upload-section">
|
|
<h2><i class="bi bi-cloud-arrow-up"></i> Upload Files</h2>
|
|
<p>Storage Capacity: Unlimited</p>
|
|
<form action="index.php" method="post" enctype="multipart/form-data" id="uploadForm">
|
|
<div class="mb-4">
|
|
<label for="file-upload" class="form-label"><strong>Upload from your device</strong></label>
|
|
<div class="input-group">
|
|
<input type="file" class="form-control" id="file-upload" name="fileToUpload" required>
|
|
<button class="btn btn-primary" type="submit" id="uploadButton">Upload</button>
|
|
</div>
|
|
<div class="progress mt-2" style="height: 5px;">
|
|
<div class="progress-bar" role="progressbar" id="uploadProgressBar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
|
|
</div>
|
|
<small class="form-text text-muted">Max. file size: 9999 PB</small>
|
|
</div>
|
|
</form>
|
|
|
|
<form action="index.php" method="post" id="remoteUploadForm">
|
|
<div class="mb-4">
|
|
<label for="remote-url-upload" class="form-label"><strong>Upload from a remote URL</strong></label>
|
|
<div class="input-group">
|
|
<input type="url" class="form-control" id="remote-url-upload" name="remote_url" placeholder="https://example.com/file.jpg" required>
|
|
<button class="btn btn-secondary" type="submit">Upload from URL</button>
|
|
</div>
|
|
<div class="progress mt-2" style="height: 5px; display: none;">
|
|
<div class="progress-bar progress-bar-striped progress-bar-animated" id="remoteProgress" role="progressbar" style="width: 100%;" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
|
|
</div>
|
|
<small class="form-text text-muted">Max. file size: 9999 PB</small>
|
|
</div>
|
|
</form>
|
|
</section>
|
|
|
|
<section class="files-section">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h2><i class="bi bi-folder"></i> My Files</h2>
|
|
<div class="btn-group">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
|
|
<i class="bi bi-funnel"></i> Sort by
|
|
</button>
|
|
<ul class="dropdown-menu">
|
|
<li><a class="dropdown-item" href="#">Alphabetical (A-Z)</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="list-group">
|
|
<?php
|
|
function format_bytes($bytes, $precision = 2) {
|
|
$units = array('B', 'KB', 'MB', 'GB', 'TB');
|
|
$bytes = max($bytes, 0);
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
$pow = min($pow, count($units) - 1);
|
|
$bytes /= (1 << (10 * $pow));
|
|
return round($bytes, $precision) . ' ' . $units[$pow];
|
|
}
|
|
|
|
try {
|
|
$conn = db();
|
|
$stmt = $conn->prepare("SELECT id, filename, filesize, upload_date FROM files ORDER BY filename ASC");
|
|
$stmt->execute();
|
|
$files = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$files = [];
|
|
$message = "Database error: " . $e->getMessage();
|
|
$message_type = "danger";
|
|
}
|
|
|
|
if (empty($files)):
|
|
?>
|
|
<div class="text-center text-muted p-5 border rounded">
|
|
<i class="bi bi-cloud-drizzle" style="font-size: 3rem;"></i>
|
|
<p class="mt-3">No files uploaded yet.</p>
|
|
<p>Use the forms above to add your first file!</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($files as $file): ?>
|
|
<div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
|
|
<div class="d-flex align-items-center">
|
|
<i class="bi bi-file-earmark-text me-3" style="font-size: 1.5rem;"></i>
|
|
<div>
|
|
<strong class="mb-1"><?php echo htmlspecialchars($file['filename']); ?></strong>
|
|
<p class="mb-1 text-muted small">
|
|
Size: <?php echo format_bytes($file['filesize']); ?> | Uploaded: <?php echo date("M d, Y", strtotime($file['upload_date'])); ?>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<button type="button" class="btn btn-sm btn-outline-info me-2 view-btn" data-filename="<?php echo htmlspecialchars($file['filename']); ?>" data-filetype="<?php echo htmlspecialchars($file['filetype']); ?>"><i class="bi bi-eye"></i> View</button>
|
|
<a href="uploads/<?php echo rawurlencode($file['filename']); ?>" class="btn btn-sm btn-outline-primary me-2" download><i class="bi bi-cloud-download"></i> Download</a>
|
|
<form action="delete.php" method="post" style="display: inline;" onsubmit="return confirm('Are you sure you want to delete this file?');">
|
|
<input type="hidden" name="filename" value="<?php echo htmlspecialchars($file['filename']); ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Delete</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="playerModal" tabindex="-1" aria-labelledby="playerModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="playerModalLabel">File Player</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body" id="playerModalBody">
|
|
<!-- Player will be injected here -->
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="text-center text-muted py-4">
|
|
<p>© <?php echo date("Y"); ?> My Files Cloud. Powered by Flatlogic.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
<script>
|
|
document.getElementById('uploadForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
let formData = new FormData(this);
|
|
let xhr = new XMLHttpRequest();
|
|
let progressBar = document.getElementById('uploadProgressBar');
|
|
|
|
progressBar.parentElement.style.display = 'block';
|
|
|
|
xhr.open('POST', 'index.php', true);
|
|
|
|
xhr.upload.onprogress = function(e) {
|
|
if (e.lengthComputable) {
|
|
let percentComplete = (e.loaded / e.total) * 100;
|
|
progressBar.style.width = percentComplete + '%';
|
|
progressBar.setAttribute('aria-valuenow', percentComplete);
|
|
}
|
|
};
|
|
|
|
xhr.onload = function() {
|
|
if (xhr.status == 200) {
|
|
// maybe show a success message before reloading
|
|
window.location.reload();
|
|
} else {
|
|
// handle error
|
|
alert('An error occurred during the upload. Please try again.');
|
|
progressBar.parentElement.style.display = 'none';
|
|
}
|
|
};
|
|
|
|
xhr.send(formData);
|
|
});
|
|
|
|
document.getElementById('remoteUploadForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
let formData = new FormData(this);
|
|
let xhr = new XMLHttpRequest();
|
|
let progressBar = document.getElementById('remoteProgress');
|
|
|
|
progressBar.parentElement.style.display = 'block';
|
|
|
|
xhr.open('POST', 'index.php', true);
|
|
|
|
xhr.onload = function() {
|
|
if (xhr.status == 200) {
|
|
window.location.reload();
|
|
} else {
|
|
alert('An error occurred during the remote upload. Please try again.');
|
|
progressBar.parentElement.style.display = 'none';
|
|
}
|
|
};
|
|
|
|
xhr.send(formData);
|
|
});
|
|
|
|
const playerModal = new bootstrap.Modal(document.getElementById('playerModal'));
|
|
const playerModalBody = document.getElementById('playerModalBody');
|
|
const playerModalLabel = document.getElementById('playerModalLabel');
|
|
let hls;
|
|
|
|
document.querySelectorAll('.view-btn').forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const filename = this.dataset.filename;
|
|
const filetype = this.dataset.filetype.toLowerCase();
|
|
const filepath = `uploads/${filename}`;
|
|
|
|
playerModalLabel.textContent = filename;
|
|
playerModalBody.innerHTML = ''; // Clear previous content
|
|
|
|
if (filetype.startsWith('audio/')) {
|
|
playerModalBody.innerHTML = `<audio controls autoplay loop class="w-100"><source src="${filepath}" type="${filetype}">Your browser does not support the audio element.</audio>`;
|
|
} else if (filetype.startsWith('video/') || filetype === 'application/x-mpegurl' || filetype === 'application/vnd.apple.mpegurl') {
|
|
if (filename.endsWith('.m3u8')) {
|
|
playerModalBody.innerHTML = `<video id="video" controls autoplay loop class="w-100"></video>`;
|
|
if (Hls.isSupported()) {
|
|
hls = new Hls();
|
|
hls.loadSource(filepath);
|
|
hls.attachMedia(document.getElementById('video'));
|
|
}
|
|
} else if (filename.endsWith('.m3u')) {
|
|
fetch(filepath)
|
|
.then(response => response.text())
|
|
.then(data => {
|
|
const lines = data.split('\n').filter(line => line.trim() !== '' && !line.startsWith('#'));
|
|
let playlistHtml = '<div class="list-group">'
|
|
lines.forEach((line, index) => {
|
|
playlistHtml += `<a href="#" class="list-group-item list-group-item-action" data-src="${line}">Track ${index + 1}</a>`;
|
|
});
|
|
playlistHtml += '</div><video id="playlist_video" controls autoplay loop class="w-100 mt-3"></video>';
|
|
playerModalBody.innerHTML = playlistHtml;
|
|
|
|
const video = document.getElementById('playlist_video');
|
|
document.querySelectorAll('.list-group-item').forEach(item => {
|
|
item.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
video.src = this.dataset.src;
|
|
video.play();
|
|
});
|
|
});
|
|
});
|
|
} else {
|
|
playerModalBody.innerHTML = `<video controls autoplay loop class="w-100"><source src="${filepath}" type="${filetype}">Your browser does not support the video tag.</video>`;
|
|
}
|
|
} else if (filetype.startsWith('image/gif')) {
|
|
playerModalBody.innerHTML = `<img src="${filepath}" class="img-fluid w-100">`;
|
|
} else if (filetype.startsWith('image/')) {
|
|
playerModalBody.innerHTML = `<img src="${filepath}" class="img-fluid w-100">`;
|
|
}
|
|
else {
|
|
playerModalBody.innerHTML = `<p class="text-center">No player available for this file type.</p>`;
|
|
}
|
|
|
|
playerModal.show();
|
|
});
|
|
});
|
|
|
|
document.getElementById('playerModal').addEventListener('hidden.bs.modal', function () {
|
|
if (hls) {
|
|
hls.destroy();
|
|
}
|
|
playerModalBody.innerHTML = '';
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|