function insertChunk($data, $csum)
{
    $received_data_csum = new Csum($data);
    global $l;
    $l->a("Started insertChunk<br>");
    $status = 0;
    $id = null;
    if (!matches($csum, $received_data_csum)) {
        $status = 8;
    } else {
        $db = new FractureDB('futuqiur_ember');
        $potentialDuplicates = $db->getColumnsUH('chunks', 'id', 'md5', $csum->md5);
        foreach ($potentialDuplicates as $potential) {
            $potentialRecord = retrieveChunk($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, 'status' => $status);
                }
            }
        }
        global $compression;
        global $coalVersion;
        $details = array('csum' => $csum->export(), 'compression' => $compression, 'coalVersion' => $coalVersion);
        $prepared_details = base64_encode(serialize($details));
        global $chunkMasterKey;
        $data = bzcompress($prepared_details . '@CoalFragmentMarker@' . $data);
        $prepared_data = mc_encrypt($data, $chunkMasterKey);
        if (mc_decrypt($prepared_data, $chunkMasterKey) != $data) {
            $status = 53;
        }
        $id = $db->addRow('chunks', 'md5', 'UNHEX(\'' . $csum->md5 . '\')');
        $identifierId = substr($id / 1000, 0, 1) / 5;
        $randomInt = substr(rand(0, 10), 0, 1);
        $randomIntAlt = substr(rand(0, 10), 0, 1);
        $identifier = $identifierId . $randomInt . '.COALPROJECT.RECORD33';
        $fallbackid = $identifierId . $randomIntAlt . '.COALPROJECT.RECORD33';
        $address = 'ia:' . $identifier;
        $filename = $id . '.coal4';
        global $iaAuthKey;
        global $iaPrivateKey;
        $upload = @ia_upload($prepared_data, $identifier, $fallbackid, $filename, $iaAuthKey, $iaPrivateKey);
        if ($upload != 0) {
            $status = 54;
            echo 'Failed to add coal. Upload returned status code ' . $upload . '.';
            $db->dropRow('chunks', $id);
        } else {
            $db->setField('chunks', 'address', $address, $id);
        }
        $db->close();
    }
    $l->a("Finished insertChunk with status " . $status . '<br>');
    return array('id' => $id, '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);
}