<?php
	$root_subdirectory = stripos($_SERVER['SERVER_NAME'], 'localhost') !== false ? '/absoluteanime.com/public_html' : '';
	include(($phpPath = ($_SERVER['DOCUMENT_ROOT'] . $root_subdirectory . '/!bin/')) . 'page-init.php');

	//$transparency	= 15; // 100: Fully Opaque, 0: Fully Transparent
	//$watermark 		= '/!bin/watermark.png';
	$src	= $aa->get_var('src', '');
	$title	= $aa->get_var('title', '');

	// No src query var, this must be a direct image passed via .htaccess
	if (empty($src)) {
		$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: $_SERVER['REQUEST_URI'];
		$path = $_SERVER['DOCUMENT_ROOT'] . $uri;
		$src = rawurldecode($path);
	}

	if (!empty($src)) {
		// @todo: Figure out if this image file contains adult content and if user has not elected to view such content... get the path and look for files.info
		//include_once $aa->hddRoot . '.data/fileexplorer-data.inc.php';
		//$pathInfo = PrimeFile::pathInfo($src);
		//$localDirectory = $pathInfo['dirname'];
		//if (!empty($files[ $pathInfo['dirname']])) {
		//	AASitemap::parseFileAddr(
		//}

		$imageResult = AAImage::getThumb($src);	// this handles caching
		$imageAddr = $src;
		if (!empty($imageResult['thumbFile'])) {
			$imageAddr = PrimeFile::joinPaths($aa->hddRoot, $imageResult['thumbFile']);
			/*
			if (file_exists($thumbAddr)) {
				$size = getimagesize($thumbAddr);

				//if (strpos($src, '!!test') !== false) {
				//	var_dump($src, $imageResult); exit;
				//}

				header('Content-Type: ' . $size['mime']);
				header('Content-Length: ' . filesize($thumbAddr));
				readfile($thumbAddr);
				exit;
			}
			*/
		}
		// Output the file to the browser if it exists
		if (file_exists($imageAddr)) {
			$size = getimagesize($imageAddr);
			header('Content-Type: ' . ($size['mime'] ?? mime_content_type($imageAddr)));
			header('Content-Length: ' . filesize($imageAddr));
			readfile($imageAddr);
			exit;
		}
	}
	//if (strpos($src, '!!test') !== false) {
	//	var_dump($src, $imageResult); exit;
	//}

	// Output 1x1 transparent GIF so at least we're outputting some image
	// https://stackoverflow.com/a/26556113/1409830
	header('Content-Type: image/gif');
	die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x90\x00\x00\xff\x00\x00\x00\x00\x00\x21\xf9\x04\x05\x10\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x04\x01\x00\x3b");
