📜 ⬆️ ⬇️

PhpBB 3 and Kohana 3 integration

image
A brief manual on how to combine these two tools. In fact, everything is not so difficult.

The essence of the problem


phpBB has its own API, the question is how to use it. In general, it is enough to connect the common.php file, but it just doesn't work that way. First, the session classes overlap. And secondly and thirdly - read inside.

Let's start


Let index.php Kohans lie in / web, and forum in / web / forum

Inclodes

In order not to spoil the forum code, first you need to create copies of the necessary files. You can of course edit live, but then the forum will stop working (thanks to Cap), but integration will work. Therefore, without hesitation, rename:
')
forum/common.php => forum/common_kohana.php
forum/includes/session.php => forum/includes/session_kohana.php

Later we will connect forum / common_kohana.php

Rule session_kohana.php

There are two classes in this file: Session and User. Session conflicts with Kokhanovsky, I renamed User too. So, we rule:

 class session // => class session_kohana 

 class user extends session // => class user_kohana extends session_kohana 


Rule common_kohana.php

Here we are connecting the session file:
 require($phpbb_root_path . 'includes/session.' . $phpEx); 

This should be urgently corrected:
 require($phpbb_root_path . 'includes/session_kohana.' . $phpEx); 


We fall a little lower. Here is the installation of the error handler:
 // Set PHP error handler to ours set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); 


Kohana already has a handler, not very bad (with blackjack and treys), so we kill:
 // Set PHP error handler to ours //set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); 


The main objects are created below, including the User instance instance:
 // Instantiate some basic classes $user = new user(); 


This class is stored in the file forum / includes / session_kohana.php . I renamed it too.
 $user = new user_kohana(); 


Now here's the deal. Kohana by default kills nafig global variables, so they need to be manually added there. Add literally below:
 $GLOBALS['user'] = $user; $GLOBALS['auth'] = $auth; $GLOBALS['template'] = $template; $GLOBALS['cache'] = $cache; $GLOBALS['db'] = $db; 


Further the config is connected:
 // Grab global variables, re-cache if necessary $config = $cache->obtain_config(); 


Instance is also overbearing. Add below
 $GLOBALS['config'] = $config; 


At the very end of the file is the installation of hooks. I did not fully understand how this affects the work, but it seems that they are not needed, because do not make sense outside the context of the user's work with the forum. I commented out the following code:
 // Add own hook handler require($phpbb_root_path . 'includes/hooks/index.' . $phpEx); $phpbb_hook = new phpbb_hook(array('exit_handler', 'phpbb_user_session_handler', 'append_sid', array('template', 'display'))); foreach ($cache->obtain_hooks() as $hook) { @include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx); } 


With common.php sorted out

Connection


I wrote a small class, which is not a class in the full sense, but a set of static functions. This function connects any forum; the actual code is taken from the index.php file:
 public static function include_libs() { //      if (self::$libs_included) return TRUE; define('IN_PHPBB', true); define('PHPBB_DB_NEW_LINK', 1); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './forum/'; $phpEx = substr(strrchr(__FILE__, '.'), 1); $GLOBALS['phpbb_root_path'] = $phpbb_root_path; $GLOBALS['phpEx'] = $phpEx; require_once($phpbb_root_path . 'common_kohana.' . $phpEx); require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx); require_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx); // Start session management $user->session_begin(); self::$libs_included = TRUE; return TRUE; } 


Shut up was this:
 define('PHPBB_DB_NEW_LINK', 1); 

When phpbb connects to the database, it does this:
 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined('PHPBB_DB_NEW_LINK') ? PHPBB_DB_NEW_LINK : false); 


image If you do not define a PHPBB_DB_NEW_LINK constant to 1, the following will occur. Your site obviously uses a database. So here.

If your forum is hanging on a different database and your databases are all on the same server, one user (and the same password, I suppose), then they, or rather pointers to them (variables like Resource ) will not work as expected. The function mysql_connect , which is called from phpBB, will try to use your current Kohanovsky mysql_connection_id if its calling parameters match those for which it was created in Kokhan. Kokhanovskie ways to control the uniqueness of the database through the hashes do not work here because they lie on a higher level, abstracting classes.
This is what is said in the documentation:

If the second function call occurred with the same arguments mysql_connect () , the new connection will not be established. Instead, the function will return a link to an already established connection. The new_link parameter can cause the mysql_connect () function to open another connection, even if the connection with the same parameters is already open.


Here you go. Now you can work. User registration is as follows:
 public static function register_user($username, $password, $email) { global $user; self::include_libs(); $user_row = array( 'username' => $username, 'user_password' => phpbb_hash($password), 'user_email' => $email, 'group_id' => 2, 'user_timezone' => 10.00, 'user_dst' => 1, 'user_lang' => 'ru', 'user_type' => 0, 'user_actkey' => '', 'user_ip' => Request::$client_ip, 'user_regdate' => time(), 'user_inactive_reason' => 0, 'user_inactive_time' => 0, ); try { $user_id = user_add($user_row, FALSE); return $user_id ? $user_id : FALSE; } catch (Exception $e) { return FALSE; } } 


And so you can log in and log in the specified user:
 public static function login($user_id, $persist_login = FALSE) { global $user; self::include_libs(); $user->session_create($user_id, false, $persist_login, true); } public static function logout() { global $user; self::include_libs(); $user->session_kill(FALSE); return TRUE; } 


Whether the user is logged in:
 public function logged_in() { global $user; $this->include_libs(); return ($user->data['user_id'] == ANONYMOUS) ? FALSE : TRUE; } 


In general, many of the forum API functions can be used, such as assigning roles. This is how access to a private forum is opened for the user:
 function forum_open($forum_id, $user_id) { $this->include_libs(); $role_id = 14; // Full access $auth_admin = new auth_admin(); $q = DB::query(Database::SELECT, "SELECT o.auth_option, r.auth_setting FROM `bazar_acl_roles_data` AS r, `bazar_acl_options` AS o WHERE o.auth_option_id = r.auth_option_id AND r.role_id = :role_id") ->param(':role_id', $role_id) ->as_object() ->execute('forum'); $auth_settings = array(); foreach ($q as $row) { if ($row->auth_option != 'f_') { $auth_settings[$row->auth_option] = $row->auth_setting; } } $auth_admin->acl_set('user', $forum_id, $user_id, $auth_settings, $role_id, true); } 


And in the end I will tell


This is a bad approach, because violates the elementary principles of the PLO. Global variables, global functions, constants ... In general, this is not necessary. And as it should, I would like to hear in the comments. When will the normal integration mechanism appear? Thank.

The service works here . If you register on the site and the forum, then when logging in to the site will go and login to the forum.
Original article

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


All Articles