Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 284 lines (229 sloc) 9.08 KB
<?php
/*
Copyright (C) 2004-2010 Kestas J. Kuliukas
This file is part of webDiplomacy.
webDiplomacy is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
webDiplomacy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with webDiplomacy. If not, see <http://www.gnu.org/licenses/>.
*/
defined('IN_CODE') or die('This script can not be run by itself.');
/**
* Forum moderator specific actions; related to silencing.
*
* These will mainly be called directly from forms in the forum, or on user profiles.
*
* @package Admin
*/
class adminActionsForum extends adminActions
{
public function __construct()
{
parent::__construct();
$forumActions = array(
'disableSilence' => array(
'name' => 'Unsilence something',
'description' => 'Disables a silence on a user/thread.',
'params' => array('silenceID'=>'Silence ID#'),
),
'changeSilenceLength' => array(
'name' => 'Change silence length',
'description' => 'Alter the length of an existing silence (note that this only applies to user silences; thread silences are indefinite).<br />0 length silences are indefinite.',
'params' => array('silenceID'=>'Silence ID#', 'length'=>'Length (days)'),
),
'createUserSilence' => array(
'name' => 'Silence user',
'description' => 'Silences a user for the given length of time (0 is indefinite).',
'params' => array('userID'=>'User ID','reason'=>'Reason','length'=>'Length (days)')
),
'createThreadSilence' => array(
'name' => 'Silence thread',
'description' => 'Silence a thread/post.',
'params' => array('postID'=>'Post ID','reason'=>'Reason')
),
'createUserThreadSilence' => array(
'name' => 'Silence thread and user',
'description' => 'Silence a thread/post and user, with the user silence acting for the given length of time (0 is indefinite). Thread silences are always indefinite.',
'params' => array('userID'=>'User ID','postID'=>'Post ID','reason'=>'Reason','length'=>'Length (days)')
)
);
adminActions::$actions = array_merge(adminActions::$actions, $forumActions);
}
private static function setNextActivePostSilence($silenceID) {
global $DB;
/*
* For this silence ID find the thread that is being silenced
* Then find silences on posts which are part of that thread
* Find an active silence, and link it to the thread
*/
$silence = new Silence($silenceID);
// Find replacement silences for the post
$tabl = $DB->sql_tabl("
SELECT silence.id
FROM wD_Silences silence
WHERE silence.postID = ".$silence->postID." AND NOT silence.id = ".$silence->id);
while(list($potentialSilenceID) = $DB->tabl_row($tabl)) {
$potentialSilence = new Silence($potentialSilenceID);
if( $potentialSilence->isEnabled() ) {
$DB->sql_put("
UPDATE wD_ForumMessages
SET silenceID = ".$silenceID."
WHERE id=".$potentialSilence->postID);
break; // Only one active silence is needed
}
}
// Find replacement silences for the thread
$tabl = $DB->sql_tabl("
SELECT thread.id, silence.id
FROM wD_ForumMessages thread
INNER JOIN wD_ForumMessages response ON response.toID = thread.id
INNER JOIN wD_Silences silence ON silence.id = response.silenceID
WHERE thread.silenceID = ".$silenceID." AND silence.id = ".$silenceID);
while(list($threadID, $potentialSilenceID) = $DB->tabl_row($tabl)) {
$potentialSilence = new Silence($potentialSilenceID);
if( $potentialSilence->isEnabled() ) {
$DB->sql_put("
UPDATE wD_ForumMessages
SET silenceID = ".$silenceID."
WHERE id=".$threadID);
break; // Only one active silence is needed
}
}
}
private static function setNextActiveUserSilence($silenceID) {
global $DB;
$silence = new Silence($silenceID);
if( !$silence->userID ) return;
$SilencedUser = new User($silence->userID);
foreach($SilencedUser->getSilences() as $potentialSilence) {
if( $potentialSilence->id == $silenceID ) continue;
if( $potentialSilence->isEnabled() ) {
$SilencedUser->silenceID = $potentialSilence->id;
$DB->sql_put("UPDATE wD_Users SET silenceID = ".$potentialSilence->id." WHERE id = ".$SilencedUser->id);
break; // Only one active silence is needed
}
}
}
public function disableSilence(array $params) {
$silence = new Silence($params['silenceID']);
$silence->disable();
/*
* Disabling a silence is tricky, because disabling one silence may bring another silence
* into play as the active silence, so other applicable active silences need to be looked for,
* and linked in the place of this one if found.
*/
self::setNextActiveUserSilence($silence->id);
self::setNextActivePostSilence($silence->id);
return $silence->toString().' disabled.';
}
public function disableSilenceConfirm(array $params) {
$silence = new Silence($params['silenceID']);
return 'Are you sure you want to disable this silence: <b>'.$silence->toString().'</b>?';
}
public function changeSilenceLength(array $params) {
$silence = new Silence($params['silenceID']);
$previousLength = $silence->length;
// This function will validate the given length and check that it's not a post silence
$silence->changeLength($params['length']);
if( !$silence->isEnabled() ) {
// Don't look for changes to posts, because they will not be affected by length changes
self::setNextActiveUserSilence($silence->id);
}
return $silence->toString().' changed from <i>'.
Silence::printLength($previousLength).'</i> to <i>'.
Silence::printLength($silence->length).'</i>.';
}
public function changeSilenceLengthConfirm(array $params) {
$silence = new Silence($params['silenceID']);
if( $params['length'] < 0 )
throw new Exception("Silence length must be non-negative.");
return 'Are you sure you want to change the silence length from <i>'.
Silence::printLength($silence->length).'</i> to <i>'.
Silence::printLength($params['length']).'</i>,
for <b>'.$silence->toString().'</b>?';
}
private static function checkSilenceParams(array $params) {
global $DB;
if( strlen($params['reason']) < 10 )
throw new Exception("Please give a reason.");
if( isset($params['userID']) ) {
$SilencedUser = new User((int)$params['userID']);
if( $params['length'] < 0 )
throw new Exception("Length in days must be greater than 0.");
}
if( isset($params['postID']) ) {
list($threadsFound) = $DB->sql_row("SELECT COUNT(*) FROM wD_ForumMessages WHERE id = ".$params['postID']);
if( $threadsFound == 0 )
throw new Exception("Thread ID # ".$params['postID']." does not exist.");
}
}
public function createUserSilence(array $params)
{
global $User;
self::checkSilenceParams($params);
$silenceID = Silence::create(
$User->id,
$params['reason'],
null,
$params['userID'],
$params['length']
);
$silence = new Silence($silenceID);
return 'User silenced: <br/>' .$silence->toString();
}
public function createUserSilenceConfirm(array $params)
{
self::checkSilenceParams($params);
$UserSilence = new User($params['userID']);
return 'Are you sure you want to silence this user '.
Silence::printLength($params['length']).' because <i>'.
$params['reason'].'</i> ?';
}
public function createThreadSilence(array $params)
{
global $User;
self::checkSilenceParams($params);
$silenceID = Silence::create(
$User->id,
$params['reason'],
$params['postID']
);
$silence = new Silence($silenceID);
return 'Thread silenced: <br/>' .$silence->toString();
}
public function createThreadSilenceConfirm(array $params)
{
self::checkSilenceParams($params);
return 'Are you sure you want to silence the thread containing post ID # '.
$params['postID'].' indefinitely because <i>'.
$params['reason'].'</i> ?';
}
public function createUserThreadSilence(array $params)
{
global $User;
self::checkSilenceParams($params);
$silenceID = Silence::create(
$User->id,
$params['reason'],
$params['postID'],
$params['userID'],
$params['length']
);
$silence = new Silence($silenceID);
return 'User and thread silenced: <br/>' .$silence->toString();
}
public function createUserThreadSilenceConfirm(array $params)
{
self::checkSilenceParams($params);
return 'Are you sure you want to silence this user '.
Silence::printLength($params['length']).', and silence the thread they were posting in,
because <i>'.$params['reason'].'</i> ?';
}
}
?>
Something went wrong with that request. Please try again.