Provide a unique message (ipb_blocked_as_range) if unblock of a single IP fails becau...
[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
167 $this->showForm ( wfMsg ( 'ipb_blocked_as_range', $this->ip, $block->mAddress ) );
168 return;
169 }
170 }
171 }
172 $success = false;
173 if ( $block ) {
174 # Delete block
175 if ( $block->delete() ) {
176 # Make log entry
177 $log = new LogPage( 'block' );
178 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
179 $success = true;
180 }
181 }
182
183 if ( $success ) {
184 # Report to the user
185 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
186 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
187 $wgOut->redirect( $success );
188 } else {
189 if ( !$this->ip && $this->id ) {
190 $this->ip = '#' . $this->id;
191 }
192 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
193 }
194 }
195
196 function showList( $msg ) {
197 global $wgOut, $wgUser;
198
199 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
200 if ( "" != $msg ) {
201 $wgOut->setSubtitle( $msg );
202 }
203
204 // Purge expired entries on one in every 10 queries
205 if ( !mt_rand( 0, 10 ) ) {
206 Block::purgeExpired();
207 }
208
209 $conds = array();
210 $matches = array();
211 // Is user allowed to see all the blocks?
212 if ( !$wgUser->isAllowed( 'oversight' ) )
213 $conds['ipb_deleted'] = 0;
214 if ( $this->ip == '' ) {
215 // No extra conditions
216 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
217 $conds['ipb_id'] = substr( $this->ip, 1 );
218 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
219 $conds['ipb_address'] = $this->ip;
220 $conds['ipb_auto'] = 0;
221 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
222 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
223 $conds['ipb_auto'] = 0;
224 } else {
225 $user = User::newFromName( $this->ip );
226 if ( $user && ( $id = $user->getID() ) != 0 ) {
227 $conds['ipb_user'] = $id;
228 } else {
229 // Uh...?
230 $conds['ipb_address'] = $this->ip;
231 $conds['ipb_auto'] = 0;
232 }
233 }
234
235 $pager = new IPBlocklistPager( $this, $conds );
236 if ( $pager->getNumRows() ) {
237 $wgOut->addHTML(
238 $this->searchForm() .
239 $pager->getNavigationBar() .
240 Xml::tags( 'ul', null, $pager->getBody() ) .
241 $pager->getNavigationBar()
242 );
243 } elseif ( $this->ip != '') {
244 $wgOut->addHTML( $this->searchForm() );
245 $wgOut->addWikiText( wfMsg( 'ipblocklist-no-results' ) );
246 } else {
247 $wgOut->addWikiText( wfMsg( 'ipblocklist-empty' ) );
248 }
249 }
250
251 function searchForm() {
252 global $wgTitle, $wgScript, $wgRequest;
253 return
254 Xml::tags( 'form', array( 'action' => $wgScript ),
255 Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
256 Xml::openElement( 'fieldset' ) .
257 Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
258 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
259 '&nbsp;' .
260 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
261 Xml::closeElement( 'fieldset' )
262 );
263 }
264
265 /**
266 * Callback function to output a block
267 */
268 function formatRow( $block ) {
269 global $wgUser, $wgLang;
270
271 wfProfileIn( __METHOD__ );
272
273 static $sk=null, $msg=null;
274
275 if( is_null( $sk ) )
276 $sk = $wgUser->getSkin();
277 if( is_null( $msg ) ) {
278 $msg = array();
279 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
280 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
281 foreach( $keys as $key ) {
282 $msg[$key] = wfMsgHtml( $key );
283 }
284 $msg['blocklistline'] = wfMsg( 'blocklistline' );
285 }
286
287 # Prepare links to the blocker's user and talk pages
288 $blocker_id = $block->getBy();
289 $blocker_name = $block->getByName();
290 $blocker = $sk->userLink( $blocker_id, $blocker_name );
291 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
292
293 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
294 if( $block->mAuto ) {
295 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
296 } else {
297 $target = $sk->userLink( $block->mUser, $block->mAddress )
298 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
299 }
300
301 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
302
303 $properties = array();
304 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
305 $properties[] = $msg['infiniteblock'];
306 } else {
307 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
308 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
309 }
310 if ( $block->mAnonOnly ) {
311 $properties[] = $msg['anononlyblock'];
312 }
313 if ( $block->mCreateAccount ) {
314 $properties[] = $msg['createaccountblock'];
315 }
316 if (!$block->mEnableAutoblock && $block->mUser ) {
317 $properties[] = $msg['noautoblockblock'];
318 }
319
320 if ( $block->mBlockEmail && $block->mUser ) {
321 $properties[] = $msg['emailblock'];
322 }
323
324 $properties = implode( ', ', $properties );
325
326 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
327
328 $unblocklink = '';
329 if ( $wgUser->isAllowed('block') ) {
330 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
331 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
332 }
333
334 $comment = $sk->commentBlock( $block->mReason );
335
336 $s = "{$line} $comment";
337 if ( $block->mHideName )
338 $s = '<span class="history-deleted">' . $s . '</span>';
339
340 wfProfileOut( __METHOD__ );
341 return "<li>$s $unblocklink</li>\n";
342 }
343 }
344
345 /**
346 * @todo document
347 * @addtogroup Pager
348 */
349 class IPBlocklistPager extends ReverseChronologicalPager {
350 public $mForm, $mConds;
351
352 function __construct( $form, $conds = array() ) {
353 $this->mForm = $form;
354 $this->mConds = $conds;
355 parent::__construct();
356 }
357
358 function getStartBody() {
359 wfProfileIn( __METHOD__ );
360 # Do a link batch query
361 $this->mResult->seek( 0 );
362 $lb = new LinkBatch;
363
364 /*
365 while ( $row = $this->mResult->fetchObject() ) {
366 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
367 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
368 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
369 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
370 }*/
371 # Faster way
372 # Usernames and titles are in fact related by a simple substitution of space -> underscore
373 # The last few lines of Title::secureAndSplit() tell the story.
374 while ( $row = $this->mResult->fetchObject() ) {
375 $name = str_replace( ' ', '_', $row->user_name );
376 $lb->add( NS_USER, $name );
377 $lb->add( NS_USER_TALK, $name );
378 $name = str_replace( ' ', '_', $row->ipb_address );
379 $lb->add( NS_USER, $name );
380 $lb->add( NS_USER_TALK, $name );
381 }
382 $lb->execute();
383 wfProfileOut( __METHOD__ );
384 return '';
385 }
386
387 function formatRow( $row ) {
388 $block = new Block;
389 $block->initFromRow( $row );
390 return $this->mForm->formatRow( $block );
391 }
392
393 function getQueryInfo() {
394 $conds = $this->mConds;
395 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
396 $conds[] = 'ipb_by=user_id';
397 return array(
398 'tables' => array( 'ipblocks', 'user' ),
399 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
400 'conds' => $conds,
401 );
402 }
403
404 function getIndexField() {
405 return 'ipb_timestamp';
406 }
407 }
408
409