Extension:PunBB Authentication

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
PunBB Authentication

Release status: unknown

Implementation User identity
Description Auto-authenticates PunBB users
Author(s) Bradley Bell
Last version 1.0
License No license specified
Download This page
Hooks used
AutoAuthenticate

UserLoginForm
UserLogout

Check usage and version matrix; Authentication stats

Purpose[edit]

PunBB Authentication is an extension that will automatically authenticate the currently logged-in PunBB user to MediaWiki.

Installation[edit]

The login and logout hooks will not work with a stock installation of PunBB, but they could be made to work with a little customization to PunBB's login.php.

Save the source code as extensions/PunBBAuth.php

Add the line:

require_once("extensions/PunBBAuth.php");

to LocalSettings.php.

Source code[edit]

<?php
/**
 * PunBB MediaWiki extension
 *
 * @file
 * @ingroup Extensions
 * @version 1.0
 * @author Bradley Bell
 * @link http://www.mediawiki.org/wiki/Extension:PunBB_Authentication Documentation
 */
 
if( !defined( 'MEDIAWIKI' ) )
        die();
 
// Extension credits that will show up on Special:Version
$wgExtensionCredits['other'][] = array(
        'name' => 'PunBB Authentication',
        'version' => '1.0',
        'author' => 'Bradley Bell',
        'url' => 'http://www.mediawiki.org/wiki/Extension:PunBB_Authentication',
        'description' => 'Auto-authenticates users using PunBB database',
);
 
 
# PunBB integration

//define('PUN_QUIET_VISIT', 1);
define('PUN_ROOT', '/path/to/punbb/');
require PUN_ROOT.'include/common.php';
 
$wgHooks['UserLoadFromSession'][] = 'AutoAuthenticatePunBB';
//$wgHooks['UserLogout'][] = 'UserLogoutPunBB';
//$wgHooks['UserLoginForm'][] = 'UserLoginFormPunBB';
 
function AutoAuthenticatePunBB( &$user, &$result ) {
        global $pun_user;
 
        wfSetupSession();
 
        if( $pun_user['is_guest'] ) {
                return true;
        }
 
        /**
         * A lot of this is from User::newFromName
         */
        $validate = true;
 
        // Force usernames to capital
        global $wgContLang;
        $name = $wgContLang->ucfirst( $pun_user['username'] );
 
        // Clean up name according to title rules
        $t = Title::newFromText( $name );
        if( is_null( $t ) ) {
                return null;
        }
 
        // Reject various classes of invalid names
        $canonicalName = $t->getText();
        global $wgAuth;
        $canonicalName = $wgAuth->getCanonicalName( $t->getText() );
 
        if( $validate && !User::isValidUserName( $canonicalName ) ) {
                return null;
        }
 
        $user->setName( $pun_user['username'] );
        $user->setId( $user->idFromName( $pun_user['username'] ) );
        if ( $user->getID() == 0 ) {
                /**
                 * A lot of this is from LoginForm::initUser
                 */
                $user->addToDatabase();
                $user->setEmail( $pun_user['email'] );
                $user->setRealName( $pun_user['realname'] );
                $user->setToken();
                $user->confirmEmail();
                if( $pun_user['g_id'] == PUN_ADMIN ) {
                        $user->addGroup('sysop');
                }
        } else {
                /* Should cache some day, I guess :) */
                $user->loadFromDatabase();
                $user->setToken();
        }
        $result = true;
        return true;
}
 
function UserLogoutPunBB( &$user ) {
        global $pun_user;
        redirect('/forums/login.php?action=out&id='.$pun_user['id'], 'Logging out. Redirecting &hellip;');
        return true;
}
 
function UserLoginFormPunBB( &$template ) {
        $referer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'index.php';
        header('Location: http://hostname/forums/login.php?redirect_url='.$referer);
        return true;
}