MyBB Community Forums

Full Version: [F] Moderators cache - array_merge() reindex bug
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
MyBB 1.4.1, PHP 5.2.4

I have found that logged regular user is made a moderator while accessing forums main page (he can see reported posts). In inc/functions.php I have added a debug message to is_moderator() function:
			$modcache = $cache->read('moderators');
			if(!empty($modcache))
			{
				foreach($modcache as $modusers)
				{
					if(isset($modusers[$uid]) && $modusers[$uid]['mid'])
					{
						print_r($modusers[$uid]) ;
						return true;
					}
				}
			}
			return false;
and it printed:
Array
(
    [mid] => 89
    [fid] => 32
    [uid] => 30
    [caneditposts] => 1
    [candeleteposts] => 1
    [canviewips] => 1
    [canopenclosethreads] => 1
    [canmanagethreads] => 1
    [canmovetononmodforum] => 1
    [username] => Amandi
    [usergroup] => 6
    [displaygroup] => 15
)


The problem is that this users UID is 2 (not 30) and his username - koziolek (not "Amandi"). It seems that problem lies in array_merge which reindex the keys. Look at file inc/class_datacache.php in function build_moderators():
					// Append - local settings override that of a parent - array_merge works here
					if($this->moderators[$forum['fid']])
					{
						if(is_array($forum_mods))
						{
							print_r($forum_mods) ; // DEBUG
							print_r($this->moderators[$forum['fid']]) ; // DEBUG
							$forum_mods = array_merge($forum_mods, $this->moderators[$forum['fid']]);
							print_r($forum_mods) ; // DEBUG
						}
						else
						{
							$forum_mods = $this->moderators[$forum['fid']];
						}
					}

Debug print_r() shows:
$forum_mods - before array_merge():
Array
(
    [1] => Array
        (
            [mid] => 1
            [fid] => 1
            [uid] => 1
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => admin
            [usergroup] => 4
            [displaygroup] => 0
        )

)
$this->moderators[$forum['fid']] - before array_merge():
Array
(
    [12780] => Array
        (
            [mid] => 151
            [fid] => 35
            [uid] => 12780
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => m.płochocki
            [usergroup] => 8
            [displaygroup] => 0
        )

    [11377] => Array
        (
            [mid] => 149
            [fid] => 35
            [uid] => 11377
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => p.bijata
            [usergroup] => 8
            [displaygroup] => 0
        )

)

$forum_mods - after array_merge() - look at array keys:
Array
(
    [0] => Array
        (
            [mid] => 1
            [fid] => 1
            [uid] => 1
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => admin
            [usergroup] => 4
            [displaygroup] => 0
        )

    [1] => Array
        (
            [mid] => 151
            [fid] => 35
            [uid] => 12780
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => m.płochocki
            [usergroup] => 8
            [displaygroup] => 0
        )

    [2] => Array
        (
            [mid] => 149
            [fid] => 35
            [uid] => 11377
            [caneditposts] => 1
            [candeleteposts] => 1
            [canviewips] => 1
            [canopenclosethreads] => 1
            [canmanagethreads] => 1
            [canmovetononmodforum] => 1
            [username] => p.bijata
            [usergroup] => 8
            [displaygroup] => 0
        )

)
Reproduction instructions? I can't seem to reproduce.
It was just an upgrade from 1.2 to 1.4.1. I tested it on two different regular users (which existed before upgrade) and both has the same problem.

You can add debug instructions to build_moderators() and rebuild moderators cache.
I can't reproduce - that's why I asked.
array_merge() (tested in PHP 5.2.4 and PHP 5.2.6-pl6-gentoo) reindex numeric keys in resulting array. Simple test for command line:
<?php
$a = array(1 => 'a') ;
$b = array(5 => 'b', 30 => 'c') ;

print_r(array_merge($a, $b)) ;
?>
Result:
Array
(
    [0] => a
    [1] => b
    [2] => c
)
So in function build_moderators() array $forum_mods is reindexed by array_merge().

I do not know how to give a reproduction instructions if the code above does not work Smile... Anyway a fix is:
$forum_mods = $forum_mods + $this->moderators[$forum['fid']];
Arrays are added without key reindex - this fixes moderators cache. At least mine Wink .
Thank you for your bug report.

This bug has been fixed in our internal code repository. Please note that the problem will not be fixed here until these forums are updated.

With regards,
MyBB Group