diff --git a/file/lib/data/chat/room/ChatRoom.class.php b/file/lib/data/chat/room/ChatRoom.class.php index 1d2d831..1b48d02 100644 --- a/file/lib/data/chat/room/ChatRoom.class.php +++ b/file/lib/data/chat/room/ChatRoom.class.php @@ -1,5 +1,6 @@ getPermission($this, 'user.canEnter') || $ph->getPermission($this, 'mod.canAlwaysEnter'); + $canEnter = $ph->getPermission($this, 'user.canEnter'); + if (isset($suspensions[$this->roomID][ChatSuspension::TYPE_BAN])) { + if ($suspensions[$this->roomID][ChatSuspension::TYPE_BAN]['time'] > TIME_NOW) { + $canEnter = false; + } + } + return $canEnter || $ph->getPermission($this, 'mod.canAlwaysEnter'); } /** @@ -56,8 +64,16 @@ public function canEnter() { */ public function canWrite() { $ph = \wcf\system\chat\permission\ChatPermissionHandler::getInstance(); + $suspensions = ChatSuspension::getSuspensionsForUser(); - return $ph->getPermission($this, 'user.canWrite') || $ph->getPermission($this, 'mod.canAlwaysWrite'); + $canWrite = $ph->getPermission($this, 'user.canWrite'); + if (isset($suspensions[$this->roomID][ChatSuspension::TYPE_MUTE])) { + if ($suspensions[$this->roomID][ChatSuspension::TYPE_MUTE]['time'] > TIME_NOW) { + $canWrite = false; + } + } + + return $canWrite || $ph->getPermission($this, 'mod.canAlwaysWrite'); } /** diff --git a/file/lib/data/chat/suspension/ChatSuspension.class.php b/file/lib/data/chat/suspension/ChatSuspension.class.php new file mode 100644 index 0000000..7e70929 --- /dev/null +++ b/file/lib/data/chat/suspension/ChatSuspension.class.php @@ -0,0 +1,52 @@ + + * @package be.bastelstu.wcf.chat + * @subpackage data.chat.suspension + */ +class ChatSuspension extends \wcf\data\DatabaseObject { + /** + * @see \wcf\data\DatabaseObject::$databaseTableName + */ + protected static $databaseTableName = 'chat_suspension'; + + /** + * @see \wcf\data\DatabaseObject::$databaseTableIndexName + */ + protected static $databaseTableIndexName = 'suspensionID'; + + const TYPE_MUTE = 1; + const TYPE_BAN = 2; + + public static function getSuspensionsForUser(\wcf\data\user\User $user = null) { + if ($user === null) $user = WCF::getUser(); + $suspensions = \wcf\util\ChatUtil::readUserData('suspensions', $user); + if ($suspensions === null) { + $sql = "SELECT + * + FROM + wcf".WCF_N."_chat_suspension + WHERE + userID = ? + AND time > ?"; + $stmt = WCF::getDB()->prepareStatement($sql); + $stmt->execute(array($user->userID, TIME_NOW)); + + $suspensions = array(); + while ($row = $stmt->fetchArray()) { + $suspensions[$row['roomID']][$row['type']] = $row; + } + + \wcf\util\ChatUtil::writeUserData(array('suspensions' => $suspensions), $user); + } + + return $suspensions; + } +} diff --git a/file/lib/data/chat/suspension/ChatSuspensionAction.class.php b/file/lib/data/chat/suspension/ChatSuspensionAction.class.php new file mode 100644 index 0000000..774aaa0 --- /dev/null +++ b/file/lib/data/chat/suspension/ChatSuspensionAction.class.php @@ -0,0 +1,38 @@ + + * @package be.bastelstu.wcf.chat + * @subpackage data.chat.suspension + */ +class ChatSuspensionAction extends \wcf\data\AbstractDatabaseObjectAction { + /** + * @see \wcf\data\AbstractDatabaseObjectAction::$className + */ + protected $className = '\wcf\data\chat\suspension\ChatSuspensionEditor'; + + /** + * Deletes expired suspensions. + * + * @return integer Number of deleted suspensions + */ + 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)); + $objectIDs = array(); + while ($objectIDs[] = $stmt->fetchColumn()); + + return call_user_func(array($this->className, 'deleteAll'), $objectIDs); + } +} diff --git a/file/lib/data/chat/suspension/ChatSuspensionEditor.class.php b/file/lib/data/chat/suspension/ChatSuspensionEditor.class.php new file mode 100644 index 0000000..d254863 --- /dev/null +++ b/file/lib/data/chat/suspension/ChatSuspensionEditor.class.php @@ -0,0 +1,29 @@ + + * @package be.bastelstu.wcf.chat + * @subpackage data.chat.suspension + */ +class ChatSuspensionEditor extends \wcf\data\DatabaseObjectEditor implements \wcf\data\IEditableCachedObject { + /** + * @see \wcf\data\DatabaseObjectDecorator::$baseClass + */ + protected static $baseClass = '\wcf\data\chat\suspension\ChatSuspension'; + + /** + * Clears the suspension cache. + */ + public static function resetCache() { + $packageID = \wcf\util\ChatUtil::getPackageID(); + $ush = \wcf\system\user\storage\UserStorageHandler::getInstance(); + + $ush->resetAll('suspensions', $packageID); + } +} diff --git a/file/lib/system/chat/permission/ChatPermissionHandler.class.php b/file/lib/system/chat/permission/ChatPermissionHandler.class.php index 4691c5c..5f0215e 100644 --- a/file/lib/system/chat/permission/ChatPermissionHandler.class.php +++ b/file/lib/system/chat/permission/ChatPermissionHandler.class.php @@ -16,12 +16,6 @@ */ class ChatPermissionHandler extends \wcf\system\SingletonFactory { protected $chatPermissions = array(); - protected static $defaults = array( - 'user.canEnter' => true, - 'user.canWrite' => true, - 'mod.canAlwaysEnter' => false, - 'mod.canAlwaysWrite' => false - ); /** * @see \wcf\system\SingletonFactory::init() diff --git a/file/lib/util/ChatUtil.class.php b/file/lib/util/ChatUtil.class.php index 7fff536..4d951b3 100644 --- a/file/lib/util/ChatUtil.class.php +++ b/file/lib/util/ChatUtil.class.php @@ -29,7 +29,7 @@ final class ChatUtil { */ const PACKAGE_IDENTIFIER = 'be.bastelstu.wcf.chat'; - public static $serialize = array('color' => true); + public static $serialize = array('color' => true, 'suspensions' => true); /** * Cached packageID of Tims Chat. @@ -158,7 +158,7 @@ public static function readUserData($field, \wcf\data\user\User $user = null) { $data[$user->userID] = array(1 => self::getRandomNumber(), 2 => self::getRandomNumber() * 0xFFFF); break; } - static::writeUserData(array($field => $data[$user->userID])); + if ($data[$user->userID] !== null) static::writeUserData(array($field => $data[$user->userID])); return $data[$user->userID]; }