1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-24 22:00:09 +00:00
Tims-Chat/file/lib/form/ChatForm.class.php

235 lines
6.4 KiB
PHP
Raw Normal View History

2011-12-13 21:10:35 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\form;
use \chat\data;
2011-12-13 22:02:00 +00:00
use \wcf\system\exception\UserInputException;
use \wcf\system\WCF;
use \wcf\util\StringUtil;
2011-12-13 21:10:35 +00:00
/**
* Inserts a message
*
* @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
* @copyright 2010-2013 Tim Düsterhus
2011-12-13 21:10:35 +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
2011-12-13 21:10:35 +00:00
* @subpackage form
*/
class ChatForm extends AbstractForm {
2012-06-04 19:10:03 +00:00
/**
* Should HTML be enabled for this message.
*
* @var integer
*/
2012-03-23 22:27:15 +00:00
public $enableHTML = 0;
2012-06-04 19:10:03 +00:00
2013-01-09 20:34:30 +00:00
/**
* Should bbcodes be enabled for this message.
*
* @var integer
*/
public $enableBBCodes = CHAT_ENABLE_BBCODES;
2012-06-04 19:10:03 +00:00
/**
* Should smilies be enabled for this message.
*
* @var integer
*/
2011-12-20 20:20:32 +00:00
public $enableSmilies = 1;
2012-06-04 19:10:03 +00:00
2012-08-08 14:51:24 +00:00
/**
* @see wcf\page\AbstractPage::$loginRequired
*/
public $loginRequired = true;
2012-06-04 19:10:03 +00:00
/**
* @see \wcf\page\AbstractPage::$neededModules
*/
public $neededModules = array('CHAT_ACTIVE');
/**
* @see \wcf\page\AbstractPage::$neededPermissions
*/
public $neededPermissions = array();
2012-06-04 19:10:03 +00:00
/**
* The given message-string.
*
* @var string
*/
2011-12-26 16:07:46 +00:00
public $message = '';
2012-06-04 19:10:03 +00:00
/**
* The current room.
*
* @var \wcf\data\chat\room\ChatRoom
*/
2011-12-26 16:07:46 +00:00
public $room = null;
2012-06-04 19:10:03 +00:00
/**
* Values read from the UserStorage of the current user.
*
* @var array
*/
2011-12-13 22:02:00 +00:00
public $userData = array();
/**
* @see \wcf\page\AbstractForm::$useTemplate
*/
2011-12-13 22:02:00 +00:00
public $useTemplate = false;
2012-08-12 18:30:08 +00:00
/**
* shortcut for the active request
* @see wcf\system\request\Request::getRequestObject()
*/
public $request = null;
/**
* Disallows direct access.
*
* @see wcf\page\IPage::__run()
*/
public function __run() {
2013-01-09 20:34:30 +00:00
if (($this->request = \wcf\system\request\RequestHandler::getInstance()->getActiveRequest()->getRequestObject()) === $this) throw new \wcf\system\exception\IllegalLinkException();
2012-08-12 18:30:08 +00:00
parent::__run();
}
2011-12-13 22:02:00 +00:00
/**
2012-02-05 17:49:17 +00:00
* @see \wcf\page\IPage::readData()
2011-12-13 22:02:00 +00:00
*/
public function readData() {
2013-01-19 19:36:40 +00:00
$this->userData['color'] = \chat\util\ChatUtil::readUserData('color');
$this->userData['roomID'] = \chat\util\ChatUtil::readUserData('roomID');
$this->userData['away'] = \chat\util\ChatUtil::readUserData('away');
2013-01-19 19:36:40 +00:00
$cache = data\room\Room::getCache();
2012-09-07 20:37:25 +00:00
if (!isset($cache[$this->userData['roomID']])) throw new \wcf\system\exception\IllegalLinkException();
$this->room = $cache[$this->userData['roomID']];
2011-12-26 16:07:46 +00:00
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
2012-03-24 20:47:03 +00:00
if (!$this->room->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
2011-12-13 22:02:00 +00:00
parent::readData();
}
/**
2012-02-05 17:49:17 +00:00
* @see \wcf\form\IForm::readFormParameters()
2011-12-13 22:02:00 +00:00
*/
public function readFormParameters() {
parent::readFormParameters();
2012-03-04 16:04:10 +00:00
if (isset($_REQUEST['text'])) $this->message = \wcf\util\MessageUtil::stripCrap(StringUtil::trim($_REQUEST['text']));
2011-12-20 20:20:32 +00:00
if (isset($_REQUEST['smilies'])) $this->enableSmilies = intval($_REQUEST['smilies']);
2011-12-13 22:02:00 +00:00
}
/**
2012-02-05 17:49:17 +00:00
* @see \wcf\form\IForm::validate()
2011-12-13 22:02:00 +00:00
*/
public function validate() {
parent::validate();
2012-03-04 14:48:10 +00:00
2011-12-13 22:02:00 +00:00
if ($this->message === '') {
throw new UserInputException('text');
}
2012-03-04 14:48:10 +00:00
if (strlen($this->message) > CHAT_MAX_LENGTH) {
throw new UserInputException('text', 'tooLong');
}
2011-12-13 22:02:00 +00:00
}
/**
2012-02-05 17:49:17 +00:00
* @see \wcf\form\IForm::save()
2011-12-13 22:02:00 +00:00
*/
public function save() {
parent::save();
2013-01-19 19:36:40 +00:00
\chat\util\ChatUtil::writeUserData(array('away' => null));
$commandHandler = new \wcf\system\chat\command\CommandHandler($this->message);
2011-12-27 13:17:13 +00:00
if ($commandHandler->isCommand()) {
try {
$command = $commandHandler->loadCommand();
2013-01-19 19:36:40 +00:00
if ($command->enableSmilies != \chat\system\command\ICommand::SETTING_USER) $this->enableSmilies = $command->enableSmilies;
2012-03-23 22:27:15 +00:00
$this->enableHTML = $command->enableHTML;
2013-01-19 19:36:40 +00:00
if ($command->enableBBCodes != \chat\system\command\ICommand::SETTING_USER) $this->enableBBCodes = $command->enableBBCodes;
2013-01-09 20:34:30 +00:00
2011-12-27 13:17:13 +00:00
$type = $command->getType();
$this->message = $command->getMessage();
$receiver = $command->getReceiver();
2011-12-27 13:17:13 +00:00
}
catch (\wcf\system\chat\command\NotFoundException $e) {
2012-10-20 16:04:46 +00:00
$this->message = WCF::getLanguage()->get('wcf.chat.error.notFound');
2013-01-19 19:36:40 +00:00
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
2011-12-27 13:17:13 +00:00
}
catch (\wcf\system\chat\command\UserNotFoundException $e) {
2012-10-20 16:04:46 +00:00
$this->message = WCF::getLanguage()->getDynamicVariable('wcf.chat.error.userNotFound', array('username' => $e->getUsername()));
2013-01-19 19:36:40 +00:00
$type = data\message\Message::TYPE_ERROR;
2012-03-23 22:27:15 +00:00
$receiver = WCF::getUser()->userID;
2012-10-20 16:04:46 +00:00
$this->enableHTML = 1;
2012-03-23 22:27:15 +00:00
}
2011-12-27 13:17:13 +00:00
catch (\wcf\system\exception\PermissionDeniedException $e) {
2012-10-20 16:04:46 +00:00
$this->message = WCF::getLanguage()->get('wcf.chat.error.permissionDenied');
2013-01-19 19:36:40 +00:00
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
2011-12-27 13:17:13 +00:00
}
2012-03-22 16:38:33 +00:00
catch (\Exception $e) {
2012-10-20 16:04:46 +00:00
$this->message = WCF::getLanguage()->get('wcf.chat.error.exception');
2013-01-19 19:36:40 +00:00
$type = data\message\Message::TYPE_ERROR;
2012-03-22 16:38:33 +00:00
$receiver = WCF::getUser()->userID;
}
2011-12-27 13:17:13 +00:00
}
else {
2013-01-19 19:36:40 +00:00
$type = data\message\Message::TYPE_NORMAL;
$receiver = null;
2011-12-27 13:17:13 +00:00
}
2012-03-22 17:45:36 +00:00
// mark user as back
if ($this->userData['away'] !== null) {
2013-01-19 19:36:40 +00:00
$messageAction = new data\message\MessageAction(array(), 'create', array(
2012-03-22 17:45:36 +00:00
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
2013-01-19 19:36:40 +00:00
'type' => data\message\Message::TYPE_BACK,
2012-03-22 17:45:36 +00:00
'message' => '',
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
$messageAction->executeAction();
}
2013-01-19 19:36:40 +00:00
$this->objectAction = new data\message\MessageAction(array(), 'create', array(
2011-12-13 22:02:00 +00:00
'data' => array(
2011-12-26 16:07:46 +00:00
'roomID' => $this->room->roomID,
2011-12-13 22:02:00 +00:00
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'receiver' => $receiver,
2011-12-13 22:02:00 +00:00
'time' => TIME_NOW,
2011-12-27 13:17:13 +00:00
'type' => $type,
2011-12-13 22:02:00 +00:00
'message' => $this->message,
2011-12-20 20:20:32 +00:00
'enableSmilies' => $this->enableSmilies,
2012-03-23 22:27:15 +00:00
'enableHTML' => $this->enableHTML,
2013-01-09 20:34:30 +00:00
'enableBBCodes' => $this->enableBBCodes,
2011-12-13 22:02:00 +00:00
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
2012-08-10 15:15:25 +00:00
$this->objectAction->executeAction();
2011-12-13 22:02:00 +00:00
2013-01-14 20:08:44 +00:00
// add activity points
2013-01-19 19:36:40 +00:00
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.message', TIME_NOW, WCF::getUser()->userID);
2013-01-14 20:08:44 +00:00
2011-12-13 22:02:00 +00:00
$this->saved();
}
2012-02-05 17:49:17 +00:00
/**
* @see \wcf\page\IPage::show()
2012-02-05 17:49:17 +00:00
*/
public function show() {
2012-03-23 22:27:15 +00:00
header("HTTP/1.0 204 No Content");
2012-02-05 17:49:17 +00:00
parent::show();
}
2011-12-13 21:10:35 +00:00
}