Integrate PunBB 1.2 with MediaWiki

From PunBB Resource Wiki

Jump to: navigation, search

The following are instructions for creating a MediaWiki extension that will automatically authenticate the currently logged-in PunBB user to MediaWiki.

Note: The following integration method has only been tested in MediaWiki 1.16.5.

Contents

Installation

1. Save the following source code as extensions/PunBBAuth.php

<?php
# PunBB MediaWiki extension

if( !defined( 'MEDIAWIKI' ) )
die();
 
$wgExtensionCredits['other'][] = array(
    'name' => 'PunBB Authentication',
    'description' => 'Auto-authenticates the current PunBB user',
    'author' => 'Koos'
);
 
 
# PunBB integration

//define('PUN_QUIET_VISIT', 1);
define('PUN_ROOT', '../'); // Path to PunBB
require PUN_ROOT.'include/common.php';
 
$wgHooks['UserLoadFromSession'][] = 'AutoAuthenticatePunBB';
$wgHooks['UserLogoutComplete'][] = 'UserLogoutPunBB';
$wgHooks['UserLoginForm'][] = 'UserLoginFormPunBB';
 
function AutoAuthenticatePunBB($user,&$result) {
        global $pun_user;
 
        if ($pun_user['is_guest']) {
                return true;
        }
 
        $utf8name = ucfirst(iconv("ISO-8859-1", "UTF-8", $pun_user['username']));
        $user = User::newFromName($utf8name,$validate='valid');
        if ( $user->getID() == 0 ) {
                $user->addToDatabase();
                $user->setEmail(iconv("ISO-8859-1", "UTF-8", $pun_user['email']));
                $user->setRealName(iconv("ISO-8859-1", "UTF-8", $pun_user['realname']));
                $user->setToken();
                $user->saveSettings();
        } else {
                if($user->loadFromDatabase()) {
                $user->saveToCache();
                $wgUser = $user;
                $wgUser->setCookies();
                }
        }
 
        return true;
}
 
function UserLogoutPunBB(&$user) {
        global $pun_user, $pun_config;
 
        redirect($pun_config['o_base_url'].'/login.php?action=out&amp;id='.$pun_user['id'].'&amp;csrf_token='.sha1($pun_user['id'].sha1(get_remote_address())), 'Logging out. Redirecting &hellip;');
}
 
function UserLoginFormPunBB(&$template) {
        global $pun_config;
 
        $referer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'index.php';
        header('Location: '.$pun_config['o_base_url'].'/login.php?redirect_url='.urlencode($referer));
        return true;
}
 
?>

Indicating the relative path to access your forum at this line:

define('PUN_ROOT', '../'); // Path to PunBB

In this example:

2. Add the line:

require_once("$IP/extensions/PunBBAuth.php");

to LocalSettings.php.

PunBB forum edits

Edit 1

1. Open config.php

2. Find

$p_connect = false;

3. Before, add

$wiki_prefix = 'wiki_'; // Your wiki database table prefix

4. Open profile.php

5. Find

		$result = $db->query('SELECT activate_string, activate_key FROM '.$db->prefix.'users WHERE id='.$id) or error('Unable to fetch activation data', __FILE__, __LINE__, $db->error());
		list($new_email, $new_email_key) = $db->fetch_row($result);

6. Replace with

		$result = $db->query('SELECT activate_string, activate_key, username FROM '.$db->prefix.'users WHERE id='.$id) or error('Unable to fetch activation data', __FILE__, __LINE__, $db->error());
		list($new_email, $new_email_key, $username) = $db->fetch_row($result);

7. Find

			$db->query('UPDATE '.$db->prefix.'users SET email=activate_string, activate_string=NULL, activate_key=NULL WHERE id='.$id) or error('Unable to update e-mail address', __FILE__, __LINE__, $db->error());

8. After, add

			$db->query('UPDATE '.$wiki_prefix.'user SET user_email=\''.$db->escape($new_email).'\' WHERE user_name=\''.$db->escape($username).'\'') or error('Unable to update e-mail address', __FILE__, __LINE__, $db->error());

9. Find

		$db->query('DELETE FROM '.$db->prefix.'users WHERE id='.$id) or error('Unable to delete user', __FILE__, __LINE__, $db->error());

10. After, add

		$db->query('DELETE FROM '.$wiki_prefix.'user WHERE user_name=\''.$db->escape($username).'\'') or error('Unable to delete user', __FILE__, __LINE__, $db->error());


11. Find

	// Fetch the user group of the user we are editing
	$result = $db->query('SELECT group_id FROM '.$db->prefix.'users WHERE id='.$id) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
	if (!$db->num_rows($result))
		message($lang_common['Bad request']);
 
	$group_id = $db->result($result);

12. Replace with

	// Fetch the user group of the user we are editing
	$result = $db->query('SELECT username, group_id FROM '.$db->prefix.'users WHERE id='.$id) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
	if (!$db->num_rows($result))
		message($lang_common['Bad request']);
 
	list($first_username, $group_id) = $db->fetch_row($result);

13. Find

	$db->query('UPDATE '.$db->prefix.'users SET '.implode(',', $temp).' WHERE id='.$id) or error('Unable to update profile', __FILE__, __LINE__, $db->error());

14. After, add

	if ($section == 'essentials' && ($pun_config['o_regs_verify'] == '0' || $pun_user['g_id'] < PUN_GUEST))
		$db->query('UPDATE '.$wiki_prefix.'user SET user_email=\''.$db->escape($form['email']).'\' WHERE user_name=\''.$db->escape($first_username).'\'') or error('Unable to update e-mail address', __FILE__, __LINE__, $db->error());
	if ($section == 'personal')
		$db->query('UPDATE '.$wiki_prefix.'user SET user_real_name=\''.$db->escape($form['realname']).'\' WHERE user_name=\''.$db->escape($first_username).'\'') or error('Unable to update e-mail address', __FILE__, __LINE__, $db->error());


15. Find

		$db->query('UPDATE '.$db->prefix.'online SET ident=\''.$db->escape($form['username']).'\' WHERE ident=\''.$db->escape($old_username).'\'') or error('Unable to update online list', __FILE__, __LINE__, $db->error());

16. After, add

		$db->query('UPDATE '.$wiki_prefix.'user SET user_name=\''.$db->escape($form['username']).'\' WHERE user_name=\''.$db->escape($old_username).'\'') or error('Unable to update e-mail address', __FILE__, __LINE__, $db->error());

Edit 2

If your wiki is located at a base domain different from your forum's base domain, e.g. http://wiki.mydomain.com, you can make the following change so that you are redirected back to the wiki after logging in from the wiki.

1. Open register.php

2. Find

$redirect_url = (isset($_SERVER['HTTP_REFERER']) && preg_match('#^'.preg_quote($pun_config['o_base_url']).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER'])) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : 'index.php';

3. Replace with

//$redirect_url = (isset($_SERVER['HTTP_REFERER']) && preg_match('#^'.preg_quote($pun_config['o_base_url']).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER'])) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : 'index.php';
// MOD: Wiki integration
$wiki_base_url = 'http://wiki.mydomain.com';
$redirect_url = (isset($_SERVER['HTTP_REFERER']) && (preg_match('#^'.preg_quote($pun_config['o_base_url']).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER']) || preg_match('#^'.preg_quote($wiki_base_url).'#i', $_SERVER['HTTP_REFERER']))) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : 'index.php';

References

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox