obsidian-web/index.php

159 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/*
* Load the config.
* This provides the directories for images and
* thumbnails, as well as settings, galleries
* and albums.
*/
require_once("backend/config.php");
/*
*
* Load in all the functions we'll be using..
*/
require_once("backend/functions.php");
2020-10-08 06:04:46 +00:00
$error = "";
$imagesFolder = $config["images"];
/*
* If something outside our control has added
* new images to the database, it should have also
* added an entry to the appropriate new[x].txt file.
*
* If it exists, we can read in the new values from there.
* If it doesn't, there's nothing interesting to do, and
* we just carry on init like normal.
*
*/
if( file_exists($config["thumbs"]."/newImages.txt")) {
$newImages = file($config["thumbs"]."/newImages.txt");
} else {
$newImages = array();
}
/*
* Images are retrieved from the database using GET
* (unlike 90% of my projects where everything is POST)
*
* Given that, we need to check if there are any requests
* in the headers and handle the appropriately.
*
* This effectively short-circuits the logic to pass single
* images.
*
* TODO: this could be more efficient!
*
*/
if ( isset( $_GET["image"] ) ) {
$image = relativeToAbsolute( stripslashes( $_GET["image"] ), $imagesFolder );
if ( isPathValid( $image, $imagesFolder ) ) {
2020-10-08 06:04:46 +00:00
console_log("Found valid image at " . $imagesFolder . $image);
return sendFile($image);
} else {
$error = "Image not found";
2020-10-08 06:04:46 +00:00
console_log($error);
}
}
/*
* There is similar logic for the thumbnails.
* Theoretically this should come before full images,
* but that breaks things in testing.
*
* TODO: optimise!
*/
if ( isset( $_GET["thumb"] ) ) {
/*
* There needs to be a fallback for when an image doesn't have a generated thumbnail.
* This will prepare both the image and the thumbnail, in preparation
* for needing to send the full image, in the case that there is no thumbnail, and
* the attempt to generate the thumbnail fails.
*
* We can only spend so much time processing a tiny image, after all.
*/
2020-10-08 06:04:46 +00:00
$image = relativeToAbsolute( stripslashes( $_GET["thumb"] ), $config["images"]);
$image = str_replace("\/", "//", $image);
$thumb = relativeToAbsolute( stripslashes( $_GET["thumb"] ), $config["thumbs"]);
2020-10-08 06:04:46 +00:00
$thumb = str_replace("\/", "//", $thumb);
2020-10-08 06:04:46 +00:00
if ( isPathValid( $image, $config["images"] ) ) {
console_log("Image is valid");
if ( !file_exists( $thumb ) ) {
createThumbnail( $image, $thumb, $config["thumbs"] );
}
2020-10-08 06:04:46 +00:00
// Generation can fail! We need to double check else risk a 400
if ( file_exists( $thumb ) ) {
return sendFile($thumb);
} else {
return sendFile($image);
}
} else {
$error = "Image not found!";
2020-10-08 06:04:46 +00:00
console_log($error);
}
}
/**
*
* We need to be able to handle custom paths as well.
* TODO: Implement this!
*/
if ( isset( $_GET["category"] ) ) {
console_log("Loading category " . $_GET["category"] );
$tempDir = relativeToAbsolute( stripslashes( $_GET["category"] ), $imagesFolder );
console_log("Directory for category is " . $tempDir);
if ( isPathValid( $tempDir, $imagesFolder ) ) {
console_log("Directory is valid.");
2020-10-08 06:04:46 +00:00
$imagesFolder = $tempDir;
} else {
console_log("Directory is invalid");
$error = "Invalid category";
}
}
/**
*
* Now that we've handled all the shortcircuit logic to handle
* sending images, we can start showing a UI.
*
* First, we handle <head>:
*/
require_once("templates/header.php");
/**
* Then we show the menu at the top.
* This will contain breadcrumbs to show you what's going on.
*/
require_once("templates/menu.php");
/**
* Then we can start showing content.
*
* As it stands, this is where most of the work is going.
* Who would've thought?
*/
require_once("templates/content.php");
require_once("templates/footer.php");
?>