-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
230 lines (188 loc) · 9.21 KB
/
plugin.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
<?php
if (!defined("IN_ESO")) exit;
class DeleteMember extends Plugin {
var $id = "DeleteMember";
var $name = "DeleteMember";
var $version = "1.0";
var $description = "Delete members and their posts";
var $author = "grntbg";
function init()
{
$this->eso->addLanguage("confirmDeleteUser", "Are you sure you want to delete this member? Seriously, you won't be able to get them back.");
$this->eso->addLanguage("Delete user", "Delete member");
$this->eso->addMessage("userDeleted", "success", "This member and their posts were deleted. ;^(");
$this->eso->addMessage("noPostsAffected", "warning", "There was a problem deleting this member's posts.");
// $this->eso->addMessage("noConversationsAffected", "warning", "There was a problem deleting this member's conversations.");
$this->eso->addMessage("noUserAffected", "warning", "There was a problem deleting this member, have they already been deleted?");
if ($this->eso->action == "profile") {
$this->eso->addScript("plugins/DeleteMember/deleteMember.js");
$this->eso->addLanguageToJS("confirmDeleteUser");
$this->eso->controller->addHook("init", array($this, "addDeleteSettings"));
}
// $this->eso->controller->addHook("init", array($this, "isUserValid"));
}// init
function canDeleteUser($memberId)
{
global $config;
// Is the end user a moderator?
if ($memberId == $config["rootAdmin"]) return false;
$account = $this->eso->db->result("SELECT account FROM {$config["tablePrefix"]}members WHERE memberId=$memberId", 0);
if ($account == "Moderator" || $account == "Administrator") return false;
return true;
}// canDeleteUser
function addDeleteSettings(&$controller)
{
global $language;
$this->member =& $controller->member;
$memberId = $this->member["memberId"];
if ($this->eso->user["moderator"] and $this->canDeleteUser($memberId) == true) $this->eso->addToBar("right", "<a href='" . makeLink("profile",$memberId, "?deleteUser", "&token={$_SESSION["token"]}") . "' onclick='return DeleteUser.deleteUser()'><span class='button buttonSmall'><input type='submit' value='{$language["Delete user"]}'></span></a>", -1);
// Delete user: delete the user and then redirect to the index.
if (isset($_GET["deleteUser"]) and $this->eso->validateToken(@$_GET["token"]) and $this->doDeleteUser($memberId)) {
$this->eso->message("userDeleted");
redirect("");
}
}// addDeleteSettings
//function isUserValid()
//{
// global $config;
// // If the user's memberId doesn't exist in the database, log them out!
// if ($this->eso->user and !$this->eso->db->result("SELECT memberId FROM {$config["tablePrefix"]}members WHERE memberId={$this->eso->user["memberId"]}")) $this->eso->logout();
//}// isUserValid
function doDeleteUser($memberId)
{
// Does the user have permission?
if (!$this->eso->user["admin"] || !$this->canDeleteUser($memberId)) {
$this->eso->message("noPermission");
return false;
} elseif (!$this->deletePosts($memberId) or !$this->deleteConversations($memberId)) {
$this->eso->message("noPostsAffected");
return false;
} elseif (!$this->deleteUser($memberId)) {
$this->eso->message("noUserAffected");
return false;
} else {
return true;
}
}// doDeleteUser
function deletePosts($memberId)
{
global $config;
// Construct the select component of the query.
$result = $this->eso->db->query("SELECT c.conversationId, c.startMember FROM {$config["tablePrefix"]}conversations c, {$config["tablePrefix"]}posts p WHERE p.memberId=$memberId AND c.conversationId=p.conversationId ORDER BY p.time", 0);
$conversations = array();
$i = 0;
while ($conversation = $this->eso->db->fetchAssoc($result)) {
$k = isset($conversation["number"]) ? $conversation["number"] : $i;
// Build the post array.
$conversations[$k] = array(
"conversationId" => $conversation["conversationId"],
"startMember" => $conversation["startMember"]
);
// Fix the lastPostMember and lastPostTime of the conversation.
$this->fixLastPost($memberId, $conversation["conversationId"]);
// Fix the post count of the conversation.
$this->fixCountPost($memberId, $conversation["conversationId"]);
// if ($conversation["startMember"] == $memberId) $this->deleteConversation($conversation["conversationId"]);
$i++;
}
return true;
}// deletePosts
function deleteConversations($memberId)
{
global $config;
// Construct the select component of the query.
$result = $this->eso->db->query("SELECT c.conversationId, c.startMember FROM {$config["tablePrefix"]}conversations c WHERE c.startMember=$memberId", 0);
$conversations = array();
$i = 0;
while ($conversation = $this->eso->db->fetchAssoc($result)) {
$k = isset($conversation["number"]) ? $conversation["number"] : $i;
// Build the post array.
$conversations[$k] = array(
"conversationId" => $conversation["conversationId"],
"startMember" => $conversation["startMember"]
);
// Delete the conversation.
if ($conversation["startMember"] == $memberId) {
$query = "DELETE c, s, p, t FROM {$config["tablePrefix"]}conversations c
LEFT JOIN {$config["tablePrefix"]}status s ON (s.conversationId=c.conversationId)
LEFT JOIN {$config["tablePrefix"]}posts p ON (p.conversationId=c.conversationId)
LEFT JOIN {$config["tablePrefix"]}tags t ON (t.conversationId=c.conversationId)
WHERE c.conversationId={$conversation["conversationId"]}";
$this->eso->db->query($query);
}
$i++;
}
return true;
}// deleteConversations
function fixLastPost($memberId, $conversationId)
{
global $config;
// Does the first post of this conversation belong to that member (did the member start the conversation)?
$startMember = $this->eso->db->result("SELECT startMember FROM {$config["tablePrefix"]}conversations WHERE conversationId=$conversationId");
// Does the last post of this conversation belong to that member?
$lastPost = $this->eso->db->result("SELECT memberId FROM {$config["tablePrefix"]}posts WHERE conversationId=$conversationId ORDER BY time DESC LIMIT 1");
// How many posts are there in the conversation?
$posts = $this->eso->db->result("SELECT COUNT(*) FROM {$config["tablePrefix"]}posts WHERE conversationId=$conversationId AND memberId!=$memberId AND deleteMember IS NULL ORDER BY time");
// If the member started the conversation, don't bother. It's going to be deleted anyway.
if ($startMember == $memberId) return false;
// Prevent necrobumping (change the lastPostMember/Time to that of the previous post.)
elseif ($posts > 0 | $lastPost == $memberId) {
$lastPostMember = $this->eso->db->result("SELECT memberId FROM {$config["tablePrefix"]}posts WHERE conversationId=$conversationId AND memberId!=$memberId AND deleteMember IS NULL ORDER BY time DESC LIMIT 1");
// If there is no previous member, all the posts in the conversation have been deleted. :(
if (empty($lastPostMember)) return false;
$lastPostTime = $this->eso->db->result("SELECT time FROM {$config["tablePrefix"]}posts WHERE conversationId=$conversationId AND memberId!=$memberId AND deleteMember IS NULL ORDER BY time DESC LIMIT 1");
$query = "UPDATE {$config["tablePrefix"]}conversations
SET lastPostMember=$lastPostMember, lastPostTime=$lastPostTime
WHERE conversationId=$conversationId";
$this->eso->db->query($query);
// If all the posts in the conversation are deleted, get the first post and use that.
} elseif ($posts = 0) {
$lastPostMember = $this->eso->db->result("SELECT startMember FROM {$config["tablePrefix"]}conversations WHERE conversationId=$conversationId");
$lastPostTime = $this->eso->db->result("SELECT startTime FROM {$config["tablePrefix"]}conversations WHERE conversationId=$conversationId");
$query = "UPDATE {$config["tablePrefix"]}conversations
SET lastPostMember=$lastPostMember, lastPostTime=$lastPostTime
WHERE conversationId=$conversationId";
$this->eso->db->query($query);
} else {
return false;
}
return true;
}// fixLastPost
function fixCountPost($memberId, $conversationId)
{
global $config;
$posts = $this->eso->db->result("SELECT COUNT(*) FROM {$config["tablePrefix"]}posts
WHERE conversationId=$conversationId AND memberId!=$memberId ORDER BY time");
if ($posts > 0) {
$query = "UPDATE {$config["tablePrefix"]}conversations SET posts=$posts WHERE conversationId=$conversationId";
$this->eso->db->query($query);
}
return true;
}// fixCountPost
function deleteUser($memberId)
{
global $config;
// Delete the member, searches, logins, and remaining posts from the database.
$query = "DELETE m, s, l, p FROM {$config["tablePrefix"]}members m
LEFT JOIN {$config["tablePrefix"]}searches s ON (s.ip=m.cookieIP)
LEFT JOIN {$config["tablePrefix"]}logins l ON (l.ip=m.cookieIP)
LEFT JOIN {$config["tablePrefix"]}posts p ON (p.memberId=m.memberId)
WHERE m.memberId=$memberId";
$this->eso->db->query($query);
// If there are shouts, delete them too!
// expand upon this for other plugins that create tables
$file = "Shouts"; // directory name of the Shouts plugin
if (is_dir(PATH_PLUGINS."/$file") and file_exists(PATH_PLUGINS."/$file/plugin.php")) {
$query = "DELETE FROM {$config["tablePrefix"]}shouts
WHERE memberFrom=$memberId";
$this->eso->db->query($query);
}
// If the query didn't affect any rows, something is wrong!
if (!$this->eso->db->affectedRows()) {
$this->eso->message("noUserAffected");
return false;
}
return true;
}// deleteUser
}
?>