diff --git a/inc/config.php b/inc/config.php index e1929766..e6856341 100644 --- a/inc/config.php +++ b/inc/config.php @@ -272,6 +272,7 @@ 'quick-reply', 'page', 'file_url', + 'json_response', ); // Enable reCaptcha to make spam even harder. Rarely necessary. diff --git a/inc/display.php b/inc/display.php index e2646c60..7cd69134 100644 --- a/inc/display.php +++ b/inc/display.php @@ -74,6 +74,14 @@ function error($message, $priority = true, $debug_stuff = false) { $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error); } + // Is there a reason to disable this? + if (isset($_POST['json_response'])) { + header('Content-Type: text/json; charset=utf-8'); + die(json_encode(array( + 'error' => $message + ))); + } + die(Element('page.html', array( 'config' => $config, 'title' => _('Error'), diff --git a/js/ajax.js b/js/ajax.js new file mode 100644 index 00000000..d29f8fc7 --- /dev/null +++ b/js/ajax.js @@ -0,0 +1,93 @@ +/* + * ajax.js + * https://github.com/savetheinternet/Tinyboard/blob/master/js/ajax.js + * + * Released under the MIT license + * Copyright (c) 2013 Michael Save + * + * Usage: + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/ajax.js'; + * + */ + +$(window).ready(function() { + var setup_form = function($form) { + $form.submit(function() { + var form = this; + var submit_txt = $(this).find('input[type="submit"]').val(); + if (typeof FormData == 'undefined') + return true; + + var formData = new FormData(this); + formData.append('json_response', '1'); + + var updateProgress = function(e) { + $(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', Math.round(e.position / e.total * 100))); + }; + + $.ajax({ + url: this.action, + type: 'POST', + xhr: function() { + var xhr = $.ajaxSettings.xhr(); + if(xhr.upload) { + xhr.upload.addEventListener('progress', updateProgress, false); + } + return xhr; + }, + success: function(post_response) { + if (post_response.error) { + alert(post_response.error); + $(form).find('input[type="submit"]').val(submit_txt); + $(form).find('input[type="submit"]').removeAttr('disabled'); + } else if (post_response.redirect && post_response.id) { + $.ajax({ + url: post_response.redirect, + success: function(data) { + console.log(data); + $(data).find('div.post.reply').each(function() { + var id = $(this).attr('id'); + if($('#' + id).length == 0) { + $(this).insertAfter($('div.post:last').next()).after('
'); + $(document).trigger('new_post', this); + } + }); + highlightReply(post_response.id); + document.location = '#' + post_response.id; + + $(form).find('input[type="submit"]').val(submit_txt); + $(form).find('input[type="submit"]').removeAttr('disabled'); + } + }, 'html'); + + $(form).find('input[type="submit"]').val('Posted...'); + } else { + alert(_('An unknown error occured when posting!')); + $(form).find('input[type="submit"]').val(submit_txt); + $(form).find('input[type="submit"]').removeAttr('disabled'); + } + }, + error: function(xhr, status, er) { + // An error occured + // TODO + console.log('Error'); + }, + // Form data + data: formData, + cache: false, + contentType: false, + processData: false + }, 'html'); + + $(form).find('input[type="submit"]').val(_('Posting...')); + $(form).find('input[type="submit"]').attr('disabled', true); + + return false; + }); + }; + setup_form($('form[name="post"]')); + $(window).on('quick-reply', function(e, quickForm) { + setup_form($('form#quick-reply')); + }); +}); diff --git a/js/quick-reply.js b/js/quick-reply.js index 824286f6..5f81f51e 100644 --- a/js/quick-reply.js +++ b/js/quick-reply.js @@ -123,6 +123,9 @@ var show_quick_reply = function(){ } // Move anti-spam nonsense and remove + $th.contents().filter(function() { + return this.nodeType == 3; // Node.TEXT_NODE + }).remove(); $th.contents().appendTo($dummyStuff); $th.remove(); @@ -180,7 +183,9 @@ var show_quick_reply = function(){ }); $postForm.find('textarea[name="body"]').removeAttr('id').removeAttr('cols').attr('placeholder', _('Comment')); - + + $postForm.find('textarea:not([name="body"]),input[type="hidden"]').appendTo($dummyStuff); + $postForm.find('br').remove(); $postForm.find('table').prepend('' + _('Quick Reply') + ''); @@ -232,6 +237,8 @@ var show_quick_reply = function(){ } $postForm.show(); + $(window).trigger('quick-reply'); + $origPostForm = $('form[name="post"]'); $(window).ready(function() { diff --git a/post.php b/post.php index 1fe3b35f..0bc26f46 100644 --- a/post.php +++ b/post.php @@ -743,7 +743,15 @@ if (isset($_POST['delete'])) { else rebuildThemes('post', $board['uri']); - header('Location: ' . $redirect, true, $config['redirect_http']); + if (!isset($_POST['json_response'])) { + header('Location: ' . $redirect, true, $config['redirect_http']); + } else { + header('Content-Type: text/json; charset=utf-8'); + echo json_encode(array( + 'redirect' => $redirect, + 'id' => $id + )); + } } else { if (!file_exists($config['has_installed'])) { header('Location: install.php', true, $config['redirect_http']);