crispage/core/actions/Crispage/Actions/PostCommentAction.php
crispycat b1ab0bced2 reorganized core
\Crispage\Events renamed to \Crispage\Signaling
Action, Component, ExtensionClass, ModuleComponent, Renderable, Runnable now part of \Crispage\Framework
PluginRunnable now \Crispage\Framework\PluginClass
core extensions modified to fit new naming scheme
/core/app/media and /core/app/search moved to /core/app/cms/
2024-05-19 12:31:01 -04:00

104 lines
2.7 KiB
PHP

<?php
/*
Crispage CMS
crispycat <the@crispy.cat>
https://crispy.cat/software/crispage
Crispage is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
namespace Crispage\Actions;
defined("ROOT") or die();
use \Crispage\Assets\Comment;
class PostCommentAction extends \Crispage\Framework\Action {
public static function getExtensionInfo(): array {
return [
"id" => "crispage.actions.postcomment",
"version" => VERSION
];
}
public function __construct(\Crispage $app, array $data) {
parent::__construct($app, $data);
}
public function run(): void {
$rl = strval($this->app->request->params["rl"] ?? "/");
if ($rl[0] != "/") $rl = "/" . $rl;
$rl = WROOT . $rl;
if (!$this->app->settings->get("crispage.comments_enabled", true)) {
$this->app->page->setPersistentMessage(
"comments_disabled",
$this->app->i18n->translate("{%COMMENTS_DISABLED}"),
"danger"
);
$this->app->page->redirect($rl);
}
$perm = Comment::getPermissions()["create"];
if (!$this->app->auth->userHasPermission(null, $perm)) {
$this->app->page->setPersistentMessage(
"no_permission",
$this->app->i18n->translate("{%YOU_DO_NOT_HAVE_PERMISSION_TO_PERFORM_}"),
"danger"
);
$this->app->page->redirect($rl);
}
$content = $this->app->assets->get(
intval($this->app->request->params["content_id"] ?? 0)
);
if (!$content || !$content->getOption("show_comments", true)) {
$this->app->page->setPersistentMessage(
"comments_disabled",
$this->app->i18n->translate("{%COMMENTS_DISABLED}"),
"danger"
);
$this->app->page->redirect($rl);
}
$body = strval($this->app->request->params["body"] ?? "");
if (empty($body)) {
$this->app->page->setPersistentMessage(
"body_empty",
$this->app->i18n->translate("{%COMMENT_BODY_EMPTY}"),
"warning"
);
$this->app->page->redirect($rl);
}
$ra = boolval(
$this->app->settings->get("crispage.comments_require_approval", 0)
);
$state = ($ra) ? Comment::PENDING_APPROVAL : Comment::APPROVED;
$this->app->assets->create(
"\\Crispage\\Assets\\Comment", [
"slug" => $content->slug . "_" . dechex(time()) . bin2hex(random_bytes(2)),
"author_id" => $this->app->auth->currentUser->id,
"content_id" => $content->id,
"body" => $body,
"state" => $state
]
);
$this->app->page->setPersistentMessage(
"comment_posted",
$this->app->i18n->translate("{%COMMENT_POSTED}"),
"success"
);
$this->app->page->redirect($rl);
}
}
?>