Topic: Login Integration Sample Code
Here is the code I wrote for use at musikcube.com and lynbrooksd.net to synchronize & link authentication. There are hacks to the forum to redirect login & registration links also. Although this code is probably not directly applicable anywhere, this should give some quick insight to those looking to integrate PunBB.
Licensed under new BSD:
<?php
/**
* 2005 Project Musik.
* Authored by Rajiv Makhijani.
*
* Musik-Site Component File
* - 3rd Party User Integration -> PUNBB
*
*/
/**
* PUNBB Login
*
* @author Rajiv Makhijani <rajiv@lynbrooksd.net>
* @package musiksite
* @version 0.1
* @lastupdated 9 April 2005
* @access public
* @copyright Rajiv Makhijani.
*
*/
class Login_PUNBB
{
function Login_PUNBB()
{
global $DB;
define('PUNBB_CK', "fadfc7a5");
}
function doLogin($nick)
{
$this->doLogout();
global $DB;
$nick = addslashes(trim($nick));
// Get PHPBB USERID
$query = "SELECT `id`, `password`, `save_pass` FROM punbb_users WHERE `username` LIKE '$nick'";
$result = $DB->sqlQuery($query);
if ($result == false) { return false; }
$result = $DB->getNextRow($result);
if (!isset($result['id'])) { return false; }
$userid = $result['id'];
$passhash = $result['password'];
$expire = ($result['save_pass'] == '1') ? time() + 31536000 : false;
setcookie('punbb_cookie', serialize(array($userid, md5(PUNBB_CK . $passhash))), $expire);
return true;
}
function doLogout()
{
global $DB;
$puncookie = $_COOKIE['punbb_cookie'];
list($userid, $passhash) = @unserialize($puncookie);
//serialize(array(1, md5(PUNBB_CK.$password_hash)))
setcookie('punbb_cookie', "", 31536000, "/");
// Remove user from "users online" list.
$query = "DELETE FROM punbb_online WHERE user_id='$userid'";
$result = $DB->sqlQuery($query);
if ($result == false) { return false; }
// Update last_visit (make sure there's something to update it with)
$ctime = time();
$query = "UPDATE punbb_users SET last_visit=$ctime WHERE id='$userid'";
$result = $DB->sqlQuery($query);
if ($result == false) { return false; }
return true;
}
function setEmail($nick, $email)
{
global $DB;
$nick = addslashes($nick);
$email = addslashes($email);
$query = "UPDATE punbb_users SET email = '$email' WHERE username LIKE '$nick'";
$result = $DB->sqlQuery($query);
if (!$result) { return false; }
return true;
}
function setPassword($nick, $password)
{
global $DB;
global $member;
$nick = addslashes($nick);
$password = $member->hashPassword($password);
$query = "UPDATE punbb_users SET password = '$password' WHERE username LIKE '$nick'";
$result = $DB->sqlQuery($query);
if (!$result) { return false; }
return true;
}
function setRealName($nick, $realname)
{
global $DB;
$nick = addslashes($nick);
$realname = addslashes($realname);
$query = "UPDATE punbb_users SET realname = '$realname' WHERE username LIKE '$nick'";
$result = $DB->sqlQuery($query);
if (!$result) { return false; }
return true;
}
function addUser($nick, $email, $password, $realname = "")
{
global $DB;
global $member;
$nick = addslashes($nick);
$email = addslashes($email);
$password = $member->hashPassword($password);
$realname = addslashes($realname);
$ctime = time();
// Add User to User Table
$query = "INSERT INTO punbb_users
(username, group_id, realname, password, email, email_setting, save_pass,
timezone, language, style, registered, registration_ip, last_visit)
VALUES
('$nick', '4', '$realname', '$password', '$email', '1', '1',
'0', 'English', '', '$ctime', '0.0.0.0', '$ctime')";
$result = $DB->sqlQuery($query);
if (!$result) { return false; }
$userid = $DB->getInsertID();
return true;
}
}
?>