MyBB Community Forums

Full Version: Plugin to delete users that never logged in
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Currently I use in my board the 'random password' activation method. This puts the users in the registered group. Some never login as there are no clickable links in the mail (this keeps most spambots away). Unfortunately I am left with hundreds of inactivated accounts every week and manually prune them via complicated SQL queries.

I was thinking about a plugin to handle those tasks via AdminCP interface as I am not skilled enough with that kind of plugins I'd like someone to code it for me and freely put available on mods.mybb.com

To help in the task I have done some sample code, untested. I waive copyright ownership of the code and release it for the public domain in hope it is useful.

<?php
function select_users()
{
	// In the future grab the settings from admincp
	$groups_to_check = array_map('intval', explode(',','2,5'));

	// This will check if groups to check setting is valid
	if(is_array($groups_to_check) && count($groups_to_check))
	{
		// Implode to use in SQL query
		$groups_to_check = implode(',',$groups_to_check);
	}
	else
	{
		return false;
	}

	$a_week_ago = time() - 604800; // This is a week ago

	$prefix = 'mybb_'; // @TODO This should be made in a way it is portable between mybb installs

	// This will select users that registed more than a week ago, belong to groups to check, lastpost and timeonline are zero and don't have any additional groups
	// Why this?
	// regdate check gives users a grace time to their first login
	// timeonline check the last time they were online (0 is never)
	// lastpost check the time they last posted (I do this for sanity, 0 is never)
	// usergroup check ensures that only registered and awaiting activation are checked (groups 2 and 5, should be made configurable in the future)
	// additionalgroups check ensures my custom edited users don't get checked
	sql_query("SELECT * FROM `{prefix}users` WHERE ((regdate < {$a_week_ago}) AND lastpost = 0 AND timeonline = 0 AND usergroup IN ({$groups_to_check}) AND additionalgroups = '');");
}

function remove_users()
{
	// In the future grab the settings from admincp
	$groups_to_check = array_map('intval', explode(',','2,5'));

	// This will check if groups to check setting is valid
	if(is_array($groups_to_check) && count($groups_to_check))
	{
		// Implode to use in SQL query
		$groups_to_check = implode(',',$groups_to_check);
	}
	else
	{
		return false;
	}

	$a_week_ago = time() - 604800; // This is a week ago

	$prefix = 'mybb_'; // @TODO This should be made in a way it is portable between mybb installs

	// This will clear the userfields table from the users we'll be deleting
	sql_query("DELETE FROM `{$prefix}userfields WHERE ufid IN (SELECT uid FROM `{prefix}users` WHERE ((regdate < {$a_week_ago}) AND lastpost = 0 AND timeonline = 0 AND usergroup IN ({$groups_to_check}) AND additionalgroups = ''));");

	// Now clear the users table from the users that were never active
	sql_query("DELETE FROM `{prefix}users` WHERE ((regdate < {$a_week_ago}) AND lastpost = 0 AND timeonline = 0 AND usergroup IN ({$groups_to_check}) AND additionalgroups = ''));");
}

Regards,
NewEraCracker
I am bumping this as I have done some work based on original MyBB code. I still need some help into integrating it in some sort of plugin as I am not skilled enough with the plugin system.
Why not use the builtin prune users option instead?