📜 ⬆️ ⬇️

Authorization on the site using phpBB / XenForo

About a year ago I needed to enable 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 there yet and had to to fantasize.

The result was something like this.
<?php global $modx, $phpbb_root_path, $phpEx, $user, $db, $config, $cache, $template, $auth; $phpbb_root_path = $modx->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+] <form method="post" action="/[~[*id*]~]"><input type="submit" name="logoutForum" value="" /></form> */ } 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']], '<a hrf="mailt:' . htmlspecialchars($config['board_contact']) . '">', '</a>'); } 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.='<br /><img sr="/forum/ucp.php?mode=confirm&confirm_id='.$template->_rootref['CONFIRM_IMAGE_LINK'].'" alt=" " /><br />   : <input type="text" name="confirm_code" id="confirm_code" size="8" maxlength="8" title=" " /><input type="hidden" name="confirm_id" id="confirm_id" value="'.$template->_rootref['CONFIRM_ID'].'" title=" " /><br />'; } $modx->setPlaceholder('ErrorMsg',$err); } echo $modx->getChunk('forumLoginForm'); /* <form method="post" action=""> [+ErrorMsg+]<BR> : <input type="text" name="username" id="username" size="10" title="login" /><br /> : <input type="password" name="password" id="password" size="10" title="password" /><br /> <input type="submit" name="login" value="" /> </form> */ } ?> 


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

')
Download, Install, Import ... Moreover, the import was done with the preservation of ID-Schnik, so that you can easily redirect the user who has come to the site by the link of the old forum to a new page.
Now it was necessary to restore the functionality of the authorization form on the site itself. As it turned out, this is not so difficult, because the XenForo code is very clear.
 <?php $noauth=isset($noauth)? $noauth : ''; //    ? $fileDir=$modx->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+]<BR> <form action="/[+linkauth+]" mthod="post" lass="xenForm " id="login" style="display: block"> <>/e-mail: <inut type="text" name="login" id="LoginControl" class="textCtrl" tabindex="101">&nbsp;&nbsp;&nbsp;&nbsp;:<inut type="password" name="password" class="textCtrl" id="ctrl_password" tabindex="102">&nbsp;&nbsp;&nbsp;&nbsp;<inut type="submit" class="button primary" value="" tabindex="104"></> <inut type="hidden" name="remember" value="1" id="ctrl_remember"> <inut type="hidden" name="cookie_check" value="1"> <inut type="hidden" name="redirect" value="[+back+]"> <inut type="hidden" name="_xfToken" value=""> </frm>*/ }else{ $data['UserName']=$xfUser->get('username'); echo $modx->parseChunk('ExitBlock', $data,'[+','+]'); } ?> 


And now the most interesting thing begins: I will explain what the 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 features.

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

And the final touch. In the beginApplication function we 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 next condition

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


Yes, I know that it’s not the best idea to climb the engine code, but that’s what it was all about. This post. So far this is the only way I have been able to circumvent the compatibility conflict modx and XenForo. The fact is that modx parses chunks, snippets using exactly ob_end_clean; And since this function was called earlier, modx gets an empty buffer with all the ensuing consequences.

Source: https://habr.com/ru/post/136833/


All Articles