2011-12-13 21:35:11 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\data\message;
|
2013-03-11 14:17:43 +00:00
|
|
|
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
|
|
|
/**
|
2012-02-26 16:55:44 +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);
|
|
|
|
}
|
2013-03-11 14:17:43 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-11 16:17:58 +00:00
|
|
|
* Validates message sending.
|
2013-03-11 14:17:43 +00:00
|
|
|
*/
|
2013-03-19 17:20:40 +00:00
|
|
|
public function validateSend() {
|
2013-03-11 14:17:43 +00:00
|
|
|
// 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;
|
2013-03-11 14:17:43 +00:00
|
|
|
|
|
|
|
// 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)));
|
|
|
|
}
|
|
|
|
|
2013-03-11 14:17:43 +00:00
|
|
|
// 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']];
|
|
|
|
|
2013-03-11 16:17:58 +00:00
|
|
|
if (!$this->parameters['room']->canEnter() || !$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
|
2013-03-11 14:17:43 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-03-11 14:17:43 +00:00
|
|
|
// handle commands
|
2013-03-11 16:17:58 +00:00
|
|
|
$commandHandler = new \chat\system\command\CommandHandler($this->parameters['text'], $this->parameters['room']);
|
2013-03-11 14:17:43 +00:00
|
|
|
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;
|
2013-03-11 14:17:43 +00:00
|
|
|
|
|
|
|
$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:36:27 +00:00
|
|
|
|
|
|
|
$this->parameters['text'] = \wcf\system\bbcode\PreParser::getInstance()->parse($this->parameters['text'], explode(',', WCF::getSession()->getPermission('user.chat.allowedBBCodes')));
|
2013-03-11 14:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 16:17:58 +00:00
|
|
|
/**
|
|
|
|
* Creates sent message.
|
|
|
|
*/
|
2013-03-19 17:05:52 +00:00
|
|
|
public function send() {
|
2013-03-11 14:17:43 +00:00
|
|
|
$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,
|
2013-03-11 14:17:43 +00:00
|
|
|
'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
|
|
|
}
|