diff --git a/libraries/Controllers/CommentsController.php b/libraries/Controllers/CommentsController.php index eec2ad8..5b53e3b 100644 --- a/libraries/Controllers/CommentsController.php +++ b/libraries/Controllers/CommentsController.php @@ -8,6 +8,7 @@ namespace Sakura\Controllers; use Sakura\Comment; +use Sakura\Template; /** * Handles comment stuff. @@ -20,8 +21,27 @@ class CommentsController extends Controller public function post($category = '', $reply = 0) { global $currentUser; + + // Set json content type + header('Content-Type: application/json; charset=utf-8'); - // todo: make not shit + // Check if the user can comment + if (!$currentUser->permission(Site::CREATE_COMMENTS)) { + $error = "You aren't allowed to make comments!"; + return $this->json(compact('error')); + } + + // Checks + $length = strlen($content); + $tooShort = $length < Config::get('comment_min_length'); + $tooLong = $length > Config::get('comment_max_length'); + + if ($tooShort || $tooLong) { + $fill = $tooShort ? "short" : "long"; + $error = "Your comment is too {$fill}!"; + + return $this->json(compact('error')); + } $text = isset($_POST['text']) ? $_POST['text'] : ''; @@ -34,5 +54,22 @@ class CommentsController extends Controller $comment->text = $text; $comment->save(); + + return $this->json($comment); + } + + public function edit($id = 0) + { + // + } + + public function delete($id = 0) + { + // + } + + public function vote($id = 0) + { + // } } diff --git a/libraries/Controllers/Controller.php b/libraries/Controllers/Controller.php index a84f964..a07c5fc 100644 --- a/libraries/Controllers/Controller.php +++ b/libraries/Controllers/Controller.php @@ -15,4 +15,11 @@ namespace Sakura\Controllers; */ class Controller { + private function json($object) + { + return json_encode( + $object, + JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_BIGINT_AS_STRING + ); + } } diff --git a/libraries/Controllers/NotificationsController.php b/libraries/Controllers/NotificationsController.php index ea430ee..9da7c12 100644 --- a/libraries/Controllers/NotificationsController.php +++ b/libraries/Controllers/NotificationsController.php @@ -32,10 +32,7 @@ class NotificationsController extends Controller // Set json content type header('Content-Type: application/json; charset=utf-8'); - return json_encode( - $currentUser->notifications(), - JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_BIGINT_AS_STRING - ); + return $this->json($currentUser->notifications()); } /**