/
silence.php
executable file
·271 lines (224 loc) · 6.98 KB
/
silence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
class Silence
{
/**
* Silence ID
* @var int
*/
public $id;
/**
* Silenced user ID
* (this may be null, if a post was silenced without a user)
* @var int/null
*/
public $userID;
/**
* Silenced post ID
* (note that this silences the thread this post belongs to, not the post itself)
* @var int/null
*/
public $postID;
/**
* UserID of the moderator which did the mute
* @var int/null
*/
public $moderatorUserID;
/**
* Is the silence enabled (false if another mod has disabled it)
* @var bool
*/
public $enabled;
/**
* A GMT+0 UNIX timestamp of when the silence was created
* @var bool
*/
public $startTime;
/**
* The length the silence will last for, in days.
* If 0 the silence is indefinite.
*
* (This does not apply to muted threads, which are always permeanently silenced)
*
* @var bool
*/
public $length;
/**
* The reason given for the silence
* @var string
*/
public $reason;
public function isEnabled()
{
return $this->enabled && !$this->isExpired();
}
protected function isExpired()
{
return !($this->length==0 || ( time() < ($this->startTime + $this->length*60*60*24)));
}
public function disable()
{
global $DB;
$this->enabled=false;
$DB->sql_put("UPDATE wD_Silences SET enabled=0 WHERE id=".$this->id);
}
/**
*
* Creates a silence record, and returns the ID. Validation is performed here, plus a
* moderator log record will be created and the post, thread and user records will all be
* linked to the newly created record.
*
* @param int $moderatorUserID
* @param string $reason
* @param int/null $postID The post ID being silenced, or null
* @param int/null $userID The user ID being silenced, or null
* @param int/null $length The length in days (for user silences). If not given this is indefinite
* @return int The silence ID
*/
public static function create($moderatorUserID,$reason,$postID=null,$userID=null,$length=null)
{
global $DB;
$moderatorUserID=(int)$moderatorUserID;
$reason=$DB->escape($reason);
$postID=($postID==null?"NULL":((int)$postID));
$userID=($userID==null?"NULL":((int)$userID));
$length=($length==null?0:((int)$length));
$DB->sql_put(
"INSERT INTO wD_Silences
(userID,postID,moderatorUserID,startTime,length,reason)
VALUES
(".$userID
.",".$postID
.",".$moderatorUserID
.",".time()
.",".$length
.",'".$reason."')"
);
$silenceID = $DB->last_inserted();
// Link the new silence record to the applicable user / forum post records
if( is_numeric($userID) )
$DB->sql_put("UPDATE wD_Users SET silenceID = ".$silenceID." WHERE id=".$userID);
if( is_numeric($postID) )
{
// The post
$DB->sql_put("UPDATE wD_ForumMessages SET silenceID = ".$silenceID." WHERE id=".$postID);
// The thread which the post is a part of
$DB->sql_put("UPDATE wD_ForumMessages thread
INNER JOIN wD_ForumMessages post ON post.toID = thread.id
SET thread.silenceID = ".$silenceID."
WHERE post.id=".$postID);
}
return $silenceID;
}
public function changeLength($length)
{
global $DB;
$length = (int) $length;
if( $length < 0 )
throw new Exception(l_t("The silence length must be non-negative"));
if( $this->userID == null || !$this->userID )
throw new Exception(l_t("Cannot apply a silence length to a post silence; post silences are indefinite."));
$DB->sql_put("UPDATE wD_Silences SET length=".$length." WHERE id=".$this->id);
$this->length = $length;
}
public function load($id)
{
global $DB;
$record = array();
if( is_array($id) )
{
// If it's an array this may be coming in from a forum joined hash
$record = $id;
}
else
{
$id= (int)$id;
// Alias the column names, so that the data can be loaded from a linked join or from here the same way
$record = $DB->sql_hash(
"SELECT
id as silenceID,
userID as silenceUserID,
postID as silencePostID,
moderatorUserID as silenceModeratorUserID,
enabled as silenceEnabled,
startTime as silenceStartTime,
length as silenceLength,
reason as silenceReason
FROM wD_Silences
WHERE id=".$id
);
}
$this->id = (int)$record['silenceID'];
$this->userID = (int)$record['silenceUserID'];
$this->postID = (int)$record['silencePostID'];
$this->moderatorUserID = (int)$record['silenceModeratorUserID'];
$this->enabled = ($record['silenceEnabled']!=0);
$this->startTime = (int)$record['silenceStartTime'];
$this->length = (int)$record['silenceLength'];
$this->reason = $record['silenceReason'];
}
public function __construct($id)
{
$this->load($id);
}
public static function isSilenced(array $forumRecord)
{
return ( isset($forumRecord['silenceID']) && is_numeric($forumRecord['silenceID']));
}
/**
* A function which detects all inputs from silence related forms,
* from $_REQUEST parameters, and acts on them (e.g. creating silences,
* disabling, etc)
*
* Validation, permission checking, etc, are done here, so that it can be
* called from anywhere.
*
* @return string A text message containing the results. Will be "" if nothing happened.
*/
public static function formActions()
{
global $User;
if( !$User->type['ForumModerator'] ) return;
// Validation is done within create(), so these values can be passed straight through
if(isset($_REQUEST['silencePostID']) && isset($_REQUEST['silenceReason']) )
{
return l_t("Silence created successfully");
}
if( isset($_REQUEST['disableSilenceID']) )
{
$silence = new Silence();
$silence->load($_REQUEST['disableSilenceID']);
$silence->disable();
return l_t("Silence disabled successfully");
}
return "";
}
public function toString()
{
$Moderator = new User($this->moderatorUserID);
$startTime = libTime::text($this->startTime);
$endTime = ( $this->length == 0 ? l_t("Indefinite") : libTime::text($this->startTime + $this->length * 60 * 60 * 24));
$silenceData = array(
'Status' => '<b>'.($this->enabled ? ( $this->isExpired() ? l_t('Ended') : l_t('Active') ) : l_t('Disabled') ).'</b>',
'Mod' => $Moderator->profile_link(),
'Started' => $startTime,
'Ends' => $endTime,
'Reason' => $this->reason
);
if( $this->userID )
{
$SilencedUser = new User($this->userID);
$silenceData['User'] = $SilencedUser->profile_link();
}
if( $this->postID )
$silenceData['Thread'] = libHTML::threadLink($this->postID);
$strArr = array('<ul class="formlist"><li>');
foreach($silenceData as $k=>$v)
$strArr[] = l_t($k).": <i>".$v."</i>";
$strArr[]='</li></ul>';
return implode("</li><li>",$strArr);
}
public static function printLength($length)
{
return ($length==0 ? l_t("indefinitely") : l_t("for %s days",$length) );
}
}