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