<?php

// ---
// --- La config globale
// ---
chdir('/opt/TOPISTO/apps');
require_once '/opt/TOPISTO/apps/global/inc/config.php';

// ---
// --- External dependances
// ---
require TOPISTO_PATH.'/ressources/vendor/autoload.php';

// ---
// --- Internal dependances
// ---
require_once APP_PATH.'/blockchain/inc/block.php';

// ---
// --- Par défaut on cherche le dernier block
// ---
$block_hash = '*';

// ---
// --- Le cas échéant, on cherche block passé en argument
// ---
if (isset($_REQUEST['hash'])) $block_hash = $_REQUEST['hash'];

$img = null;

$myarray = glob(DATA_PATH.'/hasard/'.$block_hash.'.png');
if (isset($myarray[0]))
{
        usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );
        $img = imagecreatefrompng($myarray[0]);

        $seconds_to_cache = 7200;
        $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
        header("Expires: $ts");
        header("Pragma: cache");
        header("Cache-Control: max-age=$seconds_to_cache");
        header('Content-Type: image/png');

        imagepng($img);

        imagedestroy($img);

        exit(0);
}

require_once('block_image.php');

?>