MyBB Community Forums

Full Version: Programmatically deleting a user?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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).

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?
  1. Is it really doing what I want to do? i.e. delete a user along with all traces?
  2. Is there any better way of doing this?
  3. 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??
Thank you very much in advance Smile
see inc/tasks/userpruning.php

also have a care with your queries. you are using intval for one query but not for the others.
(2013-08-14, 02:05 PM)frostschutz Wrote: [ -> ]see inc/tasks/userpruning.php
Humm .. looks close enough. Do you see any differences? In my case, I know for sure that I want to delete every thing related to the that has been spotted as being spammer. So, I think I don't need to assign there posts/threads to some other user etc. or am I missing something important?

(2013-08-14, 02:05 PM)frostschutz Wrote: [ -> ]also have a care with your queries. you are using intval for one query but not for the others.
Well ... call to inval() was left there from the code that I copied from /admin/modules/user/users.php
As I will be taking data directly from the database and not from the user input, I guess I don't need it at that place too. Toungue

Thank you very much for your insight!

P.S. Any guidance about running such a script via cron? How to get access to the globals, $db and $cache?
Anyway, if you plan to use the uid from the result of the first query you will need to replace all the $uid calls into $user['uid'], or your script may not work as expected.
(2013-08-14, 03:00 PM)Shade Wrote: [ -> ]Anyway, if you plan to use the uid from the result of the first query you will need to replace all the $uid calls into $user['uid'], or your script may not work as expected.

Ok, I will replace it ... Thanks Smile