use Prosperia\Stor\StorFromFile;
use Prosperia\Thumbnail;
if (!isset($_REQUEST['token']) || empty($_REQUEST['token'])) {
    header("HTTP/1.1 400 Bad Request");
    echo "<span style=\"color: red; font-weight: bold; font-size: 24pt;\">400 Bad Request</span><br />";
    echo "<br />";
    echo "Your browser sent a request that could not be understood.";
    exit;
}
if (!isset($_REQUEST['size']) || empty($_REQUEST['size'])) {
    $width = null;
} else {
    $width = $_REQUEST['size'];
}
if (file_exists("var/tokn/" . $_REQUEST['token'])) {
    $token = new ToknFile($_REQUEST['token']);
    if (file_exists("var/stor/" . $token->getReference())) {
        $stor = new Stor(new StorFromFile("var/stor/" . $token->getReference()));
        $thumbnail = new Thumbnail($stor, $width);
        header("Content-Disposition: inline");
        header("Content-Type: " . $stor->getType());
        header("Content-Length: " . $thumbnail->size());
        set_time_limit(0);
        print $thumbnail->raw();
        exit;
    } else {
        header("HTTP/1.1 410 Gone");
        echo "<span style=\"color: red; font-weight: bold; font-size: 24pt;\">410 Gone</span><br />";
        echo "<br />";
        echo "The requested URL is no longer available on this server.";
        exit;
<?php

require_once "lib/Prosperia/bootstrap.php";
use Prosperia\Tokn\ToknFile;
$tokns = array();
$tokn_references = array();
$stors = array();
echo "<span style=\"color: darkorange; font-weight: bold;\">" . "Searching for tokens referencing nonexistant storage objects</span>...<br />\n";
$stale_token_count = 0;
$removed_token_count = 0;
foreach (glob("var/tokn/*") as $tokenpath) {
    $tokenpath = str_replace("var/tokn/", null, $tokenpath);
    $token = new ToknFile($tokenpath);
    $tokns[] = $token->getName();
    $tokn_references[] = $token->getReference();
    if (!file_exists("var/stor/" . $token->getReference())) {
        $stale_token_count++;
        echo "<span style=\"color: darkred; font-weight: bold;\">" . "Stale token <i>{$tokenpath}</i> (referenced <i>" . $token->getReference() . "</i>)</span>";
        if (@unlink("var/tokn/" . $tokenpath)) {
            $removed_token_count++;
            echo " <span style=\"color: green; font-weight: bold;\">Deleted.</span><br />\n";
        } else {
            echo " <span style=\"color: red; font-weight: bold;\">Unable to delete.</span><br />\n";
        }
        unset($tokns[array_search($tokenpath, $tokns)]);
        unset($tokn_references[array_search($token->getReference(), $tokn_references)]);
    }
}
echo "<span style=\"color: orange; font-weight: bold;\">Found {$stale_token_count} " . "stale tokens.</span> <span style=\"color: green; font-weight: bold;\">" . "{$removed_token_count} removed.</span><br />\n";
echo "<br />\n";
echo "<span style=\"color: darkorange; font-weight: bold;\">" . "Searching for storage objects not referenced by any tokens</span>...<br />\n";