This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc.

This is a single sign-on extension for MediaWiki and phpBB3. It does not use any funny tricks like curl or anything like that. It actually uses the phpBB sessions to do it all. When a user logs into the forum, they will be logged into the wiki. If a user logs out of the forum, they are logged out of the wiki.

The extension also replaces the wiki login and logout links with the ones from the forum.

This plugin is a hacked version of Extension:Auth_remoteuser. It was used as the base for this, I just replaced all needed code to be specific to phpBB.

This is very beta - Please make any edits to this code to make it better. I probably will not keep the code current, so please take control.

So... This little guy, what he does is: When a user comes to the wiki, it checks to see if the user is currently logged into the forum. If not, nothing happens. If so, he then looks to see if the user is logged into the wiki. If the user is, then everything is good. If the user is not, he takes the session data from phpbb and logs the user in automatically to the wiki. If there is no such user on the wiki, he creates the user.

<?php$wgExtensionCredits['other'][]=array('name'=>'phpBB SSO','version'=>'0.12.1','author'=>array('Adam Meyer','Otheus Shelling','Rusty Burchfield','James Kinsman','Daniel Thomas','Ian Ward Comfort','AJ Quick','P-H Baudin'),'url'=>'http://www.mediawiki.org/wiki/Extension:Phpbb_Single_Sign-On','description'=>'Automatically logs users in/out using their phpBB session. Based on the Auth_remote_user extension',);// This requires a user be logged into the wiki to make changes and no anonymous edits$GLOBALS['wgGroupPermissions']['*']['edit']=false;$GLOBALS['wgGroupPermissions']['*']['createaccount']=false;if(!isset($wgPhpBBForumLocation)){$wgPhpBBForumLocation='./../forum/';}require('phpBBUser.php');$wgPhpBBUser=newphpBBUser($wgPhpBBForumLocation);require('AuthRemoteUser.php');$wgAuth=newAuthRemoteUser();$wgExtensionFunctions[]='phpbbSSOExtensionFunction';$wgHooks['SpecialPage_initList'][]='onSpecialPage_initList';$wgHooks['PersonalUrls'][]='onPersonalUrls';functionphpbbSSOExtensionFunction(){global$wgUser,$wgRequest,$wgPhpBBUser,$wgAuth;$user=User::newFromSession();// For a few special pages, don't do anything.$title=$wgRequest->getVal('title');if(in_array($title,array(Title::makeName(NS_SPECIAL,'UserLogout'),Title::makeName(NS_SPECIAL,'UserLogin')))){return;}if($wgPhpBBUser->isAnonymous()){$user->doLogout();return;}if(!$user->isAnon()){if($user->getName()==$wgAuth->getCanonicalName($wgPhpBBUser->username_clean)){return;// Correct user is already logged in.}else{$user->doLogout();// Logout mismatched user.}}// Copied from includes/SpecialUserlogin.phpif(!isset($wgCommandLineMode)&&!isset($_COOKIE[session_name()])){wfSetupSession();}// If the login form returns NEED_TOKEN try once more with the right token$trycount=0;$token='';$errormessage='';do{$tryagain=false;// Submit a fake login form to authenticate the user.$params=newFauxRequest(array('wpName'=>$wgAuth->getCanonicalName($wgPhpBBUser->username_clean),'wpPassword'=>' ','wpDomain'=>'','wpLoginToken'=>$token,'wpRemember'=>''));// Authenticate user data will automatically create new users.$loginForm=newLoginForm($params);$result=$loginForm->authenticateUserData();switch($result){caseLoginForm::SUCCESS:$wgUser->setOption('rememberpassword',1);$wgUser->setCookies();break;caseLoginForm::NEED_TOKEN:$token=$loginForm->getLoginToken();$tryagain=($trycount==0);break;caseLoginForm::WRONG_TOKEN:$errormessage='WrongToken';break;caseLoginForm::NO_NAME:$errormessage='NoName';break;caseLoginForm::ILLEGAL:$errormessage='Illegal';break;caseLoginForm::WRONG_PLUGIN_PASS:$errormessage='WrongPluginPass';break;caseLoginForm::NOT_EXISTS:$errormessage='NotExists';break;caseLoginForm::WRONG_PASS:$errormessage='WrongPass';break;caseLoginForm::EMPTY_PASS:$errormessage='EmptyPass';break;default:$errormessage='Unknown';break;}if($result!=LoginForm::SUCCESS&&$result!=LoginForm::NEED_TOKEN){echo$errormessage;error_log('Unexpected REMOTE_USER authentication failure. Login Error was:'.$errormessage);}$trycount++;}while($tryagain);return;}functiononSpecialPage_initList(&$aSpecialPages){unset($aSpecialPages['Userlogout']);unset($aSpecialPages['Userlogin']);returntrue;}functiononPersonalUrls(array&$personal_urls,Title$title,SkinTemplate$skin){global$wgPhpBBForumLocation,$wgPhpBBUser;$forumLocation=dirname(dirname(substr(__DIR__,strlen(str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['SCRIPT_FILENAME']))))).'/'.$wgPhpBBForumLocation;if(array_key_exists('login',$personal_urls)){$personal_urls['login']['href']=$forumLocation.'ucp.php?mode=login';}if(array_key_exists('anonlogin',$personal_urls)){$personal_urls['anonlogin']['href']=$forumLocation.'ucp.php?mode=login';}if(array_key_exists('logout',$personal_urls)){$personal_urls['logout']['href']=$forumLocation.'ucp.php?mode=logout&sid='.$wgPhpBBUser->session_id;}returntrue;}

<?phpclassphpBBUser{/* * Configuration */constREQUIRE_GROUP=false;// Must the user be part of a specific group?constWIKI_GROUP='Wiki';// If yes, what group?public$userId;public$userType;public$userName;public$email;public$username_clean;public$session_id;public$messages;/* * Connect to phpBB and setup/get session */function__construct($forumDirectory){global$db,$cache,$config,$user,$auth,$template,$phpbb_root_path,$phpEx,$request,$symfony_request,$phpbb_filesystem,$phpbb_container,$phpbb_dispatcher,$phpbb_extension_manager,$phpbb_path_helper;define('PHPBB_ROOT_PATH',$forumDirectory);define('IN_PHPBB',true);$phpbb_root_path=PHPBB_ROOT_PATH;$phpEx=substr(strrchr(__FILE__,'.'),1);include($phpbb_root_path.'common.'.$phpEx);// Start session management$user->session_begin();$auth->acl($user->data);$user->setup();$request->enable_super_globals();$this->userId=$user->data['user_id'];$this->userType=$user->data['user_type'];$this->userName=$user->data['username'];$this->email=$user->data['user_email'];$this->username_clean=$user->data['username_clean'];$this->session_id=$user->data['session_id'];$this->messages=$user->data['user_new_privmsg'];unset($db,$cache,$config,$user,$auth,$template,$phpbb_root_path,$phpEx,$request,$symfony_request,$phpbb_filesystem,$phpbb_container,$phpbb_dispatcher,$phpbb_extension_manager);}/* * Check if the user is part of a specific group */privatefunctioncheckIfUserInGroup(){global$db;// phpBB database object$sql='SELECT COUNT(*) as count FROM '.USER_GROUP_TABLE.', '.GROUPS_TABLE.' WHERE '.USER_GROUP_TABLE.'.group_id = '.GROUPS_TABLE.'.group_id AND '.USER_GROUP_TABLE.'.user_pending = 0 AND '.GROUPS_TABLE.'.group_name = "'.$db->sql_escape(self::WIKI_GROUP).'" AND '.USER_GROUP_TABLE.'.user_id = "'.$db->sql_escape($this->userId).'"';$result=$db->sql_query($sql);$row=$db->sql_fetchrow($result);return(bool)$row['count'];}/* * Find if user is anonymous or a bot, or nor part of the group */publicfunctionisAnonymous(){if(self::REQUIRE_GROUP&&!$this->checkIfUserInGroup()){// If a group is required, and that user is not in that group, say anonymousreturntrue;}if($this->userId==1||$this->userType==1||$this->userType==2){returntrue;}returnfalse;}}