2011-11-26 15:47:28 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\data\room;
|
|
|
|
use \chat\data\suspension\Suspension;
|
2012-03-01 21:17:40 +00:00
|
|
|
use \wcf\system\WCF;
|
2011-11-26 15:47:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat room.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2011-11-26 15:47:28 +00:00
|
|
|
* @author Tim Düsterhus
|
2014-02-27 22:05:09 +00:00
|
|
|
* @copyright 2010-2014 Tim Düsterhus
|
2011-11-26 15:47:28 +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 data.room
|
2011-11-26 15:47:28 +00:00
|
|
|
*/
|
2013-01-26 21:46:54 +00:00
|
|
|
class Room extends \chat\data\CHATDatabaseObject implements \wcf\system\request\IRouteController {
|
2011-11-26 15:47:28 +00:00
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableName
|
2011-11-26 15:47:28 +00:00
|
|
|
*/
|
2013-01-19 19:36:40 +00:00
|
|
|
protected static $databaseTableName = 'room';
|
2011-11-26 15:47:28 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
|
2011-11-26 15:47:28 +00:00
|
|
|
*/
|
|
|
|
protected static $databaseTableIndexName = 'roomID';
|
2012-03-22 17:45:36 +00:00
|
|
|
|
2013-08-19 20:14:59 +00:00
|
|
|
/**
|
|
|
|
* cached users
|
|
|
|
*
|
|
|
|
* @var array<\wcf\data\user\UserProfile>
|
|
|
|
*/
|
|
|
|
protected static $users = null;
|
|
|
|
|
2014-08-02 22:41:28 +00:00
|
|
|
/**
|
|
|
|
* room id of the “global” pseudo chat room
|
|
|
|
*/
|
|
|
|
const GLOBAL_ROOM = -1;
|
|
|
|
|
2012-03-01 21:11:20 +00:00
|
|
|
/**
|
|
|
|
* @see \wcf\data\chat\room\ChatRoom::getTitle();
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->getTitle();
|
|
|
|
}
|
2015-03-21 23:10:49 +00:00
|
|
|
|
2012-03-24 20:47:03 +00:00
|
|
|
/**
|
2015-03-21 23:10:49 +00:00
|
|
|
* Returns whther the user is allowed to always enter the room.
|
2012-10-20 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @param \wcf\data\user\User $user
|
2012-03-24 20:47:03 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-03-21 23:10:49 +00:00
|
|
|
public function canAlwaysEnter(\wcf\data\user\User $user = null) {
|
2012-10-19 15:44:30 +00:00
|
|
|
if ($user === null) $user = WCF::getUser();
|
2013-05-23 20:43:37 +00:00
|
|
|
if (!$user->userID) return false;
|
2013-06-22 15:57:33 +00:00
|
|
|
$user = new \wcf\data\user\UserProfile($user);
|
2012-10-19 15:44:30 +00:00
|
|
|
|
2014-02-27 18:25:30 +00:00
|
|
|
if ($this->isPermanent && $user->getPermission('admin.chat.canManageSuspensions')) return true;
|
|
|
|
if ($this->isPermanent && $user->getPermission('mod.chat.canGban')) return true;
|
2013-06-22 15:57:33 +00:00
|
|
|
|
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
|
|
|
|
if ($ph->getPermission($this, 'mod.canAlwaysEnter')) return true;
|
|
|
|
if ($ph->getPermission($this, 'mod.canBan')) return true;
|
2015-03-21 23:10:49 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to enter the room.
|
|
|
|
*
|
|
|
|
* @param \wcf\data\user\User $user
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEnter(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
|
|
|
if (!$user->userID) return false;
|
|
|
|
if ($this->canAlwaysEnter($user)) return true;
|
|
|
|
$user = new \wcf\data\user\UserProfile($user);
|
2012-03-24 20:47:03 +00:00
|
|
|
|
2015-03-21 23:10:49 +00:00
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
|
2013-06-22 16:28:50 +00:00
|
|
|
if (!$ph->getPermission($this, 'user.canEnter')) return false;
|
2013-06-22 15:57:33 +00:00
|
|
|
|
|
|
|
$suspensions = Suspension::getSuspensionsForUser($user->getDecoratedObject());
|
2012-06-04 19:10:03 +00:00
|
|
|
// room suspension
|
2013-06-22 16:28:50 +00:00
|
|
|
if (isset($suspensions[$this->roomID][Suspension::TYPE_BAN])) {
|
2013-05-23 23:21:46 +00:00
|
|
|
if ($suspensions[$this->roomID][Suspension::TYPE_BAN]->isValid()) {
|
2013-06-22 16:28:50 +00:00
|
|
|
return false;
|
2012-05-19 19:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 19:10:03 +00:00
|
|
|
|
|
|
|
// global suspension
|
2013-06-22 16:28:50 +00:00
|
|
|
if (isset($suspensions[null][Suspension::TYPE_BAN])) {
|
2013-05-23 23:21:46 +00:00
|
|
|
if ($suspensions[null][Suspension::TYPE_BAN]->isValid()) {
|
2013-06-22 16:28:50 +00:00
|
|
|
return false;
|
2012-05-19 19:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 16:28:50 +00:00
|
|
|
return true;
|
2012-03-24 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to write messages in this room.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2012-10-20 14:23:00 +00:00
|
|
|
* @param \wcf\data\user\User $user
|
2012-03-24 20:47:03 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-10-19 15:44:30 +00:00
|
|
|
public function canWrite(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
2013-05-23 20:43:37 +00:00
|
|
|
if (!$user->userID) return false;
|
2013-06-22 15:57:33 +00:00
|
|
|
$user = new \wcf\data\user\UserProfile($user);
|
|
|
|
|
|
|
|
if ($user->getPermission('admin.chat.canManageSuspensions')) return true;
|
|
|
|
if ($user->getPermission('mod.chat.canGmute')) return true;
|
|
|
|
|
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
|
|
|
|
if ($ph->getPermission($this, 'mod.canAlwaysWrite')) return true;
|
|
|
|
if ($ph->getPermission($this, 'mod.canMute')) return true;
|
2012-10-19 15:44:30 +00:00
|
|
|
|
2013-06-22 16:28:50 +00:00
|
|
|
if (!$ph->getPermission($this, 'user.canWrite')) return false;
|
2012-03-24 20:47:03 +00:00
|
|
|
|
2013-06-22 15:57:33 +00:00
|
|
|
$suspensions = Suspension::getSuspensionsForUser($user->getDecoratedObject());
|
2012-06-04 19:10:03 +00:00
|
|
|
// room suspension
|
2013-06-22 16:28:50 +00:00
|
|
|
if (isset($suspensions[$this->roomID][Suspension::TYPE_MUTE])) {
|
2013-05-23 23:21:46 +00:00
|
|
|
if ($suspensions[$this->roomID][Suspension::TYPE_MUTE]->isValid()) {
|
2013-06-22 16:28:50 +00:00
|
|
|
return false;
|
2012-05-19 19:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 19:10:03 +00:00
|
|
|
|
|
|
|
// global suspension
|
2013-06-22 16:28:50 +00:00
|
|
|
if (isset($suspensions[null][Suspension::TYPE_MUTE])) {
|
2013-05-23 23:21:46 +00:00
|
|
|
if ($suspensions[null][Suspension::TYPE_MUTE]->isValid()) {
|
2013-06-22 16:28:50 +00:00
|
|
|
return false;
|
2012-05-19 19:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-19 19:25:50 +00:00
|
|
|
|
2013-06-22 16:28:50 +00:00
|
|
|
return true;
|
2012-03-24 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 16:53:02 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to mute other users in this room.
|
|
|
|
*
|
2014-07-29 01:12:51 +00:00
|
|
|
* @param \wcf\data\user\User $user
|
2014-03-02 16:53:02 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-07-29 01:12:51 +00:00
|
|
|
public function canMute(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
|
|
|
if (!$user->userID) return false;
|
|
|
|
$user = new \wcf\data\user\UserProfile($user);
|
2014-03-02 16:53:02 +00:00
|
|
|
|
2014-07-29 01:12:51 +00:00
|
|
|
if ($user->getPermission('admin.chat.canManageSuspensions')) return true;
|
|
|
|
if ($user->getPermission('mod.chat.canGmute')) return true;
|
|
|
|
|
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
|
2014-03-02 16:53:02 +00:00
|
|
|
return $ph->getPermission($this, 'mod.canMute');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to ban other users in this room.
|
|
|
|
*
|
2014-07-29 01:12:51 +00:00
|
|
|
* @param \wcf\data\user\User $user
|
2014-03-02 16:53:02 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-07-29 01:12:51 +00:00
|
|
|
public function canBan(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
|
|
|
if (!$user->userID) return false;
|
|
|
|
$user = new \wcf\data\user\UserProfile($user);
|
2014-03-02 16:53:02 +00:00
|
|
|
|
2014-07-29 01:12:51 +00:00
|
|
|
if ($user->getPermission('admin.chat.canManageSuspensions')) return true;
|
|
|
|
if ($user->getPermission('mod.chat.canGban')) return true;
|
|
|
|
|
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
|
2014-03-02 16:53:02 +00:00
|
|
|
return $ph->getPermission($this, 'mod.canBan');
|
|
|
|
}
|
|
|
|
|
2011-11-26 16:56:51 +00:00
|
|
|
/**
|
2013-05-26 15:19:04 +00:00
|
|
|
* Returns the ID of this chatroom.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2012-03-01 21:11:20 +00:00
|
|
|
* @see \wcf\system\request\IRouteController
|
2011-11-26 16:56:51 +00:00
|
|
|
*/
|
2012-03-01 21:11:20 +00:00
|
|
|
public function getID() {
|
|
|
|
return $this->roomID;
|
2011-11-26 16:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 15:47:28 +00:00
|
|
|
/**
|
2013-05-26 15:19:04 +00:00
|
|
|
* Returns the name of this chatroom.
|
2011-11-26 15:47:28 +00:00
|
|
|
*
|
2011-11-26 16:56:51 +00:00
|
|
|
* @see \wcf\system\request\IRouteController
|
2011-11-26 15:47:28 +00:00
|
|
|
*/
|
2011-11-26 16:56:51 +00:00
|
|
|
public function getTitle() {
|
2011-12-11 22:29:43 +00:00
|
|
|
return \wcf\system\WCF::getLanguage()->get($this->title);
|
2011-11-26 15:47:28 +00:00
|
|
|
}
|
2011-11-26 16:56:51 +00:00
|
|
|
|
2013-05-26 15:19:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the topic of this chat room
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTopic() {
|
|
|
|
return \wcf\system\WCF::getLanguage()->get($this->topic);
|
|
|
|
}
|
|
|
|
|
2011-11-26 16:56:51 +00:00
|
|
|
/**
|
2012-03-01 21:11:20 +00:00
|
|
|
* Returns the users that are currently active in this room.
|
|
|
|
*
|
2013-08-19 20:14:59 +00:00
|
|
|
* @return array<\wcf\data\user\UserProfile>
|
2011-11-26 16:56:51 +00:00
|
|
|
*/
|
2012-03-01 21:11:20 +00:00
|
|
|
public function getUsers() {
|
2013-08-19 20:14:59 +00:00
|
|
|
if (self::$users === null) {
|
|
|
|
$userList = new \wcf\data\user\UserProfileList();
|
|
|
|
$userList->getConditionBuilder()->add('user_table.chatRoomID IS NOT NULL', array());
|
|
|
|
|
|
|
|
$userList->readObjects();
|
|
|
|
$users = $userList->getObjects();
|
|
|
|
|
2014-03-03 02:25:37 +00:00
|
|
|
foreach ($users as $userID => $user) {
|
2013-08-19 20:14:59 +00:00
|
|
|
if (!isset(self::$users[$user->chatRoomID])) self::$users[$user->chatRoomID] = array();
|
2014-03-03 02:25:37 +00:00
|
|
|
self::$users[$user->chatRoomID][$userID] = $user;
|
2013-08-19 20:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-02 22:41:28 +00:00
|
|
|
|
|
|
|
if ($this->roomID === self::GLOBAL_ROOM) {
|
|
|
|
return array_reduce(self::$users, function($a, $b) {
|
|
|
|
return $a + $b;
|
|
|
|
}, array());
|
|
|
|
}
|
|
|
|
|
2013-08-19 20:14:59 +00:00
|
|
|
if (!isset(self::$users[$this->roomID])) self::$users[$this->roomID] = array();
|
2012-03-22 17:45:36 +00:00
|
|
|
|
2013-08-19 20:14:59 +00:00
|
|
|
return self::$users[$this->roomID];
|
2011-11-26 16:56:51 +00:00
|
|
|
}
|
2013-04-26 22:38:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the users that "timed out".
|
|
|
|
*
|
|
|
|
* @return \wcf\data\user\UserList
|
|
|
|
*/
|
|
|
|
public static function getDeadUsers() {
|
2014-09-19 21:34:17 +00:00
|
|
|
if (\wcf\system\push\PushHandler::getInstance()->isEnabled()) {
|
2013-05-24 15:30:27 +00:00
|
|
|
$time = TIME_NOW - 180;
|
2013-04-26 22:38:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$time = TIME_NOW;
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:29:17 +00:00
|
|
|
$sql = "SELECT userID
|
|
|
|
FROM wcf".WCF_N."_user
|
|
|
|
WHERE chatRoomID IS NOT NULL
|
|
|
|
AND chatLastActivity < ?
|
|
|
|
FOR UPDATE";
|
|
|
|
$statement = WCF::getDB()->prepareStatement($sql);
|
|
|
|
$statement->execute([ $time - 30 ]);
|
|
|
|
$userIDs = [];
|
|
|
|
while ($userID = $statement->fetchColumn()) $userIDs[] = $userID;
|
|
|
|
|
2013-04-26 22:38:53 +00:00
|
|
|
$userList = new \wcf\data\user\UserList();
|
2016-05-31 21:29:17 +00:00
|
|
|
if (!empty($userIDs)) {
|
|
|
|
$userList->getConditionBuilder()->add('user_table.userID IN (?)', array($userIDs));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$userList->getConditionBuilder()->add('1 = 0', []);
|
|
|
|
}
|
2013-04-26 22:38:53 +00:00
|
|
|
|
|
|
|
$userList->readObjects();
|
|
|
|
|
|
|
|
return $userList;
|
|
|
|
}
|
2011-11-26 15:47:28 +00:00
|
|
|
}
|