DokuWiki

It's better when it's simple

ユーザ用ツール

サイト用ツール


ja:auth:punbb

PunBB 認証バックエンド

このバックエンドプログラムは同じサーバー内にインストールされている掲示板システム、PunBB 1.2.x または 1.3.x で認証を行うものです。これにより、PunBBのアカウントでDokuWikiにもログインできるようになります。(PunBBのクッキーを利用し、シングルサインオンをできるようにします。)

動作条件

  • 最近のバージョン、PunBB 1.2.x (1.3.xはベータ版でサポート)がインストールされていること。
  • PunBBとDokuWikiは同じドメイン上であること。
  • magic_quotes_gpc が off であること。1)

設定

このバックエンドはPunBBの設定を利用するので、PunBBがインストールされているディレクトリの位置をPUN_ROOTをdefine文で設定します。DokuWikiディレクトリのconf/local.protected.phpに次のように設定を追加します。

  $conf['useacl']  =1;
  $conf['authtype']='punbb';
  define('PUN_ROOT','/path/to/punbb/');

このパスは絶対パスでなければいけません。また、末尾にスラッシュ(/)が必要です。たとえば、/var/www/free.fr/2/8/mysite/punbb/のようになります。

管理者ユーザーとして認識させるには、PunBBの管理者グループの名前をDokuWiki設定のsuperuserオプションで設定する必要があります。PunBBの管理者グループの名前の付け方は言語パックによって異なります。英語の場合は@Administratorsですが、フランス語では@Administrateursとなっています。

ソースコードとインストール

PunBB 1.2.x

DokuWiki 2008-05-05までのバージョンでは、このバックエンドはDokuWikiのソースに含まれていました。それ以降のバージョンではこのバックエンドが含まれていません。

インストールするには、DokuWikiディレクトリにinc/auth/punbb.class.phpファイルを新規に作成して、つぎのコードをコピー&ペーストします。

<?php
/**
 * PunBB auth backend
 *
 * Uses external Trust mechanism to check against PunBB's
 * user cookie. PunBB's PUN_ROOT must be defined correctly.
 *
 * @author    Andreas Gohr <andi@splitbrain.org>
 */
 
