diff --git a/lib/zerobin.php b/lib/zerobin.php index 8ab3f95..5a4b6fd 100644 --- a/lib/zerobin.php +++ b/lib/zerobin.php @@ -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 ( diff --git a/lib/zerobin/data.php b/lib/zerobin/data.php index cff586c..7e81143 100644 --- a/lib/zerobin/data.php +++ b/lib/zerobin/data.php @@ -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(); diff --git a/lib/zerobin/db.php b/lib/zerobin/db.php index 0a636bb..2074901 100644 --- a/lib/zerobin/db.php +++ b/lib/zerobin/db.php @@ -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']; + } + } } } diff --git a/tst/bootstrap.php b/tst/bootstrap.php index 5f635cb..07e7d84 100644 --- a/tst/bootstrap.php +++ b/tst/bootstrap.php @@ -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); diff --git a/tst/sjcl.php b/tst/sjcl.php index 033b5db..3c55deb 100644 --- a/tst/sjcl.php +++ b/tst/sjcl.php @@ -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'); diff --git a/tst/zerobin.php b/tst/zerobin.php index 4a11ac7..16fc4b8 100644 --- a/tst/zerobin.php +++ b/tst/zerobin.php @@ -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); + } } /** diff --git a/tst/zerobin/data.php b/tst/zerobin/data.php index 200dca7..e22f4ed 100644 --- a/tst/zerobin/data.php +++ b/tst/zerobin/data.php @@ -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())); + } + } diff --git a/tst/zerobin/db.php b/tst/zerobin/db.php index a6b0d62..0c6b016 100644 --- a/tst/zerobin/db.php +++ b/tst/zerobin/db.php @@ -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 */