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