Integration of SMF 2.0 forum and CMS Livestreet 0.4.2
The article considers the integration option of the SMF 2.0 forum and CMS Livestreet 0.4.2, in which user authorization / authentication is carried out through the Livestreet user database.
The background of the task is as follows. There was a site on the Livestreet engine, everything is good in it, but once users wanted a forum. It’s just necessary and that’s it. Just installing and configuring a new engine is not an option, since each already registered user will have to register in a new way on the forum, and each new user will have to register in both CMS. Registration / entry through social. there was no network for historical reasons. And therefore the task confronted me:
The SMF engine implements a hook mechanism with which you can change the behavior of the system. Based on the above tasks, we need to redefine the behavior of the system using the following hooks:
In order for SMF to know that we want to override its behavior, you need to create an installer script in the root folder of the forum engine, let's call it add_livestreetAuth_hook.php , with the following contents:
$ sourcedir is a global variable containing the path to the Sources directory of the SMF engine
add_integration_function - a function implemented in the engine that binds your function (2nd parameter) to the hook (1st parameter). The third parameter of the TRUE or FALSE function determines whether to save the binding in the database and cache or not.
The first call to the add_integration_function function ('integrate_pre_include', "$ sourcedir / Subs-LivestreetAuth.php") tells the engine the path to the file in which the functions described below are implemented.
Next, in the Sources directory of the engine, create the Subs-LivestreetAuth.php file, in which we implement all the functions that are associated with hooks.
After that, run the script from the browserwww.mysite.com/forum/add_livestreetAuth_hook.php . If you saw a white screen without any errors, then the binding was successful. Moreover, to calm the nerves, you can check the contents of the smf_settings database table. Near the end of the table there should be entries with your hooks and functions that are attached to them.
A couple of points when implementing functions in the Sources / Subs-LivestreetAuth.php file.
The main integration work lies in the function livestreet_verify_user_add_hook () which we linked to the integrate_verify_user hook and implemented it in the Sources / Subs-LivestreetAuth.php file.
I would also like to mention the livestreet_integrate_menu_buttons_hook hook . With it, you can override the main menu items of the engine. For example, in my case it was necessary:
Actually, the implementation of the function:
A multidimensional array containing all the main menu items is passed to the function by reference.
Removing hooks.
In order to remove the binding of functions to hooks, you need to create a file in the root directory of the forum engine (for example, remove_livestreet_auth_hook.php ), in which the remove_integration_function function is called one by one . Example file contents:
Further, as in the case of the installation, you must run the script from the browser to execute.
The background of the task is as follows. There was a site on the Livestreet engine, everything is good in it, but once users wanted a forum. It’s just necessary and that’s it. Just installing and configuring a new engine is not an option, since each already registered user will have to register in a new way on the forum, and each new user will have to register in both CMS. Registration / entry through social. there was no network for historical reasons. And therefore the task confronted me:
- Expand SMF (Simple Machines Forum) 2.0 forum so that it is available at www.mysite.com/forum
- When a user logs on to the forum, check whether he is authorized or not to check through the Livestreet database.
- Users are registered only in CMS Livestreet
- Login and password verification only through CMS Livestreet
The SMF engine implements a hook mechanism with which you can change the behavior of the system. Based on the above tasks, we need to redefine the behavior of the system using the following hooks:
- integrate_verify_user
- integrate_verify_password
- integrate_validate_login
- integrate_login
- integrate_logout
- integrate_reset_pass
- integrate_register
- integrate_change_member_data
In order for SMF to know that we want to override its behavior, you need to create an installer script in the root folder of the forum engine, let's call it add_livestreetAuth_hook.php , with the following contents:
Error: Cannot install - please verify you put this in the same place as SMF\'s index.php.');
add_integration_function('integrate_pre_include', "$sourcedir/Subs-LivestreetAuth.php");
add_integration_function('integrate_verify_user', 'livestreet_verify_user_add_hook',TRUE);
add_integration_function('integrate_validate_login ', 'livestreet_validate_login_add_hook',TRUE);
add_integration_function('integrate_login', 'livestreet_integrate_login_add_hook',TRUE);
add_integration_function('integrate_logout', 'livestreet_integrate_logout_add_hook',TRUE);
add_integration_function('integrate_reset_pass', 'livestreet_integrate_reset_pass_add_hook',TRUE);
add_integration_function('integrate_register', 'livestreet_integrate_register_add_hook',TRUE);
add_integration_function('integrate_change_member_data', 'livestreet_integrate_change_member_data_add_hook',TRUE);
add_integration_function('integrate_menu_buttons', 'livestreet_integrate_menu_buttons_hook',TRUE);
?>
$ sourcedir is a global variable containing the path to the Sources directory of the SMF engine
add_integration_function - a function implemented in the engine that binds your function (2nd parameter) to the hook (1st parameter). The third parameter of the TRUE or FALSE function determines whether to save the binding in the database and cache or not.
The first call to the add_integration_function function ('integrate_pre_include', "$ sourcedir / Subs-LivestreetAuth.php") tells the engine the path to the file in which the functions described below are implemented.
Next, in the Sources directory of the engine, create the Subs-LivestreetAuth.php file, in which we implement all the functions that are associated with hooks.
After that, run the script from the browserwww.mysite.com/forum/add_livestreetAuth_hook.php . If you saw a white screen without any errors, then the binding was successful. Moreover, to calm the nerves, you can check the contents of the smf_settings database table. Near the end of the table there should be entries with your hooks and functions that are attached to them.
A couple of points when implementing functions in the Sources / Subs-LivestreetAuth.php file.
- The first line of the file should contain the following code to prevent direct access to the file from the browser:
if (! Defined ('SMF')) die ('Hacking attempt ...'); - To work with the database inside each function, declare a global variable: global $ smcFunc;
This variable contains an array of functions for working with the database. I will give a small example of how this layer works:
$res=$smcFunc['db_query']('',"SELECT id_member FROM {db_prefix}members WHERE member_name=\"user1\"",array()); if($smcFunc['db_num_rows']($res)>0) { $smf_user_row=$smcFunc['db_fetch_assoc']($res); return $smf_user_row['id_member']; }
{db_prefix} - Database table prefix specified when installing the engine.
More details in the documentation . - It is necessary to pay attention that parameters are transferred to functions by reference. For instance:
function livestreet_validate_login_add_hook(&$username,&$hashedPass=NULL,&$cookieLifetime) { return 'ok';// retry|ok }
Carefully read in the manual what the function bound to this or that hook should return.
The main integration work lies in the function livestreet_verify_user_add_hook () which we linked to the integrate_verify_user hook and implemented it in the Sources / Subs-LivestreetAuth.php file.
- The first thing she should do is check the $ _COOKIE ['key'], which installs CMS Livestreet. If it is not there, or it is incorrect, then return 0 (zero), thereby indicating that the logged-in user is a Guest.
- Next, the function on $ _COOKIE ['key'] selects user_id from the table {$ prefix} session of the Livestreet database, after which user_id from the table {$ prefix} user selects all the information about the user.
- Checks in the table {db_prefix} members of the SMF database whether there is a user with this login, if not, then he logs
- Returns id_member of table {db_prefix} members of SMF database
I would also like to mention the livestreet_integrate_menu_buttons_hook hook . With it, you can override the main menu items of the engine. For example, in my case it was necessary:
- Add a link to the main page of the site
- so that by clicking on the “Login” link the user is sent to the login page for the CMS Livestreet login and password
- By clicking on the “Registration” link, the user is sent to the CMS Livestreet registration page.
Actually, the implementation of the function:
function livestreet_integrate_menu_buttons_hook(&$buttons)
{
//print_r($buttons);exit;//Чтобы глянуть что внутри
if($config=getLivestreetConfig())
{
$new_buttons=array(
'login'=>array(
'href'=>'http://mysite.com/login/',
),
'register'=>array(
'href'=>'http://mysite.com/registration/',
),
);
foreach($new_buttons as $key=>$val)
{
if(isset($buttons[$key]))
{
//unset($buttons[$val]);
$buttons[$key]['href']=$val['href'];
}
}
//Добавление ссылки на главную всего сайта
$buttons=array(
'mainpage'=>array(
'title'=>'На главную',
'href'=>'http://mysite.com/',
'show'=>1,
'sub_buttons'=>array()
)
)+$buttons;
}
}
A multidimensional array containing all the main menu items is passed to the function by reference.
Removing hooks.
In order to remove the binding of functions to hooks, you need to create a file in the root directory of the forum engine (for example, remove_livestreet_auth_hook.php ), in which the remove_integration_function function is called one by one . Example file contents:
Error: Cannot install - please verify you put this in the same place as SMF\'s index.php.');
remove_integration_function('integrate_pre_include', "$sourcedir/Subs-YourAction.php");
remove_integration_function('integrate_verify_user ', 'livestreet_verify_user_add_hook');
remove_integration_function('integrate_validate_login ', 'livestreet_validate_login_add_hook');
remove_integration_function('integrate_login', 'livestreet_integrate_login_add_hook');
remove_integration_function('integrate_logout', 'livestreet_integrate_logout_add_hook');
remove_integration_function('integrate_reset_pass', 'livestreet_integrate_reset_pass_add_hook');
remove_integration_function('integrate_register', 'livestreet_integrate_register_add_hook');
remove_integration_function('integrate_change_member_data', 'livestreet_integrate_change_member_data_add_hook');
Further, as in the case of the installation, you must run the script from the browser to execute.