From 6ab827bf9415a09a8fef7244d528832837aaff8a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 24 Sep 2016 14:43:15 +0200 Subject: [PATCH] Ciphered GIST cloning --- src/Gist/Resources/views/Edit/clone.html.twig | 19 +++- src/Gist/Resources/views/View/embed.html.twig | 27 +++--- .../Resources/views/View/revisions.html.twig | 90 +++++++++---------- src/Gist/Resources/views/View/view.html.twig | 52 +++++------ src/Gist/Resources/views/base.html.twig | 4 +- web/app/js/app.js | 55 ++++++++---- 6 files changed, 143 insertions(+), 104 deletions(-) diff --git a/src/Gist/Resources/views/Edit/clone.html.twig b/src/Gist/Resources/views/Edit/clone.html.twig index b4f5683..eb50e12 100644 --- a/src/Gist/Resources/views/Edit/clone.html.twig +++ b/src/Gist/Resources/views/Edit/clone.html.twig @@ -17,7 +17,14 @@

{{ form_errors(form.content) }} - {{ form_widget(form.content) }} + + {% set class = 'form-control' %} + + {% if gist.cipher %} + {% set class = class ~ ' cipher-editor' %} + {% endif %} + + {{ form_widget(form.content, {attr: {class: class}}) }}

@@ -29,3 +36,13 @@ {% endblock %} + +{% block js %} + {% if gist.cipher %} + + {% endif %} + + {{ parent() }} +{% endblock %} diff --git a/src/Gist/Resources/views/View/embed.html.twig b/src/Gist/Resources/views/View/embed.html.twig index c7f65a2..27e3056 100644 --- a/src/Gist/Resources/views/View/embed.html.twig +++ b/src/Gist/Resources/views/View/embed.html.twig @@ -50,14 +50,14 @@

diff --git a/src/Gist/Resources/views/View/revisions.html.twig b/src/Gist/Resources/views/View/revisions.html.twig index 1040117..7ba0b47 100644 --- a/src/Gist/Resources/views/View/revisions.html.twig +++ b/src/Gist/Resources/views/View/revisions.html.twig @@ -11,21 +11,19 @@ {% block body %}
- {% if not gist.cipher %} - - {% endif %} +
@@ -33,42 +31,40 @@
- {% if not gist.cipher %} -
- {% for commit in history %} -
-

- - {{ commit.commit|slice(0, 10) }} +

+ {% for commit in history %} +
+

+ + {{ commit.commit|slice(0, 10) }} + + + {% if loop.first %}init{% endif %} + + {% if not loop.first %} + + diff + {% endif %} +

+

+ {{ commit.date|date('date.format'|trans) }} +

+
+ {% if not loop.first %} - {% if loop.first %}init{% endif %} - - {% if not loop.first %} - - diff - - {% endif %} -

-

- {{ commit.date|date('date.format'|trans) }} -

-
- {% if not loop.first %} - -
- {{ commit.diff|raw }} -
- {% endif %} -
+
+ {{ commit.diff|raw }} +
+ {% endif %}
+
- {% if not loop.last %} -
- {% endif %} - {% endfor %} -
- {% endif %} + {% if not loop.last %} +
+ {% endif %} + {% endfor %} +
diff --git a/src/Gist/Resources/views/View/view.html.twig b/src/Gist/Resources/views/View/view.html.twig index 0c4ab0e..518a0ed 100644 --- a/src/Gist/Resources/views/View/view.html.twig +++ b/src/Gist/Resources/views/View/view.html.twig @@ -16,29 +16,28 @@ {% block body %}
- {% if not gist.cipher %} - - {% endif %} + +
- {% if not gist.cipher %} -
- - {{ commit|slice(0, 10) }} - + - {% endif %} + {% endif %} + + + + {{ 'gist.action.clone'|trans }} + +
{{ gist.title ? gist.title : 'gist.untitled'|trans }} diff --git a/src/Gist/Resources/views/base.html.twig b/src/Gist/Resources/views/base.html.twig index 6bacb7e..2070a5b 100644 --- a/src/Gist/Resources/views/base.html.twig +++ b/src/Gist/Resources/views/base.html.twig @@ -71,10 +71,10 @@ {% block langs %} diff --git a/web/app/js/app.js b/web/app/js/app.js index fe2a6eb..59c691c 100644 --- a/web/app/js/app.js +++ b/web/app/js/app.js @@ -94,8 +94,15 @@ var mainEditorEvents = function() { }); $('#main-form').submit(function(e) { - if ($('.cipher-input:checked').val() === 'yes') { - var passphrase = randomString(256, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + if ($('.cipher-input:checked').val() === 'yes' || typeof cipherGistClone !== 'undefined') { + var key = getKey(); + + if (key) { + var passphrase = key; + } else { + var passphrase = randomString(256, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + } + var content = $('#form_content').val(); var encrypted = CryptoJS.AES.encrypt(content, passphrase, { format: JsonFormatter @@ -123,25 +130,43 @@ var viewerEvents = function() { $(document).ready(function() { var key = getKey(); + + var $cipherEditor = $('.cipher-editor'); var $embedInput = $('#embed-input'); - var to = ' '; - if (0 !== $render.length && key) { - var decrypted = CryptoJS.AES.decrypt($render.html(), key, { - format: JsonFormatter + if (key) { + $('.cipher-link').each(function() { + var href = $(this).attr('href'); + href = href + '#key=' + key; + + $(this).attr('href', href); }); - $render.text(decrypted.toString(CryptoJS.enc.Utf8)); - SyntaxHighlighter.all(); - to = ' data-key="#key=' + key + '" '; + var to = ' '; - $('.lang').each(function() { - $(this).attr('href', $(this).attr('href') + '#key=' + key); - }); - } + if (0 !== $render.length || $cipherEditor.length !== 0) { - if ($embedInput.length) { - $embedInput.val($embedInput.val().replace('%key%', to)); + if ($render.length !== 0) { + var decrypted = CryptoJS.AES.decrypt($render.html(), key, { + format: JsonFormatter + }); + + $render.text(decrypted.toString(CryptoJS.enc.Utf8)); + SyntaxHighlighter.all(); + + to = ' data-key="#key=' + key + '" '; + } else { + var decrypted = CryptoJS.AES.decrypt($cipherEditor.val(), key, { + format: JsonFormatter + }); + + $cipherEditor.val(decrypted.toString(CryptoJS.enc.Utf8)); + } + } + + if ($embedInput.length) { + $embedInput.val($embedInput.val().replace('%key%', to)); + } } }); }