<?php if (! defined ('PmWiki')) exit;
/*
 * Authenticate Against a bbPress user database.
 *
 * To use, import and set the BbPressAuthDb array as follows:
 * $BbPressAuthDb = array(
 *    'host' => 'localhost', # won't have to change unless your MySQL database is on a different server
 *    'db' => 'dbname',
 *    'user' => 'dbuser',
 *    'password' => 'dbpass',
 *    'table' => 'bb_users', # might need to change if you have multiple forums
 *    'userfield' => 'user_login', # probably don't need to change
 *    'pwfield' => 'user_pass' # probably don't need to change
 * );
 */

$AuthUserFunctions['bbpress'] = 'AuthUserBbPress';

function AuthUserBbPress($pagename, $id, $pw, $pwlist) {
  global $BbPressAuthDb;
  
  $link = mysql_connect($BbPressAuthDb['host'],$BbPressAuthDb['user'],$BbPressAuthDb['password']);
  if (!$link)  die('Could not connect: '.mysql_error());
  
  @mysql_select_db($BbPressAuthDb['db']) or die("Unable to select database $BbPressAuthDb[db]: ".mysql_error());

  $user = $BbPressAuthDb['userfield'];
  $pass = $BbPressAuthDb['pwfield'];
  $table = $BbPressAuthDb['table'];
 
  $id = user_sanitize($id);
  $pw = user_sanitize(md5($pw));
  $ask = "SELECT * from $table WHERE $user='$id' AND $pass='$pw';";
  
  $result = mysql_query($ask);
  if (!$result) die("Could not successfully run query ($query) from DB: ".mysql_error());
  
  return (mysql_num_rows($result) > 0) ? true : false;
}

function user_sanitize( $text ) {
    $text = preg_replace('/[^a-z0-9_-]/i', '', $text);
        return $text;
}

?>