Mediawiki

From FUDforum Wiki
Jump to: navigation, search

Contents

Mediawiki, the software that is used on Wikipedia, can easily be integrated with FUDforum. The Mediawiki extension below will automatically authenticate wiki users from your FUDforum database.

Code

Save the below code under your Mediawiki's extensions directory (extensions/AuthFudforum/AuthFudforum.php):

<?php
 $wgExtensionCredits['parserhook'][] = array(
   'name'   => 'FUDforumAuth',
   'author' => 'naudefj',
   'url'    => 'http://fudforum.org/',
 );
 
 if (!class_exists('AuthPlugin')) {
   require_once('AuthPlugin.php');
 }
 
 function SetupAuthFudforum() {
   global $wgHooks;
   global $wgAuth;
 
   if ( ! empty($_COOKIE[ $GLOBALS['wgAuthFudforum_cookie'] ]) ) {
     $wgHooks['UserLoadFromSession'][] = 'Auth_fudforum_autologin_hook';
     $wgHooks['UserLogoutComplete'][] = 'Auth_fudforum_UserLogoutComplete';
     $wgAuth = new AuthFudforum();
   } else {
     $wgHooks['PersonalUrls'][] = 'Auth_fudforum_PersonalUrls_hook';
   }
 }
 
 function Auth_fudforum_autologin_hook( $return_user, &$result )
 {
   global $wgUser;
   global $wgAuth;
   global $wgContLang;
 
   // Give us a user, see if we're around
   $tmpuser = new User() ;
   $rc = $tmpuser->newFromSession();
 
   $rc = $tmpuser->load();
 
   // If there's a prior session, check that it matches the current FUDForum user
   if( $rc && $rc->isLoggedIn() ) {
     if ( $rc->authenticate($rc->getName(), '') ) {
     	wfDebug("AuthFudforum: User [" . $rc->getName() . "] already logged in. Return.\n");
       return true;
     } else {
      	wfDebug("AuthFudforum: User logged in with wrong username [" . $rc->getName() . "] Logout!\n");
	$rc->logout();
     }
   } else {
     wfDebug("AuthFudforum: User not logged on to wiki!\n");
   }
 
   $name = getCurrentFUDForumUsername();
   if (empty($name)) {
     wfDebug("AuthFudforum: No forum user - cannot log user in.\n");
     $result = false;	// Deny access
     return true;
   }
 
   $user =  User::newFromName($name);
 
   // is it a new user?
   if (0 == $user->getID() ) {
 	// we have a new user to add...
    	wfDebug("AuthFudforum: Create and login NEW user [" . $name . "]\n");
 
 	$user->setName($wgContLang->ucfirst( $name ));
 	$user->addToDatabase();
 
 	$ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
 	$ssUpdate->doUpdate();
   } else {
 	wfDebug("AuthFudforum: login existing user [" . $name . "]\n");
   }
 
   // Update email, real name, etc.
   $wgAuth->updateUser( $user );
 
   // Go ahead and log 'em in
   $user->setToken();
   $user->saveSettings();
   wfSetupSession();
   $user->setCookies();
 
   $return_user = $user;
 
   wfDebug("AuthFudforum: authenticate user ". $name ."\n");
   return true;
 }
 
 function Auth_fudforum_UserLogoutComplete(&$user, &$inject_html, $old_name) { 
     	setcookie($GLOBALS['wgAuthFudforum_cookie'], FALSE, time() - 86400, '/', $GLOBALS['wgCookieDomain']);
 	return true;
 }
 
 function Auth_fudforum_PersonalUrls_hook( &$personal_urls, &$title ) {
 	global $wgUser;
 
 	foreach (array('login', 'anonlogin') as $k) {
 		if (array_key_exists($k, $personal_urls)) {
 			unset($personal_urls[$k]);
 		}
 	}
 
 	if ( !$wgUser->isLoggedIn() ) {
 		$personal_urls['login'] = array(
 			'text' => wfMsg('userlogin'),
 			'href' => $GLOBALS['wgAuthFudforum_LoginURL']
 		);
 	}
 
 	return true;
 }
 
 class AuthFudforum extends AuthPlugin {
 
 	function userExists( $username ) {
 		return true;
 	}
 
 	function authenticate( $username, $password ) {
 		$name = getCurrentFUDForumUsername();
 
 		// ignore case since wiki wants first letter upcase and fudforum does not
 		$same = ( 0 === strcasecmp($username, $name) );
 
 		if (! $same) {
 			wfDebug("AuthFudforum: authenticate() called with mismatched username: "
 			  . "wiki=" . $username . " vs forum=" . $name . "\n");
 		}
 
 		return $same;
 	}
 
 	function updateUser( &$user ) {
 		global $wgAuth;
                global $fud_email;	// Set in getCurrentFUDForumUsername()
                global $fud_name;	// Set in getCurrentFUDForumUsername()
                global $fud_useropt;	// Set in getCurrentFUDForumUsername()
 
 		$name = $user->getName();
 
 		$user->setRealName($fud_name);
 		$user->setEmail($fud_email);
 		$user->mEmailAuthenticated = wfTimestampNow();
 
 		//turn on e-mail notifications by default
 		$user->setOption('enotifwatchlistpages', 1);
 		$user->setOption('enotifusertalkpages', 1);
 		$user->setOption('enotifminoredits', 1);
 		$user->setOption('enotifrevealaddr', 1);
 
 		 if ($fud_useropt &524288 || $fud_useropt &1048576 ) { // 524288=is_mod, 1048576=is_admin
 		   $user->addGroup('sysop');
 		 }
 
 		$user->saveSettings();
 
 		return true;
 	}
 
 	function autoCreate() {
 		return true;
 	}
 
 	function disallowPrefsEditByUser() {
 		return array(
 			'wpRealName' => true,
 			'wpUserEmail' => true,
 			'wpNick' => true
 		);
 	}
 
 	function allowPasswordChange() {
 		return false;
 	}
 
 	function setPassword( $user, $password ) {
 		return false;
 	}
 
 	function strict() {
 		return false;
 	}
 
 	function initUser( &$user ) {
 	}
 
 	function getCanonicalName( $username ) {
 		global $wgContLang;
 		return  $wgContLang->ucfirst( $username );
 	}
 
 }
 
 /**
  * Utility function for decoding cookie with username
  */
 function getCurrentFUDForumUsername()
 {
   global $fud_email;
   global $fud_name;
   global $fud_useropt;
 
   /* load forum config */
   $data = file_get_contents($GLOBALS['wgAuthFudforum_conf']);
   eval(str_replace('<?php', '', substr_replace($data, '', strpos($data, 'require'))));
 
   $fuddb = mysql_connect($GLOBALS['DBHOST'], $GLOBALS['DBHOST_USER'], $GLOBALS['DBHOST_PASSWORD'], true);
   if (!$fuddb) {
     wfDebug("AuthFudforum: Unable to connect to FUDforum database!\n");
     return '';
   }
 
   $db_selected = mysql_select_db($GLOBALS['DBHOST_DBNAME'], $fuddb);
   if (!$db_selected) {
     wfDebug("AuthFudforum: Unable to select FUDforum database!\n");
     return '';
   }
 
   $sess = $_COOKIE[ $GLOBALS['COOKIE_NAME'] ];;
   wfDebug("AuthFudforum: Fudforum cookie = " . $sess . "\n");
 
   if ($fuddb) {
     $qstr = 'SELECT u.login, u.email, u.name, u.users_opt FROM {ses} s INNER JOIN {users} u ON u.id=(CASE WHEN s.user_id>2000000000 THEN 1 ELSE s.user_id END) WHERE s.ses_id = "'. mysql_real_escape_string($sess) . '" AND u.id > 1';
     $qstr = preg_replace('/{([^}]+)}/', $GLOBALS['DBHOST_TBL_PREFIX'] .'\\1', $qstr);
     $q = mysql_query($qstr);
     $r = mysql_fetch_row($q);
     wfDebug("AuthFudforum: Fudforum user = " . $r[0] . ", email = " . $r[1] . "\n");
     if ( $r[0] == 'Anonymous Coward' || ( $r[3] & 65536) ) {	// 65536 = banned
       return '';
     } else {
       $fud_email    = $r[1];	// Save as global vars for use in updateUser
       $fud_name     = $r[2];
       $fud_useropt  = $r[3];
       return str_replace('_', ' ', $r[0]); // Remove underscores and return
     }
   } else {
     wfDebug("AuthFudforum: Unable to connect to FUDforum database.");
     return '';
   }
   mysql_close($fuddb);
 }
 ?>

Enable

To enable the extension, add the following to your wiki's LocalSetting.php file:

# Settings for AuthFudforum
$wgAuthFudforum_LoginURL  = 'http://myforum-site.com/forum/l/0/';
$wgAuthFudforum_LogoutURL = 'http://myforum-site.com/forum?q=logout';
$wgAuthFudforum_cookie    = 'fudforum_session'; 
$wgCookieDomain           = '.myforum-site.com';
$wgCookieExpiration       = 2592000;
$wgAuthFudforum_conf      = '/var/www/vhosts/mysite.com/httpdocs/forum/GLOBALS.php';
require_once 'extensions/AuthFudforum/AuthFudforum.php';
SetupAuthFudforum();

See also

  • Wikilinks.plugin, plugin that allows users to use [[wikilink]] tags as shorthand to link to an external wiki site.

External links

Languages
Personal tools
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Can't contact the database server: Cannot return last error, no db connection)


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.