You Are Here: Home » How-To » Programming

PHPFox & Question2Answer Q2A Single Sign-on – Compulsory Functions

By on February 12th, 2014 
Advertisement

We are in continuation of our article on PHPFox and Question2Answer Q2A single sign-on integration that will help any PHPFox user to easily use the Q2A platform without having to create an account or sign in to Question2Answer separately.

If you have landed on this article without having completed the first two steps then you must go through these first before continuing:

1. Download & Install Q2A with support for Single sign-on user database in Q2A - digitizor.com/2014/02/12/integrate-phpfox-question2answer-q2a-sso

2. Setup PHPFox Autoloading to expose its APIs and methods to Q2A - digitizor.com/2014/02/12/autoload-phpfox-access-from-external-php-script

The 5 compulsory functions that we need to implement in the file qa-external-users.php order to make this work are:

  • qa_get_login_links
  • qa_get_logged_in_user
  • qa_get_user_email
  • qa_get_userids_from_public
  • qa_get_public_from_userids

Before we go ahead and define these functions, we have to include the PHPFox autoload script in this file and start a new PHPFox session. This can be done by adding the following code snippet in the qa-external-users.php file from around line 40:

    @include("phpfox-autoload.php");

    /** Current USER ID */
    $phpfox_user = array();
    $phpfoxUID = Phpfox::getUserId();
    $phpfoxUSESS = Phpfox::getService('user.auth')->getUserSession();
    if ($phpfoxUID > 0) {
        $phpfox_user["uid"] = $phpfoxUID;
        //user_name, might want to use full_name but the obvious
        $phpfox_user["username"] = $phpfoxUSESS["user_name"];
        $phpfox_user["email"] = $phpfoxUSESS["email"];
        //user_group_id = 1 (admin) else (not admin)
        $phpfox_user["level"] = $phpfoxUSESS["user_group_id"];
    } else {
    }

The phpfox-autoload.php file was created in the 2nd part of this tutorial and must be placed inside the qa-external directory.

Now delete the entire function qa_get_login_links including all of its comments and replace it with the following code snippet:

function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
{
return array(
'login' => $relative_url_prefix.'../index.php',
'register' => $relative_url_prefix.'../index.php',
'logout' => $relative_url_prefix.'../index.php?do=/user/logout/'
);
}

This function basically changes Q2A login and register links to match those of PHPFox so that Users first log-in to PHPFox when they want to take part in the Q2A forums while being logged into PHPFox.

Next, delete the entire function qa_get_logged_in_user including all of its comments and replace it with the following code snippet:

function qa_get_logged_in_user()
{
global $phpfox_user;
if (!empty($phpfox_user)) {
return array(
'userid' => $phpfox_user["uid"],
'publicusername' => $phpfox_user["username"],
'email' => $phpfox_user['email'],
'level' => ($phpfox_user["uid"]===1 || $phpfox_user["level"]===1) ? QA_USER_LEVEL_ADMIN : QA_USER_LEVEL_BASIC
);
}
return null;
}

This function returns all details about the currently logged in PHPFox user so that it can be used by Q2A Question2Answer to generate details about the current logged in user in the Q2A platform.

Now delete the entire function qa_get_user_email including all of its comments and replace it with the following code snippet:

function qa_get_user_email($userid)
{
$userObject = Phpfox::getService('user')->getUser($userid);
if (!empty($userObject))
return $userObject["email"];
return null;
}

This function returns the email address corresponding to the user id passed in.

Next, delete the entire function qa_get_userids_from_public including all of its comments and replace it with the following code snippet:

function qa_get_userids_from_public($publicusernames)
{

$publictouserid=array();

if (count($publicusernames)) {
$qa_db_connection=qa_db_connection();

$escapedusernames=array();
foreach ($publicusernames as $publicusername)
$escapedusernames[]="'".mysql_real_escape_string($publicusername, $qa_db_connection)."'";

$aRows = Phpfox::getLib('database')->select('*')
->from('phpfox_user')
->where('user_name IN ('.implode(',', $escapedusernames).')')
->execute('getRows');

foreach ($aRows as $key=>$value) {
$publictouserid[$value['user_name']]=$value['user_id'];
}

}

return $publictouserid;
}

This function basically returns an array of user ids for the corresponding usernames passed in as an array. This is used by Q2A in the question list pages, answers pages and the users list pages, etc.

This is the last function among the list of the compulsory functions required to be changed. Delete the entire function qa_get_public_from_userids including all of its comments and replace it with the following code snippet:

function qa_get_public_from_userids($userids)
{

$useridtopublic=array();

if (count($userids)) {
$qa_db_connection=qa_db_connection();

$escapeduserids=array();
foreach ($userids as $userid)
$escapeduserids[]="'".mysql_real_escape_string($userid, $qa_db_connection)."'";

$aRows = Phpfox::getLib('database')->select('*')
->from('phpfox_user')
->where('user_id IN ('.implode(',', $escapeduserids).')')
->execute('getRows');

foreach ($aRows as $key=>$value) {
$useridtopublic[$value['user_id']]=$value['user_name'];
}
}

return $useridtopublic;

}

This function does exactly the opposite of what the last function does.

With all these changes we are pretty much all set with the single sign on to work. You should now be able to test your Question2Answer - PHPFox single sign-on integration and if you have done all the steps correctly then, there should not be any issues.

You can also download the txt version of the qa-external-users.php file from here. Next in line is the implementation of the 4 additional functions which, you can skip if you want as these are not the compulsory functions. In case you are interested then continue reading this article - digitizor.com/2014/02/12/phpfox-q2a-sso-user-functions

You can also hire me or one of our developers to take care of the Q2A and PHPFox integration for your websites, just drop a mail to debjitsaha87@gmail.com

Advertisement







PHPFox & Question2Answer Q2A Single Sign-on – Compulsory Functions was originally published on Digitizor.com on February 12, 2014 - 7:32 am (Indian Standard Time)