This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
/
fluxbb_login.module
executable file
·335 lines (294 loc) · 11.1 KB
/
fluxbb_login.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
*---------------------------------------------------------------------------
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*---------------------------------------------------------------------------
*/
/**
* @file
* Authenticate users using fluxbb system and database.
*/
require_once('fluxbb_integration_api.php');
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the fluxbb path as returned from arg() function
*/
function fluxbb_login_help($path, $arg) {
switch ($path) {
case "admin/help#fluxbb_login":
return '<p>'. t("Displays links to nodes on fluxbb login module") .'</p>';
break;
}
}
/**
* Implements hook_block_info().
*/
function fluxbb_login_block_info() {
$blocks['fluxbb_login'] = array(
'info' => t('Fluxbb login'), //The name that will appear in the block list.
'cache' => DRUPAL_CACHE_PER_ROLE, //Default
);
return $blocks;
}
/**
* Implements hook_menu().
*/
function fluxbb_login_menu() {
$items = array();
$items['admin/config/people/fluxbb_login'] = array(
'title' => 'Configure Fluxbb login',
'description' => 'Configuration for Fluxbb Login module',
'page callback' => 'drupal_get_form',
'page arguments' => array('fluxbb_login_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Form function, called by drupal_get_form()
* in fluxbb_login_menu().
*/
function fluxbb_login_form($form, &$form_state) {
$form['fluxbb_integration_root_path'] = array(
'#type' => 'textfield',
'#title' => t('Server path of your fluxbb forum code'),
'#default_value' => variable_get('fluxbb_integration_root_path', 'forum/'),
'#description' => t('Set this to allow module Fluxbb_login and Fluxbb_posts to interact with fluxbb files'),
'#required' => TRUE,
);
$form['fluxbb_integration_url_path'] = array(
'#type' => 'textfield',
'#title' => t('URL Path to your fluxbb forum site'),
'#default_value' => variable_get('fluxbb_integration_url_path', 'http://www.example.com/forum'),
'#description' => t('Set this to create correct link to your forum'),
'#required' => TRUE,
);
$form['fluxbb_user_remember'] = array(
'#type' => 'checkbox',
'#title' => t('View or not fluxbb remember checkbox.'),
'#default_value' => variable_get('fluxbb_user_remember', true),
'#description' => t('Set this to display checkbox if need to remember login.'),
);
$form['fluxbb_user_register'] = array(
'#type' => 'checkbox',
'#title' => t('View or not fluxbb register link.'),
'#default_value' => variable_get('fluxbb_user_register', true),
'#description' => t('Set this to display link to fluxbb register page.'),
);
$form['fluxbb_user_lost_password'] = array(
'#type' => 'checkbox',
'#title' => t('View or not fluxbb lost password link.'),
'#default_value' => variable_get('fluxbb_user_lost_password', true),
'#description' => t('Set this to display link to fluxbb request new password page.'),
);
return system_settings_form($form);
}
/**
* Implements validation from the Form API.
*
* @param $form
* A structured array containing the elements and properties of the form.
* @param $form_state
* An array that stores information about the form's fluxbb state
* during processing.
*/
function fluxbb_login_form_validate($form, &$form_state){
$form_state['values']['fluxbb_integration_root_path'] = trim($form_state['values']['fluxbb_integration_root_path']);
if(substr($form_state['values']['fluxbb_integration_root_path'],-1) != '/')
$form_state['values']['fluxbb_integration_root_path'] = $form_state['values']['fluxbb_integration_root_path'] . '/';
if (!is_dir($form_state['values']['fluxbb_integration_root_path'])){
form_set_error('fluxbb_integration_root_path', t('The Path is not correct.'));
}
if (valid_url(trim($form_state['values']['fluxbb_integration_url_path']), TRUE) == FALSE) {
form_set_error('fluxbb_integration_url_path', t('The URL Path is not correct.'));
}
}
/**
* Implement hook_form_alter() to change the behaviour of the login form.
*
* Login validators are set in the user_login_default_validators() function in user.module.
* They are normally set to array('user_login_name_validate',
* 'user_login_authenticate_validate', 'user_login_final_validate').
* We simply replace 'user_login_authenticate_validate' with 'fluxbb_login_login_validate'.
*/
function fluxbb_login_form_user_login_alter( &$form, $form_state )
{
unset($form['links']);
$items = array();
if (variable_get('fluxbb_user_register', TRUE)) {
$items[] = l(t('Create new account'), get_forum_url() . '/register.php', array('attributes' => array('title' => t('Create a new user account.'))));
}
if (variable_get('fluxbb_user_lost_password', TRUE)) {
$items[] = l(t('Request new password'), get_forum_url() . '/login.php?action=forget', array('attributes' => array('title' => t('Request new password via e-mail.'))));
}
if (variable_get('fluxbb_user_remember', TRUE)) {
$form['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember me'),
'#default_value' => false,
);
}
$form['links'] = array('#markup' => theme('item_list', array('items' => $items)));
$form['#validate'] = array( 'user_login_name_validate', 'fluxbb_login_login_validate', 'user_login_final_validate' );
}
function fluxbb_login_form_user_login_block_alter( &$form, $form_state )
{
return fluxbb_login_form_user_login_alter( $form, $form_state );
}
/**
* Implement hook_user_profile_form_alter() to disable the ability to change email address and
* password for externally authenticated users.
*/
function fluxbb_login_form_user_profile_form_alter( &$form, $form_state )
{
global $user;
if (!in_array('administrator', $user->roles)) {
$fluxbb = get_fluxbb_object();
drupal_goto(get_forum_url() . '/profile.php?id=' . $fluxbb->getUserId($user->name), array(), 303);
}
}
/**
* The fluxbb_login_login_validate() function attempts to authenticate a user off the external system
*/
function fluxbb_login_login_validate( $form, &$form_state )
{
global $user;
$username = $form_state['values']['name'];
if(db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $username))->fetchField() && !user_external_load($username)) {
// Perform normal login in drupal
user_login_authenticate_validate( $form, $form_state );
// Perform login in fluxbb too
validateFluxbbUser( $username, $form_state['values']['pass']);
} else {
// Perform external authentication and login in fluxbb
if ( validateFluxbbUser( $username, $form_state['values']['pass'], $form_state['values']['remember'] )) {
user_external_login_register( $username, 'fluxbb_login' );
$form_state['uid'] = $user->uid;
}
}
}
/**
* This is the helper function that you will need to modify in order to invoke your external
* authentication mechanism.
*/
function validateFluxbbUser( $username, $password, $remember = false )
{
$fluxbb = get_fluxbb_object();
if($fluxbb) {
return $fluxbb->login($username, $password, $remember);
} else {
return false;
}
}
/**
* fluxbb_login_user_logout
* Implement hook_user_logout
* Logout in fluxbb too
*
* @param mixed $account
* @access public
* @return void
*/
function fluxbb_login_user_logout( $account ) {
$fluxbb = get_fluxbb_object();
if($fluxbb){
$fluxbb->logout();
}
}
function fluxbb_login_comment_view_alter(&$build) {
$style = variable_get('user_picture_style', '');
$username = $build['comment_body']['#object']->name;
$alt = t("@user's picture", array('@user' => $username));
$fluxbb = get_fluxbb_object();
$filepath = $fluxbb->get_user_avatar_url($fluxbb->getUserId($username));
if($filepath !== null) {
$filepath = get_forum_url() . '/' . $filepath;
$picture = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
$build['user_picture']['#markup'] = '<div class="user-picture">' . $picture . '</div>';
}
}
/**
* fluxbb_login_boot
* Implement hook_boot
*
* @access public
* @return void
*/
function fluxbb_login_boot() {
global $user;
// get user module and include some handy functions
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
if($_GET['q']) {
switch($_GET['q']){
case 'user/register':
unset($_GET['destination']);
drupal_goto(get_forum_url() . '/register.php', array(), 303);
break;
case 'user/password':
drupal_goto(get_forum_url() . '/login.php?action=forget', array(), 303);
break;
}
}
$fluxbb = get_fluxbb_object();
if($fluxbb){
$pun_user = array();
if($fluxbb->check_cookie($pun_user)){
if ($user->uid != 0) {
if($user->mail != $pun_user['email'] OR $user->signature != $pun_user['signature']) {
user_save($user, array('mail' => $pun_user['email'], 'signature' => $pun_user['signature']));
}
return;
}
// get account (reload from db) , bail if no loaded active user
$account = user_load_by_name($pun_user['username'], TRUE);
// If user never logged in drupal perform a registration
if($account == false){
user_external_login_register( $pun_user['username'], 'fluxbb_login' );
}
if (!$account || $account->status <> 1 ) return;
// login by assigning account to global $user object
$user = $account;
// following borrowed from user_authenticate_finalize(), but with slightly different message
watchdog('user', 'Session opened for %name by Fluxbb login module.', array('%name' => $user->name));
// This is also used to invalidate one-time login links.
$user->login = time();
db_update('users')
->fields(array('login' => $user->login))
->condition('uid', $user->uid)
->execute();
// Regenerate the session ID to prevent against session fixation attacks.
// This is called before hook_user in case one of those functions fails
// or incorrectly does a redirect which would leave the old session in place.
$edit = NULL;
drupal_session_regenerate();
user_module_invoke('login', $edit, $user);
} else {
if($user->uid <> 0) {
watchdog('user', 'Session closed for %name by Fluxbb login module.', array('%name' => $user->name));
module_invoke_all('user_logout', $user);
// Destroy the current session, and reset $user to the anonymous user.
session_destroy();
}
}
}
}