1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-26 22:20:08 +00:00
Tims-Chat/file/lib/data/suspension/Suspension.class.php

140 lines
4.1 KiB
PHP
Raw Normal View History

2012-05-19 19:25:50 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\data\suspension;
2012-05-19 19:25:50 +00:00
use \wcf\system\WCF;
/**
* Represents a chat suspension.
2014-02-28 16:06:50 +00:00
*
2012-05-19 19:25:50 +00:00
* @author Tim Düsterhus
2014-02-27 22:05:09 +00:00
* @copyright 2010-2014 Tim Düsterhus
2012-05-19 19:25:50 +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.suspension
2012-05-19 19:25:50 +00:00
*/
class Suspension extends \chat\data\CHATDatabaseObject {
2012-05-19 19:25:50 +00:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableName
*/
2013-01-19 19:36:40 +00:00
protected static $databaseTableName = 'suspension';
2012-05-19 19:25:50 +00:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
*/
protected static $databaseTableIndexName = 'suspensionID';
2013-06-22 15:40:24 +00:00
const TYPE_MUTE = 'mute';
const TYPE_BAN = 'ban';
2012-05-19 19:25:50 +00:00
2013-05-23 23:21:46 +00:00
/**
* Returns whether the suspension still is valid.
*
* @return boolean
*/
public function isValid() {
return $this->expires > TIME_NOW;
2013-05-23 23:21:46 +00:00
}
2013-06-22 15:40:24 +00:00
/**
* Returns whether the given user may view this suspension.
*
* @param \wcf\data\user\User $user
* @return boolean
*/
public function isVisible($user = null) {
if ($user === null) $user = WCF::getUser();
2013-06-22 15:57:33 +00:00
$user = new \wcf\data\user\UserProfile($user);
$ph = new \chat\system\permission\PermissionHandler($user->getDecoratedObject());
2013-06-22 16:28:50 +00:00
2013-06-22 15:57:33 +00:00
if ($user->getPermission('admin.chat.canManageSuspensions')) return true;
if ($user->getPermission('mod.chat.canG'.$this->type)) return true;
2013-06-22 16:28:50 +00:00
if (!$this->roomID) return false;
2013-06-22 15:40:24 +00:00
if ($ph->getPermission($this->getRoom(), 'mod.can'.ucfirst($this->type))) return true;
2013-06-22 16:28:50 +00:00
2013-06-22 15:40:24 +00:00
return false;
}
/**
* Returns the room of this suspension.
*
* @return \chat\data\room\Room
*/
public function getRoom() {
if (!$this->roomID) return new \chat\data\room\Room(null, array('roomID' => null));
return \chat\data\room\RoomCache::getInstance()->getRoom($this->roomID);
}
2013-05-18 20:05:03 +00:00
/**
* Returns all the suspensions for the specified user (current user if no user was specified).
*
* @param \wcf\data\user\User $user
* @return array
*/
2012-05-19 19:25:50 +00:00
public static function getSuspensionsForUser(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
2013-04-23 14:16:27 +00:00
// load storage
$ush->loadStorage(array($user->userID));
$data = $ush->getStorage(array($user->userID), 'chatSuspensions');
try {
$suspensions = unserialize($data[$user->userID]);
if ($suspensions === false) throw new \wcf\system\exception\SystemException();
}
catch (\wcf\system\exception\SystemException $e) {
2013-06-22 12:40:54 +00:00
$condition = new \wcf\system\database\util\PreparedStatementConditionBuilder();
$condition->add('userID = ?', array($user->userID));
$condition->add('expires > ?', array(TIME_NOW));
2012-05-19 19:25:50 +00:00
$sql = "SELECT
*
FROM
2013-01-19 19:36:40 +00:00
chat".WCF_N."_suspension
2013-06-22 12:40:54 +00:00
".$condition;
2012-05-19 19:25:50 +00:00
$stmt = WCF::getDB()->prepareStatement($sql);
2013-06-22 12:40:54 +00:00
$stmt->execute($condition->getParameters());
2012-05-19 19:25:50 +00:00
$suspensions = array();
2013-04-23 14:16:27 +00:00
while ($suspension = $stmt->fetchObject('\chat\data\suspension\Suspension')) {
$suspensions[$suspension->roomID][$suspension->type] = $suspension;
2012-05-19 19:25:50 +00:00
}
$ush->update($user->userID, 'chatSuspensions', serialize($suspensions));
2012-05-19 19:25:50 +00:00
}
return $suspensions;
}
/**
* Returns the appropriate suspension for user, room and type.
2013-06-22 12:40:54 +00:00
* Returns false if no active suspension was found.
*
2013-01-19 19:36:40 +00:00
* @param \wcf\data\user\User $user
* @param \chat\data\room\Room $room
* @param integer $type
* @return \chat\data\suspension\Suspension
*/
2013-01-19 19:36:40 +00:00
public static function getSuspensionByUserRoomAndType(\wcf\data\user\User $user, \chat\data\room\Room $room, $type) {
2013-06-22 12:40:54 +00:00
$condition = new \wcf\system\database\util\PreparedStatementConditionBuilder();
$condition->add('userID = ?', array($user->userID));
$condition->add('type = ?', array($type));
$condition->add('expires > ?', array(TIME_NOW));
if ($room->roomID) $condition->add('roomID = ?', array($room->roomID));
else $condition->add('roomID IS NULL');
$sql = "SELECT
*
FROM
2013-01-19 19:36:40 +00:00
chat".WCF_N."_suspension
2013-06-22 12:40:54 +00:00
".$condition;
2013-02-01 19:41:59 +00:00
$statement = WCF::getDB()->prepareStatement($sql);
2013-06-22 12:40:54 +00:00
$statement->execute($condition->getParameters());
$row = $statement->fetchArray();
2013-05-18 20:05:03 +00:00
if (!$row) return false;
return new self(null, $row);
}
2012-05-19 19:25:50 +00:00
}