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