Authorization on the site using phpBB / XenForo

    About a year ago, I needed to allow users registered on the forum (phpBB) to log in to the site (modX). At that time, the forum was already working and users were actively communicating. MODxBB solutions were not yet and had to be fantasized.

    The result is something like this
    config['base_path'] . 'forum/';
    define('IN_PHPBB', true);
    $phpEx = "php";
    include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('ucp');
    $login = array();
    if(isset($_POST['logoutForum']) && $user->data['user_id'] != ANONYMOUS) $user->session_kill(); //Покидаем кабинет
    if(isset($_POST['login']) && $user->data['user_id'] == ANONYMOUS){
    	$username = request_var('username', '', true);
    	$password = request_var('password', '', true);
    	$autologin	= (!empty($_POST['autologin'])) ? true : false;
    	$viewonline	= (!empty($_POST['viewonline'])) ? false : true;
    	$login = $auth->login($username, $password, $autologin, $viewonline);
    }
    header('Content-type: text/html; charset=UTF-8');
    header('Cache-Control: private, no-cache="set-cookie"');
    header('Expires: 0');
    header('Pragma: no-cache');
    if((!empty($login) && $login['status'] == LOGIN_SUCCESS) || $user->data['user_id'] != ANONYMOUS){
    	if(!empty($login)) $auth->acl($user->data);
    	$modx->setPlaceholder('UserName',get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']));
    	echo $modx->getChunk('ExitBlock');
    	/*
    		Hello [+UserName+] 
    */ } else{ if(isset($login['error_msg']) && $login['error_msg']){ //Обработка ошибок $err = $user->lang[$login['error_msg']]; if ($login['error_msg'] == 'LOGIN_ERROR_USERNAME' || $login['error_msg'] == 'LOGIN_ERROR_PASSWORD'){ $err = (!$config['board_contact']) ? sprintf($user->lang[$login['error_msg']], '', '') : sprintf($user->lang[$login['error_msg']], '', ''); } if($login['error_msg']=='LOGIN_ERROR_ATTEMPTS'){ //Брут? $captcha = & phpbb_captcha_factory::get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); $template->assign_vars(array('CAPTCHA_TEMPLATE' => $captcha->get_template())); $err = $user->lang[$login['error_msg']]; $err.='
    Код подтверждения
    Код с картинки:
    '; } $modx->setPlaceholder('ErrorMsg',$err); } echo $modx->getChunk('forumLoginForm'); /*
    [+ErrorMsg+]
    Логин:
    Пароль:
    */ } ?>


    The code is certainly not perfect, but it worked flawlessly.
    The topic would have ended on this, if one day the idea had not occurred to change the forum engine. After wandering around the Internet, it was decided to install XenForo. The determining factors were such items as:
    1. Social buns out of the box
    2. Ability to import users, topics and messages from the phpBB 3.0 forum
    3. Comfort Code
    4. Active community


    Download, Install, Import ... Moreover, the import was done with the preservation of ID-Schnick, so that it would be possible to redirect the user who visited the site using the link of the old forum to a new page without problems.
    Now it was necessary to restore the authorization form on the site itself. As it turned out, this is not so difficult, because the XenForo code is extremely clear.
    config['base_path'].'forum/';
    $startTime = microtime(true);
    require ($fileDir . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
    XenForo_Application::modxParserActive(); //Говорим XenForo, что у нас свой обработчик ошибок
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    XenForo_Application::set('page_start_time', $startTime);
    XenForo_Session::startPublicSession();
    $xfUser = XenForo_Visitor::getInstance();
    $data=array();
    if($xfUser->get('user_id')=='0'){
    	$data['back']=isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'/';
        $data['linkauth']='forum/login/login';
    	$data['noauth']=$modx->getChunk($noauth); //Вам нужно авторизоваться
    	echo $modx->parseChunk('forumLoginForm', $data,'[+','+]');
    /*[+noauth+]
    <р>Логин/e-mail:     Пароль:     */ }else{ $data['UserName']=$xfUser->get('username'); echo $modx->parseChunk('ExitBlock', $data,'[+','+]'); } ?>


    And now the most interesting part begins: I will explain what kind of mysterious function modxParserActive is.

    Open the Application.php file from the library / XenForo / folder and add the definition of a new variable to the XenForo_Application class
    protected static $_modxParser = false;

    Then we define new functions

    public static function modxParserActive()
    {
    	self::$_modxParser = true;
    }
    public static function GetModxParser()
    {
    	return self::$_modxParser;
    }

    And the final touch. In the beginApplication function, wrap the code
    @ini_set('output_buffering', false);
    // see http://bugs.php.net/bug.php?id=36514
    if (!@ini_get('output_handler')) while (@ob_end_clean());
    error_reporting(E_ALL | E_STRICT & ~8192);
    set_error_handler(array('XenForo_Application', 'handlePhpError'));
    set_exception_handler(array('XenForo_Application', 'handleException'));


    in the following condition

    if(!self::GetModxParser()){
    …...
    }


    Yes, I know that crawling the engine code is not the best idea, but this is exactly what all the salt was about. This post. So far, this is the only way I was able to get around the conflict of compatibility between modx and XenForo. The fact is that modx parses chunks, snippets using ob_end_clean exactly; And since this function was called earlier, modx receives an empty buffer with all the ensuing consequences.

    Also popular now: