function __construct()
 {
     $params = $this->getParams();
     // bridge/phpbb path can be not set or doesn't exist, this would cause errors including configuration files
     if (!$params->get('bridge_path') || !JFolder::exists(JPATH_SITE . DS . $params->get('bridge_path')) || !$params->get('phpbb3_path') || !JFolder::exists(JPATH_SITE . DS . $params->get('phpbb3_path'))) {
         return;
     }
     $this->phpbb_path = $params->get('phpbb3_path');
     $this->bridge_path = $params->get('bridge_path');
     $this->bridge_params = $params;
     $this->link_format = $params->get('link_format', 'bridged');
     if (!JFile::exists(JPATH_ROOT . DS . $this->phpbb_path . DS . 'config.php')) {
         return;
     }
     //Include the phpBB3 configuration
     require JPATH_ROOT . DS . $this->phpbb_path . DS . 'config.php';
     // Config is incomplete
     if (!isset($dbms, $dbhost, $dbuser, $dbpasswd, $dbname, $table_prefix)) {
         return;
     }
     $options = array('driver' => $dbms, 'host' => $dbhost, 'user' => $dbuser, 'password' => $dbpasswd, 'database' => $dbname, 'prefix' => $table_prefix);
     $this->phpbb_db =& JDatabase::getInstance($options);
     if (JFile::exists(JPATH_ROOT . DS . $this->bridge_path . DS . 'configuration.php')) {
         //Include the bridge configuration
         require_once JPATH_ROOT . DS . $this->bridge_path . DS . 'includes' . DS . 'helper.php';
         //load phpBB3 elements
         JForumHelper::loadPHPBB3(JPATH_ROOT . DS . $this->bridge_path);
     }
 }
 /**
  * This method should handle any authentication and report back to the subject
  *
  * @access	public
  * @param   array 	$credentials Array holding the user credentials
  * @param 	array   $options     Array of extra options
  * @param	object	$response	 Authentication response object
  * @return	boolean
  * @since 1.5
  */
 function onAuthenticate($credentials, $options, &$response)
 {
     global $dbhost, $dbname, $dbuser, $dbpasswd, $table_prefix;
     // Joomla does not like blank passwords
     if (empty($credentials['password'])) {
         $response->status = JAUTHENTICATE_STATUS_FAILURE;
         $response->error_message = 'Empty password not allowed';
         return false;
     }
     if (!defined('IN_PHPBB')) {
         define('IN_PHPBB', true);
     }
     $table =& JTable::getInstance('component');
     $table->loadByOption('com_rokbridge');
     $params = new JParameter($table->params, JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_rokbridge' . DS . 'config.xml');
     if (!file_exists(JPATH_ROOT . DS . $params->get('bridge_path') . DS . 'configuration.php')) {
         return;
     }
     //Include the bridge configuration
     require_once JPATH_ROOT . DS . $params->get('bridge_path') . DS . 'configuration.php';
     if (!class_exists('JConfigForum')) {
         return;
     }
     //Create a bridge configration object
     $config = new JConfigForum();
     if (!file_exists(JPATH_ROOT . DS . $config->phpbb_path . DS . 'config.php')) {
         return;
     }
     //Include the PHPBB3 configuration
     require JPATH_ROOT . DS . $config->phpbb_path . DS . 'config.php';
     // Config is incomplete
     if (!isset($dbhost, $dbuser, $dbpasswd, $dbname, $table_prefix)) {
         return;
     }
     //Include the PHPBB3 helper functions
     if (!defined('LOGIN_PHPBB')) {
         //Include the bridge configuration
         $path = JPATH_ROOT . DS . $params->get('bridge_path');
         require_once $path . DS . 'includes' . DS . 'helper.php';
         JForumHelper::loadPHPBB3($path);
     }
     // Get a database object
     $options = array('driver' => $dbms, 'host' => $dbhost, 'user' => $dbuser, 'password' => $dbpasswd, 'database' => $dbname, 'prefix' => $table_prefix);
     $db =& JDatabase::getInstance($options);
     /*
      * Check if the login_name field exists if so use it to get the user data
      * Note : this fields is getting added by the SMF to phpBB3 convertor.
      */
     $fields = $db->getTableFields('#__users');
     if (isset($fields['#__users']['login_name'])) {
         $query = "SELECT user_id, username, user_email, user_type, user_password, login_name, user_type" . "\n FROM #__users" . "\n WHERE login_name = " . $db->Quote(utf8_clean_string($credentials['username']));
     } else {
         $query = "SELECT user_id, username, user_email, user_type, user_password, user_type" . "\n FROM #__users" . "\n WHERE username_clean = " . $db->Quote(utf8_clean_string($credentials['username']));
     }
     $db->setQuery($query);
     $result = $db->loadObject();
     if ($result && phpbb_check_hash($credentials['password'], $result->user_password)) {
         $response->status = JAUTHENTICATE_STATUS_SUCCESS;
         $response->error_message = '';
         $response->email = $result->user_email;
         $response->fullname = $result->username;
     } else {
         $response->status = JAUTHENTICATE_STATUS_FAILURE;
         $response->error_message = 'Invalid response from database';
     }
 }
    /**
     * This method should handle any logout logic and report back to the subject
     *
     * @access public
     * @param array holds the user data
     * @return boolean True on success
     * @since 1.5
     */
    function onLogoutUser($user_data, $options = array())
    {
        global $phpbb_root_path, $phpEx;
        global $auth, $user, $template, $cache, $db, $config, $mainframe;
        // don't perform phpbb3 login for joomla admin logins
        $me =& JFactory::getUser();
        // don't log yourself out when you logout of the joomla admin
        if ($mainframe->isAdmin() && !$me->username) {
            return true;
        }
        if (defined('LOGOUT_PHPBB')) {
            return true;
        }
        $table =& JTable::getInstance('component');
        $table->loadByOption('com_rokbridge');
        $params = new JParameter($table->params, JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_rokbridge' . DS . 'config.xml');
        $path = JPATH_ROOT . DS . $params->get('bridge_path');
        if (!file_exists($path . DS . 'includes' . DS . 'helper.php')) {
            return;
        }
        //Include the bridge configuration
        require_once $path . DS . 'includes' . DS . 'helper.php';
        JForumHelper::loadPHPBB3($path);
        require_once $phpbb_root_path . DS . 'includes/functions_user.php';
        $username = $user_data['username'];
        $fullname = $this->_fullNameSupport();
        $userid = $this->_getUserId($username, $fullname);
        //clear remember me cookie if set
        setcookie(JUtility::getHash('JLOGIN_REMEMBER'), '', time() - 86400, '/');
        // Don't try to logout a user which doesn't exist yet in phpBB
        if (empty($userid)) {
            return true;
        }
        // Hit the user last visit field
        $sql = 'UPDATE ' . USERS_TABLE . '
				SET user_lastvisit = ' . (int) time() . '
				WHERE user_id = ' . (int) $userid['user_id'];
        $db->sql_query($sql);
        //Remove the session from the database
        $sql = 'DELETE FROM ' . SESSIONS_TABLE . "\r\n\t\t\tWHERE session_user_id = " . (int) $userid['user_id'];
        $db->sql_query($sql);
        //Remove the session keys from the database
        $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . "\r\n\t\t\tWHERE user_id = " . (int) $userid['user_id'];
        $db->sql_query($sql);
        // Start session management
        $user->session_begin();
        $auth->acl($user->data);
        if ($user->data['user_id'] == $userid['user_id']) {
            // Destroy the php session for this user
            $user->session_kill();
            $user->session_begin();
            return true;
        }
        return false;
    }