diff --git a/public_html/dispatch.php b/public_html/dispatch.php index 476ef729..182d6b54 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -154,12 +154,18 @@ $context->layoutName = $context->json : 'layout-normal'; $context->viewName = ''; $context->transport = new StdClass; -StatusHelper::init(); session_start(); if (!Auth::isLoggedIn()) Auth::tryAutoLogin(); +register_shutdown_function(function() +{ + $error = error_get_last(); + if ($error !== null) + \Chibi\Util\Headers::setCode(400); +}); + try { try @@ -183,7 +189,9 @@ catch (SimpleException $e) { if ($e instanceof SimpleNotFoundException) \Chibi\Util\Headers::setCode(404); - StatusHelper::failure($e->getMessage()); + else + \Chibi\Util\Headers::setCode(400); + Messenger::message($e->getMessage(), false); if (!$context->handleExceptions) $context->viewName = 'message'; renderView(); @@ -191,7 +199,7 @@ catch (SimpleException $e) catch (Exception $e) { \Chibi\Util\Headers::setCode(400); - StatusHelper::failure($e->getMessage()); + Messenger::message($e->getMessage()); $context->transport->exception = $e; $context->transport->queries = \Chibi\Database::getLogs(); $context->viewName = 'error-exception'; diff --git a/public_html/media/js/comment-edit.js b/public_html/media/js/comment-edit.js index b0844f9f..5023067f 100644 --- a/public_html/media/js/comment-edit.js +++ b/public_html/media/js/comment-edit.js @@ -40,52 +40,45 @@ $(function() success: function(data) { - if (data['success']) + if (preview) { - if (preview) - { - formDom.find('.preview').html(data['textPreview']).show(); - } - else - { - disableExitConfirmation(); - - formDom.find('.preview').hide(); - var cb = function() - { - $.get(window.location.href, function(data) - { - $('.comments-wrapper').replaceWith($(data).find('.comments-wrapper')); - $('body').trigger('dom-update'); - }); - } - if (formDom.hasClass('add-comment')) - { - cb(); - formDom.find('textarea').val(''); - } - else - { - formDom.slideUp(function() - { - cb(); - $(this).remove(); - }); - } - } - formDom.find(':input').attr('readonly', false); - formDom.removeClass('inactive'); + formDom.find('.preview').html(data['textPreview']).show(); } else { - alert(data['message']); - formDom.find(':input').attr('readonly', false); - formDom.removeClass('inactive'); + disableExitConfirmation(); + + formDom.find('.preview').hide(); + var cb = function() + { + $.get(window.location.href, function(data) + { + $('.comments-wrapper').replaceWith($(data).find('.comments-wrapper')); + $('body').trigger('dom-update'); + }); + } + if (formDom.hasClass('add-comment')) + { + cb(); + formDom.find('textarea').val(''); + } + else + { + formDom.slideUp(function() + { + cb(); + $(this).remove(); + }); + } } + formDom.find(':input').attr('readonly', false); + formDom.removeClass('inactive'); }, - error: function() + error: function(xhr) { - alert('Fatal error'); + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); formDom.find(':input').attr('readonly', false); formDom.removeClass('inactive'); } diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index 6ba96eb5..41ff3972 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -63,15 +63,15 @@ $(function() aDom.addClass('inactive'); var url = $(this).attr('href') + '?json'; - $.get(url).always(function(data) + $.get(url).success(function(data) { - if (data['success']) - window.location.reload(); - else - { - alert(data['message'] ? data['message'] : 'Fatal error'); - aDom.removeClass('inactive'); - } + window.location.reload(); + }).error(function(xhr) + { + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); + aDom.removeClass('inactive'); }); }); }); @@ -112,22 +112,20 @@ $(function() aDom.addClass('inactive'); var url = $(this).attr('href') + '?json'; - $.get(url, {submit: 1}).always(function(data) + $.post(url, {submit: 1}).success(function(data) { - if (data['success']) - { - if (aDom.hasAttr('data-redirect-url')) - window.location.href = aDom.attr('data-redirect-url'); - else if (aDom.data('callback')) - aDom.data('callback')(); - else - window.location.reload(); - } + if (aDom.hasAttr('data-redirect-url')) + window.location.href = aDom.attr('data-redirect-url'); + else if (aDom.data('callback')) + aDom.data('callback')(); else - { - alert(data['message'] ? data['message'] : 'Fatal error'); - aDom.removeClass('inactive'); - } + window.location.reload(); + }).error(function(xhr) + { + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); + aDom.removeClass('inactive'); }); }); diff --git a/public_html/media/js/post-list.js b/public_html/media/js/post-list.js index 300efd7c..99303353 100644 --- a/public_html/media/js/post-list.js +++ b/public_html/media/js/post-list.js @@ -14,23 +14,21 @@ $(function() var enable = !aDom.parents('.post').hasClass('tagged'); var url = $(this).attr('href') + '?json'; url = url.replace('_enable_', enable ? '1' : '0'); - $.get(url, {submit: 1}).always(function(data) + $.get(url, {submit: 1}).success(function(data) { - if (data['success']) - { - aDom.removeClass('inactive'); - aDom.parents('.post').removeClass('tagged'); - if (enable) - aDom.parents('.post').addClass('tagged'); - aDom.text(enable - ? aDom.attr('data-text-tagged') - : aDom.attr('data-text-untagged')); - } - else - { - alert(data['message'] ? data['message'] : 'Fatal error'); - aDom.removeClass('inactive'); - } + aDom.removeClass('inactive'); + aDom.parents('.post').removeClass('tagged'); + if (enable) + aDom.parents('.post').addClass('tagged'); + aDom.text(enable + ? aDom.attr('data-text-tagged') + : aDom.attr('data-text-untagged')); + }).error(function(xhr) + { + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); + aDom.removeClass('inactive'); }); }); }); diff --git a/public_html/media/js/post-upload.js b/public_html/media/js/post-upload.js index f03b375b..40d18c93 100644 --- a/public_html/media/js/post-upload.js +++ b/public_html/media/js/post-upload.js @@ -107,23 +107,20 @@ $(function() type: 'POST', success: function(data) { - if (data['success']) + postDom.slideUp(function() { - postDom.slideUp(function() - { - postDom.remove(); - sendNextPost(); - }); - } - else - { - postDom.find('.alert').html(data['messageHtml']).slideDown(); - enableUpload(); - } + postDom.remove(); + sendNextPost(); + }); }, - error: function(data) + error: function(xhr) { - postDom.find('.alert').html('Fatal error').slideDown(); + postDom + .find('.alert') + .html(xhr.responseJSON + ? xhr.responseJSON.messageHtml + : 'Fatal error') + .slideDown(); enableUpload(); } }; diff --git a/public_html/media/js/post-view.js b/public_html/media/js/post-view.js index 51060b3e..34526e65 100644 --- a/public_html/media/js/post-view.js +++ b/public_html/media/js/post-view.js @@ -110,28 +110,23 @@ $(function() success: function(data) { - if (data['success']) - { - disableExitConfirmation(); + disableExitConfirmation(); - $.get(window.location.href, function(data) - { - $('#sidebar').replaceWith($(data).find('#sidebar')); - $('#edit-token').replaceWith($(data).find('#edit-token')); - $('body').trigger('dom-update'); - }); - formDom.parents('.unit').hide(); - } - else + $.get(window.location.href, function(data) { - alert(data['message']); - } + $('#sidebar').replaceWith($(data).find('#sidebar')); + $('#edit-token').replaceWith($(data).find('#edit-token')); + $('body').trigger('dom-update'); + }); + formDom.parents('.unit').hide(); formDom.find(':input').attr('readonly', false); formDom.removeClass('inactive'); }, - error: function() + error: function(xhr) { - alert('Fatal error'); + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); formDom.find(':input').attr('readonly', false); formDom.removeClass('inactive'); } diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index e53e8e27..d6d8cfde 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -20,8 +20,6 @@ class AuthController $suppliedPassword = InputHelper::get('password'); $remember = boolval(InputHelper::get('remember')); $dbUser = Auth::login($suppliedName, $suppliedPassword, $remember); - - StatusHelper::success(); self::redirectAfterLog(); } diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 0b33b914..05cceb5e 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -61,7 +61,6 @@ class CommentController LogHelper::log('{user} commented on {post}', ['post' => TextHelper::reprPost($post->id)]); } $context->transport->textPreview = $comment->getText(); - StatusHelper::success(); } public function editAction($id) @@ -89,7 +88,6 @@ class CommentController 'post' => TextHelper::reprPost($comment->getPost())]); } $context->transport->textPreview = $comment->getText(); - StatusHelper::success(); } public function deleteAction($id) @@ -104,6 +102,5 @@ class CommentController LogHelper::log('{user} removed comment from {post}', [ 'post' => TextHelper::reprPost($comment->getPost())]); - StatusHelper::success(); } } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index c74b31d1..2021e372 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -101,7 +101,6 @@ class PostController $post->setTags($tags); PostModel::save($post); - StatusHelper::success(); } public function favoritesAction($page = 1) @@ -167,8 +166,6 @@ class PostController LogHelper::flush(); PostModel::save($post); }); - - StatusHelper::success(); } public function editAction($id) @@ -190,8 +187,6 @@ class PostController PostModel::save($post); TagModel::removeUnused(); - - StatusHelper::success(); } public function flagAction($id) @@ -211,7 +206,6 @@ class PostController SessionHelper::set('flagged', $flagged); LogHelper::log('{user} flagged {post} for moderator attention', ['post' => TextHelper::reprPost($post)]); - StatusHelper::success(); } public function hideAction($id) @@ -226,7 +220,6 @@ class PostController PostModel::save($post); LogHelper::log('{user} hidden {post}', ['post' => TextHelper::reprPost($post)]); - StatusHelper::success(); } public function unhideAction($id) @@ -241,7 +234,6 @@ class PostController PostModel::save($post); LogHelper::log('{user} unhidden {post}', ['post' => TextHelper::reprPost($post)]); - StatusHelper::success(); } public function deleteAction($id) @@ -255,7 +247,6 @@ class PostController PostModel::remove($post); LogHelper::log('{user} deleted {post}', ['post' => TextHelper::reprPost($id)]); - StatusHelper::success(); } public function addFavoriteAction($id) @@ -270,7 +261,6 @@ class PostController UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1); UserModel::addToUserFavorites(Auth::getCurrentUser(), $post); - StatusHelper::success(); } public function removeFavoriteAction($id) @@ -284,7 +274,6 @@ class PostController return; UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post); - StatusHelper::success(); } public function scoreAction($id, $score) @@ -298,7 +287,6 @@ class PostController return; UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score); - StatusHelper::success(); } public function featureAction($id) @@ -309,7 +297,6 @@ class PostController PropertyModel::set(PropertyModel::FeaturedPostId, $post->id); PropertyModel::set(PropertyModel::FeaturedPostDate, time()); PropertyModel::set(PropertyModel::FeaturedPostUserName, Auth::getCurrentUser()->name); - StatusHelper::success(); LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]); } diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index ed140a84..f8ff5cc2 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -103,7 +103,7 @@ class TagController 'source' => TextHelper::reprTag($suppliedSourceTag), 'target' => TextHelper::reprTag($suppliedTargetTag)]); - StatusHelper::success('Tags merged successfully.'); + Messenger::message('Tags merged successfully.'); } public function renameAction() @@ -130,7 +130,7 @@ class TagController 'source' => TextHelper::reprTag($suppliedSourceTag), 'target' => TextHelper::reprTag($suppliedTargetTag)]); - StatusHelper::success('Tag renamed successfully.'); + Messenger::message('Tag renamed successfully.'); } public function massTagRedirectAction() diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 6ab1a233..7eb25bd4 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -46,8 +46,6 @@ class UserController LogHelper::log('{user} flagged {subject} for moderator attention', [ 'subject' => TextHelper::reprUser($user)]); - - StatusHelper::success(); } public function banAction($name) @@ -64,7 +62,6 @@ class UserController UserModel::save($user); LogHelper::log('{user} banned {subject}', ['subject' => TextHelper::reprUser($user)]); - StatusHelper::success(); } public function unbanAction($name) @@ -81,7 +78,6 @@ class UserController UserModel::save($user); LogHelper::log('{user} unbanned {subject}', ['subject' => TextHelper::reprUser($user)]); - StatusHelper::success(); } public function acceptRegistrationAction($name) @@ -96,7 +92,6 @@ class UserController $user->staffConfirmed = true; UserModel::save($user); LogHelper::log('{user} confirmed {subject}\'s account', ['subject' => TextHelper::reprUser($user)]); - StatusHelper::success(); } public function deleteAction($name) @@ -133,7 +128,6 @@ class UserController \Chibi\Util\Url::forward(\Chibi\Router::linkTo(['IndexController', 'indexAction'])); LogHelper::log('{user} removed {subject}\'s account', ['subject' => TextHelper::reprUser($name)]); - StatusHelper::success(); } public function settingsAction($name) @@ -167,7 +161,7 @@ class UserController UserModel::save($user); if ($user->id == Auth::getCurrentUser()->id) Auth::setCurrentUser($user); - StatusHelper::success('Browsing settings updated!'); + Messenger::message('Browsing settings updated!'); } public function editAction($name) @@ -278,7 +272,7 @@ class UserController $message = 'Account settings updated!'; if ($confirmMail) $message .= ' You will be sent an e-mail address confirmation message soon.'; - StatusHelper::success($message); + Messenger::message($message); } catch (Exception $e) { @@ -343,8 +337,6 @@ class UserController if ($user->accessRank != AccessRank::Anonymous) UserModel::save($user); Auth::setCurrentUser($user); - - StatusHelper::success(); } public function registrationAction() @@ -420,7 +412,7 @@ class UserController $message .= ' Your registration must be now confirmed by staff.'; LogHelper::log('{subject} just signed up', ['subject' => TextHelper::reprUser($dbUser)]); - StatusHelper::success($message); + Messenger::message($message); if (!getConfig()->registration->needEmailForRegistering and !getConfig()->registration->staffActivation) { @@ -448,7 +440,7 @@ class UserController $message = 'Activation completed successfully.'; if (getConfig()->registration->staffActivation) $message .= ' However, your account still must be confirmed by staff.'; - StatusHelper::success($message); + Messenger::message($message); if (!getConfig()->registration->staffActivation) { @@ -479,7 +471,7 @@ class UserController LogHelper::log('{subject} just reset password', ['subject' => TextHelper::reprUser($dbUser)]); $message = 'Password reset successful. Your new password is **' . $randomPassword . '**.'; - StatusHelper::success($message); + Messenger::message($message); Auth::setCurrentUser($dbUser); } @@ -499,7 +491,7 @@ class UserController throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed'); self::sendPasswordResetConfirmation($user); - StatusHelper::success('E-mail sent. Follow instructions to reset password.'); + Messenger::message('E-mail sent. Follow instructions to reset password.'); } public function activationProxyAction() @@ -521,7 +513,7 @@ class UserController throw new SimpleException('This user has no e-mail specified; activation cannot proceed'); } self::sendEmailChangeConfirmation($user); - StatusHelper::success('Activation e-mail resent.'); + Messenger::message('Activation e-mail resent.'); } private function loadUserView($user) diff --git a/src/Helpers/StatusHelper.php b/src/Helpers/StatusHelper.php deleted file mode 100644 index 223956e3..00000000 --- a/src/Helpers/StatusHelper.php +++ /dev/null @@ -1,33 +0,0 @@ -transport->message = $message; - $context->transport->messageHtml = TextHelper::parseMarkdown($message, true); - } - $context->transport->success = $success; - } - - public static function init() - { - $context = getContext(); - $context->transport->success = null; - } - - public static function success($message = null) - { - self::flag(true, $message); - } - - public static function failure($message = null) - { - self::flag(false, $message); - } -} diff --git a/src/Messenger.php b/src/Messenger.php new file mode 100644 index 00000000..156dcba6 --- /dev/null +++ b/src/Messenger.php @@ -0,0 +1,18 @@ +transport->success = $success; + $context->transport->message = $message; + $context->transport->messageHtml = TextHelper::parseMarkdown($message, true); + } +}