forked from sembrestels/mediawiki-bitid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitId.hooks.php
347 lines (278 loc) · 8.81 KB
/
BitId.hooks.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
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
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* Redirect classes to hijack the core UserLogin and CreateAccount facilities, because
* they're so badly written as to be impossible to extend
*/
class BitIdHooks {
public static function onSpecialPage_initList( &$specialPagesList ) {
global $wgBitIdLoginOnly, $wgSpecialPageGroups, $wgUser;
# redirect all special login pages to our own BitId login pages
# but only for entitled users
$addBitIdSpecialPagesList = array();
if ( $wgBitIdLoginOnly
&& !$wgUser->isAllowed( 'bitid-create-account-without-bitid' )
&& $wgUser->isAllowed( 'bitid-login-with-bitid' ) ) {
$specialPagesList['Userlogin'] = 'SpecialBitIdLogin';
# as Special:CreateAccount is an alias for Special:UserLogin/signup
# we show our own BitId page here, too
$specialPagesList['CreateAccount'] = 'SpecialBitIdLogin';
}
if ( !$wgUser->isLoggedIn()
&& ( $wgUser->isAllowed( 'bitid-login-with-bitid' )
|| $wgUser->isAllowed( 'bitid-create-account-with-bitid' ) ) ) {
$addBitIdSpecialPagesList[] = 'Login';
}
$addBitIdSpecialPagesList[] = 'Convert';
$addBitIdSpecialPagesList[] = 'Dashboard';
# add the server-related Special pages
foreach ( $addBitIdSpecialPagesList as $sp ) {
$key = 'BitId' . $sp;
$specialPagesList[$key] = 'SpecialBitId' . $sp;
$wgSpecialPageGroups[$key] = 'bitid';
}
return true;
}
/**
* @param $personal_urls array
* @param $title Title
* @return bool
*/
public static function onPersonalUrls( &$personal_urls, &$title ) {
global $wgBitIdHideBitIdLoginLink, $wgUser, $wgOut, $wgBitIdLoginOnly;
if ( !$wgBitIdHideBitIdLoginLink
&& ( $wgUser->getID() == 0 )) {
$sk = $wgOut->getSkin();
$returnto = $title->isSpecial( 'Userlogout' ) ? '' : ( 'returnto=' . $title->getPrefixedURL() );
$personal_urls['bitidlogin'] = array(
'text' => wfMessage( 'bitidlogin' )->text(),
'href' => $sk->makeSpecialUrl( 'BitIdLogin', $returnto ),
'active' => $title->isSpecial( 'BitIdLogin' )
);
if ( $wgBitIdLoginOnly ) {
# remove other login links
foreach ( array( 'createaccount', 'login', 'anonlogin' ) as $k ) {
if ( array_key_exists( $k, $personal_urls ) ) {
unset( $personal_urls[$k] );
}
}
}
}
return true;
}
/**
* Add the BitID module to the OutputPage.
*
* @param &$out OutputPage object
* @param &$skin Skin object
*/
public static function onBeforePageDisplay( $out, &$skin ) {
global $wgBitIdLoginAnywhere;
if( !$wgBitIdLoginAnywhere ) {
return true;
} elseif( !isset( $_SESSION ) ) {
wfSetupSession();
}
$out->addModules( 'ext.bitid' );
if( !LoginForm::getLoginToken() ) {
LoginForm::setLoginToken();
}
$out->addHTML( Html::input(
'wpLoginToken',
LoginForm::getLoginToken(),
'hidden'
) );
return true;
}
/**
* @param $user User
* @return string
*/
private static function getAssociatedBitIdsTable( $user ) {
global $wgLang;
$bitid_addrs_registration = MediawikiBitId::getUserBitIdInformation( $user );
$delTitle = SpecialPage::getTitleFor( 'BitIdConvert', 'Delete' );
$rows = '';
foreach ( $bitid_addrs_registration as $addr_reg ) {
if ( !empty( $addr_reg->uoi_user_registration ) ) {
$registrationTime = wfMessage(
'bitid-addrs-registration-date-time',
$wgLang->timeanddate( $addr_reg->uoi_user_registration, true ),
$wgLang->date( $addr_reg->uoi_user_registration, true ),
$wgLang->time( $addr_reg->uoi_user_registration, true )
)->text();
} else {
$registrationTime = '';
}
$rows .= Xml::tags( 'tr', array(),
Xml::tags( 'td',
array(),
Xml::element( 'a', array( 'href' => 'bitcoin:'.$addr_reg->uoi_bitid ), $addr_reg->uoi_bitid )
) .
Xml::tags( 'td',
array(),
$registrationTime
) .
Xml::tags( 'td',
array(),
Linker::link( $delTitle, wfMessage( 'bitid-addrs-delete' )->text(),
array(),
array( 'url' => $addr_reg->uoi_bitid )
)
)
) . "\n";
}
$info = Xml::tags( 'table', array( 'class' => 'wikitable' ),
Xml::tags( 'tr', array(),
Xml::element( 'th',
array(),
wfMessage( 'bitid-addrs-addr' )->text() ) .
Xml::element( 'th',
array(),
wfMessage( 'bitid-addrs-registration' )->text() ) .
Xml::element( 'th',
array(),
wfMessage( 'bitid-addrs-action' )->text() )
) . "\n" .
$rows
);
$info .= Linker::link(
SpecialPage::getTitleFor( 'BitIdConvert' ),
wfMessage( 'bitid-add-url' )->escaped()
);
return $info;
}
/**
* @param $user User
* @param $preferences array
* @return bool
*/
public static function onGetPreferences( $user, &$preferences ) {
global $wgHiddenPrefs, $wgAuth, $wgUser, $wgLang;
// setting up user_properties up_property database key names
// example 'bitid-userinfo-update-on-login-nickname'
// FIXME: this could better be saved as a JSON encoded array in a single key
$update = array();
$update[ wfMessage( 'bitidnickname' )->text() ] = '-nickname';
$update[ wfMessage( 'bitidemail' )->text() ] = '-email';
if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
$update[ wfMessage( 'bitidfullname' )->text() ] = '-fullname';
}
$update[ wfMessage( 'bitidlanguage' )->text() ] = '-language';
$update[ wfMessage( 'bitidtimezone' )->text() ] = '-timezone';
/*
$preferences['bitid-userinfo-update-on-login'] =
array(
'section' => 'bitid/bitid-userinfo-update-on-login',
'type' => 'multiselect',
'label-message' => 'bitid-userinfo-update-on-login-label',
'options' => $update,
);
*/
$preferences['bitid-associated-bitids'] =
array(
'section' => 'bitid/bitid-associated-bitids',
'type' => 'info',
'label-message' => 'bitid-associated-bitids-label',
'default' => self::getAssociatedBitIdsTable( $user ),
'raw' => true,
);
if ( $wgAuth->allowPasswordChange() ) {
$resetlink = Linker::link(
SpecialPage::getTitleFor( 'PasswordReset' ),
wfMessage( 'passwordreset' )->escaped(),
array(),
array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) )
);
if ( empty( $wgUser->mPassword ) && empty( $wgUser->mNewpassword ) ) {
$preferences['password'] = array(
'section' => 'personal/info',
'type' => 'info',
'raw' => true,
'default' => $resetlink,
'label-message' => 'yourpassword',
);
} else {
$preferences['resetpassword'] = array(
'section' => 'personal/info',
'type' => 'info',
'raw' => true,
'default' => $resetlink,
'label-message' => null,
);
}
global $wgCookieExpiration;
if ( $wgCookieExpiration > 0 ) {
unset( $preferences['rememberpassword'] );
$preferences['rememberpassword'] = array(
'section' => 'personal/info',
'type' => 'toggle',
'label' => wfMessage(
'tog-rememberpassword',
$wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )
)->escaped(),
);
}
}
return true;
}
/**
* @param $user User
* @return bool
*/
public static function onDeleteAccount( &$user ) {
global $wgOut;
if ( is_object( $user ) ) {
$username = $user->getName();
$userID = $user->getID();
$dbw = wfGetDB( DB_MASTER );
$dbw->delete( 'bitid_users', array( 'uoi_user' => $userID ) );
$wgOut->addHTML( "BitID " . wfMessage( 'usermerge-userdeleted', $username, $userID )->escaped() . "<br />\n" );
}
return true;
}
/**
* @param $fromUserObj User
* @param $toUserObj User
* @return bool
*/
public static function onMergeAccountFromTo( &$fromUserObj, &$toUserObj ) {
global $wgOut, $wgBitIdMergeOnAccountMerge;
if ( is_object( $fromUserObj ) && is_object( $toUserObj ) ) {
$fromUsername = $fromUserObj->getName();
$fromUserID = $fromUserObj->getID();
$toUsername = $toUserObj->getName();
$toUserID = $toUserObj->getID();
if ( $wgBitIdMergeOnAccountMerge ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->update( 'bitid_users', array( 'uoi_user' => $toUserID ), array( 'uoi_user' => $fromUserID ) );
$wgOut->addHTML( "BitID " . wfMessage( 'usermerge-updating', 'bitid_users', $fromUsername, $toUsername )->escaped() . "<br />\n" );
} else {
$wgOut->addHTML( wfMessage( 'bitid-bitids-were-not-merged' )->escaped() . "<br />\n" );
}
}
return true;
}
/**
* @param $updater DatabaseUpdater
* @return bool
*/
public static function onLoadExtensionSchemaUpdates( $updater = null ) {
switch ( $updater->getDB()->getType() ) {
case "mysql":
return self::MySQLSchemaUpdates( $updater );
default:
throw new MWException("BitId does not support {$updater->getDB()->getType()} yet.");
}
}
/**
* @param $updater MysqlUpdater
* @return bool
*/
public static function MySQLSchemaUpdates( $updater = null ) {
$updater->addExtensionTable( 'bitid_nonces',
dirname( __FILE__ ) . '/scheme/nonces.sql', true );
$updater->addExtensionTable( 'bitid_users',
dirname( __FILE__ ) . '/scheme/users.sql', true );
return true;
}
}