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

219 lines
5.8 KiB
PHP
Raw Normal View History

2011-12-13 21:10:35 +00:00
<?php
namespace wcf\form;
2011-12-13 22:02:00 +00:00
use \wcf\data\chat;
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
2012-01-28 16:50:33 +00:00
* @copyright 2010-2012 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>
* @package be.bastelstu.wcf.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
/**
* 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
*/
2012-03-23 16:45:26 +00:00
public $neededPermissions = array('user.chat.canEnter');
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() {
if (($this->request = RequestHandler::getInstance()->getActiveRequest()->getRequestObject()) === $this) throw new IllegalLinkException();
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() {
$this->userData['color'] = \wcf\util\ChatUtil::readUserData('color');
$this->userData['roomID'] = \wcf\util\ChatUtil::readUserData('roomID');
2012-03-22 17:45:36 +00:00
$this->userData['away'] = \wcf\util\ChatUtil::readUserData('away');
2011-12-26 16:07:46 +00:00
$this->room = chat\room\ChatRoom::getCache()->search($this->userData['roomID']);
if (!$this->room) throw new \wcf\system\exception\IllegalLinkException();
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();
2012-03-22 17:45:36 +00:00
\wcf\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();
if ($command->enableSmilies != \wcf\system\chat\command\ICommand::SMILEY_USER) $this->enableSmilies = $command->enableSmilies;
2012-03-23 22:27:15 +00:00
$this->enableHTML = $command->enableHTML;
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) {
2011-12-27 13:17:13 +00:00
$this->message = WCF::getLanguage()->get('wcf.chat.command.error.notFound');
$type = chat\message\ChatMessage::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
2011-12-27 13:17:13 +00:00
}
catch (\wcf\system\chat\command\UserNotFoundException $e) {
2012-03-23 22:27:15 +00:00
$this->message = WCF::getLanguage()->get('wcf.chat.command.error.userNotFound');
$type = chat\message\ChatMessage::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
}
2011-12-27 13:17:13 +00:00
catch (\wcf\system\exception\PermissionDeniedException $e) {
$this->message = WCF::getLanguage()->get('wcf.chat.command.error.permissionDenied');
$type = chat\message\ChatMessage::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) {
$this->message = WCF::getLanguage()->get('wcf.chat.command.error.exception');
$type = chat\message\ChatMessage::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
}
2011-12-27 13:17:13 +00:00
}
else {
$type = chat\message\ChatMessage::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) {
$messageAction = new chat\message\ChatMessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => chat\message\ChatMessage::TYPE_BACK,
'message' => '',
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
$messageAction->executeAction();
}
2012-08-10 15:15:25 +00:00
$this->objectAction = new chat\message\ChatMessageAction(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,
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
$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
}