1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-01 14:20:07 +00:00
Tims-Chat/file/lib/data/message/MessageAction.class.php

155 lines
6.0 KiB
PHP
Raw Normal View History

2011-12-13 21:35:11 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\data\message;
use chat\data\room;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;
use wcf\util\MessageUtil;
2011-12-13 21:35:11 +00:00
/**
* Executes message related actions.
*
* @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
* @copyright 2010-2013 Tim Düsterhus
2011-12-13 21:35:11 +00:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 19:36:40 +00:00
* @package be.bastelstu.chat
* @subpackage chat.message
2011-12-13 21:35:11 +00:00
*/
2013-01-19 19:36:40 +00:00
class MessageAction extends \wcf\data\AbstractDatabaseObjectAction {
2011-12-13 21:35:11 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$className
2011-12-13 21:35:11 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $className = '\chat\data\message\MessageEditor';
2012-05-19 18:33:25 +00:00
/**
* Removes old messages.
*
* @return integer Number of deleted messages.
*/
public function prune() {
$sql = "SELECT
".call_user_func(array($this->className, 'getDatabaseTableIndexName'))."
FROM
".call_user_func(array($this->className, 'getDatabaseTableName'))."
WHERE
time < ?";
$stmt = \wcf\system\WCF::getDB()->prepareStatement($sql);
$stmt->execute(array(TIME_NOW - CHAT_LOG_ARCHIVETIME));
$objectIDs = array();
while ($objectIDs[] = $stmt->fetchColumn());
return call_user_func(array($this->className, 'deleteAll'), $objectIDs);
}
/**
* Validates message sending.
*/
2013-03-19 17:20:40 +00:00
public function validateSend() {
// read parameters
$this->readString('text');
$this->readBoolean('enableSmilies');
$this->parameters['text'] = MessageUtil::stripCrap($this->parameters['text']);
2013-03-11 14:42:05 +00:00
$this->parameters['enableHTML'] = false;
// validate text
if (strlen($this->parameters['text']) > CHAT_MAX_LENGTH) throw new UserInputException('text', 'tooLong');
2013-03-11 14:28:51 +00:00
// search for disallowed bbcodes
$disallowedBBCodes = \wcf\system\bbcode\BBCodeParser::getInstance()->validateBBCodes($this->parameters['text'], explode(',', WCF::getSession()->getPermission('user.chat.allowedBBCodes')));
if (!empty($disallowedBBCodes)) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('wcf.message.error.disallowedBBCodes', array('disallowedBBCodes' => $disallowedBBCodes)));
}
// search for censored words
if (ENABLE_CENSORSHIP) {
$result = \wcf\system\message\censorship\Censorship::getInstance()->test($this->parameters['text']);
if ($result) {
throw new UserInputException('message', WCF::getLanguage()->getDynamicVariable('wcf.message.error.censoredWordsFound', array('censoredWords' => $result)));
}
}
// read user data
$this->parameters['userData']['color'] = \chat\util\ChatUtil::readUserData('color');
$this->parameters['userData']['roomID'] = \chat\util\ChatUtil::readUserData('roomID');
$this->parameters['userData']['away'] = \chat\util\ChatUtil::readUserData('away');
// read and validate room
$cache = room\Room::getCache();
if (!isset($cache[$this->parameters['userData']['roomID']])) throw new \wcf\system\exception\IllegalLinkException();
$this->parameters['room'] = $cache[$this->parameters['userData']['roomID']];
if (!$this->parameters['room']->canEnter() || !$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
2013-03-19 17:05:52 +00:00
\chat\util\ChatUtil::writeUserData(array('away' => null));
// mark user as back
if ($this->parameters['userData']['away'] !== null) {
$messageAction = new MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['room']->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => Message::TYPE_BACK,
'message' => '',
'color1' => $this->parameters['userData']['color'][1],
'color2' => $this->parameters['userData']['color'][2]
)
));
$messageAction->executeAction();
}
// handle commands
$commandHandler = new \chat\system\command\CommandHandler($this->parameters['text'], $this->parameters['room']);
if ($commandHandler->isCommand()) {
try {
$command = $commandHandler->loadCommand();
if ($command->enableSmilies != \chat\system\command\ICommand::SETTING_USER) $this->parameters['enableSmilies'] = $command->enableSmilies;
2013-03-11 14:42:05 +00:00
$this->parameters['enableHTML'] = $command->enableHTML;
$this->parameters['type'] = $command->getType();
$this->parameters['text'] = $command->getMessage();
$this->parameters['receiver'] = $command->getReceiver();
}
catch (\chat\system\command\NotFoundException $e) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.notFound', array('exception' => $e)));
}
catch (\chat\system\command\UserNotFoundException $e) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.userNotFound', array('exception' => $e)));
}
}
else {
$this->parameters['type'] = Message::TYPE_NORMAL;
$this->parameters['receiver'] = null;
}
2013-03-19 17:28:56 +00:00
$this->parameters['text'] = \wcf\system\bbcode\PreParser::getInstance()->parse($this->parameters['text'], explode(',', WCF::getSession()->getPermission('user.chat.allowedBBCodes')));
}
/**
* Creates sent message.
*/
2013-03-19 17:05:52 +00:00
public function send() {
$this->objectAction = new MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['room']->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'receiver' => $this->parameters['receiver'],
'time' => TIME_NOW,
'type' => $this->parameters['type'],
'message' => $this->parameters['text'],
2013-03-11 14:42:05 +00:00
'enableSmilies' => $this->parameters['enableSmilies'] ? 1 : 0,
'enableHTML' => $this->parameters['enableHTML'] ? 1 : 0,
'color1' => $this->parameters['userData']['color'][1],
'color2' => $this->parameters['userData']['color'][2]
)
));
$this->objectAction->executeAction();
$returnValues = $this->objectAction->getReturnValues();
// add activity points
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.message', $returnValues['returnValues']->messageID, WCF::getUser()->userID);
}
2011-12-13 21:35:11 +00:00
}