Open main menu

Extension:SMF Authentication

MediaWiki extensions manual
OOjs UI icon advanced.svg
SMF_Authentication
Release status: stable
Implementation User identity , User activity
Description Authenticates users from SMF(Simple Machines Forum) and automatic log them in/out at MediaWiki.
Author(s) Snakehittalk
Latest version 1.2 (2008-01-11)
MediaWiki 1.11.0
License No license specified
Download http://www.snakehit.be/Downloads/SMF_Authentication.zip
Translate the SMF Authentication extension if it is available at translatewiki.net
Check usage and version matrix.

Contents

Version InfomationEdit

  • Extension: SMF Authentication
  • Forum: SMF (http://www.simplemachines.org/)
  • Version: 1.2
  • Released: 18.09.2007
  • Last updated: 11.01.2008
  • SMF Version: <1.1.6
  • MW Version: 1.11.0


What can this extension do?Edit

This extension is a connection between SMF (Simple Machines Forum) and MediaWiki. This causes users who are logged in to SMF to be (created if necessary and) logged in to the wiki. When the session expires or they logout via SMF, they will no longer be logged in to the wiki.

This is ideal for my situation since I desire tight user integration between SMF and MediaWiki, and I require users to be logged in to edit pages. My set of wiki users will be an exact subset of my SMF users, and the admin username is the same.

Installation instructionsEdit

1. Download SMF_Authentication.zip.

2. Add wiki/extensions/Auth_SMF to your extension folder (can be found in the zip file)

<?php
/**********************************************************************************
* Auth_SMF.php                                                                       						   
***********************************************************************************
* Connection between SMF and MediaWiki.										                                             
* =================================================================================
* Software Version:   	1.2                           					
* Software by:		Snakehit (snakehit (at) gmail.com)     						
***********************************************************************************/
	//Necessary for MediaWiki.com
	$wgExtensionCredits['validextensionclass'][] = array(
		'name' 			=> 'SMF_Authentication',
		'author' 		=> 'Snakehit (snakehit@gmail.com)', 
		'url' 			=> 'http://www.mediawiki.org/wiki/User:Snakehit', 
		'description' 	=> 'This Extension is a connection between SMF and MediaWiki. It automatic logins and logouts on both platforms'
	);

	error_reporting(E_ALL); // Debug

	//Load the SMF files ;-) 
	if(!require_once($wgSMFPathAPI))
		die('Could not load the SMF API');
		
	//Authenticate the SMF User
	smf_authenticateUser();
	
	//Hooks! 
	$wgHooks['AutoAuthenticate'][] 	= 'AutoAuthenticateSMF';
	$wgHooks['UserLogout'][] 		= 'AutoLogoutSMF';
						
	/**
	 * AutoAuthenticateSMF is the function that creates an automatic login if you are logged in to SMF.
	 *
	 * @param User $user
	 * @return bool
	 * @access public
	 */
	function AutoAuthenticateSMF(&$user) {
        global $smf_user_info;
		
		//Is user a guest?
		if ($smf_user_info['is_guest']) 
				return true;	
		
		//User isn't a guest
		if (!$smf_user_info['is_guest']) {
	        $user->setName($smf_user_info['memberName']);
	        $user->setId($user->idForName());
				
			if ($user->getID() == 0) {
				//User doesn't exist :'(
				$user->setName($smf_user_info['memberName']);		// Set memberName
				$user->setEmail($smf_user_info['email']); 			// Set Email Address.
				$user->setRealName($smf_user_info['realName']);  	// Set Real Name.
				$user->addToDatabase();	
				
				//if user is_admin add him to the SysOp group! Wii ;-)
				if ($smf_user_info['is_admin']) 
                        $user->addGroup("sysop");
						
			} else {
				//User exists     
				$user->loadFromDatabase();
			}
			
			// Go ahead and log 'em in
			$user->setToken();
			$user->saveSettings();
        }

		return true;
	}
	
	/**
	 * AutoLogoutSMF is the function that logout on MediaWiki and on SMF
	 *
	 * @param User $user
	 * @return bool
	 * @access public
	 */
	function AutoLogoutSMF(&$user) {
		global $smf_user_info, $smf_settings, $wgSMFPathIndex;
		
		smf_query("
			DELETE FROM $smf_settings[db_prefix]log_online
			WHERE ID_MEMBER = '".$smf_user_info['ID_MEMBER']."'
			LIMIT 1", __FILE__, __LINE__);
		$_SESSION['log_time'] = 0;
		smf_setLoginCookie(-3600, $smf_user_info['ID_MEMBER'], $smf_user_info['passwd']);
		
		header("Location: $wgSMFPathIndex");
		exit(0);
	}
	
	
    // First check if class has already been defined, isn't it require it
    if (!class_exists('AuthPlugin')) 
        require_once './includes/AuthPlugin.php';
		
	/**
	  * Handles the authentication with the SMF database.
	  *
	  */
	class Auth_SMF extends AuthPlugin {
		/**
		 * Check whether there exists a user account with the given name.
		 *
		 * @param string $username
		 * @return bool
		 * @access public
		 */
		function userExists($username) {
			return smf_authenticateUser();
		}
	
		/**
		 * Check if a username and password pair is a valid login.
		 *
		 * @param string $username
		 * @param string $password
		 * @return bool
		 * @access public
		 */
		function authenticate($username, $password) {	
			global $smf_user_info, $smf_settings;
			if($smf_user_info['is_guest']) {
				$result = smf_query("
					SELECT passwd
					FROM $smf_settings[db_prefix]members
					WHERE memberName = '$username'
					LIMIT 1", __FILE__, __LINE__);
				
				list ($pass) = mysql_fetch_row($result);
				mysql_free_result($result);

				$pw = sha1(strtolower($username) . $password);

				if ($pw == $pass)
					return smf_setLoginCookie(3600, $username, $password, false);
			}
			return false;
		}
	
		/**
		 * Modify options in the login template.
		 *
		 * @param UserLoginTemplate $template
		 * @access public
		 */
		function modifyUITemplate(&$template) {
			$template->set('domain', 	  false);  // Don't get in touch with domain
            $template->set('usedomain',   false); // We do not want a domain name.
            $template->set('create',      false); // Remove option to create new accounts from the wiki.
            $template->set('useemail',    false); // Disable the mail new password box.
			$template->set('remember',    false); // Disable 'remember me' box
		}

		/**
		 * Set the domain this plugin is supposed to use when authenticating.
		 *
		 * @param string $domain
		 * @access public
		 */
		function setDomain($domain) {
			$this->domain = $domain;
		}

		/**
		 * Check to see if the specific domain is a valid domain.
		 *
		 * @param string $domain
		 * @return bool
		 * @access public
		 */
		function validDomain($domain) {
			return true;
		}

		/**
		 * When a user logs in, optionally fill in preferences and such.
		 * For instance, you might pull the email address or real name from the
		 * external user database.
		 *
		 * The User object is passed by reference so it can be modified; don't
		 * forget the & on your function declaration.
		 *
		 * @param User $user
		 * @access public
		 */
		function updateUser( &$user ) {
			global $smf_user_info;
			$user->mEmail       = $smf_user_info['email']; 		// Set Email Address.
			$user->mRealName    = $smf_user_info['realName'];  	// Set Real Name.
			return true;
		}

		/**
		 * Return true if the wiki should create a new local account automatically
		 * when asked to login a user who doesn't exist locally but does in the
		 * external auth database.
		 *
		 * If you don't automatically create accounts, you must still create
		 * accounts in some way. It's not possible to authenticate without
		 * a local account.
		 *
		 * This is just a question, and shouldn't perform any actions.
		 *
		 * @return bool
		 * @access public
		 */
		function autoCreate() {
			return true;
		}
	
		/**
		 * Set the given password in the authentication database.
		 * Return true if successful.
		 *
		 * @param string $password
		 * @return bool
		 * @access public
		 */
		function setPassword($password) {
			return false;
		}

		/**
		 * Update user information in the external authentication database.
		 * Return true if successful.
		 *
		 * @param User $user
		 * @return bool
		 * @access public
		 */
		function updateExternalDB($user) {
			return true;
		}

		/**
		 * Check to see if external accounts can be created.
		 * Return true if external accounts can be created.
		 *
		 * @return bool
		 * @access public
		 */
		function canCreateAccounts() {
			return false;
		}

		/**
		 * Add a user to the external authentication database.
		 * Return true if successful.
		 *
		 * @param User $user
		 * @param string $password
		 * @return bool
		 * @access public
		 */
		function addUser($user, $password) {
			return false;
		}

		/**
		 * Return true to prevent logins that don't authenticate here from being checked against the local database's password fields.
		 *
		 * This is just a question, and shouldn't perform any actions.
		 *
		 * @return bool
		 * @access public
		 */
		function strict() {
			return true;
		}
	
		/**
		 * Can users change their passwords?
		 *
		 * @return bool
		 */
		function allowPasswordChange() {
			return false;
		}
		
		/**
		 * When creating a user account, optionally fill in preferences and such.
		 * For instance, you might pull the email address or real name from the external user database.
		 *
		 * The User object is passed by reference so it can be modified; don't forget the & on your function declaration.
		 *
		 * @param User $user
		 * @access public
		 */
		function initUser(&$user) {
			global $smf_user_info;
			
			$user->mEmail       		= $smf_user_info['email']; 		// Set Email Address.
			$user->mRealName    		= $smf_user_info['realName'];  	// Set Real Name.
			$user->mEmailAuthenticated 	= wfTimestampNow(); 			// Add TimeStamp
		    $user->setToken();
		 
		    $user->saveSettings();
			return true;
		}
	
		/**
		 * If you want to munge the case of an account name before the final check, now is your chance.
		 *
		 * @param Username $username
		 * @return  username
		 * @access public
		 */
		function getCanonicalName($username) {
			global $smf_user_info;
			if(empty($smf_user_info['memberName']))
				return $username;
			
	     	return ucfirst(strtolower($smf_user_info['memberName']));
		}
	}

3. Upload the other files from the zip

---> ./wiki/includes/templates/Userlogin.php (Changes the Userlogin :)) This is a part of the code inside Userlogin.php to show the message where users need to login.

global $smf_user_info, $smf_settings;
echo '<p>Please login at <a href="', $smf_settings['forum_url'] ,'/">with your forum username</a> or <a href="', $smf_settings['forum_url'] ,'/index.php?action=register">Register here</a> your account</p>';

---> ./wiki/includes/GlobalFunctions.php (Fixed some little bugs)

---> ./wiki/includes/User.php (Fixed some little bugs)

---> ./smf_api.php (API of SMF (the newest one is 1.1.4, it's included in the zip file))

---> ./Sources/LogInOut.php (Fixes the logout function of mediawiki in combination with SMF)

4. Open LocalSettings.php and add the following changes to the bottom of the file(before the ?>).

#### SMF Authentication
#Snakehit (snakehit (at) gmail.com)

# For all non-logged in users
$wgGroupPermissions['*']['createaccount']   = false; # This requires a user be logged into the wiki to make changes.
$wgGroupPermissions['*']['edit']            = false; # Specify who may create new accounts: 0 means no, 1 means yes

# Disable everything of caching, caching is boe for the login system.
$wgEnableParserCache = false;
$wgCachePages = false;

# Users can use the login function inside of the wiki to login.
$wgUseWikiLogin = false;

# This requires the link to SMF API. 
$wgSMFPathAPI= "../smf_api.php"; 	# The smf_api.php file can be download from SMF website and must be placed in rootdir of your forum)
$wgSMFPathIndex= "http://localhost/SMF/index.php";	# Full path to index.php file of SMF

// require the Auth_SMF.php
require_once './extensions/Auth_SMF.php';

$wgAuth = new Auth_SMF();

5. Try ;-)


  • TIP:

Read the readme file in the package and the installation will work without any problem.

Author contactEdit

You can contact me at snakehit at gmail.com.