2013-08-14, 01:50 PM
Hi guys,
I am planning to develop a plugin that will check the existing user base of a mybb forum for spam registrations based on StopForumSpam.com api and will ban/delete them based on some settings paramater configurable by the board admins. One thing that I need to do as part of it is delete a user when it has been confirmed that it is a spam account.
Is there a function in mybb that can delete a user compeletly from the database if provided with the ID of that user?
I have extracted the following code segment from the /admin/modules/user/users.php file that seems to do the job perfectly. (Assuming that $uid is the ID of the user that we want to delete).
What are your views on it?
I am planning to develop a plugin that will check the existing user base of a mybb forum for spam registrations based on StopForumSpam.com api and will ban/delete them based on some settings paramater configurable by the board admins. One thing that I need to do as part of it is delete a user when it has been confirmed that it is a spam account.
Is there a function in mybb that can delete a user compeletly from the database if provided with the ID of that user?
I have extracted the following code segment from the /admin/modules/user/users.php file that seems to do the job perfectly. (Assuming that $uid is the ID of the user that we want to delete).
global $db, $cache;
$query = $db->simple_select("users", "*", "uid='".intval($uid)."'");
$user = $db->fetch_array($query);
// if the user exists
if ( $user['uid'] ) {
$db->delete_query("userfields", "ufid='{$uid}'");
$db->delete_query("privatemessages", "uid='{$uid}'");
$db->delete_query("events", "uid='{$uid}'");
$db->delete_query("forumsubscriptions", "uid='{$uid}'");
$db->delete_query("threadsubscriptions", "uid='{$uid}'");
$db->delete_query("sessions", "uid='{$uid}'");
$db->delete_query("banned", "uid='{$uid}'");
$db->delete_query("threadratings", "uid='{$uid}'");
$db->delete_query("users", "uid='{$uid}'");
$db->delete_query("joinrequests", "uid='{$uid}'");
$db->delete_query("warnings", "uid='{$uid}'");
$db->delete_query("reputation", "uid='{$uid}' OR adduid='{$uid}'");
$db->delete_query("awaitingactivation", "uid='{$uid}'");
$db->delete_query("posts", "uid = '{$uid}' AND visible = '-2'");
$db->delete_query("threads", "uid = '{$uid}' AND visible = '-2'");
// Update forum stats
update_stats(array('numusers' => '-1'));
// Update forums & threads if user is the lastposter
$db->update_query("posts", array('uid' => 0), "uid='{$uid}'");
$db->update_query("forums", array("lastposteruid" => 0), "lastposteruid = '{$uid}'");
$db->update_query("threads", array("lastposteruid" => 0), "lastposteruid = '{$uid}'");
// Did this user have an uploaded avatar?
if($user['avatartype'] == "upload")
{
// Removes the ./ at the beginning the timestamp on the end...
@unlink("../".substr($user['avatar'], 2, -20));
}
// Was this user a moderator?
if(is_moderator($uid))
{
$db->delete_query("moderators", "id='{$uid}' AND isgroup = '0'");
$cache->update_moderators();
}
// Log admin action
log_admin_action($uid, $user['username']);
What are your views on it?
- Is it really doing what I want to do? i.e. delete a user along with all traces?
- Is there any better way of doing this?
- I will probably run it as part of a backgroud script that will be run via cron jobs. Will $db and $cache global variables be available to it automatically? If not (which I suspect will be the case) then which files from mybb and in what order I have to include in my script to get access to these global variables??