📜 ⬆️ ⬇️

Integration of site users and forum on phpbb 3

More recently, there was a need for rapid integration of the site and the forum phpbb3. Googling, I did not find a normal example. I had to write myself. Perhaps someone will come in handy.

So, let there be a website with its own authorization system. You need to add the phpbb forum with a quick (and perhaps even correct) solution of problems with the integration of the existing user base. Authorization and registration of new users will continue to occur on the site, already registered users will get into the forum. Registration and authorization on the forum will be closed.

To begin, disable the ability to register new users:
To do this, go to the “General” tab in the administrator account, and in the left menu select “User Registration”. Next, disable registration:

image
')
So, let's start writing our plugin:

All authorization plugins are stored in the forum subdirectory includes / auth /. There are already several plugin files in addition to the standard auth_db. Add your plugin: let it be called auth_simple. It is important that the name of the file will determine the names of the functions contained. Create the file includes / auth / auth_simple.php.

At a minimum, our file must have a function.

login_simple (& $ username, & $ password)

The function must return an array of this content:

array( 'status' => LOGIN_SUCCESS, //   'error_msg' => false, //   'user_row' => array( 'user_id' => $row['user_id'], //   phpbb 'username' => $row['username'], //    'user_password' => $row['user_password'], // phpbb-  'user_email' => $row['user_email'], // E-mail ,   'user_type' => 0, //  (\   ..) 'group_id' => 2 //    ) ); 


Everything seems to be understandable, but a reasonable question may arise - where can I get user_id if the user is not yet registered on the forum? The answer is also simple: If the user is not registered yet, the value of the 'status' key should be changed to LOGIN_SUCCESS_CREATE_PROFILE. In this case, the user will be created automatically.

Secondly, our authorization plugin must contain a function that will automatically register and authorize a user on the forum when he first visits the forum:

autologin_simple ()

The function should return an associative array with all the information about the current user.

I will say right away that our example will register such a user here:

 function get_user_data() { $result['user_email'] = “user@example.com”; $result['username'] = “username”; $result['user_password'] = “user_password”; return $result; } 


In the real case, this function will transfer data from the session of the current user of the real site.
So, the contents of our plugin file:

 <?php /** * *     phpBB3 * */ if (!defined('IN_PHPBB')) { exit; } /** *      */ function get_user_data() { $result['user_email'] = “user@example.com”; $result['username'] = “username”; $result['user_password'] = “user_password”; return $result; } /** * ,   . */ function login_simple(&$username, &$password) { $auth = get_user_data(); //    if (!is_array($auth) || empty($auth)) { return array( 'status' => LOGIN_ERROR_USERNAME, //    ACCESS_DIRECTLY_DENIDED     language/<language>/common.php 'error_msg' => 'ACCESS_DIRECTLY_DENIDED', 'user_row' => array('user_id' => ANONYMOUS), ); } global $db; $sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type FROM ' . USERS_TABLE . " WHERE username_clean = '" . $db->sql_escape($auth['username']) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); if ($row){ $res = array( 'status' => LOGIN_SUCCESS, 'error_msg' => false, 'user_row' => array( 'user_id' => $row['user_id'], 'username' => $row['username'], //    'user_password' => $row['user_password'], // phpbb-  'user_email' => $row['user_email'], // E-mail ,   '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_password" => phpbb_hash($auth['user_password']), // phpbb-  "user_email" => $auth['user_email'], // E-mail ,   "user_type" => 0, "group_id" => 2 ), ); return $res; } /** * ,         . */ function autologin_simple() { //        ,   $u = ""; $user_row = login_simple($u, $u); //      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; } ?> 

Links

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


All Articles