(bug 8688) Handle underscores/spaces in Special:Blockip and Special:Ipblocklist in...
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * @todo document
10 */
11 function wfSpecialIpblocklist() {
12 global $wgUser, $wgOut, $wgRequest;
13
14 $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
15 $id = $wgRequest->getVal( 'id' );
16 $reason = $wgRequest->getText( 'wpUnblockReason' );
17 $action = $wgRequest->getText( 'action' );
18 $successip = $wgRequest->getVal( 'successip' );
19
20 $ipu = new IPUnblockForm( $ip, $id, $reason );
21
22 if ( "success" == $action ) {
23 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
24 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
25 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
26 if ( ! $wgUser->isAllowed('block') ) {
27 $wgOut->permissionRequired( 'block' );
28 return;
29 }
30 $ipu->doSubmit();
31 } else if ( "unblock" == $action ) {
32 $ipu->showForm( "" );
33 } else {
34 $ipu->showList( "" );
35 }
36 }
37
38 /**
39 *
40 * @package MediaWiki
41 * @subpackage SpecialPage
42 */
43 class IPUnblockForm {
44 var $ip, $reason, $id;
45
46 function IPUnblockForm( $ip, $id, $reason ) {
47 $this->ip = strtr( $ip, '_', ' ' );
48 $this->id = $id;
49 $this->reason = $reason;
50 }
51
52 function showForm( $err ) {
53 global $wgOut, $wgUser, $wgSysopUserBans;
54
55 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
56 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
57
58 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
59 $ipr = wfMsgHtml( 'ipbreason' );
60 $ipus = wfMsgHtml( 'ipusubmit' );
61 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
62 $action = $titleObj->escapeLocalURL( "action=submit" );
63
64 if ( "" != $err ) {
65 $wgOut->setSubtitle( wfMsg( "formerror" ) );
66 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
67 }
68 $token = htmlspecialchars( $wgUser->editToken() );
69
70 $addressPart = false;
71 if ( $this->id ) {
72 $block = Block::newFromID( $this->id );
73 if ( $block ) {
74 $encName = htmlspecialchars( $block->getRedactedName() );
75 $encId = htmlspecialchars( $this->id );
76 $addressPart = $encName . "<input type='hidden' name=\"id\" value=\"$encId\" />";
77 }
78 }
79 if ( !$addressPart ) {
80 $addressPart = "<input tabindex='1' type='text' size='20' " .
81 "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
82 }
83
84 $wgOut->addHTML( "
85 <form id=\"unblockip\" method=\"post\" action=\"{$action}\">
86 <table border='0'>
87 <tr>
88 <td align='right'>{$ipa}:</td>
89 <td align='left'>
90 {$addressPart}
91 </td>
92 </tr>
93 <tr>
94 <td align='right'>{$ipr}:</td>
95 <td align='left'>
96 <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
97 </td>
98 </tr>
99 <tr>
100 <td>&nbsp;</td>
101 <td align='left'>
102 <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
103 </td>
104 </tr>
105 </table>
106 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
107 </form>\n" );
108
109 }
110
111 function doSubmit() {
112 global $wgOut;
113
114 if ( $this->id ) {
115 $block = Block::newFromID( $this->id );
116 if ( $block ) {
117 $this->ip = $block->getRedactedName();
118 }
119 } else {
120 $block = new Block();
121 $this->ip = trim( $this->ip );
122 if ( substr( $this->ip, 0, 1 ) == "#" ) {
123 $id = substr( $this->ip, 1 );
124 $block = Block::newFromID( $id );
125 } else {
126 $block = Block::newFromDB( $this->ip );
127 if ( !$block ) {
128 $block = null;
129 }
130 }
131 }
132 $success = false;
133 if ( $block ) {
134 # Delete block
135 if ( $block->delete() ) {
136 # Make log entry
137 $log = new LogPage( 'block' );
138 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
139 $success = true;
140 }
141 }
142
143 if ( $success ) {
144 # Report to the user
145 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
146 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
147 $wgOut->redirect( $success );
148 } else {
149 if ( !$this->ip && $this->id ) {
150 $this->ip = '#' . $this->id;
151 }
152 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
153 }
154 }
155
156 function showList( $msg ) {
157 global $wgOut;
158
159 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
160 if ( "" != $msg ) {
161 $wgOut->setSubtitle( $msg );
162 }
163
164 // Purge expired entries on one in every 10 queries
165 if ( !mt_rand( 0, 10 ) ) {
166 Block::purgeExpired();
167 }
168
169 $conds = array();
170 $matches = array();
171 if ( $this->ip == '' ) {
172 // No extra conditions
173 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
174 $conds['ipb_id'] = substr( $this->ip, 1 );
175 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
176 $conds['ipb_address'] = $this->ip;
177 $conds['ipb_auto'] = 0;
178 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
179 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
180 $conds['ipb_auto'] = 0;
181 } else {
182 $user = User::newFromName( $this->ip );
183 if ( $user && ( $id = $user->getID() ) != 0 ) {
184 $conds['ipb_user'] = $id;
185 } else {
186 // Uh...?
187 $conds['ipb_address'] = $this->ip;
188 $conds['ipb_auto'] = 0;
189 }
190 }
191
192 $pager = new IPBlocklistPager( $this, $conds );
193 $s = $pager->getNavigationBar() .
194 $this->searchForm();
195 if ( $pager->getNumRows() ) {
196 $s .= "<ul>" .
197 $pager->getBody() .
198 "</ul>";
199 } else {
200 $s .= '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
201 }
202 $s .= $pager->getNavigationBar();
203 $wgOut->addHTML( $s );
204 }
205
206 function searchForm() {
207 global $wgTitle, $wgScript, $wgRequest;
208 return
209 wfElement( 'form', array(
210 'action' => $wgScript ),
211 null ) .
212 wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
213 wfElement( 'input', array(
214 'type' => 'hidden',
215 'name' => 'action',
216 'value' => 'search' ) ).
217 wfElement( 'input', array(
218 'type' => 'hidden',
219 'name' => 'limit',
220 'value' => $wgRequest->getText( 'limit' ) ) ) .
221 wfElement( 'input', array(
222 'name' => 'ip',
223 'value' => $this->ip ) ) .
224 wfElement( 'input', array(
225 'type' => 'submit',
226 'value' => wfMsg( 'searchbutton' ) ) ) .
227 '</form>';
228 }
229
230 /**
231 * Callback function to output a block
232 */
233 function formatRow( $block ) {
234 global $wgUser, $wgLang;
235
236 wfProfileIn( __METHOD__ );
237
238 static $sk=null, $msg=null;
239
240 if( is_null( $sk ) )
241 $sk = $wgUser->getSkin();
242 if( is_null( $msg ) ) {
243 $msg = array();
244 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
245 'anononlyblock', 'createaccountblock', 'noautoblockblock' );
246 foreach( $keys as $key ) {
247 $msg[$key] = wfMsgHtml( $key );
248 }
249 $msg['blocklistline'] = wfMsg( 'blocklistline' );
250 $msg['contribslink'] = wfMsg( 'contribslink' );
251 }
252
253 # Prepare links to the blocker's user and talk pages
254 $blocker_id = $block->getBy();
255 $blocker_name = $block->getByName();
256 $blocker = $sk->userLink( $blocker_id, $blocker_name );
257 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
258
259 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
260 if( $block->mAuto ) {
261 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
262 } else {
263 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
264 $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
265 }
266
267 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
268
269 $properties = array();
270 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
271 $properties[] = $msg['infiniteblock'];
272 } else {
273 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
274 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
275 }
276 if ( $block->mAnonOnly ) {
277 $properties[] = $msg['anononlyblock'];
278 }
279 if ( $block->mCreateAccount ) {
280 $properties[] = $msg['createaccountblock'];
281 }
282 if (!$block->mEnableAutoblock && $block->mUser ) {
283 $properties[] = $msg['noautoblockblock'];
284 }
285
286 $properties = implode( ', ', $properties );
287
288 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
289
290 $s = "<li>{$line}";
291
292 if ( $wgUser->isAllowed('block') ) {
293 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
294 $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
295 }
296 $s .= $sk->commentBlock( $block->mReason );
297 $s .= "</li>\n";
298 wfProfileOut( __METHOD__ );
299 return $s;
300 }
301 }
302
303 class IPBlocklistPager extends ReverseChronologicalPager {
304 public $mForm, $mConds;
305
306 function __construct( $form, $conds = array() ) {
307 $this->mForm = $form;
308 $this->mConds = $conds;
309 parent::__construct();
310 }
311
312 function getStartBody() {
313 wfProfileIn( __METHOD__ );
314 # Do a link batch query
315 $this->mResult->seek( 0 );
316 $lb = new LinkBatch;
317
318 /*
319 while ( $row = $this->mResult->fetchObject() ) {
320 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
321 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
322 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
323 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
324 }*/
325 # Faster way
326 # Usernames and titles are in fact related by a simple substitution of space -> underscore
327 # The last few lines of Title::secureAndSplit() tell the story.
328 while ( $row = $this->mResult->fetchObject() ) {
329 $name = str_replace( ' ', '_', $row->user_name );
330 $lb->add( NS_USER, $name );
331 $lb->add( NS_USER_TALK, $name );
332 $name = str_replace( ' ', '_', $row->ipb_address );
333 $lb->add( NS_USER, $name );
334 $lb->add( NS_USER_TALK, $name );
335 }
336 $lb->execute();
337 wfProfileOut( __METHOD__ );
338 return '';
339 }
340
341 function formatRow( $row ) {
342 $block = new Block;
343 $block->initFromRow( $row );
344 return $this->mForm->formatRow( $block );
345 }
346
347 function getQueryInfo() {
348 $conds = $this->mConds;
349 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
350 $conds[] = 'ipb_by=user_id';
351 return array(
352 'tables' => array( 'ipblocks', 'user' ),
353 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
354 'conds' => $conds,
355 );
356 }
357
358 function getIndexField() {
359 return 'ipb_timestamp';
360 }
361 }
362
363 ?>