refactoring how attachments are stored

This commit is contained in:
El RIDO 2015-09-26 12:29:27 +02:00
parent 22b4c89227
commit d04eab52c9
8 changed files with 92 additions and 13 deletions

View File

@ -407,8 +407,8 @@ class zerobin
) return $this->_return_message(1, 'You are unlucky. Try again.');
// Add attachment and its name, if one was sent
if ($has_attachment) $storage['attachment'] = $attachment;
if ($has_attachmentname) $storage['attachmentname'] = $attachmentname;
if ($has_attachment) $storage['meta']['attachment'] = $attachment;
if ($has_attachmentname) $storage['meta']['attachmentname'] = $attachmentname;
// New paste
if (

View File

@ -75,9 +75,20 @@ class zerobin_data extends zerobin_abstract
public function read($pasteid)
{
if(!$this->exists($pasteid)) return false;
return json_decode(
$paste = json_decode(
file_get_contents(self::_dataid2path($pasteid) . $pasteid)
);
if (property_exists($paste->meta, 'attachment'))
{
$paste->attachment = $paste->meta->attachment;
unset($paste->meta->attachment);
if (property_exists($paste->meta, 'attachmentname'))
{
$paste->attachmentname = $paste->meta->attachmentname;
unset($paste->meta->attachmentname);
}
}
return $paste;
}
/**
@ -90,7 +101,7 @@ class zerobin_data extends zerobin_abstract
public function delete($pasteid)
{
// Delete the paste itself.
unlink(self::_dataid2path($pasteid) . $pasteid);
@unlink(self::_dataid2path($pasteid) . $pasteid);
// Delete discussion if it exists.
$discdir = self::_dataid2discussionpath($pasteid);
@ -100,7 +111,7 @@ class zerobin_data extends zerobin_abstract
$dir = dir($discdir);
while (false !== ($filename = $dir->read()))
{
if (is_file($discdir.$filename)) unlink($discdir.$filename);
if (is_file($discdir.$filename)) @unlink($discdir.$filename);
}
$dir->close();

View File

@ -264,6 +264,24 @@ class zerobin_db extends zerobin_abstract
if (
$paste['burnafterreading']
) self::$_cache[$pasteid]->meta->burnafterreading = true;
if (property_exists(self::$_cache[$pasteid]->meta, 'attachment'))
{
self::$_cache[$pasteid]->attachment = self::$_cache[$pasteid]->meta->attachment;
unset(self::$_cache[$pasteid]->meta->attachment);
if (property_exists(self::$_cache[$pasteid]->meta, 'attachmentname'))
{
self::$_cache[$pasteid]->attachmentname = self::$_cache[$pasteid]->meta->attachmentname;
unset(self::$_cache[$pasteid]->meta->attachmentname);
}
}
elseif (array_key_exists('attachment', $paste))
{
self::$_cache[$pasteid]->attachment = $paste['attachment'];
if (array_key_exists('attachmentname', $paste))
{
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
}
}
}
}

View File

@ -24,12 +24,12 @@ class helper
*/
private static $paste = array(
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
'meta' => array(
'postdate' => 1344803344,
'opendiscussion' => true,
'formatter' => 'plaintext',
'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
),
);
@ -70,6 +70,18 @@ class helper
* @return array
*/
public static function getPaste($meta = array())
{
$example = self::getPasteWithAttachment($meta);
unset($example['attachment'], $example['attachmentname']);
return $example;
}
/**
* get example paste
*
* @return array
*/
public static function getPasteWithAttachment($meta = array())
{
$example = self::$paste;
$example['meta'] = array_merge($example['meta'], $meta);

View File

@ -3,10 +3,10 @@ class sjclTest extends PHPUnit_Framework_TestCase
{
public function testSjclValidatorValidatesCorrectly()
{
$paste = helper::getPaste();
$paste = helper::getPasteWithAttachment();
$this->assertTrue(sjcl::isValid($paste['data']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['meta']['attachment']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['meta']['attachmentname']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['attachment']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['attachmentname']), 'valid sjcl');
$this->assertTrue(sjcl::isValid(helper::getComment()['data']), 'valid sjcl');
$this->assertTrue(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'valid sjcl');

View File

@ -311,10 +311,9 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$options['main']['fileupload'] = true;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
$_POST['attachment'] = $_POST['meta']['attachment'];
$_POST['attachmentname'] = $_POST['meta']['attachmentname'];
$_POST = helper::getPasteWithAttachment();
$_SERVER['REMOTE_ADDR'] = '::1';
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
ob_start();
new zerobin;
$content = ob_get_contents();
@ -326,6 +325,11 @@ class zerobinTest extends PHPUnit_Framework_TestCase
'outputs valid delete token'
);
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
$original = json_decode(json_encode($_POST));
$stored = $this->_model->read($response['id']);
foreach (array('data', 'attachment', 'attachmentname') as $key) {
$this->assertEquals($original->$key, $stored->$key);
}
}
/**

View File

@ -20,6 +20,8 @@ class zerobin_dataTest extends PHPUnit_Framework_TestCase
public function testFileBasedDataStoreWorks()
{
$this->_model->delete(helper::getPasteId());
// storing pastes
$paste = helper::getPaste(array('expire_date' => 1344803344));
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
@ -46,4 +48,19 @@ class zerobin_dataTest extends PHPUnit_Framework_TestCase
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
$this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
}
public function testFileBasedAttachmentStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$paste['meta']['attachment'] = $paste['attachment'];
$paste['meta']['attachmentname'] = $paste['attachmentname'];
unset($paste['attachment'], $paste['attachmentname']);
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
$this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
$this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
}
}

View File

@ -18,6 +18,9 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
public function testDatabaseBasedDataStoreWorks()
{
$this->_model->delete(helper::getPasteId());
// storing pastes
$paste = helper::getPaste(array('expire_date' => 1344803344));
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
$this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
@ -44,6 +47,20 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
$this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
}
public function testDatabaseBasedAttachmentStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$paste['meta']['attachment'] = $paste['attachment'];
$paste['meta']['attachmentname'] = $paste['attachmentname'];
unset($paste['attachment'], $paste['attachmentname']);
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
$this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
$this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
}
/**
* @expectedException PDOException
*/