1 (edited by rajivm 2006-11-05 05:08)

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;

    }
}
?>

Re: Login Integration Sample Code

Hi,

I'd like to do something similar. My question: I just want to change the script that recognizes users. Instead of using PunBB cookies, I want to check with my own login cookies, and if matched, perhaps automatically set the PunBB cookies. What programs should I edit to make this happen?

Thanks!

Re: Login Integration Sample Code

Hi,

A view questions:

        define('PUNBB_CK', "fadfc7a5");

Is this the:

$cookie_seed = 'c3fc8ccb';

found in config.php


If is: maybe it's nice to add in a comment so people that don't know much of PHP can use this.

I found another strange comment in your class:
// Get PHPBB USERID

I think that need to be punBB.

I tested it and it worked.

Re: Login Integration Sample Code

I will post a documented version some time soon. All the above code is licensed under modified BSD.

Re: Login Integration Sample Code

rajivm wrote:

I will post a documented version some time soon. All the above code is licensed under modified BSD.

Doesn't it need to be under the GPL? wink

Re: Login Integration Sample Code

You're right I probably borrowed a couple lines of PunBB code for that - GPL is fine w/ me too. I was just trying to say that I really didn't care what people did with the code as before I never really gave permission for someone to use it.