Welcome Guest, Not a member yet? Register   Sign In
Logging a User as Logged in to FreakAuth - Without Launching CI?
#1

[eluser]Vik[/eluser]
I'm integrating my CI app, with the Simple Machines Forum forum software. I'm using FreakAuth for authentication.

I've already got it working so that anyone who registers, logs on, or logs out on my CI app, is automatically registered, logged on, or logged out on my SMF forum. Smile

Now I'm working on getting this to happen in the other direction, so that anyone who registers, logs on, or logs out on my SMF forum, is automatically registered, logged on, or logged out on my CI app.

SMF has very convenient, elegant hooks built into it, so that you can have SMF call any PHP function you want when a user registers, logs on, or logs out.

But of course those hooks can't launch a CI app and call a specific CI function. Grahack has actually figured out a way to launch CI and "dip into" it, as he describes in this forum thread.

But that may be overkill for my purposes. All I need is to mark a user as being logged in for FreakAuth's purposes.

So, here's my question.

To mark a user as logged in for FreakAuth's purposes, is it correct that I just need to make the appropriate entry in the CI_Sesssions database, and write the appropriate cookie to the user's system?
#2

[eluser]Grahack[/eluser]
Sorry, I hope that someone will answer this question, because I worked on the "dip into CI" thing to avoid it when I asked it to myself.

You say "overkill for your purpose", but what if it solves your problem? Anyway, remember that I didn't receive much feedback on it, neither negative nor positive. I would have been happy to see it work with another app.

(thinking loud, I'd be happy to see a growing list of compatible external php apps)

Last note: I found it simpler to remove any authentication by external apps. What if you want to integrate again another app (let's call it NewApp) to your website, will you have to code a bridge between SMF and NewApp, and another one between FAL and NewApp?

Thanks using FAL and having a look a CI dip (even if I realise that I meant 'dive' instead of 'dip', my English is not perfect).
#3

[eluser]Vik[/eluser]
I was able to put together some code that tells a CI app that the user is logged in, without launching CI.

FAL and CI's db_sessions consider someone to be logged in, if there's a cookie which matches an entry in the ci_sessions database.

It took all day, but this seems to be working. Smile

Code:
//SMF calls this routine and provides the $username as a parameter.
//If you're using this routine with an app other than SMF, just delete the parameters -
//this code ignores them.
//However, this code does need a way to get the user's email address, since it looks the user
//up in the fa_user database via the email address.
function login_to_CI_app($username, $hash_password, $cookieTime)
{
    //SMF has validated the user. So, log the user in, by
    //setting a cookie and putting a matching entry in the ci_sessions database.
    
    //ACCESS GLOBALS FROM SIMPLE MACHINES FORUM TO GET
    //USER'S EMAIL ADDRESS, AND THE NAME OF THE SMF DATABASE
    global $user_settings, $db_name;
    
    //LOAD CONFIG INFO
    $system_folder = "system";
    define('BASEPATH', $system_folder.'/');
    require_once('system/application/config/config.php');
    require_once('system/application/config/database.php');

    //SET THE COOKIE
    $theCookieName = $config['sess_cookie_name'];
    
    $theSessionID = '';
    while (strlen($theSessionID) < 32)
    {    
        $theSessionID .= mt_rand(0, mt_getrandmax());
    }
    $theSessionID = md5(uniqid($sessid, TRUE));
    
    if (strtolower($config['time_reference']) == 'gmt')
    {
        $now = time();
        $now = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));  
    
        if (strlen($now) < 10)
        {
            $this->now = time();
            log_message('error', 'The session class could not set a proper GMT timestamp so the local time() value was used.');
        }
    }
    else
    {
        $now = time();
    }
    $theExpireTime = $config['sess_expiration'] + $now;
    
    $theCookiePath = $config['cookie_path'];
    $theDomain = $config['cookie_domain'];
    
    setcookie(
        $theCookieName,
        $theSessionID,
        $theExpireTime,
        $theCookiePath,
        $theDomain,
        0
        );    
            
    //INSERT THE MATCHING ENTRY INTO THE CI_SESSIONS DATABASE    
    $hostname = $db['default']['hostname'];
    $user = $db['default']['username'];
    $password = $db['default']['password'];
    $thedatabase = $db['default']['database'];
    
    $db_link = mysql_connect($hostname, $user, $password);
    mysql_select_db($thedatabase);
    
    //Get the info that needs to go into the ci_sessions database.
    //Some of it we can get from the browser.
    $theIPAddress = get_ip_address();
    $theUserAgent = ( ! isset($_SERVER['HTTP_USER_AGENT']) ? FALSE : $_SERVER['HTTP_USER_AGENT']);
    $theUserAgent = substr($theUserAgent, 0, 50);
                        
    //Some of it we get from the fa_user database
    $email = $user_settings['emailAddress'];
    $theQuery = "SELECT id, user_name, country_id, email, role, last_visit, created, modified FROM fa_user WHERE email = '$email'";
    $result = mysql_query($theQuery);
    $theUserData = mysql_fetch_array($result, MYSQL_ASSOC);
    $theSerializedUserData = serialize($theUserData);
      
    //Insert the data into the ci_sessions table
    $theQuery = "INSERT INTO ci_sessions (session_id, ip_address, user_agent, last_activity, session_data) VALUES ('$theSessionID', '$theIPAddress', '$theUserAgent', '$now', '$theSerializedUserData')";
    $result = mysql_query($theQuery);
    
    if (!$result)
        die("Couldn't connect to database.");
        
    //return database to SMF in a state it expects
    mysql_select_db($db_name);
        
//    mysql_close($db_link);        
}

    //This is the valid_ip function in system/libraries/input.php
    function valid_ip($ip)
    {
        $ip_segments = explode('.', $ip);
        
        // Always 4 segments needed
        if (count($ip_segments) != 4)
        {
            return FALSE;
        }
        // IP can not start with 0
        if (substr($ip_segments[0], 0, 1) == '0')
        {
            return FALSE;
        }
        // Check each segment
        foreach ($ip_segments as $segment)
        {
            // IP segments must be digits and can not be
            // longer than 3 digits or greater then 255
            if (preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
            {
                return FALSE;
            }
        }
        
        return TRUE;
    }

     //Adapted from the ip_address function in system/libraries/input.php
    function get_ip_address()
    {
        $ip_address = FALSE;
        
        if (isset($_SERVER['REMOTE_ADDR']) AND isset($_SERVER['HTTP_CLIENT_IP']))
        {
             $ip_address = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (isset($_SERVER['REMOTE_ADDR']))
        {
             $ip_address = $_SERVER['REMOTE_ADDR'];
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
        {
             $ip_address = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
             $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        
        if ($ip_address === FALSE)
        {
            $ip_address = '0.0.0.0';
            return $ip_address;
        }
        
        if (strstr($ip_address, ','))
        {
            $x = explode(',', $ip_address);
            $ip_address = end($x);
        }
        
        if ( !valid_ip($ip_address))
        {
            $ip_address = '0.0.0.0';
        }
                
        return $ip_address;
    }
#4

[eluser]Grahack[/eluser]
You said "It took all day"
Thanks taking time to do this and report here, but this is an overkill to me! Especially if you have to copy-paste or adapt some Ci code.
#5

[eluser]Vik[/eluser]
Smile You have a very good point! CI dip is cool, and I'm glad that it's available .

Thanks very much for FAL, which does a great job.




Theme © iAndrew 2016 - Forum software by © MyBB