aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs/themes/bootstrap/js/stikked.js
blob: 19c57be51dfe295efa99fe5a2bbcbda2de9b8c9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var ST = window.ST || {};

ST.show_embed = function () {
    $embed_field = $('#embed_field');
    var lang_showcode = $embed_field.data('lang-showcode');
    $embed_field.hide();
    $embed_field.after('<a id="show_code" href="#">' + lang_showcode + '</a>');
    $('#show_code').on('click', function (e) {
        e.preventDefault();
        $(this).hide();
        $embed_field.show().select();
    });
    $embed_field.on("blur", function () {
        $(this).hide();
        $('#show_code').show();
    });
};

ST.spamadmin = function () {
    if ($('.content h1').text() == 'Spamadmin') {
        $('.content .hidden').show();
        $('.content .quick_remove').live('click', function (ev) {
            var ip = $(ev.target).data('ip');
            if (confirm('Delete all pastes belonging to ' + ip + '?')) {
                $.post(base_url + 'spamadmin/' + ip, {
                    'confirm_remove': 'yes',
                    'block_ip': 1
                }, function () {
                    window.location.reload();
                });
            }
            return false;
        });
    }

    // needed by .selectable
    $.fn.addBack = function (selector) {
        return this.add(selector == null ? this.prevObject : this.prevObject.filter(selector));
    }

    $('.selectable>tbody').selectable({
        filter: 'tr',
        cancel: 'a',
        stop: function () {
            var $deletestack = $(".paste_deletestack");
            var $input = $("input[name=pastes_to_delete]");
            $('.inv').show();
            $deletestack.empty();
            $input.empty();
            var res = [];
            $(".ui-selected").each(function (i, el) {
                var id = $('a', el).attr('href').split('view/')[1];
                res.push(id);
            });
            $deletestack.text(res.join(' '));
            $input.val(res.join(' '));
        }
    });
};

