From f21567133cbd7b7752c6d403f78f8db0ba56bcd3 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 18 Oct 2015 11:08:28 +0200 Subject: [PATCH] changing paste read output for API refactoring --- js/zerobin.js | 42 +++++++++++++++++++++--------------------- lib/model/paste.php | 4 ++++ lib/zerobin.php | 6 ++---- lib/zerobin/db.php | 2 +- tst/bootstrap.php | 17 +++++++++++++++++ tst/jsonApi.php | 14 ++++++++++++-- tst/zerobin.php | 25 +++++++++++++++++-------- 7 files changed, 74 insertions(+), 36 deletions(-) diff --git a/js/zerobin.js b/js/zerobin.js index 492e8a2..39c8072 100644 --- a/js/zerobin.js +++ b/js/zerobin.js @@ -592,28 +592,28 @@ $(function() { * Show decrypted text in the display area, including discussion (if open) * * @param string key : decryption key - * @param array comments : Array of messages to display (items = array with keys ('data','meta') + * @param object paste : paste object including comments to display (items = array with keys ('data','meta') */ - displayMessages: function(key, comments) + displayMessages: function(key, paste) { // Try to decrypt the paste. var password = this.passwordInput.val(); if (!this.prettyPrint.hasClass('prettyprinted')) { try { - if (comments[0].attachment) + if (paste.attachment) { - var attachment = filter.decipher(key, password, comments[0].attachment); + var attachment = filter.decipher(key, password, paste.attachment); if (attachment.length == 0) { if (password.length == 0) password = this.requestPassword(); - attachment = filter.decipher(key, password, comments[0].attachment); + attachment = filter.decipher(key, password, paste.attachment); } if (attachment.length == 0) throw 'failed to decipher attachment'; - if (comments[0].attachmentname) + if (paste.attachmentname) { - var attachmentname = filter.decipher(key, password, comments[0].attachmentname); + var attachmentname = filter.decipher(key, password, paste.attachmentname); if (attachmentname.length > 0) this.attachmentLink.attr('download', attachmentname); } this.attachmentLink.attr('href', attachment); @@ -631,20 +631,20 @@ $(function() { this.image.removeClass('hidden'); } } - var cleartext = filter.decipher(key, password, comments[0].data); - if (cleartext.length == 0 && password.length == 0 && !comments[0].attachment) + var cleartext = filter.decipher(key, password, paste.data); + if (cleartext.length == 0 && password.length == 0 && !paste.attachment) { password = this.requestPassword(); - cleartext = filter.decipher(key, password, comments[0].data); + cleartext = filter.decipher(key, password, paste.data); } - if (cleartext.length == 0 && !comments[0].attachment) throw 'failed to decipher message'; + if (cleartext.length == 0 && !paste.attachment) throw 'failed to decipher message'; this.passwordInput.val(password); if (cleartext.length > 0) { helper.setElementText(this.clearText, cleartext); helper.setElementText(this.prettyPrint, cleartext); - this.formatPaste(comments[0].meta.formatter); + this.formatPaste(paste.meta.formatter); } } catch(err) @@ -658,9 +658,9 @@ $(function() { } // Display paste expiration / for your eyes only. - if (comments[0].meta.expire_date) + if (paste.meta.expire_date) { - var expiration = helper.secondsToHuman(comments[0].meta.remaining_time), + var expiration = helper.secondsToHuman(paste.meta.remaining_time), expirationLabel = [ 'This document will expire in %d ' + expiration[1] + '.', 'This document will expire in %d ' + expiration[1] + 's.' @@ -669,7 +669,7 @@ $(function() { this.remainingTime.removeClass('foryoureyesonly') .removeClass('hidden'); } - if (comments[0].meta.burnafterreading) + if (paste.meta.burnafterreading) { // unfortunately many web servers don't support DELETE (and PUT) out of the box $.ajax({ @@ -692,15 +692,15 @@ $(function() { } // If the discussion is opened on this paste, display it. - if (comments[0].meta.opendiscussion) + if (paste.meta.opendiscussion) { this.comments.html(''); // iterate over comments - for (var i = 1; i < comments.length; i++) + for (var i = 0; i < paste.comments.length; ++i) { var place = this.comments; - var comment=comments[i]; + var comment = paste.comments[i]; var cleartext = '[' + i18n._('Could not decrypt comment; Wrong key?') + ']'; try { @@ -837,7 +837,7 @@ $(function() { { if (data.status == 0) { - zerobin.displayMessages(zerobin.pageKey(), data.messages); + zerobin.displayMessages(zerobin.pageKey(), data); } else if (data.status == 1) { @@ -1293,12 +1293,12 @@ $(function() { } // List of messages to display. - var messages = $.parseJSON(this.cipherData.text()); + var data = $.parseJSON(this.cipherData.text()); // Show proper elements on screen. this.stateExistingPaste(); - this.displayMessages(this.pageKey(), messages); + this.displayMessages(this.pageKey(), data); } // Display error message from php code. else if (this.errorMessage.text().length > 1) diff --git a/lib/model/paste.php b/lib/model/paste.php index 5159c90..0fc4cb2 100644 --- a/lib/model/paste.php +++ b/lib/model/paste.php @@ -52,6 +52,10 @@ class model_paste extends model_abstract $this->_data->meta->formatter = $this->_conf->getKey('defaultformatter'); } } + $this->_data->comments = array_values($this->getComments()); + $this->_data->comment_count = count($this->_data->comments); + $this->_data->comment_offset = 0; + $this->_data->{'@context'} = 'js/paste.jsonld'; return $this->_data; } diff --git a/lib/zerobin.php b/lib/zerobin.php index 515a57e..bcf4b56 100644 --- a/lib/zerobin.php +++ b/lib/zerobin.php @@ -339,9 +339,7 @@ class zerobin $paste = $this->_model->getPaste($dataid); if ($paste->exists()) { - // The paste itself is the first in the list of encrypted messages. - $messages = array_merge(array($paste->get()), $paste->getComments()); - $this->_data = json_encode($messages); + $this->_data = json_encode($paste->get()); } else { @@ -359,7 +357,7 @@ class zerobin } else { - $this->_return_message(0, $dataid, array('messages' => $messages)); + $this->_return_message(0, $dataid, json_decode($this->_data, true)); } } } diff --git a/lib/zerobin/db.php b/lib/zerobin/db.php index 1e0695a..799ba8d 100644 --- a/lib/zerobin/db.php +++ b/lib/zerobin/db.php @@ -384,7 +384,7 @@ class zerobin_db extends zerobin_abstract $comments[$i]->meta->nickname = $row['nickname']; if (array_key_exists('vizhash', $row)) $comments[$i]->meta->vizhash = $row['vizhash']; - $comments[$i]->meta->postdate = $i; + $comments[$i]->meta->postdate = (int) $row['postdate']; $comments[$i]->meta->commentid = $row['dataid']; $comments[$i]->meta->parentid = $row['parentid']; } diff --git a/tst/bootstrap.php b/tst/bootstrap.php index ba73cee..117c5dd 100644 --- a/tst/bootstrap.php +++ b/tst/bootstrap.php @@ -88,6 +88,23 @@ class helper return $example; } + /** + * get example paste + * + * @return array + */ + public static function getPasteAsJson($meta = array()) + { + $example = self::getPaste(); + if (count($meta)) + $example['meta'] = $meta; + $example['comments'] = array(); + $example['comment_count'] = 0; + $example['comment_offset'] = 0; + $example['@context'] = 'js/paste.jsonld'; + return json_encode($example); + } + /** * get example paste ID * diff --git a/tst/jsonApi.php b/tst/jsonApi.php index 102db85..6509448 100644 --- a/tst/jsonApi.php +++ b/tst/jsonApi.php @@ -142,7 +142,8 @@ class jsonApiTest extends PHPUnit_Framework_TestCase public function testRead() { $this->reset(); - $this->_model->create(helper::getPasteId(), helper::getPaste()); + $paste = helper::getPasteWithAttachment(); + $this->_model->create(helper::getPasteId(), $paste); $_SERVER['QUERY_STRING'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; ob_start(); @@ -150,7 +151,16 @@ class jsonApiTest extends PHPUnit_Framework_TestCase $content = ob_get_contents(); $response = json_decode($content, true); $this->assertEquals(0, $response['status'], 'outputs success status'); - $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly'); + $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly'); + $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste'); + $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly'); + $this->assertEquals($paste['attachment'], $response['attachment'], 'outputs attachment correctly'); + $this->assertEquals($paste['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly'); + $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly'); + $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly'); + $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly'); + $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly'); + $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly'); } } \ No newline at end of file diff --git a/tst/zerobin.php b/tst/zerobin.php index 6844535..99ef3f0 100644 --- a/tst/zerobin.php +++ b/tst/zerobin.php @@ -587,7 +587,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $this->assertTag( array( 'id' => 'cipherdata', - 'content' => htmlspecialchars(json_encode(helper::getPaste()), ENT_NOQUOTES) + 'content' => htmlspecialchars(helper::getPasteAsJson(), ENT_NOQUOTES) ), $content, 'outputs data correctly' @@ -671,7 +671,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $this->assertTag( array( 'id' => 'cipherdata', - 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES) + 'content' => htmlspecialchars(helper::getPasteAsJson($burnPaste['meta']), ENT_NOQUOTES) ), $content, 'outputs data correctly' @@ -684,7 +684,8 @@ class zerobinTest extends PHPUnit_Framework_TestCase public function testReadJson() { $this->reset(); - $this->_model->create(helper::getPasteId(), helper::getPaste()); + $paste = helper::getPaste(); + $this->_model->create(helper::getPasteId(), $paste); $_SERVER['QUERY_STRING'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; ob_start(); @@ -692,7 +693,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase $content = ob_get_contents(); $response = json_decode($content, true); $this->assertEquals(0, $response['status'], 'outputs success status'); - $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly'); + $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly'); + $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste'); + $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly'); + $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly'); + $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly'); + $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly'); + $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly'); + $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly'); } /** @@ -717,21 +725,22 @@ class zerobinTest extends PHPUnit_Framework_TestCase { $this->reset(); $oldPaste = helper::getPaste(); - $oldPaste['meta'] = array( + $meta = array( 'syntaxcoloring' => true, 'postdate' => $oldPaste['meta']['postdate'], 'opendiscussion' => $oldPaste['meta']['opendiscussion'], ); + $oldPaste['meta'] = $meta; $this->_model->create(helper::getPasteId(), $oldPaste); $_SERVER['QUERY_STRING'] = helper::getPasteId(); ob_start(); new zerobin; $content = ob_get_contents(); - $oldPaste['meta']['formatter'] = 'syntaxhighlighting'; + $meta['formatter'] = 'syntaxhighlighting'; $this->assertTag( array( 'id' => 'cipherdata', - 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES) + 'content' => htmlspecialchars(helper::getPasteAsJson($meta), ENT_NOQUOTES) ), $content, 'outputs data correctly' @@ -755,7 +764,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $this->assertTag( array( 'id' => 'cipherdata', - 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES) + 'content' => htmlspecialchars(helper::getPasteAsJson($oldPaste['meta']), ENT_NOQUOTES) ), $content, 'outputs data correctly'