⬆️ ⬇️

A bunch of MODX Revolution and phpBB forum

Updated November 6, 2012.

In the previous version, it was not done very well and povylazili stones from under the water.

Updated November 7, 2012.

Minor bug fixed

Updated November 13, 2012.

Only the /forum/includes/auth/auth_modx.php file has been fixed:



PhpBB version: 3.0.11

MODX Version: 2.2.5-pl



Site folder: /

Forum folder: / forum

')

I have different databases, but you can also use one, everything seems to be the way to go.



Authorization takes place almost entirely on the MODX side. The forum creates copies of MODX users, but their data is automatically updated from MODX.





PhpBB authorization plugin



For authorization on the forum, a system of "authorization plug-ins" is used, briefly about them.



To use the authorization plugin, you need:



1. Come up with the name of the authorization plugin, for example: modx

2. Create file /forum/includes/auth/auth_modx.php (file name suffix (modx) == plugin name)

3. In the file /forum/includes/auth/auth_modx.php, there must be at least one login_modx function (suffix == name of the plugin)

4. Disable registration on the forum: GENERAL -> Registration of users -> Disable registration

5. In the admin panel of the forum: GENERAL -> Authentication -> select Modx from the list



Code file /forum/includes/auth/auth_modx.php:
<?php # /forum/includes/auth/auth_modx.php if (!defined('IN_PHPBB')){ exit; } /** *      */ function get_user_data(){ //  define('MODX_API_MODE', true); require dirname(dirname(dirname(dirname(__FILE__)))) . '/index.php'; //      /forum // MODX     ,     , //        : set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); //   if($modx->user->get('id') != 0){ //   anonimous $result['username'] = $modx->user->get('username'); $profile = $modx->user->getOne('Profile'); $result['user_email'] = $profile->get('email'); $fields = $profile->get('extended'); $result['user_from'] = (string) $fields['region']; //   ,   NULL,        NULL //      , : // $result['_______users'] = ____modx; //     ,     MODX,    , // :     MODX extended  region,       user_from //     validate_session_modx () } return $result; } /** * ,   . */ function login_modx(){ $auth = get_user_data(); //    if (!is_array($auth) || empty($auth)) { return array( 'status' => LOGIN_ERROR_USERNAME, 'error_msg' => 'ACCESS_DIRECTLY_DENIDED', 'user_row' => array('user_id' => ANONYMOUS), ); } global $db; $sql = 'SELECT user_id, username, user_password, user_email, user_from, user_type FROM ' . USERS_TABLE . " WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($auth['username'])) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); if ($row){ foreach($auth as $auK => $auV){ if($row[$auK] != $auV){ //         modx ( ) //  UPDATE unset($auth['username']); $sql = "UPDATE " . USERS_TABLE . " SET " . $db->sql_build_array('UPDATE', $auth) . " WHERE user_id = '" . $db->sql_escape($row['user_id']) . "'"; $db->sql_query($sql); break; } } $res = array( 'status' => LOGIN_SUCCESS, 'error_msg' => false, 'user_row' => array( 'user_id' => $row['user_id'], 'username' => $row['username'], //    'user_email' => $row['user_email'], // E-mail ,   'user_from' => $row['user_from'], 'user_type' => 0, 'group_id' => 2 ) ); return $res; } // ,    . $res = array( 'status' => LOGIN_SUCCESS_CREATE_PROFILE, 'error_msg' => false, 'user_row' => array( "username" => $auth['username'], //    "user_email" => $auth['user_email'], // E-mail ,   "user_from" => $auth['user_from'], "user_type" => 0, "group_id" => 2 ), ); return $res; } /** * ,         . */ function autologin_modx(){ $user_row = login_modx(); //      if ($user_row['status'] == LOGIN_SUCCESS_CREATE_PROFILE) { global $phpbb_root_path, $phpEx; if (!function_exists('user_add')) { include($phpbb_root_path . 'includes/functions_user.' . $phpEx); } $user_row['user_row']['user_id'] = user_add($user_row['user_row']); } //    global $db; $sql = 'SELECT * FROM ' . USERS_TABLE . " WHERE user_id = '" . $db->sql_escape($user_row['user_row']['user_id']) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); return $row; } function logout_modx(){ //  modx API define('MODX_API_MODE', true); require dirname(dirname(dirname(dirname(__FILE__)))) . '/index.php'; $modx->getService('error','error.modError'); $modx->runProcessor('/security/logout'); //$modx->cacheManager->refresh(); } function validate_session_modx($locUser){ $auth = get_user_data(); if($locUser['username'] == 'Anonymous'){ if($auth){ return false; }else{ return true; } }else{ //   modx,    ,     if(!$auth){ //    global $user; $user->session_kill(); $user->session_begin(); }elseif(($auth['username'] != $locUser['username'])){ return false; } return true; } } ?> 




Admin forum



Since authorization is almost entirely on the MODX side, the forum admin (username, for example, admin) must be registered in MODX.



Now, let's say we have the admin user in MODX, and the forum has admin with the same admin name.

The problem is that the forum administrator must confirm his password, which cannot be verified, because we have authorization in MODX, and not through the forum database.



image



Therefore, disable additional verification password admin forum:

In the file: /forum/adm/index.php

 // Have they authenticated (again) as an admin for this session? if (!isset($user->data['session_admin']) || !$user->data['session_admin']) { login_box('', $user->lang['LOGIN_ADMIN_CONFIRM'], $user->lang['LOGIN_ADMIN_SUCCESS'], true, false); } 


Change to:

 // Have they authenticated (again) as an admin for this session? //if (!isset($user->data['session_admin']) || !$user->data['session_admin']) //{ // login_box('', $user->lang['LOGIN_ADMIN_CONFIRM'], $user->lang['LOGIN_ADMIN_SUCCESS'], true, false); //} 


After:

 if (!$auth->acl_get('a_')) { trigger_error('NO_ADMIN'); } 


Add line:

 $user->data['session_admin'] = 1; 


Login, password recovery



In the file /forum/ucp.php, log in simultaneously on the site and the forum, through the authorization form on the forum:

Between case 'login': and break; (inclusive):

  case 'login': define('MODX_API_MODE', true); require dirname(dirname(__FILE__)) . '/index.php'; $modx->getService('error','error.modError'); set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); if($_REQUEST["username"] and $_REQUEST["password"]){ $data = array( 'username' => $_REQUEST["username"], 'password' => $_REQUEST["password"], 'rememberme' => 1, 'login_context' => 'web', ); $response = $modx->runProcessor('/security/login', $data); if ($response->isError()) { trigger_error($response->getMessage()); } } if ($user->data['is_registered']) { redirect(append_sid("{$phpbb_root_path}index.$phpEx")); } login_box(request_var('redirect', "index.$phpEx")); break; 


Even if the user was not in the forum database, it is automatically created.



To link “Forgot your password?” On the forum, get on the MODX password recovery page (/forum/ucp.php):



  case 'sendpassword': define('MODX_API_MODE', true); require dirname(dirname(__FILE__)) . '/index.php'; $modx->getService('error','error.modError'); set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); $modx->sendRedirect($modx->makeUrl(865,'','','full')); //  865 -  ID  MODX     break; 


The article, which was taken as a basis:

Integration of site users and forum on phpbb 3

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



All Articles