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;
|
2011-11-26 16:37:46 +00:00
|
|
|
use \wcf\system\cache\CacheHandler;
|
2012-03-01 21:17:40 +00:00
|
|
|
use \wcf\system\WCF;
|
2011-11-26 15:47:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat room.
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
2013-01-19 19:36:40 +00:00
|
|
|
* @copyright 2010-2013 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-19 19:36:40 +00:00
|
|
|
class Room extends \wcf\data\DatabaseObject 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
|
|
|
|
2011-11-26 15:47:28 +00:00
|
|
|
/**
|
|
|
|
* Caches rooms.
|
|
|
|
*
|
|
|
|
* @var array<wcf\data\chat\room\ChatRoom>
|
|
|
|
*/
|
|
|
|
protected static $cache = null;
|
|
|
|
|
2012-03-01 21:11:20 +00:00
|
|
|
/**
|
|
|
|
* @see \wcf\data\chat\room\ChatRoom::getTitle();
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->getTitle();
|
|
|
|
}
|
|
|
|
|
2012-03-24 20:47:03 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to 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
|
|
|
|
*/
|
2012-10-19 15:44:30 +00:00
|
|
|
public function canEnter(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
|
|
|
|
2013-01-19 19:36:40 +00:00
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user);
|
|
|
|
$suspensions = Suspension::getSuspensionsForUser($user);
|
2012-03-24 20:47:03 +00:00
|
|
|
|
2012-05-19 19:25:50 +00:00
|
|
|
$canEnter = $ph->getPermission($this, 'user.canEnter');
|
2012-06-04 19:10:03 +00:00
|
|
|
// room suspension
|
2013-01-19 19:36:40 +00:00
|
|
|
if ($canEnter && isset($suspensions[$this->roomID][Suspension::TYPE_BAN])) {
|
|
|
|
if ($suspensions[$this->roomID][Suspension::TYPE_BAN]['time'] > TIME_NOW) {
|
2012-05-19 19:25:50 +00:00
|
|
|
$canEnter = false;
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 19:10:03 +00:00
|
|
|
|
|
|
|
// global suspension
|
2013-01-19 19:36:40 +00:00
|
|
|
if ($canEnter && isset($suspensions[null][Suspension::TYPE_BAN])) {
|
|
|
|
if ($suspensions[null][Suspension::TYPE_BAN]['time'] > TIME_NOW) {
|
2012-05-19 19:27:49 +00:00
|
|
|
$canEnter = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-19 19:25:50 +00:00
|
|
|
return $canEnter || $ph->getPermission($this, 'mod.canAlwaysEnter');
|
2012-03-24 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the user is allowed to write messages in this 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
|
|
|
|
*/
|
2012-10-19 15:44:30 +00:00
|
|
|
public function canWrite(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
|
|
|
|
2013-01-19 19:36:40 +00:00
|
|
|
$ph = new \chat\system\permission\PermissionHandler($user);
|
|
|
|
$suspensions = Suspension::getSuspensionsForUser($user);
|
2012-03-24 20:47:03 +00:00
|
|
|
|
2012-05-19 19:25:50 +00:00
|
|
|
$canWrite = $ph->getPermission($this, 'user.canWrite');
|
2012-06-04 19:10:03 +00:00
|
|
|
// room suspension
|
2013-01-19 19:36:40 +00:00
|
|
|
if ($canWrite && isset($suspensions[$this->roomID][Suspension::TYPE_MUTE])) {
|
|
|
|
if ($suspensions[$this->roomID][Suspension::TYPE_MUTE]['time'] > TIME_NOW) {
|
2012-05-19 19:25:50 +00:00
|
|
|
$canWrite = false;
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 19:10:03 +00:00
|
|
|
|
|
|
|
// global suspension
|
2013-01-19 19:36:40 +00:00
|
|
|
if ($canWrite && isset($suspensions[null][Suspension::TYPE_MUTE])) {
|
|
|
|
if ($suspensions[null][Suspension::TYPE_MUTE]['time'] > TIME_NOW) {
|
2012-05-19 19:27:49 +00:00
|
|
|
$canWrite = false;
|
|
|
|
}
|
|
|
|
}
|
2012-05-19 19:25:50 +00:00
|
|
|
|
|
|
|
return $canWrite || $ph->getPermission($this, 'mod.canAlwaysWrite');
|
2012-03-24 20:47:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 21:11:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of users currently active in this room.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function countUsers() {
|
|
|
|
$sql = "SELECT
|
2012-03-22 17:45:36 +00:00
|
|
|
COUNT(*)
|
2012-03-01 21:11:20 +00:00
|
|
|
FROM
|
|
|
|
wcf".WCF_N."_user_storage
|
|
|
|
WHERE
|
2012-03-22 17:45:36 +00:00
|
|
|
field = ?
|
|
|
|
AND fieldValue = ?";
|
2012-03-01 21:11:20 +00:00
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
2013-01-09 16:35:30 +00:00
|
|
|
$stmt->execute(array('roomID', $this->roomID));
|
2012-03-01 21:11:20 +00:00
|
|
|
|
2012-03-22 17:45:36 +00:00
|
|
|
return $stmt->fetchColumn();
|
2012-03-01 21:11:20 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 15:47:28 +00:00
|
|
|
/**
|
|
|
|
* Loads the room cache.
|
|
|
|
*/
|
2011-11-26 16:37:46 +00:00
|
|
|
public static function getCache() {
|
|
|
|
if (self::$cache === null) {
|
|
|
|
CacheHandler::getInstance()->addResource(
|
2013-01-19 19:36:40 +00:00
|
|
|
'chatRooms',
|
|
|
|
CHAT_DIR.'cache/cache.rooms.php',
|
|
|
|
'\chat\system\cache\builder\RoomCacheBuilder'
|
2011-11-26 16:37:46 +00:00
|
|
|
);
|
2013-01-19 19:36:40 +00:00
|
|
|
self::$cache = CacheHandler::getInstance()->get('chatRooms');
|
2011-11-26 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$cache;
|
2011-11-26 15:47:28 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 16:56:51 +00:00
|
|
|
/**
|
2012-03-01 21:11:20 +00:00
|
|
|
* Returns the ID of this chat-room.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Returns the name of this chat-room.
|
|
|
|
*
|
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
|
|
|
|
|
|
|
/**
|
2012-03-01 21:11:20 +00:00
|
|
|
* Returns the users that are currently active in this room.
|
|
|
|
*
|
|
|
|
* @return array<\wcf\data\user\User>
|
2011-11-26 16:56:51 +00:00
|
|
|
*/
|
2012-03-01 21:11:20 +00:00
|
|
|
public function getUsers() {
|
|
|
|
$sql = "SELECT
|
|
|
|
userID
|
|
|
|
FROM
|
|
|
|
wcf".WCF_N."_user_storage
|
|
|
|
WHERE
|
2012-03-22 17:45:36 +00:00
|
|
|
field = ?
|
|
|
|
AND fieldValue = ?";
|
2012-03-01 21:11:20 +00:00
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
2013-01-09 16:35:30 +00:00
|
|
|
$stmt->execute(array('roomID', $this->roomID));
|
2012-03-01 21:11:20 +00:00
|
|
|
$userIDs = array();
|
2013-01-14 19:56:22 +00:00
|
|
|
while ($userID = $stmt->fetchColumn()) $userIDs[] = $userID;
|
2012-03-22 17:45:36 +00:00
|
|
|
|
2013-01-14 19:56:22 +00:00
|
|
|
if (empty($userIDs)) return array();
|
2012-03-22 17:45:36 +00:00
|
|
|
|
2012-03-01 21:11:20 +00:00
|
|
|
$sql = "SELECT
|
2012-03-22 17:45:36 +00:00
|
|
|
u.*,
|
2012-10-19 15:44:30 +00:00
|
|
|
st.fieldValue AS awayStatus
|
2012-03-01 21:11:20 +00:00
|
|
|
FROM
|
2012-03-22 17:45:36 +00:00
|
|
|
wcf".WCF_N."_user u
|
|
|
|
LEFT JOIN
|
2012-05-19 19:37:15 +00:00
|
|
|
wcf".WCF_N."_user_storage st
|
2012-03-22 17:45:36 +00:00
|
|
|
ON (
|
2012-05-19 19:37:15 +00:00
|
|
|
u.userID = st.userID
|
2013-01-09 16:35:30 +00:00
|
|
|
AND st.field = ?
|
2012-05-19 19:37:15 +00:00
|
|
|
)
|
2012-03-01 21:11:20 +00:00
|
|
|
WHERE
|
2012-05-19 18:49:50 +00:00
|
|
|
u.userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').")
|
2012-03-01 21:11:20 +00:00
|
|
|
ORDER BY
|
2012-03-22 17:45:36 +00:00
|
|
|
u.username ASC";
|
2012-03-01 21:11:20 +00:00
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
2013-01-09 16:35:30 +00:00
|
|
|
array_unshift($userIDs, 'away');
|
2012-05-19 18:49:50 +00:00
|
|
|
$stmt->execute($userIDs);
|
2012-03-22 17:45:36 +00:00
|
|
|
|
2012-03-01 21:11:20 +00:00
|
|
|
return $stmt->fetchObjects('\wcf\data\user\User');
|
2011-11-26 16:56:51 +00:00
|
|
|
}
|
2011-11-26 15:47:28 +00:00
|
|
|
}
|