1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2025-01-23 02:10:41 +00:00
Tims-Chat/file/lib/data/room/Room.class.php

206 lines
5.2 KiB
PHP
Raw Normal View History

2011-11-26 16:47:28 +01:00
<?php
2013-01-19 20:36:40 +01:00
namespace chat\data\room;
use \chat\data\suspension\Suspension;
2012-03-01 22:17:40 +01:00
use \wcf\system\WCF;
2011-11-26 16:47:28 +01:00
/**
* Represents a chat room.
*
* @author Tim Düsterhus
2013-01-19 20:36:40 +01:00
* @copyright 2010-2013 Tim Düsterhus
2011-11-26 16:47:28 +01:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 20:36:40 +01:00
* @package be.bastelstu.chat
* @subpackage data.room
2011-11-26 16:47:28 +01:00
*/
class Room extends \chat\data\CHATDatabaseObject implements \wcf\system\request\IRouteController {
2011-11-26 16:47:28 +01:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableName
2011-11-26 16:47:28 +01:00
*/
2013-01-19 20:36:40 +01:00
protected static $databaseTableName = 'room';
2011-11-26 16:47:28 +01:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
2011-11-26 16:47:28 +01:00
*/
protected static $databaseTableIndexName = 'roomID';
2012-03-22 18:45:36 +01:00
/**
* @see \wcf\data\chat\room\ChatRoom::getTitle();
*/
public function __toString() {
return $this->getTitle();
}
2012-03-24 21:47:03 +01:00
/**
* Returns whether the user is allowed to enter the room.
2012-10-20 16:23:00 +02:00
*
* @param \wcf\data\user\User $user
2012-03-24 21:47:03 +01:00
* @return boolean
*/
public function canEnter(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
2013-01-19 20:36:40 +01:00
$ph = new \chat\system\permission\PermissionHandler($user);
$suspensions = Suspension::getSuspensionsForUser($user);
2012-03-24 21:47:03 +01:00
2012-05-19 21:25:50 +02:00
$canEnter = $ph->getPermission($this, 'user.canEnter');
2012-06-04 21:10:03 +02:00
// room suspension
2013-01-19 20:36:40 +01:00
if ($canEnter && isset($suspensions[$this->roomID][Suspension::TYPE_BAN])) {
2013-04-23 16:16:27 +02:00
if ($suspensions[$this->roomID][Suspension::TYPE_BAN]->time > TIME_NOW) {
2012-05-19 21:25:50 +02:00
$canEnter = false;
}
}
2012-06-04 21:10:03 +02:00
// global suspension
2013-01-19 20:36:40 +01:00
if ($canEnter && isset($suspensions[null][Suspension::TYPE_BAN])) {
2013-04-23 16:16:27 +02:00
if ($suspensions[null][Suspension::TYPE_BAN]->time > TIME_NOW) {
$canEnter = false;
}
}
2012-05-19 21:25:50 +02:00
return $canEnter || $ph->getPermission($this, 'mod.canAlwaysEnter');
2012-03-24 21:47:03 +01:00
}
/**
* Returns whether the user is allowed to write messages in this room.
*
2012-10-20 16:23:00 +02:00
* @param \wcf\data\user\User $user
2012-03-24 21:47:03 +01:00
* @return boolean
*/
public function canWrite(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
2013-01-19 20:36:40 +01:00
$ph = new \chat\system\permission\PermissionHandler($user);
$suspensions = Suspension::getSuspensionsForUser($user);
2012-03-24 21:47:03 +01:00
2012-05-19 21:25:50 +02:00
$canWrite = $ph->getPermission($this, 'user.canWrite');
2012-06-04 21:10:03 +02:00
// room suspension
2013-01-19 20:36:40 +01:00
if ($canWrite && isset($suspensions[$this->roomID][Suspension::TYPE_MUTE])) {
2013-04-23 16:16:27 +02:00
if ($suspensions[$this->roomID][Suspension::TYPE_MUTE]->time > TIME_NOW) {
2012-05-19 21:25:50 +02:00
$canWrite = false;
}
}
2012-06-04 21:10:03 +02:00
// global suspension
2013-01-19 20:36:40 +01:00
if ($canWrite && isset($suspensions[null][Suspension::TYPE_MUTE])) {
2013-04-23 16:16:27 +02:00
if ($suspensions[null][Suspension::TYPE_MUTE]->time > TIME_NOW) {
$canWrite = false;
}
}
2012-05-19 21:25:50 +02:00
return $canWrite || $ph->getPermission($this, 'mod.canAlwaysWrite');
2012-03-24 21:47:03 +01:00
}
2011-11-26 16:47:28 +01:00
/**
* Loads the room cache.
*/
2011-11-26 17:37:46 +01:00
public static function getCache() {
2013-02-04 19:27:03 +01:00
return \chat\system\cache\builder\RoomCacheBuilder::getInstance()->getData();
2011-11-26 16:47:28 +01:00
}
/**
* Returns the ID of this chat-room.
*
* @see \wcf\system\request\IRouteController
*/
public function getID() {
return $this->roomID;
}
2011-11-26 16:47:28 +01:00
/**
* Returns the name of this chat-room.
*
* @see \wcf\system\request\IRouteController
2011-11-26 16:47:28 +01:00
*/
public function getTitle() {
2011-12-11 23:29:43 +01:00
return \wcf\system\WCF::getLanguage()->get($this->title);
2011-11-26 16:47:28 +01:00
}
/**
* Returns the number of users currently active in this room.
*
* @return integer
*/
public function countUsers() {
$sql = "SELECT
COUNT(*)
FROM
wcf".WCF_N."_user_storage
WHERE
field = ?
AND fieldValue = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('roomID', $this->roomID));
return $stmt->fetchColumn();
}
/**
* Returns the users that are currently active in this room.
*
2013-04-27 00:38:53 +02:00
* @return \wcf\data\user\UserProfileList
*/
public function getUsers() {
$sql = "SELECT
userID
FROM
wcf".WCF_N."_user_storage
WHERE
2012-03-22 18:45:36 +01:00
field = ?
AND fieldValue = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('roomID', $this->roomID));
$userIDs = array();
2013-01-14 20:56:22 +01:00
while ($userID = $stmt->fetchColumn()) $userIDs[] = $userID;
2012-03-22 18:45:36 +01:00
2013-04-21 20:21:51 +02:00
$userList = new \wcf\data\user\UserProfileList();
if (!empty($userIDs)) $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($userIDs));
else $userList->getConditionBuilder()->add('1 = 0', array());
2013-04-21 20:21:51 +02:00
$userList->readObjects();
2012-03-22 18:45:36 +01:00
2013-04-21 20:21:51 +02:00
return $userList;
}
2013-04-27 00:38:53 +02:00
/**
* Returns the users that "timed out".
*
* @return \wcf\data\user\UserList
*/
public static function getDeadUsers() {
if (\chat\util\ChatUtil::nodePushRunning()) {
$time = TIME_NOW - 120;
}
else {
$time = TIME_NOW;
}
$sql = "SELECT
r.userID
FROM
wcf".WCF_N."_user_storage r
LEFT JOIN
wcf".WCF_N."_user_storage a
ON a.userID = r.userID
AND a.field = ?
WHERE
r.field = ?
AND r.fieldValue IS NOT NULL
AND (
a.fieldValue < ?
OR a.fieldValue IS NULL
)";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('lastActivity', 'roomID', $time - 30));
$userIDs = array();
while ($userID = $stmt->fetchColumn()) $userIDs[] = $userID;
$userList = new \wcf\data\user\UserList();
if (!empty($userIDs)) $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($userIDs));
else $userList->getConditionBuilder()->add('1 = 0', array());
$userList->readObjects();
return $userList;
}
2011-11-26 16:47:28 +01:00
}