1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-01 14:20:07 +00:00
Tims-Chat/files/lib/system/command/AbstractInputProcessedCommand.class.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
3.4 KiB
PHP
Raw Normal View History

2018-08-16 22:30:59 +00:00
<?php
2022-03-04 17:10:24 +00:00
2018-08-16 22:30:59 +00:00
/*
2022-03-04 17:10:24 +00:00
* Copyright (c) 2010-2022 Tim Düsterhus.
2018-08-16 22:30:59 +00:00
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2023-02-22 16:45:50 +00:00
* Change Date: 2027-02-22
2018-08-16 22:30:59 +00:00
*
* On the date above, in accordance with the Business Source
* License, use of this software will be governed by version 2
* or later of the General Public License.
*/
namespace chat\system\command;
2022-03-04 17:10:24 +00:00
use wcf\data\DatabaseObject;
use wcf\system\bbcode\BBCodeHandler;
use wcf\system\exception\UserInputException;
use wcf\system\html\input\HtmlInputProcessor;
use wcf\system\message\censorship\Censorship;
use wcf\system\WCF;
2018-08-16 22:30:59 +00:00
/**
* Represents a command that processes the input using HtmlInputProcessor.
*/
2022-03-04 17:10:24 +00:00
abstract class AbstractInputProcessedCommand extends AbstractCommand
{
/**
* HtmlInputProcessor to use.
* @var \wcf\system\html\input\HtmlInputProcessor
*/
protected $processor;
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* The text processed last.
* @var string
*/
private $text;
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
public function __construct(DatabaseObject $object)
{
parent::__construct($object);
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
$this->processor = new HtmlInputProcessor();
$this->setDisallowedBBCodes();
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
private function setDisallowedBBCodes()
{
BBCodeHandler::getInstance()->setDisallowedBBCodes(\explode(
',',
WCF::getSession()->getPermission('user.chat.disallowedBBCodes')
));
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
public function setText($text)
{
if ($this->text === $text) {
return;
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
$this->text = $text;
$this->setDisallowedBBCodes();
$this->processor->process(
$text,
'be.bastelstu.chat.message',
0
);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
public function validateText()
{
if ($this->processor->appearsToBeEmpty()) {
throw new UserInputException(
'message',
WCF::getLanguage()->getDynamicVariable('wcf.global.form.error.empty')
);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
$message = $this->processor->getTextContent();
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
// validate message length
if (\mb_strlen($message) > CHAT_MAX_LENGTH) {
throw new UserInputException(
'message',
WCF::getLanguage()->getDynamicVariable(
'wcf.message.error.tooLong',
[
'maxTextLength' => CHAT_MAX_LENGTH,
]
)
);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
// search for disallowed bbcodes
$this->setDisallowedBBCodes();
$disallowedBBCodes = $this->processor->validate();
if (!empty($disallowedBBCodes)) {
throw new UserInputException(
'message',
WCF::getLanguage()->getDynamicVariable(
'wcf.message.error.disallowedBBCodes',
[
'disallowedBBCodes' => $disallowedBBCodes,
]
)
);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
// search for censored words
if (ENABLE_CENSORSHIP) {
$result = Censorship::getInstance()->test($message);
if ($result) {
throw new UserInputException(
'message',
WCF::getLanguage()->getDynamicVariable(
'wcf.message.error.censoredWordsFound',
[
'censoredWords' => $result,
]
)
);
}
}
}
2018-08-16 22:30:59 +00:00
}