obsidian-web/index.php

154 lines
3.7 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");
$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 ) ) {
return sendFile($image);
} else {
$error = "Image not found";
}
}
/*
* 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.
*/
$image = relativeToAbsolute( stripslashes( $_GET["thumb"] ), $imagesFolder );
$thumb = relativeToAbsolute( stripslashes( $_GET["thumb"] ), $config["thumbs"]);
if ( isPathValid( $image, $imagesFolder ) ) {
if ( !file_exists( $thumb ) ) {
createThumbnail( $image, $thumb, $config["thumbs"] );
}
// 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!";
}
}
/**
*
* 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.");
$dir = $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");
?>