MyBB Community Forums

Full Version: [SOLVED] Hide certain user id from memberlist
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
I'm working to a plugin (for personal use - in MyBB 1.6.11) to hide super admins (defined in "config.php") from all visible sections of my forum. A kind of "invisible super-admin" (please, don't ask me why I need this - I just want to find the better way to do this). I know how to do this with modification of core files but I try to do it with a plugin.

Ok, so... I need a little help with "memberlist.php".
There are 4 hooks:
$plugins->run_hooks("memberlist_start");
$plugins->run_hooks("memberlist_search");
$user = $plugins->run_hooks("memberlist_user", $user); // probably the most important for me in this case
$plugins->run_hooks("memberlist_end");

If I want to hide super admin with id 1 from Member List page, it works if I change "where" condition from this query (line 224):
	$query = $db->query("
		SELECT u.*, f.*
		FROM ".TABLE_PREFIX."users u
		LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
		WHERE {$search_query}
		ORDER BY {$sort_field} {$sort_order}
		LIMIT {$start}, {$per_page}
	");
	while($user = $db->fetch_array($query))
	{
		$user = $plugins->run_hooks("memberlist_user", $user);
		if(!$user['username'])
		{
			continue;
		}
		// rest of code ...
with:
	$query = $db->query("
		SELECT u.*, f.*
		FROM ".TABLE_PREFIX."users u
		LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
		WHERE {$search_query} AND u.uid != {$mybb->config['super_admins']}
		ORDER BY {$sort_field} {$sort_order}
		LIMIT {$start}, {$per_page}
	");
	while($user = $db->fetch_array($query))
	{
		$user = $plugins->run_hooks("memberlist_user", $user);
		if(!$user['username'])
		{
			continue;
		}
		// rest of code ...

With a plugin, probably I must to use this hook:
$user = $plugins->run_hooks("memberlist_user", $user);
but... above query is called before this hook so...

I would be grateful if you give me an idea about how can be excluded a certain user id from memberlist ?.
Sad
If you at all want to use this hook 'memberlist_user' then no need to (can't by plugin as such) change 'where' condition. May be this is your function in plugin to call by the hook:
if($user['uid'] == {$mybb->config['super_admins'])
        {
            continue;
        }
(2013-11-19, 01:31 PM)effone Wrote: [ -> ]If you at all want to use this hook 'memberlist_user' then no need to (can't by plugin as such) change 'where' condition.
Thank you for reply. I know. Sorry for my english.
But... how to return "continue" to "memberlist.php" if the condition is satisfied ?

This is plugin example:
<?php
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

// Hook
$plugins->add_hook("memberlist_user", "my_plugin_run");


function my_plugin_info()
{
	return array(
		"name"			=> "My Plugin",
		"description"	=> "My plugin info.",
		"website"		=> "http://mybb.com",
		"author"		=> "Nobody",
		"authorsite"	=> "http://mybb.com",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "*"
	);
}

// _activate
function my_plugin_activate()
{
}

// _deactivate
function my_plugin_deactive()
{
}

// _run
function my_plugin_run()
{
	global $mybb; // I'm sure that I must to include something else in global list, but... what ???
	if($user['uid'] != $mybb->config['super_admins'])
	{
		continue; // How to return "continue" from a function back to "memberlist.php" ???
	}
}

I can't break/continue a loop outside a function, from within a function. I mean... the continue statement is valid inside looping structures only ("while" in this case).
So... I don't know how to do this without modification of core file "memberlist.php"... as long as there are no other hooks. Sad
Have you tried it?

I was just giving you an idea and as far I believe WHILE acts as loop and if the user id is superadmin the WHILE loop should skip the entry from the fetched $user array to to build as the table row, but in other uid cases it will build the table row (I may be wrong as I'm new to plugins).

If it doesn't work and you don't get answer from others I'll try in spare time and get back.
I understand. Ok. Your idea is a good one but only if I modify core file "memberlist.php" and include your code:
(2013-11-19, 01:31 PM)effone Wrote: [ -> ]
if($user['uid'] == $mybb->config['super_admins'])
        {
            continue;
        }
but I want to do this without modifications of core files, so... with a plugin.
This is what I'm trying to do, and I'm afraid that it's not possible because the hook "memberlist_user" is inside a loop function ("where"). Probably, if I use "memberlist_end" hook, it should be possible. But I must to repeat a lot of code (including one or more query) just to change a simple "where" condition. All I want is to eliminate a certain user id from memberlist WITHOUT modification of core file "memberlist.php" (so... with a plugin).

I appreciate your intention and... of course, when you will have a little free time, will be nice if you will give me a solution that can be applied effectively. Thank you!

(2013-11-19, 01:54 PM)effone Wrote: [ -> ]Have you tried it?
And yes... I tried. This is the result:
Fatal error: Cannot break/continue 1 level in ....\inc\plugins\my_plugin.php on line ...

Finally, I found a solution. This method will hide certain user id (super_admin) in my case, from memberlist.

<?php
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

// Hook
$plugins->add_hook("memberlist_user", "my_plugin_run");

// _info
function my_plugin_info()
{
	return array(
		"name"			=> "My Plugin",
		"description"	=> "My plugin info.",
		"website"		=> "http://mybb.com",
		"author"		=> "Nobody",
		"authorsite"	=> "http://mybb.com",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "*"
	);
}

// _activate
function my_plugin_activate()
{
}

// _deactivate
function my_plugin_deactive()
{
}

// _run
function my_plugin_run()
{
	global $mybb, $user;
	if($user['uid'] == $mybb->config['super_admins'])
	{
		return array(
			$user['username'] => NULL
		);
	}
	else return false;
}
?>

It works (super_admins are hidden in members list) but I want to know if is a proper way to declare $user as global ? I mean... is a safe method ?
$plugins->add_hook("memberlist_user", "somethingblabla");
function somethingblabla()
{
    global $mybb, $user;
	
	$supadmins = explode(",", $mybb->config['super_admins']);
	
    if(in_array($user['uid'], $supadmins))
    {
        $user['username'] = "";
    }
}

@down, right, don't have warnings turned on so didn't notice. Also thought it would be checked by empty. Corrected.
Now I is more clearly! THANK YOU, Destroy666!
Heart

It works if I change this line from your code:
$user = "";
with
$user['username'] = "";
or
$user = array();
else I receive an error:
Warning [2] Illegal string offset 'username' - Line: 235
because in memberlist.php we have this condition:
if(!$user['username'])
{
	continue;
}
Your code is very good especially when there are more super admins.

Thank you again for help!

Also it works with
$user = NULL;
and probably this method is more logical because verification in "memberlist.php" is made for
if(!$user
~ like "if not exists".