This script made some assumptions that were incorrect:
1) The script assumes that column values come out in the same order always when selecting on *. This is not always so.
2) The script only gives out doku permissions for the mybb user's primary group -- however, mybb users can be part of multiple groups.
I have fixed both issues.
I didn't do a very thorough fix though, you could further clean up the SQL queries to be ultra specific, but I wasn't bothered by that for now.
<?php
/**
* MyBB auth backend
*
* Bridges user login with MyBB database.
*
*/
define('TABLE_PREFIX', '');
define('DB_HOST', 'localhost');
define('DB_USER', '');
define('DB_PWD', '');
define('DB_NAME', '');
define('DOKU_AUTH', dirname(__FILE__));
require_once(DOKU_AUTH.'/basic.class.php');
class auth_mybb extends auth_basic
{
//Link to database's resource connection
var $link;
//Usergroup conversion rules
var $rules = array(
"4" => array("admin", "user"),
"default" => array("user")
);
//The constructor
function auth_mybb()
{
$this->link = mysql_connect(DB_HOST, DB_USER, DB_PWD) or die('Connection to MySQL Server failed');
mysql_select_db(DB_NAME, $this->link) or die('Select Database failed');
$this->success = true;
}
//Check if an user exists and if a password is exact [OVERRIDEN]
function checkPass($user, $pass)
{
$q = mysql_query("SELECT * FROM ".TABLE_PREFIX."users WHERE username='$user' LIMIT 0,1", $this->link);
$r = mysql_fetch_assoc($q);
if($r['password'] == $this->mybb_psw($pass, $r['salt'])) return true;
return false;
}
//Generate MyBB's Password Hash
function mybb_psw($password, $salt)
{
$password = md5($password);
return md5(md5($salt).$password);
}
//Get user data [OVERRIDEN]
function getUserData($user)
{
$q = mysql_query("SELECT * FROM ".TABLE_PREFIX."users WHERE username='$user' LIMIT 0,1", $this->link);
$r = mysql_fetch_assoc($q);
$retn = array('name' => $r['username'], 'mail' => $r['email'], 'grps' => $this->mybb_2_dw_usergroups($r['usergroup'],$r['additionalgroups']));
return $retn;
}
//Function for converting mybb usergroups to DokuWiki usergroups
function mybb_2_dw_usergroups($usergroup, $additional_groups)
{
// a user could have multiple groups which could end up mappig to multiple doku permission sets
if( ! empty($additional_groups) )
{
$groups = explode(',', $usergroup .','. $additional_groups);
}
else
{
$groups = array($usergroup);
}
$doku_groups = array();
foreach( $groups as $mybb_group_id )
{
if(isset($this->rules[$mybb_group_id]))
{
foreach($this->rules[$mybb_group_id] as $target_group)
{
$doku_groups[] = $target_group;
}
}
}
// return the unique set of groups or the default values
if(count($doku_groups))
{
return array_unique($doku_groups);
}
else
{
return $this->rules["default"];
}
}
}
?>