function processPasteFetch($pasteid) { if (preg_match('/\\A[a-f\\d]{16}\\z/', $pasteid)) { // Is this a valid paste identifier ? $filename = dataid2path($pasteid) . $pasteid; if (!is_file($filename)) { // Check that paste exists. return array('', 'Paste does not exist!', ''); } } else { return array('', 'Invalid data!', ''); } // Get the paste itself. $paste = json_decode(file_get_contents($filename)); $messages = array($paste); // The paste itself is the first in the list of encrypted messages. $CIPHERDATA = json_encode($messages); return array($CIPHERDATA, '', ''); }
function processPasteFetch($pasteid) { if (preg_match('/\\A[a-f\\d]{16}\\z/', $pasteid)) { $filename = dataid2path($pasteid) . $pasteid; if (!is_file($filename)) { return array('', 'Paste does not exist, has expired or has been deleted.', ''); } } // Get the paste itself. $paste = json_decode(file_get_contents($filename)); // See if paste has expired. if (isset($paste->meta->expire_date) && $paste->meta->expire_date < time()) { deletePaste($pasteid); // Delete the paste return array('', 'Paste does not exist, has expired or has been deleted.', ''); } // We kindly provide the remaining time before expiration (in seconds) if (property_exists($paste->meta, 'expire_date')) { $paste->meta->remaining_time = $paste->meta->expire_date - time(); } $messages = array($paste); // The paste itself is the first in the list of encrypted messages. // If it's a discussion, get all comments. if (property_exists($paste->meta, 'opendiscussion') && $paste->meta->opendiscussion) { $comments = array(); $datadir = dataid2discussionpath($pasteid); if (!is_dir($datadir)) { mkdir($datadir, $mode = 0705, $recursive = true); } $dhandle = opendir($datadir); while (false !== ($filename = readdir($dhandle))) { if (is_file($datadir . $filename)) { $comment = json_decode(file_get_contents($datadir . $filename)); // Filename is in the form pasteid.commentid.parentid: // - pasteid is the paste this reply belongs to. // - commentid is the comment identifier itself. // - parentid is the comment this comment replies to (It can be pasteid) $items = explode('.', $filename); $comment->meta->commentid = $items[1]; // Add some meta information not contained in file. $comment->meta->parentid = $items[2]; $comments[$comment->meta->postdate] = $comment; // Store in table } } closedir($dhandle); ksort($comments); // Sort comments by date, oldest first. $messages = array_merge($messages, $comments); } $CIPHERDATA = json_encode($messages); // If the paste was meant to be read only once, delete it. if (property_exists($paste->meta, 'burnafterreading') && $paste->meta->burnafterreading) { deletePaste($pasteid); } return array($CIPHERDATA, '', ''); }
} // Make sure format is correct. if (!validSJCL($data)) { echo json_encode(array('status' => 1, 'message' => 'Invalid data.')); exit; } // Read additional meta-information. $meta = array(); if ($error) { echo json_encode(array('status' => 1, 'message' => 'Invalid data.')); exit; } // We just want a small hash to avoid collisions: Half-MD5 (64 bits) will do the trick. $dataid = substr(hash('md5', $data), 0, 16); $storage = array('data' => $data); $storagedir = dataid2path($dataid); if (!is_dir($storagedir)) { mkdir($storagedir, $mode = 0705, $recursive = true); } if (is_file($storagedir . $dataid)) { // Oups... improbable collision. echo json_encode(array('status' => 1, 'message' => 'Infite improbability drive activated! Try again, please.')); exit; } // New paste file_put_contents($storagedir . $dataid, json_encode($storage), LOCK_EX); // Generate the "delete" token. // The token is the hmac of the pasteid signed with the server salt. // The paste can be delete by calling http://myserver.com/zerobin/?pasteid=<pasteid>&deletetoken=<deletetoken> $deletetoken = hash_hmac('sha1', $dataid, getServerSalt()); echo json_encode(array('status' => 0, 'id' => $dataid, 'deletetoken' => $deletetoken));
function deletePaste($pasteid) { // Delete the paste itself unlink(dataid2path($pasteid) . $pasteid); // Delete discussion if it exists. $discdir = dataid2discussionpath($pasteid); if (is_dir($discdir)) { // Delete all files in discussion directory $dhandle = opendir($discdir); while (false !== ($filename = readdir($dhandle))) { if (is_file($discdir . $filename)) { unlink($discdir . $filename); } } closedir($dhandle); // Delete the discussion directory. rmdir($discdir); } }