WPMW

WPMW Commit Details

Date:2008-12-05 19:01:39 (3 years 5 months ago)
Author:unknown
Branch:master
Commit:f6a0db7d2120a5821955e0bb09531a360a0dd882
Parents: 45fa2210feddb70b0b4f04914e8aff2ded788a7c
Message:Changes to work better with WP 2.7

Changes:
MAuthWP.php (1 diff)

File differences

AuthWP.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
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
<?php
// AuthWP.php - part of MPMW
// MediaWiki extension to delegate authentication and user management
// to a local Wordpress installation.
// Version 0.1
// Copyright (C) 2008 Ciaran Gultnieks <ciaran@ciarang.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Relative path to Wordpress installation. In the default '..' we
// have MediaWiki installed in a 'wiki' directory off the main
// Wordpress root.
$WP_relpath='..';
// We'll derive our class from MediaWiki's AuthPlugin class...
require_once('AuthPlugin.php');
// Bootstrap Wordpress. This seems rather foolish since surely the
// names of things are bound to clash somewhere, but we want to be
// able to handle everything as if Wordpress was doing it natively
// including respecting any plugins that might be in place.
require($WP_relpath.'/wp-load.php');
// Wordpress has escaped all these in wp-settings.php - we need to
// unescape them again if they weren't meant to be escaped.
if(!get_magic_quotes_gpc()) {
$_GET = stripslashes_deep($_GET );
$_POST = stripslashes_deep($_POST );
$_COOKIE = stripslashes_deep($_COOKIE);
}
// Handler for the MediaWiki UserLoadFromSession hook. Allows users
// already signed in to Wordpress to be automatically signed in to
// MediaWiki. Always returns true, but sets $result to true if auth
// has been done.
function AuthWPUserLoadFromSession($user, &$result) {
// Is there a Wordpress user with a valid session?
$wpuser=wp_get_current_user();
if(!$wpuser || $wpuser->user_login=='')
return true;
$u=User::newFromName($wpuser->user_login);
if(!$u)
wp_die("Your username '".$wpuser->user_login."' is not a valid MediaWiki username");
if(0==$u->getID()) {
$u->addToDatabase();
$u->setToken();
}
$id=User::idFromName($wpuser->user_login);
if(!$id) {
wp_die("Failed to get ID from name '".$wpuser->user_login."'");
return true;
}
if($id==0) {
wp_die("Wikipedia '".$wpuser->user_login."' was not found.");
return true;
}
$user->setID($id);
$user->loadFromId();
$user->setCookies();
$user->saveSettings();
$result=true;
return true;
}
// Handler for the MediaWiki UserLogout hook.
function AuthWPUserLogout(&$user) {
// Log out of Wordpress as well...
wp_logout();
return true;
}
class AuthWP extends AuthPlugin {
// Constructor
function AuthWP(){
// Add hooks...
global $wgHooks;
$wgHooks['UserLoadFromSession'][]='AuthWPUserLoadFromSession';
$wgHooks['UserLogout'][] = 'AuthWPUserLogout';
}
// MediaWiki API HANDLER
// See if the given user exists - true if so, false if not...
function userExists($username) {
return username_exists($username);
}
// MediaWiki API HANDLER
// Handle authentication, returning true if the given credentials
// are good, or false if they're bad.
function authenticate($username,$password) {
$credentials=array( 'user_login'=>$username,'user_password'=>$password);
return wp_signon($credentials,false);
}
// MediaWiki API HANDLER
// Modify the login template...
function modifyUITemplate(&$template) {
$template->set('create',false);
$template->set('usedomain',false);
$template->set('useemail',true);
}
// MediaWiki API HANDLER
// Always return true - tells it to automatically create a local
// account when asked to log in a user that doesn't exist locally.
function autoCreate() {
return true;
}
// MediaWiki API HANDLER
// Always return true - users can change their passwords from
// MediaWiki - we'll hash them and update the Wordpress DB.
function allowPasswordChange() {
return true;
}
// MediaWiki API HANDLER
// Set a new password for the given user...
function setPassword($user,$password) {
$wpuser=get_userdatabylogin($user->mName);
if(!$wpuser)
return false;
wp_set_password($password,$wpuser->user_id);
return true;
}
// MediaWiki API HANDLER
// Update the details of a user that's logging in - i.e. fill in any
// details we can retrieve from the Wordpress user details...
function updateUser(&$user) {
$wpuser=get_userdatabylogin($user->mName);
if(!$wpuser)
return false;
$user->setEmail($wpuser->user_email);
$user->setRealName($wpuser->user_nicename);
return true;
}
// MediaWiki API HANDLER
// Update user details in Wordpress database...
function updateExternalDB($user) {
// Not doing anything here (yet?)
return true;
}
// MediaWiki API HANDLER
// Add a user created in MediaWiki to the Wordpress database...
function addUser($user,$password) {
require_once($WP_relpath.'/wp-includes/registration.php');
wp_create_user($user->mName,$password,$user->mEmail);
return true;
}
// MediaWiki API HANDLER
// Just return true meaning that logins can only be authenticated in
// this module, and not checked against the mediawiki db...
function strict() {
return true;
}
// MediaWiki API HANDLER
// As with strict(), only authenticate through this plugin.
function strictUserAuth($username) {
return true;
}
// MediaWiki API HANDLER
// We can create external accounts so always return true...
function canCreateAccounts() {
return true;
}
}
?>
<?php
// AuthWP.php
// MediaWiki extension to delegate authentication and user management
// to a local Wordpress installation.
// Version 0.1
// Copyright (C) 2008 Ciaran Gultnieks <ciaran@ciarang.com>
//
// Relative path to Wordpress installation. In the default '..' we
// have MediaWiki installed in a 'wiki' directory off the main
// Wordpress root.
$WP_relpath='..';
// We'll derive our class from MediaWiki's AuthPlugin class...
require_once('AuthPlugin.php');
// Bootstrap Wordpress. This seems rather foolish since surely the
// names of things are bound to clash somewhere, but we want to be
// able to handle everything as if Wordpress was doing it natively
// including respecting any plugins that might be in place.
require($WP_relpath.'/wp-load.php');
// Wordpress has escaped all these in wp-settings.php - we need to
// unescape them again if they weren't meant to be escaped.
if(!get_magic_quotes_gpc()) {
$_GET = stripslashes_deep($_GET );
$_POST = stripslashes_deep($_POST );
$_COOKIE = stripslashes_deep($_COOKIE);
}
// Handler for the MediaWiki UserLoadFromSession hook. Allows users
// already signed in to Wordpress to be automatically signed in to
// MediaWiki. Always returns true, but sets $result to true if auth
// has been done.
function AuthWPUserLoadFromSession($user, &$result) {
// Is there a Wordpress user with a valid session?
$wpuser=wp_get_current_user();
if(!$wpuser->ID)
return true;
$u=User::newFromName($wpuser->user_login);
if(!$u)
wp_die("Your username '".$wpuser->user_login."' is not a valid MediaWiki username");
if(0==$u->getID()) {
$u->addToDatabase();
$u->setToken();
}
$id=User::idFromName($wpuser->user_login);
if(!$id) {
wp_die("Failed to get ID from name '".$wpuser->user_login."'");
return true;
}
if($id==0) {
wp_die("Wikipedia '".$wpuser->user_login."' was not found.");
return true;
}
$user->setID($id);
$user->loadFromId();
wfSetupSession();
$user->setCookies();
$user->saveSettings();
$result=true;
return true;
}
// Handler for the MediaWiki UserLogout hook.
function AuthWPUserLogout(&$user) {
// Log out of Wordpress as well...
wp_logout();
return true;
}
class AuthWP extends AuthPlugin {
// Constructor
function AuthWP(){
// Add hooks...
global $wgHooks;
$wgHooks['UserLoadFromSession'][]='AuthWPUserLoadFromSession';
$wgHooks['UserLogout'][] = 'AuthWPUserLogout';
}
// MediaWiki API HANDLER
// See if the given user exists - true if so, false if not...
function userExists($username) {
return username_exists($username);
}
// MediaWiki API HANDLER
// Handle authentication, returning true if the given credentials
// are good, or false if they're bad.
function authenticate($username,$password) {
$credentials=array( 'user_login'=>$username,'user_password'=>$password);
return wp_signon($credentials,false);
}
// MediaWiki API HANDLER
// Modify the login template...
function modifyUITemplate(&$template) {
$template->set('create',false);
$template->set('usedomain',false);
$template->set('useemail',true);
}
// MediaWiki API HANDLER
// Always return true - tells it to automatically create a local
// account when asked to log in a user that doesn't exist locally.
function autoCreate() {
return true;
}
// MediaWiki API HANDLER
// Always return true - users can change their passwords from
// MediaWiki - we'll hash them and update the Wordpress DB.
function allowPasswordChange() {
return true;
}
// MediaWiki API HANDLER
// Set a new password for the given user...
function setPassword($user,$password) {
$wpuser=get_userdatabylogin($user->mName);
if(!$wpuser)
return false;
wp_set_password($password,$wpuser->user_id);
return true;
}
// MediaWiki API HANDLER
// Update the details of a user that's logging in - i.e. fill in any
// details we can retrieve from the Wordpress user details...
function updateUser(&$user) {
$wpuser=get_userdatabylogin($user->mName);
if(!$wpuser)
return false;
$user->setEmail($wpuser->user_email);
$user->setRealName($wpuser->user_nicename);
return true;
}
// MediaWiki API HANDLER
// Update user details in Wordpress database...
function updateExternalDB($user) {
// Not doing anything here (yet?)
return true;
}
// MediaWiki API HANDLER
// Add a user created in MediaWiki to the Wordpress database...
function addUser($user,$password) {
require_once($WP_relpath.'/wp-includes/registration.php');
wp_create_user($user->mName,$password,$user->mEmail);
return true;
}
// MediaWiki API HANDLER
// Just return true meaning that logins can only be authenticated in
// this module, and not checked against the mediawiki db...
function strict() {
return true;
}
// MediaWiki API HANDLER
// As with strict(), only authenticate through this plugin.
function strictUserAuth($username) {
return true;
}
// MediaWiki API HANDLER
// We can create external accounts so always return true...
function canCreateAccounts() {
return true;
}
}
?>

Archive Download the corresponding diff file

Branches