if(!defined('PUN_ROOT')) define('PUN_ROOT', DOKU_INC.'../forum/');
if(get_magic_quotes_gpc()){
  nice_die('Sorry the punbb auth backend requires the PHP option
  <a href="http://www.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc">magic_quotes_gpc</a>
  to be disabled for proper operation. Either setup your PHP install accordingly or
  choose a different auth backend.');
}
 
require_once PUN_ROOT.'include/common.php';
require_once DOKU_INC.'inc/auth/mysql.class.php';
 
#dbg($GLOBALS);
#dbg($pun_user);

class auth_punbb extends auth_mysql {
 
  /**
   * Constructor.
   *
   * Sets additional capabilities and config strings
   */
  function auth_punbb(){
    global $conf;
    $this->cando['external'] = true;
    $this->cando['logoff']   = true;
 
    // make sure we use a crypt understood by punbb
    if(function_exists('sha1')){
      $conf['passcrypt'] = 'sha1';
    }else{
      $conf['passcrypt'] = 'md5';
    }
 
    // get global vars from PunBB config
    global $db_host;
    global $db_name;
    global $db_username;
    global $db_password;
    global $db_prefix;
 
    // now set up the mysql config strings
    $conf['auth']['mysql']['server']   = $db_host;
    $conf['auth']['mysql']['user']     = $db_username;
    $conf['auth']['mysql']['password'] = $db_password;
    $conf['auth']['mysql']['database'] = $db_name;
 
    $conf['auth']['mysql']['checkPass']   = "SELECT u.password AS pass
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'
                                                AND g.g_title   != 'Guest'";
    $conf['auth']['mysql']['getUserInfo'] = "SELECT password AS pass, realname AS name, email AS mail,
                                                    id, g_title as `group`
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'";
    $conf['auth']['mysql']['getGroups']   = "SELECT g.g_title as `group`
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'";
    $conf['auth']['mysql']['getUsers']    = "SELECT DISTINCT u.username AS user
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id";
    $conf['auth']['mysql']['FilterLogin'] = "u.username LIKE '%{user}'";
    $conf['auth']['mysql']['FilterName']  = "u.realname LIKE '%{name}'";
    $conf['auth']['mysql']['FilterEmail'] = "u.email    LIKE '%{email}'";
    $conf['auth']['mysql']['FilterGroup'] = "g.g_title    LIKE '%{group}'";
    $conf['auth']['mysql']['SortOrder']   = "ORDER BY u.username";
    $conf['auth']['mysql']['addUser']     = "INSERT INTO ${db_prefix}users
                                                    (username, password, email, realname)
                                             VALUES ('%{user}', '%{pass}', '%{email}', '%{name}')";
    $conf['auth']['mysql']['addGroup']    = "INSERT INTO ${db_prefix}groups (g_title) VALUES ('%{group}')";
    $conf['auth']['mysql']['addUserGroup']= "UPDATE ${db_prefix}users
                                                SET group_id=%{gid}
                                              WHERE id='%{uid}'";
    $conf['auth']['mysql']['delGroup']    = "DELETE FROM ${db_prefix}groups WHERE g_id='%{gid}'";
    $conf['auth']['mysql']['getUserID']   = "SELECT id FROM ${db_prefix}users WHERE username='%{user}'";
    $conf['auth']['mysql']['updateUser']  = "UPDATE ${db_prefix}users SET";
    $conf['auth']['mysql']['UpdateLogin'] = "username='%{user}'";
    $conf['auth']['mysql']['UpdatePass']  = "password='%{pass}'";
    $conf['auth']['mysql']['UpdateEmail'] = "email='%{email}'";
    $conf['auth']['mysql']['UpdateName']  = "realname='%{name}'";
    $conf['auth']['mysql']['UpdateTarget']= "WHERE id=%{uid}";
    $conf['auth']['mysql']['delUserGroup']= "UPDATE ${db_prefix}users SET g_id=4 WHERE id=%{uid}";
    $conf['auth']['mysql']['getGroupID']  = "SELECT g_id AS id FROM ${db_prefix}groups WHERE g_title='%{group}'";
 
    $conf['auth']['mysql']['TablesToLock']= array("${db_prefix}users", "${db_prefix}users AS u",
                                                  "${db_prefix}groups", "${db_prefix}groups AS g");
 
    $conf['auth']['mysql']['debug'] = 1;
    // call mysql constructor
    $this->auth_mysql();
  }
 
  /**
   * Just checks against the $pun_user variable
   */
  function trustExternal($user,$pass,$sticky=false){
    global $USERINFO;
    global $conf;
    global $lang;
    global $pun_user;
    global $pun_config;
    $sticky ? $sticky = true : $sticky = false; //sanity check
 
    // someone used the login form
    if(!empty($user)){
      if($this->checkPass($user,$pass)){
        $expire = ($sticky) ? time() + 31536000 : 0;
        $uinfo  = $this->getUserData($user);
        pun_setcookie($uinfo['id'], auth_cryptPassword($pass), $expire);
        $pun_user = array();
        $pun_user['password'] = auth_cryptPassword($pass);
        $pun_user['username'] = $user;
        $pun_user['realname'] = $uinfo['name'];
        $pun_user['email']    = $uinfo['mail'];
        $pun_user['g_title']  = $uinfo['group'];
      }else{
        //invalid credentials - log off
        msg($lang['badlogin'],-1);
        auth_logoff();
        return false;
      }
    }
 
    if(isset($pun_user) && !$pun_user['is_guest']){
      // okay we're logged in - set the globals
      $USERINFO['pass'] = $pun_user['password'];
      $USERINFO['name'] = $pun_user['realname'];
      $USERINFO['mail'] = $pun_user['email'];
      $USERINFO['grps'] = array($pun_user['g_title']);
 
      $_SERVER['REMOTE_USER'] = $pun_user['username'];
      $_SESSION[DOKU_COOKIE]['auth']['user'] = $pun_user['username'];
      $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
      return true;
    }
 
    // to be sure
    auth_logoff();
    return false;
  }
 
  /**
   * remove punbb cookie on logout
   */
  function logOff(){
    global $pun_user;
    $pun_user = array();
    $pun_user['is_guest'] = 1;
    pun_setcookie(1, random_pass(8), time() + 31536000);
  }
}
//Setup VIM: ex: et ts=2 enc=utf-8 :

punbb 1.3.x

DokuWiki 2008-05-05までのバージョンではバックエンドがDokuWikiソースに含まれていました。それ以降のバージョンでは含まれていません。

インストールするには、DokuWikiディレクトリにinc/auth/punbb.class.phpというファイルを新規に作成して、次のコードをコピー&ペーストします。

<?php
/**
 * PunBB 1.3.x auth backend
 *
 * Uses external Trust mechanism to check against PunBB's
 * user cookie. PunBB's FORUM_ROOT must be defined correctly.
 *
 * @author    Andreas Gohr <andi@splitbrain.org>
 */
 
// needed if you use URL rewrite in you Wiki and Punbb
// i.e.: example.com/wiki/article  and  example.com/punbb/topic/1515/subject/
define('FORUM_IGNORE_REQUEST_URI', 1);
 
// do not confirm the action in punbb, usefull for ajax in dokuwiki
define('FORUM_SKIP_CSRF_CONFIRM', 1);
 
if(!defined('FORUM_ROOT')) define('FORUM_ROOT', DOKU_INC.'../forum/');
if(get_magic_quotes_gpc()){
  nice_die('Sorry the punbb auth backend requires the PHP option
  <a href="http://www.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc">magic_quotes_gpc</a>
  to be disabled for proper operation. Either setup your PHP install accordingly or
  choose a different auth backend.');
}
 
require_once FORUM_ROOT.'include/common.php';
require_once DOKU_INC.'inc/auth/mysql.class.php';
 
#dbg($GLOBALS);
#dbg($forum_user);

class auth_punbb extends auth_mysql {
 
  /**
   * Constructor.
   *
   * Sets additional capabilities and config strings
   */
  function auth_punbb(){
    global $conf;
    $this->cando['external'] = true;
    $this->cando['logoff']   = true;
 
    // make sure we use a crypt understood by punbb
    if(function_exists('sha1')){
      $conf['passcrypt'] = 'sha1';
    }else{
      $conf['passcrypt'] = 'md5';
    }
 
    // get global vars from PunBB config
    global $db_host;
    global $db_name;
    global $db_username;
    global $db_password;
    global $db_prefix;
 
    // now set up the mysql config strings
    $conf['auth']['mysql']['server']   = $db_host;
    $conf['auth']['mysql']['user']     = $db_username;
    $conf['auth']['mysql']['password'] = $db_password;
    $conf['auth']['mysql']['database'] = $db_name;
 
    $conf['auth']['mysql']['checkPass']   = "SELECT u.password AS pass
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'
                                                AND g.g_title   != 'Guest'";
    $conf['auth']['mysql']['getUserInfo'] = "SELECT password AS pass, realname AS name, email AS mail,
                                                    id, g_title as `group`
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'";
    $conf['auth']['mysql']['getGroups']   = "SELECT g.g_title as `group`
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id
                                                AND u.username = '%{user}'";
    $conf['auth']['mysql']['getUsers']    = "SELECT DISTINCT u.username AS user
                                               FROM ${db_prefix}users AS u, ${db_prefix}groups AS g
                                              WHERE u.group_id = g.g_id";
    $conf['auth']['mysql']['FilterLogin'] = "u.username LIKE '%{user}'";
    $conf['auth']['mysql']['FilterName']  = "u.realname LIKE '%{name}'";
    $conf['auth']['mysql']['FilterEmail'] = "u.email    LIKE '%{email}'";
    $conf['auth']['mysql']['FilterGroup'] = "g.g_title    LIKE '%{group}'";
    $conf['auth']['mysql']['SortOrder']   = "ORDER BY u.username";
    $conf['auth']['mysql']['addUser']     = "INSERT INTO ${db_prefix}users
                                                    (username, password, email, realname)
                                             VALUES ('%{user}', '%{pass}', '%{email}', '%{name}')";
    $conf['auth']['mysql']['addGroup']    = "INSERT INTO ${db_prefix}groups (g_title) VALUES ('%{group}')";
    $conf['auth']['mysql']['addUserGroup']= "UPDATE ${db_prefix}users
                                                SET group_id=%{gid}
                                              WHERE id='%{uid}'";
    $conf['auth']['mysql']['delGroup']    = "DELETE FROM ${db_prefix}groups WHERE g_id='%{gid}'";
    $conf['auth']['mysql']['getUserID']   = "SELECT id FROM ${db_prefix}users WHERE username='%{user}'";
    $conf['auth']['mysql']['updateUser']  = "UPDATE ${db_prefix}users SET";
    $conf['auth']['mysql']['UpdateLogin'] = "username='%{user}'";
    $conf['auth']['mysql']['UpdatePass']  = "password='%{pass}'";
    $conf['auth']['mysql']['UpdateEmail'] = "email='%{email}'";
    $conf['auth']['mysql']['UpdateName']  = "realname='%{name}'";
    $conf['auth']['mysql']['UpdateTarget']= "WHERE id=%{uid}";
    $conf['auth']['mysql']['delUserGroup']= "UPDATE ${db_prefix}users SET g_id=4 WHERE id=%{uid}";
    $conf['auth']['mysql']['getGroupID']  = "SELECT g_id AS id FROM ${db_prefix}groups WHERE g_title='%{group}'";
 
    $conf['auth']['mysql']['TablesToLock']= array("${db_prefix}users", "${db_prefix}users AS u",
                                                  "${db_prefix}groups", "${db_prefix}groups AS g");
 
    $conf['auth']['mysql']['debug'] = 1;
    // call mysql constructor
    $this->auth_mysql();
  }
 
  /**
   * Just checks against the $forum_user variable
   */
  function trustExternal($user,$pass,$sticky=false){
    global $USERINFO;
    global $conf;
    global $lang;
    global $forum_user;
    global $pun_config;
    $sticky ? $sticky = true : $sticky = false; //sanity check
 
    // someone used the login form
    if(!empty($user)){
      if($this->checkPass($user,$pass)){
        $expire = ($sticky) ? time() + 31536000 : 0;
        $uinfo  = $this->getUserData($user);
        forum_setcookie($uinfo['id'], auth_cryptPassword($pass), $expire);
        $forum_user = array();
        $forum_user['password'] = auth_cryptPassword($pass);
        $forum_user['username'] = $user;
        $forum_user['realname'] = $uinfo['name'];
        $forum_user['email']    = $uinfo['mail'];
        $forum_user['g_title']  = $uinfo['group'];
      }else{
        //invalid credentials - log off
        msg($lang['badlogin'],-1);
        auth_logoff();
        return false;
      }
    }
 
    if(isset($forum_user) && !$forum_user['is_guest']){
      // okay we're logged in - set the globals
      $USERINFO['pass'] = $pun_user['password'];
      $USERINFO['name'] = utf8_encode($forum_user['realname']);
      $USERINFO['mail'] = $forum_user['email'];
      $USERINFO['grps'] = array($forum_user['g_title']);
 
      $_SERVER['REMOTE_USER'] = utf8_encode($forum_user['username']);
      $_SESSION[DOKU_COOKIE]['auth']['user'] = utf8_encode($forum_user['username']);
      $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
      return true;
    }
 
    // to be sure
    auth_logoff();
    return false;
  }
 
  /**
   * remove punbb cookie on logout
   */
  function logOff(){
    global $forum_user;
    $forum_user = array();
    $forum_user['is_guest'] = 1;
    forum_setcookie(1, random_key(8), time() + 31536000);
  }
}
//Setup VIM: ex: et ts=2 enc=utf-8 :

Discussion

  • Some username are not understand by DokuWiki (Pérot became P�rot or Pérot)

DokuWiki uses UTF-8 and PunBB ISO8859-1, so this is just a character encoding problem. In order to get the right usernames, open /dokuwiki/inc/auth/punbb.class.php, find (l136 on v. 2007-06-26b) :

    if(isset($pun_user) && !$pun_user['is_guest']){
      // okay we're logged in - set the globals
      $USERINFO['pass'] = $pun_user['password'];
      $USERINFO['name'] = $pun_user['realname'];
      $USERINFO['mail'] = $pun_user['email'];
      $USERINFO['grps'] = array($pun_user['g_title']);
 
      $_SERVER['REMOTE_USER'] = $pun_user['username'];
      $_SESSION[DOKU_COOKIE]['auth']['user'] = $pun_user['username'];
      $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
      return true;
    }

And replace by :

    if(isset($pun_user) && !$pun_user['is_guest']){
      // okay we're logged in - set the globals
      $USERINFO['pass'] = $pun_user['password'];
      $USERINFO['name'] = utf8_encode($pun_user['realname']);
      $USERINFO['mail'] = $pun_user['email'];
      $USERINFO['grps'] = array($pun_user['g_title']);
 
      $_SERVER['REMOTE_USER'] = utf8_encode($pun_user['username']);
      $_SESSION[DOKU_COOKIE]['auth']['user'] = utf8_encode($pun_user['username']);
      $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
      return true;
    }

Maximilien Thiel 2007-11-13 - 11:00 UTC +01:00

  • As punbb 1.3 will support utf8, a better patch is :
          // okay we're logged in - set the globals
          $USERINFO['pass'] = $pun_user['password'];
          $USERINFO['name'] = preg_match('/^1\.2/',$pun_config['o_cur_version']) ? utf8_encode($pun_user['realname']) : $pun_user['realname'];
          $USERINFO['mail'] = $pun_user['email'];
          $USERINFO['grps'] = array($pun_user['g_title']);

    (only the $USERINFO['name'] line is changed)
    Stéphane Gully

  • I think there is a problem when you use DokuWiki's ACL : a regular member with appropriate rights under PunBB can not edit a page though ACL seems properly configured (changes forbidden in : but allowed in :help: ). The problem may come from the fact DokuWiki has a “user” group while PunBB has a “members” and this is not *always* correctly seen.
    • yann: edit the file conf/acl.auth.php, add there the group members (duplicate the line with “user”, and replace “user” by “members”.
    • Better yet, use the group Members as is defined in the table groups in the PunBB MySQL database.

~ ここまでのDiscussionは2010/01/06のコピーです。英語版の最新ページもあわせて閲覧することを推奨します。 ~

PunBBの公式サイトの下記フォーラムも参考に。

utf8関数でエラーが出る問題

※PunBB-1.3.4/DokuWiki-2009-12-25の場合

PunBBのinclude/utf8ディレクトリ内には、utf8用の文字列操作関数を定義したファイルが複数存在する。これらは実際のところDokuWikiのinc/utf8.phpのコードをバラにして格納したものである。このままだと統合したときに「同じ関数が再定義されている」というエラーが出てしまう。そこで、PunBBでもDokuWiki内のinc/utf8.phpのコードを使うように変更する。

PunBB内の、include/essentials.php内の25行目~27行目をコメントアウトし、DOKU_ROOTにDokuWikiの絶対パスを定義して、inc/utf8.phpを読み込ませる。

変更前

// Load UTF-8 functions
require FORUM_ROOT.'include/utf8/utf8.php';
require FORUM_ROOT.'include/utf8/ucwords.php';
require FORUM_ROOT.'include/utf8/trim.php';

変更後

// Load UTF-8 functions
//require FORUM_ROOT.'include/utf8/utf8.php';
//require FORUM_ROOT.'include/utf8/ucwords.php';
//require FORUM_ROOT.'include/utf8/trim.php';
if(!defined('DOKU_ROOT')){
  define('DOKU_ROOT','/home/wiki/');
}
require DOKU_ROOT.'inc/utf8.php';
1)
php.ini による設定か .htaccess内にphp_value magic_quotes_gpc offを記述
ja/auth/punbb.txt · 最終更新: 2011-01-11 18:10 by 114.22.211.121

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki