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

262 lines
10 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
2014-02-27 22:05:09 +00:00
* @copyright 2010-2014 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.
*
2013-05-23 23:21:46 +00:00
* @return integer Number of deleted messages.
2012-05-19 18:33:25 +00:00
*/
public function prune() {
if (CHAT_LOG_ARCHIVETIME == -1) return 0;
2012-05-19 18:33:25 +00:00
$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 * 60));
2013-05-23 23:21:46 +00:00
2012-05-19 18:33:25 +00:00
$objectIDs = array();
2013-05-23 23:21:46 +00:00
while ($objectID = $stmt->fetchColumn()) $objectIDs[] = $objectID;
2012-05-19 18:33:25 +00:00
return call_user_func(array($this->className, 'deleteAll'), $objectIDs);
}
/**
* Validates message sending.
*/
2013-03-19 17:20:40 +00:00
public function validateSend() {
// read user data
$this->parameters['userData']['color1'] = WCF::getUser()->chatColor1;
$this->parameters['userData']['color2'] = WCF::getUser()->chatColor2;
$this->parameters['userData']['roomID'] = WCF::getUser()->chatRoomID;
$this->parameters['userData']['away'] = WCF::getUser()->chatAway;
// read and validate room
2013-05-24 00:21:49 +00:00
$this->parameters['room'] = room\RoomCache::getInstance()->getRoom($this->parameters['userData']['roomID']);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
if (!$this->parameters['room']->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
if (!$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
// 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 (mb_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)));
}
}
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatAway' => null,
'chatLastActivity' => TIME_NOW
));
2013-03-19 17:05:52 +00:00
// 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']['color1'],
'color2' => $this->parameters['userData']['color2']
2013-03-19 17:05:52 +00:00
)
));
$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();
$this->parameters['additionalData'] = $command->getAdditionalData();
}
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)));
}
catch (\InvalidArgumentException $e) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.invalidArgument', array('exception' => $e)));
}
}
else {
$this->parameters['type'] = Message::TYPE_NORMAL;
$this->parameters['receiver'] = null;
$this->parameters['additionalData'] = 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')));
}
}
/**
* Creates sent message.
*/
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']['color1'],
'color2' => $this->parameters['userData']['color2'],
'additionalData' => serialize($this->parameters['additionalData'])
)
));
$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);
2013-11-19 19:47:42 +00:00
return $returnValues['returnValues'];
}
/**
* Fetches messages in between the specified timestamps.
*
* @return array Array containing message table, containerID and information about an empty message table.
*/
public function getMessages() {
// read messages
2013-09-05 20:54:12 +00:00
$messages = ViewableMessageList::getMessagesBetween($this->parameters['room'], $this->parameters['start'], $this->parameters['end']);
return array(
'noMessages' => (count($messages) == 0) ? true : null,
'containerID' => $this->parameters['containerID'],
'template' => WCF::getTPL()->fetch('__messageLogTable', 'chat', array('messages' => $messages), true)
);
}
/**
* Validates getting messages.
*/
public function validateGetMessages() {
// check permissions
WCF::getSession()->checkPermissions(array('admin.chat.canReadLog'));
// read parameters
$this->readInteger('roomID');
$this->readInteger('start');
$this->readInteger('end');
// read and validate room
$this->parameters['room'] = room\RoomCache::getInstance()->getRoom($this->parameters['roomID']);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
}
public function validateSendAttachment() {
// read user data
$this->parameters['userData']['color1'] = WCF::getUser()->chatColor1;
$this->parameters['userData']['color2'] = WCF::getUser()->chatColor2;
$this->parameters['userData']['roomID'] = WCF::getUser()->chatRoomID;
$this->parameters['userData']['away'] = WCF::getUser()->chatAway;
// read and validate room
$this->parameters['room'] = room\RoomCache::getInstance()->getRoom($this->parameters['userData']['roomID']);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
if (!$this->parameters['room']->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
if (!$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
$this->readInteger('objectID');
if (!$this->parameters['objectID']) throw new \wcf\system\exception\IllegalLinkException();
$this->readInteger('parentObjectID');
if (!$this->parameters['parentObjectID']) throw new \wcf\system\exception\IllegalLinkException();
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatAway' => null,
'chatLastActivity' => TIME_NOW
));
// 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']['color1'],
'color2' => $this->parameters['userData']['color2']
)
));
$messageAction->executeAction();
}
}
public function sendAttachment() {
2013-11-19 19:47:42 +00:00
$this->parameters['type'] = Message::TYPE_ATTACHMENT;
$this->parameters['text'] = '[attach]'. $this->parameters['objectID'] .'[/attach]';
$this->parameters['additionalData'] = null;
$this->parameters['receiver'] = null;
$this->parameters['enableSmilies'] = false;
$this->parameters['enableHTML'] = false;
2013-11-19 19:47:42 +00:00
$messageAction = new MessageAction(array(), 'send', $this->parameters);
$messageAction->executeAction();
$returnValues = $messageAction->getReturnValues();
2013-11-19 19:47:42 +00:00
$attachmentHandler = new \wcf\system\attachment\AttachmentHandler('be.bastelstu.chat.message', $this->parameters['objectID'], $this->parameters['tmpHash'], 0);
$attachmentHandler->updateObjectID($returnValues['returnValues']->messageID);
}
2011-12-13 21:35:11 +00:00
}