-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-phpbb.php
234 lines (183 loc) · 6.32 KB
/
connect-phpbb.php
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
<?php
/*
Plugin Name: PHPBB Single Sign On
Version: 0.8.5
Plugin URI: http://www.onigoetz.ch/plugins/wordpress-phpbb-plugin/
Description: Connecte un site wordpress à PHPBB
Author: Stéphane Goetz
Author URI: http://www.onigoetz.ch
*/
if (!defined('LOADED_PHPBB')) {
define('LOADED_WP', true);
}
include (dirname(__FILE__) . '/module.phpbb.php');
include (dirname(__FILE__) . '/module.wp.php');
include (dirname(__FILE__) . '/common-functions.php');
$wp2bb_enabled = get_option('wp2bb_enabled', false);
if($wp2bb_enabled){
include (dirname(__FILE__) . '/wp2bb/main.php');
}
define('WPBB_OPTIONS_PAGE', get_option('siteurl') . '/wp-admin/admin.php?page=' . dirname(plugin_basename(__FILE__)) . '/options.php');
////////////////////////////////////////////////////////////////////////////////
/**
* Installation Hook
*/
function wpbb_install() {
$default_options = array(
'path' => 'forum/'
);
add_option('connect_phpbb_options', $default_options);
}
register_activation_hook(__FILE__, 'wpbb_install');
////////////////////////////////////////////////////////////////////////////////
/**
* Options Page
*/
function wpbb_options_page() {
add_options_page('PHP BB', 'PHP BB Options', 10, 'phpbb-single-sign-on/options.php');
}
add_action('admin_menu', 'wpbb_options_page');
////////////////////////////////////////////////////////////////////////////////
/**
* On login actions
*
* @param string $userdata
* @param string $password
* @return object
*/
function wpbb_login($userdata, $password) {
// check to see if phpBB exists
if (!wpbb_phpBB3::phpbbConfig() OR !wpbb_phpBB3::phpbbPatched()){
return $userdata;
}
define('FROM_WP', TRUE);
// Set up phpBB environment
wpbb_phpBB3::load();
global $db;
//PHPBB
$phpBB_user = wpbb_phpBB3::getUserByName($userdata->user_login);
//Wordpress
$in_wp = ($userdata->ID == 0) ? FALSE : TRUE;
//WP 1 BB 0 ?
if (!$phpBB_user && $in_wp) { //If he doesn't exists, create the user in phpbb
$email = $userdata->user_email ? $userdata->user_email : '';
// since group IDs may change, use a query to make sure it is the right default group.
$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $db->sql_escape(REGISTERED) . "' AND group_type = " . GROUP_SPECIAL;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$group_id = $row['group_id'];
$user_row = array(
'username' => $userdata->user_login,
'user_password' => phpbb_hash($password),
'group_id' => $group_id,
'user_email' => $email,
'user_type' => 0
);
$id = wpbb_phpBB3::addUser($user_row);
$phpBB_user = wpbb_phpBB3::getUserById($id);
}
$user = Registry::get('user');
$result = $user->session_create($phpBB_user->user_id, 0, 1, 1);
return $userdata;
}
//Doesn't add the hook if we're in phpbb
if (defined('LOADED_WP')) {
add_action('wp_authenticate_user', 'wpbb_login', 10, 2);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Logout action
*/
function wpbb_logout() {
if (!defined('FROM_WP')){
define('FROM_WP', TRUE);
}
if (wpbb_phpBB3::phpbbConfig()) { // check to see if phpBB exists
wpbb_phpBB3::load();
wpbb_phpBB3::logout();
}
return false;
}
add_action('wp_logout', 'wpbb_logout');
////////////////////////////////////////////////////////////////////////////////
/**
* PASSWORD CHANGES, when a user changes his password in wordpress, it applies to phpbb
*
* @param string $username
* @param string $pass1
* @param string $pass2
* @return bool
*/
function wpbb_change_password($username, $pass1, $pass2) {
if (!empty($pass1) && !empty($pass2) && $pass1 == $pass2) {
wpbb_phpBB3::load();
if (wpbb_phpBB3::getUserByName($username)) {
wpbb_phpBB3::changePassword($username, $pass1);
}
}
return true;
}
add_filter('check_passwords', 'wpbb_change_password', 10, 3);
////////////////////////////////////////////////////////////////////////////////
/**
* Runs tests in admin to show if the user has some configs to do.
*
* @return null
*/
function wpbb_admin_warnings() {
if (wpbb_run_test(false)) {
function wpbb_warning() {
echo "
<div id='phpbb-warning' class='updated fade'>
<p><strong>" . __('PHP Single Sign On is almost ready.') . "</strong>
" . sprintf(__('You have to <a href="%s">Follow the instructions</a> for it to work.'),
get_option('siteurl') . '/wp-admin/admin.php?page=phpbb-single-sign-on/options.php') . "</p></div>
";
}
add_action('admin_notices', 'wpbb_warning');
return;
}
}
add_action('init', 'wpbb_admin_warnings');
////////////////////////////////////////////////////////////////////////////////
/**
* Email existing
*
* @param string $login
* @param string $email
* @param object $errors
* @return null
*/
function wpbb_register_post($login, $email, $errors) {
if ($errors->get_error_code()) {
return; // No need to add to the despair
}
if (wpbb_phpBB3::get_wp_user_by_email($email)) {
$errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
}
}
add_action('register_post', 'wpbb_register_post', 10, 3);
////////////////////////////////////////////////////////////////////////////////
/**
* Error Hints
*
* @param string $login
* @param string $email
* @param object $errors
* @return null
*/
function wpbb_register_hint($login, $email, $errors) {
error_log($errors->get_error_code());
if ($errors->get_error_code() != 'email_exists' and
$errors->get_error_code() != 'username_exists'
)
return;
$errors->add('email_exists', sprintf("<a href=\"%s\">%s</a>",
site_url('wp-login.php', 'login'),
__('Log In')));
$errors->add('email_exists', sprintf("<a href=\"%s\" title=\"%s\">%s</a>",
site_url('wp-login.php?action=lostpassword', 'recover password'),
__('Password Lost and Found'),
__('Lost your password?')));
}
add_action('register_post', 'wpbb_register_hint', 11, 3);