<?php
include "../db.php";
/* ---------------- BASIC VALIDATION ---------------- */
if (!isset($_POST['chapter_id'], $_POST['upload_category'])) {
die("Invalid request");
}
$chapter_id = (int)$_POST['chapter_id'];
$category = $_POST['upload_category']; // pdf | image | text
$file_type = "note";
$description = mysqli_real_escape_string($conn, $_POST['description'] ?? '');
$linksRaw = $_POST['links'] ?? '';
if ($chapter_id <= 0 || empty($category)) {
die("Missing required fields");
}
/* ---------------- LINKS ---------------- */
$linksArray = array_filter(array_map('trim', explode("\n", $linksRaw)));
$linksJson = json_encode($linksArray);
/* ---------------- DIRECTORIES ---------------- */
$pdfDir = "../uploads/pdf/";
$imageDir = "../uploads/images/";
$textDir = "../uploads/text/";
if (!is_dir($pdfDir)) mkdir($pdfDir, 0777, true);
if (!is_dir($imageDir)) mkdir($imageDir, 0777, true);
if (!is_dir($textDir)) mkdir($textDir, 0777, true);
/* =================================================
PDF UPLOAD
================================================= */
if ($category === "pdf") {
if (empty($_FILES['pdf_file']['name'])) {
die("PDF file missing");
}
$safeName = time() . "_" . preg_replace("/[^A-Za-z0-9._-]/", "_", $_FILES['pdf_file']['name']);
move_uploaded_file($_FILES['pdf_file']['tmp_name'], $pdfDir . $safeName);
$path = "uploads/pdf/" . $safeName;
mysqli_query($conn, "
INSERT INTO uploads
(chapter_id, file_name, file_path, file_type, description, links)
VALUES
('$chapter_id', '$safeName', '$path', '$file_type', '$description', '$linksJson')
");
}
/* =================================================
IMAGE UPLOAD
================================================= */
elseif ($category === "image") {
if (empty($_FILES['image_file']['name'])) {
die("Image file missing");
}
$safeName = time() . "_" . preg_replace("/[^A-Za-z0-9._-]/", "_", $_FILES['image_file']['name']);
move_uploaded_file($_FILES['image_file']['tmp_name'], $imageDir . $safeName);
$path = "uploads/images/" . $safeName;
mysqli_query($conn, "
INSERT INTO uploads
(chapter_id, file_name, file_path, file_type, description, links)
VALUES
('$chapter_id', '$safeName', '$path', '$file_type', '$description', '$linksJson')
");
}
/* =================================================
TEXT UPLOAD (EDITOR: TEXT + IMAGES)
================================================= */
elseif ($category === "text") {
$content = $_POST['content'] ?? '';
if (trim($content) === "") {
die("No content received");
}
/* Save editor content as .txt (HTML inside, same as components) */
$baseName = "note_" . time();
$textFile = $baseName . ".txt";
$textPath = $textDir . $textFile;
file_put_contents($textPath, $content);
/* Save pasted image if exists */
if (!empty($_POST['pasted_image'])) {
$data = explode(',', $_POST['pasted_image']);
if (count($data) === 2) {
$imgData = base64_decode($data[1]);
file_put_contents($textDir . $baseName . ".png", $imgData);
}
}
$dbPath = "uploads/text/" . $textFile;
mysqli_query($conn, "
INSERT INTO uploads
(chapter_id, file_name, file_path, file_type, description, links)
VALUES
('$chapter_id', '$textFile', '$dbPath', '$file_type', '$description', '$linksJson')
");
}
/* ---------------- DONE ---------------- */
header("Location: upload_notes.php?success=1");
exit;