function DataRetrieveHandler()
{
    global $l;
    $l = new llog();
    global $generalAuthKey;
    if (authorized($generalAuthKey)) {
        $l->a("Started DataRetrieveHandler<br>");
        $status = 0;
        $insertion = retrieve($_REQUEST['id']);
        //print_r($insertion);
        $status = $insertion['status'];
        $csum = Csum_import($insertion['csum']);
        if (check($status, true)) {
            echo $csum->len . '|' . $csum->md5 . '|' . $csum->sha . '|' . $csum->s512 . '|' . $insertion['data'];
        }
    }
}
function CoalIntakeHandler()
{
    global $l;
    $l = new llog();
    $l->a("Started CoalIntakeHandler<br>");
    global $generalAuthKey;
    if (authorized($generalAuthKey)) {
        global $coalVersion;
        $coalVersion = 5;
        global $coalCompressionType;
        $coalCompressionType = 'bz2';
        $insertion = insertCoal();
        //echo 'INSERTION: '. print_r($insertion,true);
        $id = $insertion['id'];
        $details = $insertion['details'];
        $status = $insertion['status'];
        if (check($status, true)) {
            if (isset($_REQUEST['outputwebloc'])) {
                $filename = base64_decode($details['filename']);
                $smallified = "[InternetShortcut]\nURL=http://futuramerlin.com/d/r/active.php?coalId=" . $id . "&authorizationKey=" . urlencode($generalAuthKey) . "&handler=1&coalVerbose=1&handlerNeeded=CoalRetrieve\nIconIndex=0";
                start_file_download($filename, strlen($smallified));
                echo $smallified;
            } else {
                if (check($status, true)) {
                    if (isset($_REQUEST['coalVerbose'])) {
                        echo 'Added coal: ';
                    }
                    $csum = Csum_import($details['csum']);
                    //echo 'CSUM: '.print_r($csum,true);
                    echo $id . '|' . $csum->len . '|' . $csum->md5 . '|' . $csum->sha . '|' . $csum->s512;
                    if (isset($_REQUEST['coalVerbose'])) {
                        echo '; used ' . memory_get_peak_usage() . ' bytes of memory at peak; currently using ' . memory_get_usage() . ' bytes of memory.<br><h1>Log output:</h1><br><small>';
                        $l->e();
                    }
                }
            }
        }
    }
}
function insertCoal($file = null, $csump = null)
{
    $db = new FractureDB('futuqiur_ember');
    global $l;
    $l->a("Started insertCoal<br>");
    $status = 0;
    $id = null;
    if (is_null($file)) {
        $coal = coalFromUpload();
        $details = $coal['details'];
    } else {
        $coal = coalFromFile($file, $csump);
        $details = $coal['details'];
    }
    $status = status_add($status, $coal['status']);
    global $coalCompressionType;
    $details['compression'] = $coalCompressionType;
    global $coalVersion;
    $details['coalVersion'] = $coalVersion;
    $detailsCsum = Csum_import($details['csum']);
    if ($detailsCsum->len == 0) {
        $details['blocks'] = '';
    }
    $compressed = serialize($details);
    $c = new Csum($compressed);
    $chunkInfo = insertChunk($compressed, $c);
    $status = status_add($status, $chunkInfo['status']);
    if (check($status, true)) {
        $chunkId = $chunkInfo['id'];
        $id = $db->addRow('strings', 'chunk, md5', '\'' . $chunkId . '\', UNHEX(\'' . $detailsCsum->md5 . '\')');
        //sleep(6);
        if (!checkCoal($id)) {
            $status = 45;
        }
    }
    unlink($file);
    $db->close();
    return array('id' => $id, 'csum' => $details['csum'], 'filename' => $details['filename'], 'status' => $status);
}
function store($data, $csumb)
{
    $csum = new Csum($data);
    if (!$csum->matches($csumb)) {
        return null;
    }
    $status = 0;
    //Why I'm not doing this type of deduplication: It could lead to inaccurate metadata about the coal.
    //Ya know, screw that. Coal *shouldn't support* file uploads — that should be handled by higher level software. I'm putting this back in for now, and just remember that the Coal file-level metadata is only an ugly, non-archival-quality, incomplete hack for while Ember doesn't exist yet to take care of that.
    $db = new FractureDB('futuqiur_ember');
    $potentialDuplicates = $db->getColumnsUH('strings', 'id', 'md5', $csum->md5);
    foreach ($potentialDuplicates as $potential) {
        //echo 'duplicate testing';
        $potentialRecord = retrieveCoal($potential['id']);
        if (!is_null($potentialRecord)) {
            $potentialData = $potentialRecord['data'];
            $potentialCsum = Csum_import($potentialRecord['csum']);
            if ($potentialData === $data && matches($csum, $potentialCsum)) {
                $duplicateId = $potential['id'];
                return array('id' => $duplicateId, 'csum' => $potentialRecord['csum'], 'status' => $status);
            }
        }
    }
    $db->close();
    //echo 'gotten here';
    $filename = 'coal_temp/' . uniqid() . '.cstf';
    file_put_contents($filename, $data);
    return insertCoal($filename, $csum);
}