ST.line_highlighter = function () {
    var org_href = window.location.href.replace(/(.*?)#(.*)/, '$1');
    var first_line = false;
    var second_line = false;

    $('blockquote').on('mousedown', function (ev) {
        if (ev.which == 1) { // left mouse button has been clicked
            window.getSelection().removeAllRanges();
        }
    });

    $('blockquote').on('click', 'li', function (ev) {
        var $this = $(this);
        var li_num = ($this.index() + 1);
        if (ev.shiftKey == 1) {
            second_line = li_num;
        } else {
            first_line = li_num;
            second_line = false;
        }

        if (second_line) {
            // determine mark
            if (first_line < second_line) {
                sel_start = first_line;
                sel_end = second_line;
            } else {
                sel_start = second_line;
                sel_end = first_line;
            }
            window.location.href = org_href + '#L' + sel_start + '-L' + sel_end;
        } else {
            window.location.href = org_href + '#L' + first_line;
        }

        ST.highlight_lines();
    });

    ST.highlight_lines();

}

ST.highlight_lines = function () {
    var wloc = window.location.href;
    if (wloc.indexOf('#') > -1) {
        $('.container .CodeMirror li').css('background', 'none');

        var lines = wloc.split('#')[1];
        if (lines.indexOf('-') > -1) {
            var start_line = parseInt(lines.split('-')[0].replace('L', ''), 10);
            var end_line = parseInt(lines.split('-')[1].replace('L', ''), 10);
            for (var i = start_line; i <= end_line; i++) {
                $('.container .CodeMirror li:nth-child(' + i + ')').css('background', '#F8EEC7');
            }
        } else {
            var re = new RegExp('^L[0-9].*?$');
            var r = lines.match(re);
            if (r) {
                var marked_line = lines.replace('L', '');
                $('.container .CodeMirror li:nth-child(' + marked_line + ')').css('background', '#F8EEC7');
            }
        }
    }
}

ST.crypto = function () {
    $('button[name=submit]').after('&nbsp;&nbsp;<button type="submit" id="create_encrypted" class="btn-large btn-success"> <i class="icon-lock icon-white"></i> Create encrypted</button>');
    $('#create_encrypted').on('click', function () {
        var $code = $('#code');

        // encrypt the paste
        var key = ST.crypto_generate_key(32);
        var plaintext = $code.val();
        plaintext = LZString.compressToBase64(plaintext);
        var encrypted = CryptoJS.AES.encrypt(plaintext, key) + '';

        // linebreak after 100 chars
        encrypted = encrypted.replace(/(.{100})/g, "$1\n");

        // post request via JS
        $.post(base_url + 'post_encrypted', {
            'name': $('#name').val(),
            'title': $('#title').val(),
            'code': encrypted,
            'lang': $('#lang').val(),
            'expire': $('#expire').val(),
            'captcha': $('#captcha').val(),
            'reply': $('input[name=reply]').val()
        },
            function (redirect_url) {
                if (redirect_url.indexOf('E_CAPTCHA') > -1) {
                    $('.container .message').remove();
                    $('.container:eq(1)').prepend('<div class="message error"><div class="container">The captcha is incorrect.</div></div>');
                } else if (redirect_url.indexOf('invalid') > -1) {
                    // burn on read
                    redirect_url = redirect_url.replace('" /><!-- behind you -->', '#' + key + '" />')
                    $('#create_encrypted').parent().html('<p>' + redirect_url + '</p>');
                } else {
                    window.location.href = base_url + redirect_url + '#' + key;
                }
            });

        return false;
    });

    // decryption routine
    w_href = window.location.href;
    if (w_href.indexOf('#') > -1) {
        key = w_href.split('#')[1];
        var re = new RegExp('^L[0-9].*?$');
        var r = key.match(re);
        if (key.indexOf('-') > -1 || r) {
            // line highlighter
        } else {
            try {
                var $code = $('#code');
                var encrypted = $code.val().replace(/\n/g, '');
                var decrypted = CryptoJS.AES.decrypt(encrypted, key).toString(CryptoJS.enc.Utf8) + '';
                decrypted = LZString.decompressFromBase64(decrypted);
                $code.val(decrypted);

                // add a breaking_space after 90 chars (for later)
                decrypted = decrypted.replace(/(.{90}.*?) /g, "$1{{{breaking_space}}}");

                // remove html entities
                decrypted = decrypted
                    .replace(/&/g, '&amp;')
                    .replace(/"/g, '&quot;')
                    .replace(/'/g, '&#039;')
                    .replace(/</g, '&lt;')
                    .replace(/>/g, '&gt;')
                    .replace(/ /g, '&nbsp;')
                    .replace(/{{{breaking_space}}}/g, ' ')
                    .replace(/\n/g, '<br />')

                $('.row .span12 .CodeMirror').html(decrypted);

                // kick out potential dangerous and unnecessary stuff
                $('.text_formatted').css('background', '#efe');
                $('.replies').hide();
                for (var i = 2; i <= 7; i++) {
                    $('.meta .detail:nth-child(' + i + ')').hide();
                }
                $('.meta .spacer:first').hide();
                $('.qr').hide();
            } catch (e) { }
        }
    }
}

// generate a random key
ST.crypto_generate_key = function (len) {
    var index = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var key = '';
    for (var i = 0; i < len; i++) {
        key += index[Math.floor(Math.random() * index.length)]
    };
    return key;
}

ST.dragdrop = function () {
    $("#code").fileReaderJS({
        // CSS Class to add to the drop element when a drag is active
        dragClass: "drag",

        // A string to match MIME types, for instance
        accept: false,

        // An object specifying how to read certain MIME types
        // For example: {
        //  'image/*': 'DataURL',
        //  'data/*': 'ArrayBuffer',
        //  'text/*' : 'Text'
        // }
        readAsMap: {},

        // How to read any files not specified by readAsMap
        readAsDefault: 'DataURL',
        on: {
            loadend: function (e, file) {
                try {
                    var words = CryptoJS.enc.Base64.parse(e.target.result.split(',')[1]);
                    var utf8 = CryptoJS.enc.Utf8.stringify(words);
                    $('#code').val(utf8);
                } catch (err) {
                    console.error(err);
                    console.info('event: ', e);
                    console.info('file: ', file);
                };
            }
        }
    });
}

ST.ace_init = function () {
    // prepare the editor, needs to be a div
    var $code = $('#code');

    // exit if there is no #code textarea
    if ($code.length < 1) {
        return false;
    }

    if (typeof ace == 'undefined') {
        return false;
    }

    // replace textarea
    $code.after('<div id="editor" style="left: 0px; width: 900px; height: 379px;"></div>');
    $code.hide();

    // init modes
    ST.ace_modes = $.parseJSON($('#ace_modes').text());

    // init ace
    ace.config.set("basePath", base_url + "themes/default/js/ace");
    ST.ace_editor = ace.edit("editor");
    ST.ace_editor.setTheme("ace/theme/clouds");
    ST.ace_editor.getSession().setValue($code.val());
    ST.ace_editor.getSession().on('change', function (e) {
        $code.val(ST.ace_editor.getValue());
    });
    ST.ace_setlang();
    $('#lang').change(function () {
        ST.ace_setlang();
    });
}

ST.ace_setlang = function () {
    var lang = $('#lang').val();
    var mode = '';
    try {
        mode = ST.ace_modes[lang];
    } catch (undefined) {
        mode = 'text';
    }
    if (mode === undefined) {
        mode = 'text';
    }
    ST.ace_editor.getSession().setMode("ace/mode/" + mode);
}

ST.codemirror_init = function () {
    if (typeof CodeMirror == 'undefined') {
        return false;
    }
    ST.cm_modes = $.parseJSON($('#codemirror_modes').text());
    $('#lang').change(function () {
        ST.codemirror_setlang();
    });
    if (typeof ST.cm_editor == 'undefined') {
        ST.cm_editor = CodeMirror.fromTextArea(document.getElementById('code'), {
            mode: "scheme",
            lineNumbers: true,
            lineWrapping: true,
            tabMode: "indent"
        });
    }
    ST.codemirror_setlang();
}

ST.codemirror_setlang = function () {
    var lang = $('#lang').val();
    var mode = ST.cm_modes[lang];

    $.get(base_url + 'main/get_cm_js/' + lang,
        function (data) {
            if (data != '') {
                ST.cm_editor.setOption('mode', mode);
            } else {
                ST.cm_editor.setOption('mode', null);
            }
        },
        'script');
}

ST.clickable_urls = function () {
    $('.container .row .span12').linkify();
}

ST.init = function () {
    ST.show_embed();
    ST.spamadmin();
    ST.line_highlighter();
    ST.crypto();
    ST.dragdrop();
    ST.clickable_urls();
    ST.codemirror_init();
    ST.ace_init();
};

$(document).ready(function () {
    ST.init();
});

© 2014-2024 Faster IT GmbH | imprint | privacy policy