Index: trunk/extensions/UsernameBlacklist/UsernameBlacklist |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | USERNAME BLACKLIST EXTENSION |
3 | 3 | |
4 | | - Version 1.1 |
| 4 | + Version 1.2 |
5 | 5 | � 2006 Rob Church |
6 | 6 | |
7 | 7 | This is free software licensed under the GNU General Public License. Please |
— | — | @@ -25,6 +25,10 @@ |
26 | 26 | maintain a "blacklist" of usernames which cannot be registered. This blacklist |
27 | 27 | is checked against and enforced during new account creation. |
28 | 28 | |
| 29 | +Privileged users are permitted to override the blacklist when creating accounts |
| 30 | +manually. By default, this permission is available to sysops, but can be restricted |
| 31 | +further or removed to prevent any override. |
| 32 | + |
29 | 33 | The blacklist accepts simple text and more powerful regular expressions, for |
30 | 34 | matching against particular patterns of usernames. |
31 | 35 | |
— | — | @@ -91,6 +95,9 @@ |
92 | 96 | 1.1 |
93 | 97 | Make extension compatible with MediaWiki 1.5.8 |
94 | 98 | Allow commenting out lines in the blacklist with # |
| 99 | +1.2 |
| 100 | + Use a class based code structure and rewrite some code to scrape up milliseconds |
| 101 | + Allow users with the "uboverride" permission to pass the blacklist (manual acct. creation) |
95 | 102 | |
96 | 103 | == 8. Thanks... == |
97 | 104 | |
Index: trunk/extensions/UsernameBlacklist/UsernameBlacklist.php |
— | — | @@ -1,4 +1,4 @@ |
2 | | -<?php |
| 2 | +<?php |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Extension to provide a global "bad username" list |
— | — | @@ -6,68 +6,116 @@ |
7 | 7 | * @author Rob Church <robchur@gmail.com> |
8 | 8 | * @package MediaWiki |
9 | 9 | * @subpackage Extensions |
10 | | - * @copyright 2006 Rob Church |
| 10 | + * @copyright © 2006 Rob Church |
11 | 11 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 |
12 | 12 | */ |
13 | 13 | |
14 | 14 | if( defined( 'MEDIAWIKI' ) ) { |
15 | 15 | |
16 | | - $wgExtensionFunctions[] = 'UsernameBlacklist_Init'; |
| 16 | + $wgExtensionFunctions[] = 'efUsernameBlacklistSetup'; |
17 | 17 | $wgExtensionCredits['other'][] = array( 'name' => 'Username Blacklist', 'author' => 'Rob Church', 'url' => 'http://meta.wikimedia.org/wiki/Username_Blacklist' ); |
18 | 18 | |
| 19 | + $wgAvailableRights[] = 'uboverride'; |
| 20 | + $wgGroupPermissions['sysop']['uboverride'] = true; |
| 21 | + |
19 | 22 | /** |
20 | | - * Constructor |
21 | | - */ |
22 | | - function UsernameBlacklist_Init() { |
| 23 | + * Register the extension |
| 24 | + */ |
| 25 | + function efUsernameBlacklistSetup() { |
23 | 26 | global $wgMessageCache, $wgHooks; |
24 | | - $wgHooks['AbortNewAccount'][] = 'UsernameBlacklist_Hook'; |
| 27 | + $wgHooks['AbortNewAccount'][] = 'efUsernameBlacklist'; |
25 | 28 | $wgMessageCache->addMessage( 'blacklistedusername', 'Blacklisted username' ); |
26 | | - $wgMessageCache->addMessage( 'blacklistedusernametext', 'The username you have chosen matches the [[MediaWiki:Usernameblacklist|list of blacklisted usernames]].' ); |
| 29 | + $wgMessageCache->addMessage( 'blacklistedusernametext', 'The username you have chosen matches the [[MediaWiki:Usernameblacklist|list of blacklisted usernames]]. Please choose another.' ); |
27 | 30 | } |
28 | 31 | |
29 | 32 | /** |
30 | | - * Hooked function used to check the username against a blacklist. |
31 | | - * Bring an error page if there is any match. |
32 | | - * |
33 | | - * @return boolean false if username is blacklisted. |
| 33 | + * Perform the check |
| 34 | + * @param $user User to be checked |
| 35 | + * @return bool |
34 | 36 | */ |
35 | | - function UsernameBlacklist_Hook( $user ) { |
36 | | - global $wgOut; |
37 | | - $username = $user->getName(); |
38 | | - $blacklist = wfMsg( 'usernameblacklist' ); |
39 | | - if( $blacklist != '<usernameblacklist>' ) { |
40 | | - $list = explode( "\n", $blacklist ); |
41 | | - foreach( $list as $item ) { |
42 | | - $item = UsernameBlacklist_Trim( $item ); |
43 | | - if( $item ) { |
44 | | - $regex = '/' . $item . '/'; |
45 | | - if( preg_match( $regex, $username ) > 0 ) { |
46 | | - $rt_title = Title::makeTitle( NS_SPECIAL, 'Userlogin' ); |
47 | | - $wgOut->errorPage( 'blacklistedusername', 'blacklistedusernametext' ); |
48 | | - $wgOut->returnToMain( false, $rt_title->getPrefixedText() ); |
49 | | - return( false ); |
50 | | - } |
| 37 | + function efUsernameBlacklist( &$user ) { |
| 38 | + global $wgUser; |
| 39 | + $blackList =& UsernameBlacklist::fetch(); |
| 40 | + if( $blackList->match( $user->getName() ) && !$wgUser->isAllowed( 'uboverride' ) ) { |
| 41 | + global $wgOut; |
| 42 | + $returnTitle = Title::makeTitle( NS_SPECIAL, 'Userlogin' ); |
| 43 | + $wgOut->errorPage( 'blacklistedusername', 'blacklistedusernametext' ); |
| 44 | + $wgOut->returnToMain( false, $returnTitle->getPrefixedText() ); |
| 45 | + return false; |
| 46 | + } else { |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + class UsernameBlacklist { |
| 52 | + |
| 53 | + var $regex; |
| 54 | + |
| 55 | + /** |
| 56 | + * Trim leading spaces and asterisks from the text |
| 57 | + * @param $text Text to trim |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + function transform( $text ) { |
| 61 | + return trim( $text, ' *' ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Is the supplied text a comment? |
| 66 | + * @param $text Text to check |
| 67 | + * @return bool |
| 68 | + */ |
| 69 | + function isComment( $text ) { |
| 70 | + return substr( $this->transform( $text ), 0, 1 ) == '#'; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return a regular expression representing the blacklist |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + function buildBlacklist() { |
| 78 | + $blacklist = wfMsg( 'usernameblacklist' ); |
| 79 | + if( $blacklist != '<usernameblacklist>' ) { |
| 80 | + $lines = explode( "\n", $blacklist ); |
| 81 | + foreach( $lines as $line ) { |
| 82 | + if( !$this->isComment( $line ) ) |
| 83 | + $groups[] = $this->transform( $line ); |
51 | 84 | } |
| 85 | + return count( $groups ) ? '/(' . implode( '|', $groups ) . ')/' : false; |
| 86 | + } else { |
| 87 | + return false; |
52 | 88 | } |
53 | | - return( true ); |
54 | | - } else { |
55 | | - return( true ); |
56 | 89 | } |
57 | | - } |
| 90 | + |
| 91 | + /** |
| 92 | + * Match a username against the blacklist |
| 93 | + * @param $username Username to check |
| 94 | + * @return bool |
| 95 | + */ |
| 96 | + function match( $username ) { |
| 97 | + return $this->regex ? preg_match( $this->regex, $username ) : false; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Constructor |
| 102 | + * Prepare the regular expression |
| 103 | + */ |
| 104 | + function UsernameBlacklist() { |
| 105 | + $this->regex = $this->buildBlacklist(); |
| 106 | + } |
58 | 107 | |
59 | | - /** |
60 | | - * Remove occurences of ' ' or '*' at the beginning of a string |
61 | | - * and check for commented lines |
62 | | - * |
63 | | - * @param string $text A text to trim. |
64 | | - * @return string The trimmed text. |
65 | | - */ |
66 | | - function UsernameBlacklist_Trim( $text ) { |
67 | | - while( ( substr( $text, 0, 1 ) == '*' ) || ( substr( $text, 0, 1 ) == ' ' ) ) { |
68 | | - $text = substr( $text, 1, strlen( $text ) - 1 ); |
| 108 | + /** |
| 109 | + * Fetch an instance of the blacklist class |
| 110 | + * @return UsernameBlacklist |
| 111 | + */ |
| 112 | + function fetch() { |
| 113 | + static $blackList = false; |
| 114 | + if( !$blackList ) |
| 115 | + $blackList = new UsernameBlacklist(); |
| 116 | + return $blackList; |
69 | 117 | } |
70 | | - return( substr( $text, 0, 1 ) == '#' ? false : $text ); |
71 | | - } |
| 118 | + |
| 119 | + } |
72 | 120 | |
73 | 121 | } else { |
74 | 122 | die( 'This file is an extension to the MediaWiki package, and cannot be executed separately.' ); |